blob: 2cd4ee7aef80d2dd1d101dfe4d44c083c03c0961 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
John Durkop07cc04a2020-11-16 22:08:34 -080025#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
26#include "check_crypto_config.h"
27#endif
28
Gilles Peskinee59236f2018-01-27 23:32:46 +010029#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010030#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031
Ronald Crond6d28882020-12-14 14:56:02 +010032#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020035#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010036#include "psa_crypto_ecp.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010037#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010038#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010039#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010040#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020041#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020042#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020043#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010044#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010045/* Include internal declarations that are useful for implementing persistently
46 * stored keys. */
47#include "psa_crypto_storage.h"
48
Gilles Peskineb2b64d32020-12-14 16:43:58 +010049#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010050
Gilles Peskineb46bef22019-07-30 21:32:04 +020051#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010052#include <stdlib.h>
53#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010054#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010055
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010056#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020057#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000058#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020059#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020061#include "mbedtls/chacha20.h"
62#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/cipher.h"
64#include "mbedtls/ccm.h"
65#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020067#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010068#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010069#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/error.h"
71#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010072#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000074#include "md_wrap.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000076#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000078#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010080#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/sha1.h"
82#include "mbedtls/sha256.h"
83#include "mbedtls/sha512.h"
Paul Elliott588f8ed2022-12-02 18:10:26 +000084#include "hash_info.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010085
Gilles Peskine449bd832023-01-11 14:50:10 +010086#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020087
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020088#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
89 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
90 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020091#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020092#endif
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/****************************************************************/
95/* Global data, support functions and library management */
96/****************************************************************/
97
Gilles Peskine449bd832023-01-11 14:50:10 +010098static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020099{
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200101}
102
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100103/* Values for psa_global_data_t::rng_state */
104#define RNG_NOT_INITIALIZED 0
105#define RNG_INITIALIZED 1
106#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100109 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100110 unsigned rng_state : 2;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100111 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100112} psa_global_data_t;
113
114static psa_global_data_t global_data;
115
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100116#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
117mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
118 &global_data.rng.drbg;
119#endif
120
itayzafrir0adf0fc2018-09-06 16:24:41 +0300121#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 if (global_data.initialized == 0) \
123 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100126{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100127 /* Mbed TLS error codes can combine a high-level error code and a
128 * low-level error code. The low-level error usually reflects the
129 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 int low_level_ret = -(-ret & 0x007f);
131 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100132 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100134
135 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
136 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200138 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
139 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
140 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
141 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
142 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200144 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200146 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200148
Jaeden Amero93e21112019-02-20 13:57:28 +0000149#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000150 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000151#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100152 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100154
155 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100157 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100159
Gilles Peskine26869f22019-05-06 15:25:00 +0200160 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200162
163 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200165 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200167
Gilles Peskinea5905292018-02-07 20:59:33 +0100168 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100170 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100172 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100178 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100180 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
184 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100185 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
186 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100187 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100189 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
190 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100192 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100194#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100195
196 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100198
Gilles Peskinee59236f2018-01-27 23:32:46 +0100199 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
200 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
201 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100203
204 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200206 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100208 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100210
Gilles Peskine82e57d12020-11-13 21:31:17 +0100211#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100213 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
214 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100215 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100217 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
218 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100220 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100222#endif
223
Gilles Peskinea5905292018-02-07 20:59:33 +0100224 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100226 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100228 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100230 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100232
Gilles Peskinef76aa772018-10-29 19:24:33 +0100233 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100235 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100237 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100239 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100241 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100243 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100245 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100247 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100249
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100250 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100252 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
253 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100255 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100257 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
258 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100260 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100262 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
263 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100265 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100267 case MBEDTLS_ERR_PK_INVALID_ALG:
268 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
269 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200273 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100275
Gilles Peskineff2d2002019-05-06 15:26:23 +0200276 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200278 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200280
Gilles Peskinea5905292018-02-07 20:59:33 +0100281 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100283 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100285 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100287 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100289 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
290 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100292 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200298
itayzafrir5c753392018-05-08 11:18:38 +0300299 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300300 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300302 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300304 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300306 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
307 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300309 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100311 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100313
Paul Elliott588f8ed2022-12-02 18:10:26 +0000314 case MBEDTLS_ERR_ECP_IN_PROGRESS:
315 return PSA_OPERATION_INCOMPLETE;
316
Janos Follatha13b9052019-11-22 12:48:59 +0000317 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300319
Gilles Peskinee59236f2018-01-27 23:32:46 +0100320 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100322 }
323}
324
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200325
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200326
327
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100328/****************************************************************/
329/* Key management */
330/****************************************************************/
331
John Durkop9814fa22020-11-04 12:28:15 -0800332#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800333 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cronf467d632021-11-19 18:02:34 +0100334 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
335 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
336 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100337mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
338 size_t bits,
339 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200340{
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100342 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100344#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100345 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100347#endif
348#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100349 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100351#endif
352#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100353 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100355#endif
356#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100357 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100359#endif
360#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100361 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100363 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 if (bits_is_sloppy) {
365 return MBEDTLS_ECP_DP_SECP521R1;
366 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100367 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100368#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100369 }
370 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100371
Paul Elliott8ff510a2020-06-02 17:19:28 +0100372 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100374#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100375 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100377#endif
378#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100379 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100381#endif
382#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100383 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100385#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100386 }
387 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100388
Paul Elliott8ff510a2020-06-02 17:19:28 +0100389 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100391#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100392 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100394 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 if (bits_is_sloppy) {
396 return MBEDTLS_ECP_DP_CURVE25519;
397 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100398 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100399#endif
400#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100401 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100403#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100404 }
405 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100406
Paul Elliott8ff510a2020-06-02 17:19:28 +0100407 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100409#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100410 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100412#endif
413#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100414 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100416#endif
417#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100418 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100420#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100421 }
422 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200423 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100424
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100425 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200427}
John Durkop9814fa22020-11-04 12:28:15 -0800428#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cronf467d632021-11-19 18:02:34 +0100429 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
430 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
431 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
432 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
435 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200436{
437 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200439 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200440 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200441 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200442 case PSA_KEY_TYPE_PASSWORD:
443 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200444 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100445#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if (bits != 128 && bits != 192 && bits != 256) {
448 return PSA_ERROR_INVALID_ARGUMENT;
449 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200450 break;
451#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200452#if defined(PSA_WANT_KEY_TYPE_ARIA)
453 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 if (bits != 128 && bits != 192 && bits != 256) {
455 return PSA_ERROR_INVALID_ARGUMENT;
456 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200457 break;
458#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100459#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200460 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if (bits != 128 && bits != 192 && bits != 256) {
462 return PSA_ERROR_INVALID_ARGUMENT;
463 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200464 break;
465#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100466#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200467 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if (bits != 64 && bits != 128 && bits != 192) {
469 return PSA_ERROR_INVALID_ARGUMENT;
470 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200471 break;
472#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100473#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200474 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 if (bits != 256) {
476 return PSA_ERROR_INVALID_ARGUMENT;
477 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200478 break;
479#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200480 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200482 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 if (bits % 8 != 0) {
484 return PSA_ERROR_INVALID_ARGUMENT;
485 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200488}
489
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100490/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100491 *
Steven Cooremancd640932021-03-08 12:00:27 +0100492 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
493 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100494 *
495 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100496 * \param[in] key_type The key type of the key to be used with the
497 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100498 *
499 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100500 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100501 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100502 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100503 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100504MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100505 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100507{
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 if (PSA_ALG_IS_HMAC(algorithm)) {
509 if (key_type == PSA_KEY_TYPE_HMAC) {
510 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100511 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100512 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
515 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
516 * key. */
517 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
518 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
519 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
520 * the block length (larger than 1) for block ciphers. */
521 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
522 return PSA_SUCCESS;
523 }
524 }
525 }
526
527 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100528}
529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
531 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200532{
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 if (slot->key.data != NULL) {
534 return PSA_ERROR_ALREADY_EXISTS;
535 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 slot->key.data = mbedtls_calloc(1, buffer_length);
538 if (slot->key.data == NULL) {
539 return PSA_ERROR_INSUFFICIENT_MEMORY;
540 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200541
Ronald Cronea0f8a62020-11-25 17:52:23 +0100542 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200544}
545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
547 const uint8_t *data,
548 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200549{
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 psa_status_t status = psa_allocate_buffer_to_slot(slot,
551 data_length);
552 if (status != PSA_SUCCESS) {
553 return status;
554 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 memcpy(slot->key.data, data, data_length);
557 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200558}
559
Ronald Crona0fe59f2020-11-29 11:14:53 +0100560psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100561 const psa_key_attributes_t *attributes,
562 const uint8_t *data, size_t data_length,
563 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100565{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100566 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
567 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100568
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200569 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if (data_length == 0) {
571 return PSA_ERROR_NOT_SUPPORTED;
572 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if (key_type_is_raw_bytes(type)) {
575 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
578 *bits);
579 if (status != PSA_SUCCESS) {
580 return status;
581 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200582
Ronald Crondd04d422020-11-28 15:14:42 +0100583 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100585 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return PSA_SUCCESS;
589 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800590#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
592 if (PSA_KEY_TYPE_IS_ECC(type)) {
593 return mbedtls_psa_ecp_import_key(attributes,
594 data, data_length,
595 key_buffer, key_buffer_size,
596 key_buffer_length,
597 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100598 }
John Durkop9814fa22020-11-04 12:28:15 -0800599#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
600 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700601#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
603 if (PSA_KEY_TYPE_IS_RSA(type)) {
604 return mbedtls_psa_rsa_import_key(attributes,
605 data, data_length,
606 key_buffer, key_buffer_size,
607 key_buffer_length,
608 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200609 }
John Durkop9814fa22020-11-04 12:28:15 -0800610#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
611 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100612 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100615}
616
Gilles Peskinef603c712019-01-19 13:40:11 +0100617/** Calculate the intersection of two algorithm usage policies.
618 *
619 * Return 0 (which allows no operation) on incompatibility.
620 */
621static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100622 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100623 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100625{
Gilles Peskine549ea862019-05-22 11:45:59 +0200626 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 if (alg1 == alg2) {
628 return alg1;
629 }
Steven Cooreman03488022021-02-18 12:07:20 +0100630 /* If the policies are from the same hash-and-sign family, check
631 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
633 PSA_ALG_IS_SIGN_HASH(alg2) &&
634 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
635 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
636 return alg2;
637 }
638 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
639 return alg1;
640 }
Steven Cooreman03488022021-02-18 12:07:20 +0100641 }
642 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100643 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100644 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
646 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
647 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
648 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
649 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100650 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100651
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100652 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
654 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
655 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
656 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100657 }
658 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
660 (alg1_len <= alg2_len)) {
661 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100662 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
664 (alg2_len <= alg1_len)) {
665 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100666 }
667 }
668 /* If the policies are from the same MAC family, check whether one
669 * of them is a minimum-MAC-length policy. Calculate the most
670 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
672 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
673 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100674 /* Validate the combination of key type and algorithm. Since the base
675 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
677 return 0;
678 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100679
Steven Cooreman31a876d2021-03-03 20:47:40 +0100680 /* Get the (exact or at-least) output lengths for both sides of the
681 * requested intersection. None of the currently supported algorithms
682 * have an output length dependent on the actual key size, so setting it
683 * to a bogus value of 0 is currently OK.
684 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100685 * Note that for at-least-this-length wildcard algorithms, the output
686 * length is set to the shortest allowed length, which allows us to
687 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
689 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100690 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100691
Steven Cooremana1d83222021-02-25 10:20:29 +0100692 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
694 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
695 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100696 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100697
698 /* If only one is an at-least-this-length policy, the intersection would
699 * be the other (fixed-length) policy as long as said fixed length is
700 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
702 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100703 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
705 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100706 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100707
Steven Cooremancd640932021-03-08 12:00:27 +0100708 /* If none of them are wildcards, check whether they define the same tag
709 * length. This is still possible here when one is default-length and
710 * the other specific-length. Ensure to always return the
711 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (alg1_len == alg2_len) {
713 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
714 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100715 }
716 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100718}
719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720static int psa_key_algorithm_permits(psa_key_type_t key_type,
721 psa_algorithm_t policy_alg,
722 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200723{
Gilles Peskine549ea862019-05-22 11:45:59 +0200724 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 if (requested_alg == policy_alg) {
726 return 1;
727 }
Steven Cooreman03488022021-02-18 12:07:20 +0100728 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
729 * and requested_alg is the same hash-and-sign family with any hash,
730 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
732 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
733 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
734 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100735 }
736 /* If policy_alg is a wildcard AEAD algorithm of the same base as
737 * the requested algorithm, check the requested tag length to be
738 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 if (PSA_ALG_IS_AEAD(policy_alg) &&
740 PSA_ALG_IS_AEAD(requested_alg) &&
741 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
742 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
743 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
744 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
745 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100746 }
Steven Cooremancd640932021-03-08 12:00:27 +0100747 /* If policy_alg is a MAC algorithm of the same base as the requested
748 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 if (PSA_ALG_IS_MAC(policy_alg) &&
750 PSA_ALG_IS_MAC(requested_alg) &&
751 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
752 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100753 /* Validate the combination of key type and algorithm. Since the policy
754 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
756 return 0;
757 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100758
Steven Cooreman31a876d2021-03-03 20:47:40 +0100759 /* Get both the requested output length for the algorithm which is to be
760 * verified, and the default output length for the base algorithm.
761 * Note that none of the currently supported algorithms have an output
762 * length dependent on actual key size, so setting it to a bogus value
763 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100764 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100766 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 key_type, 0,
768 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100769
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100770 /* If the policy is default-length, only allow an algorithm with
771 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
773 return requested_output_length == default_output_length;
774 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100775
776 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100777 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
779 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
780 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100781 }
782
Steven Cooremancd640932021-03-08 12:00:27 +0100783 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
784 * check for the requested MAC length to be equal to or longer than the
785 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
787 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
788 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100789 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200790 }
Steven Cooremance48e852020-10-05 16:02:45 +0200791 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200792 * a key derivation with that key agreement should also be allowed. This
793 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
795 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
796 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
797 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200798 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100799 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200801}
802
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100803/** Test whether a policy permits an algorithm.
804 *
805 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100806 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100807 * \note This function requires providing the key type for which the policy is
808 * being validated, since some algorithm policy definitions (e.g. MAC)
809 * have different properties depending on what kind of cipher it is
810 * combined with.
811 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100812 * \retval PSA_SUCCESS When \p alg is a specific algorithm
813 * allowed by the \p policy.
814 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
815 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
816 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100817 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100818static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
819 psa_key_type_t key_type,
820 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100821{
Steven Cooremand788fab2021-02-25 11:29:17 +0100822 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 if (alg == 0) {
824 return PSA_ERROR_INVALID_ARGUMENT;
825 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100826
Steven Cooreman7e39f052021-02-23 14:18:32 +0100827 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 if (PSA_ALG_IS_WILDCARD(alg)) {
829 return PSA_ERROR_INVALID_ARGUMENT;
830 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
833 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
834 return PSA_SUCCESS;
835 } else {
836 return PSA_ERROR_NOT_PERMITTED;
837 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100838}
839
Gilles Peskinef603c712019-01-19 13:40:11 +0100840/** Restrict a key policy based on a constraint.
841 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100842 * \note This function requires providing the key type for which the policy is
843 * being restricted, since some algorithm policy definitions (e.g. MAC)
844 * have different properties depending on what kind of cipher it is
845 * combined with.
846 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100847 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100848 * \param[in,out] policy The policy to restrict.
849 * \param[in] constraint The policy constraint to apply.
850 *
851 * \retval #PSA_SUCCESS
852 * \c *policy contains the intersection of the original value of
853 * \c *policy and \c *constraint.
854 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100855 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100856 * \c *policy is unchanged.
857 */
858static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100859 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100860 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100862{
863 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 psa_key_policy_algorithm_intersection(key_type, policy->alg,
865 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200866 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
868 constraint->alg2);
869 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
870 return PSA_ERROR_INVALID_ARGUMENT;
871 }
872 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
873 return PSA_ERROR_INVALID_ARGUMENT;
874 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100875 policy->usage &= constraint->usage;
876 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200877 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100879}
880
Przemek Stekiel348410f2022-11-15 22:22:07 +0100881psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100882 mbedtls_svc_key_id_t key,
883 psa_key_slot_t **p_slot,
884 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100886{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200887 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
888 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100889
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 status = psa_get_and_lock_key_slot(key, p_slot);
891 if (status != PSA_SUCCESS) {
892 return status;
893 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200894 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100895
896 /* Enforce that usage policy for the key slot contains all the flags
897 * required by the usage parameter. There is one exception: public
898 * keys can always be exported, so we treat public key objects as
899 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100901 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200903
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100905 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200906 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100907 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100908
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800909 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 if (alg != 0) {
911 status = psa_key_policy_permits(&slot->attr.policy,
912 slot->attr.type,
913 alg);
914 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100915 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100917 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200920
921error:
922 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100926}
Darryl Green940d72c2018-07-13 13:18:51 +0100927
Ronald Cron5c522922020-11-14 16:35:34 +0100928/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200929 *
930 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200931 * available, as opposed to a key in a secure element and/or to be used
932 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200933 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200934 * This is a temporary function that may be used instead of
935 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
936 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200937 *
Ronald Cron5c522922020-11-14 16:35:34 +0100938 * On success, the returned key slot is locked. It is the responsibility of the
939 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200940 */
Ronald Cron5c522922020-11-14 16:35:34 +0100941static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
942 mbedtls_svc_key_id_t key,
943 psa_key_slot_t **p_slot,
944 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +0200946{
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
948 usage, alg);
949 if (status != PSA_SUCCESS) {
950 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +0200951 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
954 psa_unlock_key_slot(*p_slot);
955 *p_slot = NULL;
956 return PSA_ERROR_NOT_SUPPORTED;
957 }
958
959 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +0200960}
Gilles Peskine28f8f302019-07-24 13:30:31 +0200961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +0000963{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100964 /* Data pointer will always be either a valid pointer or NULL in an
965 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 if (slot->key.data != NULL) {
967 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
968 }
Ronald Cronea0f8a62020-11-25 17:52:23 +0100969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +0100971 slot->key.data = NULL;
972 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +0000973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +0000975}
976
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100977/** Completely wipe a slot in memory, including its policy.
978 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100979psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100980{
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +0100982
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 /*
984 * As the return error code may not be handled in case of multiple errors,
985 * do our best to report an unexpected lock counter. Assert with
986 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
987 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
988 * function is called as part of the execution of a test suite, the
989 * execution of the test suite is stopped in error if the assertion fails.
990 */
991 if (slot->lock_count != 1) {
992 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +0100993 status = PSA_ERROR_CORRUPTION_DETECTED;
994 }
995
Gilles Peskine3f7cd622019-08-13 15:01:08 +0200996 /* Multipart operations may still be using the key. This is safe
997 * because all multipart operation objects are independent from
998 * the key slot: if they need to access the key after the setup
999 * phase, they have a copy of the key. Note that this means that
1000 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001001 /* At this point, key material and other type-specific content has
1002 * been wiped. Clear remaining metadata. We can call memset and not
1003 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 memset(slot, 0, sizeof(*slot));
1005 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001006}
1007
Gilles Peskine449bd832023-01-11 14:50:10 +01001008psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001009{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001010 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001011 psa_status_t status; /* status of the last operation */
1012 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001013#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1014 psa_se_drv_table_entry_t *driver;
1015#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001016
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 if (mbedtls_svc_key_id_is_null(key)) {
1018 return PSA_SUCCESS;
1019 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001020
Ronald Cronf2911112020-10-29 17:51:10 +01001021 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001022 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001023 * key, this will load the key description from persistent memory if not
1024 * done yet. We cannot avoid this loading as without it we don't know if
1025 * the key is operated by an SE or not and this information is needed by
1026 * the current implementation.
1027 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 status = psa_get_and_lock_key_slot(key, &slot);
1029 if (status != PSA_SUCCESS) {
1030 return status;
1031 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001032
Ronald Cronf2911112020-10-29 17:51:10 +01001033 /*
1034 * If the key slot containing the key description is under access by the
1035 * library (apart from the present access), the key cannot be destroyed
1036 * yet. For the time being, just return in error. Eventually (to be
1037 * implemented), the key should be destroyed when all accesses have
1038 * stopped.
1039 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 if (slot->lock_count > 1) {
1041 psa_unlock_key_slot(slot);
1042 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001043 }
1044
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001046 /* Refuse the destruction of a read-only key (which may or may not work
1047 * if we attempt it, depending on whether the key is merely read-only
1048 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001049 * Just do the best we can, which is to wipe the copy in memory
1050 * (done in this function's cleanup code). */
1051 overall_status = PSA_ERROR_NOT_PERMITTED;
1052 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001053 }
1054
Gilles Peskine354f7672019-07-12 23:46:38 +02001055#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1057 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001058 /* For a key in a secure element, we need to do three things:
1059 * remove the key file in internal storage, destroy the
1060 * key inside the secure element, and update the driver's
1061 * persistent data. Start a transaction that will encompass these
1062 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001064 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001066 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 status = psa_crypto_save_transaction();
1068 if (status != PSA_SUCCESS) {
1069 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001070 /* We should still try to destroy the key in the secure
1071 * element and the key metadata in storage. This is especially
1072 * important if the error is that the storage is full.
1073 * But how to do it exactly without risking an inconsistent
1074 * state after a reset?
1075 * https://github.com/ARMmbed/mbed-crypto/issues/215
1076 */
1077 overall_status = status;
1078 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001079 }
1080
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 status = psa_destroy_se_key(driver,
1082 psa_key_slot_get_slot_number(slot));
1083 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001084 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001086 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001087#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1088
Darryl Greend49a4992018-06-18 17:27:26 +01001089#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1091 status = psa_destroy_persistent_key(slot->attr.id);
1092 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001093 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001095
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001096 /* TODO: other slots may have a copy of the same key. We should
1097 * invalidate them.
1098 * https://github.com/ARMmbed/mbed-crypto/issues/214
1099 */
Darryl Greend49a4992018-06-18 17:27:26 +01001100 }
1101#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001102
Gilles Peskinefc762652019-07-22 19:30:34 +02001103#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 if (driver != NULL) {
1105 status = psa_save_se_persistent_data(driver);
1106 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001107 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 }
1109 status = psa_crypto_stop_transaction();
1110 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001111 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001113 }
1114#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1115
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001116exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001118 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001120 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 }
1122 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001123}
1124
John Durkop07cc04a2020-11-16 22:08:34 -08001125#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001126 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001127static psa_status_t psa_get_rsa_public_exponent(
1128 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001130{
1131 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001132 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001133 uint8_t *buffer = NULL;
1134 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1138 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001139 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 }
1141 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001142 /* It's the default value, which is reported as an empty string,
1143 * so there's nothing to do. */
1144 goto exit;
1145 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 buflen = mbedtls_mpi_size(&mpi);
1148 buffer = mbedtls_calloc(1, buflen);
1149 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001150 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1151 goto exit;
1152 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1154 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001155 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001157 attributes->domain_parameters = buffer;
1158 attributes->domain_parameters_size = buflen;
1159
1160exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 mbedtls_mpi_free(&mpi);
1162 if (ret != 0) {
1163 mbedtls_free(buffer);
1164 }
1165 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001166}
John Durkop07cc04a2020-11-16 22:08:34 -08001167#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001168 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001169
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001170/** Retrieve all the publicly-accessible attributes of a key.
1171 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001172psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1173 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001174{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001175 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001176 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001177 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1182 if (status != PSA_SUCCESS) {
1183 return status;
1184 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001185
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001186 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1188 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001189
1190#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1192 psa_set_key_slot_number(attributes,
1193 psa_key_slot_get_slot_number(slot));
1194 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001195#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001198#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001199 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001200 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001201 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001202 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001203 * is not yet implemented.
1204 * https://github.com/ARMmbed/mbed-crypto/issues/216
1205 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001207 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001208
Ronald Cron90857082020-11-25 14:58:33 +01001209 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 slot->attr.type,
1211 slot->key.data,
1212 slot->key.bytes,
1213 &rsa);
1214 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001215 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 status = psa_get_rsa_public_exponent(rsa,
1219 attributes);
1220 mbedtls_rsa_free(rsa);
1221 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001222 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001223 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001224#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001225 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001226 default:
1227 /* Nothing else to do. */
1228 break;
1229 }
1230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 if (status != PSA_SUCCESS) {
1232 psa_reset_key_attributes(attributes);
1233 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001236
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001238}
1239
Gilles Peskinec8000c02019-08-02 20:15:51 +02001240#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1241psa_status_t psa_get_key_slot_number(
1242 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001244{
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001246 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 return PSA_SUCCESS;
1248 } else {
1249 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001250 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001251}
1252#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1255 size_t key_buffer_size,
1256 uint8_t *data,
1257 size_t data_size,
1258 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001259{
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 if (key_buffer_size > data_size) {
1261 return PSA_ERROR_BUFFER_TOO_SMALL;
1262 }
1263 memcpy(data, key_buffer, key_buffer_size);
1264 memset(data + key_buffer_size, 0,
1265 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001266 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001268}
1269
Ronald Cron7285cda2020-11-26 14:46:37 +01001270psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001271 const psa_key_attributes_t *attributes,
1272 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001274{
Ronald Crond18b5f82020-11-25 16:46:05 +01001275 psa_key_type_t type = attributes->core.type;
1276
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 if (key_type_is_raw_bytes(type) ||
1278 PSA_KEY_TYPE_IS_RSA(type) ||
1279 PSA_KEY_TYPE_IS_ECC(type)) {
1280 return psa_export_key_buffer_internal(
1281 key_buffer, key_buffer_size,
1282 data, data_size, data_length);
1283 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001284 /* This shouldn't happen in the reference implementation, but
1285 it is valid for a special-purpose implementation to omit
1286 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001288 }
1289}
1290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1292 uint8_t *data,
1293 size_t data_size,
1294 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001295{
1296 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1297 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1298 psa_key_slot_t *slot;
1299
Ronald Crone907e552021-01-18 13:22:38 +01001300 /* Reject a zero-length output buffer now, since this can never be a
1301 * valid key representation. This way we know that data must be a valid
1302 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 if (data_size == 0) {
1304 return PSA_ERROR_BUFFER_TOO_SMALL;
1305 }
Ronald Crone907e552021-01-18 13:22:38 +01001306
Ronald Cron9486f9d2020-11-26 13:36:23 +01001307 /* Set the key to empty now, so that even when there are errors, we always
1308 * set data_length to a value between 0 and data_size. On error, setting
1309 * the key to empty is a good choice because an empty key representation is
1310 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001311 *data_length = 0;
1312
Ronald Cron9486f9d2020-11-26 13:36:23 +01001313 /* Export requires the EXPORT flag. There is an exception for public keys,
1314 * which don't require any flag, but
1315 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1316 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1318 PSA_KEY_USAGE_EXPORT, 0);
1319 if (status != PSA_SUCCESS) {
1320 return status;
1321 }
mohammad160306e79202018-03-28 13:17:44 +03001322
Ronald Crond18b5f82020-11-25 16:46:05 +01001323 psa_key_attributes_t attributes = {
1324 .core = slot->attr
1325 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 status = psa_driver_wrapper_export_key(&attributes,
1327 slot->key.data, slot->key.bytes,
1328 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001329
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001333}
1334
Ronald Cron7285cda2020-11-26 14:46:37 +01001335psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001336 const psa_key_attributes_t *attributes,
1337 const uint8_t *key_buffer,
1338 size_t key_buffer_size,
1339 uint8_t *data,
1340 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001342{
Ronald Crond18b5f82020-11-25 16:46:05 +01001343 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1346 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001347 /* Exporting public -> public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 return psa_export_key_buffer_internal(
1349 key_buffer, key_buffer_size,
1350 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001351 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001352
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001354#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1356 return mbedtls_psa_rsa_export_public_key(attributes,
1357 key_buffer,
1358 key_buffer_size,
1359 data,
1360 data_size,
1361 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001362#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001363 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001365#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1366 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001368#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1370 return mbedtls_psa_ecp_export_public_key(attributes,
1371 key_buffer,
1372 key_buffer_size,
1373 data,
1374 data_size,
1375 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001376#else
1377 /* We don't know how to convert a private ECC key to public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001379#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1380 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001381 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001383 /* This shouldn't happen in the reference implementation, but
1384 it is valid for a special-purpose implementation to omit
1385 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001387 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001388}
Ronald Crond18b5f82020-11-25 16:46:05 +01001389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1391 uint8_t *data,
1392 size_t data_size,
1393 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001394{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001395 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001396 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001397 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001398
Ronald Crone907e552021-01-18 13:22:38 +01001399 /* Reject a zero-length output buffer now, since this can never be a
1400 * valid key representation. This way we know that data must be a valid
1401 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 if (data_size == 0) {
1403 return PSA_ERROR_BUFFER_TOO_SMALL;
1404 }
Ronald Crone907e552021-01-18 13:22:38 +01001405
Darryl Greendd8fb772018-11-07 16:00:44 +00001406 /* Set the key to empty now, so that even when there are errors, we always
1407 * set data_length to a value between 0 and data_size. On error, setting
1408 * the key to empty is a good choice because an empty key representation is
1409 * unlikely to be accepted anywhere. */
1410 *data_length = 0;
1411
1412 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1414 if (status != PSA_SUCCESS) {
1415 return status;
1416 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1419 status = PSA_ERROR_INVALID_ARGUMENT;
1420 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001421 }
1422
Ronald Crond18b5f82020-11-25 16:46:05 +01001423 psa_key_attributes_t attributes = {
1424 .core = slot->attr
1425 };
Ronald Cron67227982020-11-26 15:16:05 +01001426 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001427 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001429
1430exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001434}
1435
Gilles Peskine91e8c332019-08-02 19:19:39 +02001436#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001437static_assert((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1438 "One or more key attribute flag is listed as both external-only and dual-use");
1439static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1440 "One or more key attribute flag is listed as both internal-only and dual-use");
1441static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1442 "One or more key attribute flag is listed as both internal-only and external-only");
Gilles Peskine91e8c332019-08-02 19:19:39 +02001443#endif
1444
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001445/** Validate that a key policy is internally well-formed.
1446 *
1447 * This function only rejects invalid policies. It does not validate the
1448 * consistency of the policy with respect to other attributes of the key
1449 * such as the key type.
1450 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001451static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001452{
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1454 PSA_KEY_USAGE_COPY |
1455 PSA_KEY_USAGE_ENCRYPT |
1456 PSA_KEY_USAGE_DECRYPT |
1457 PSA_KEY_USAGE_SIGN_MESSAGE |
1458 PSA_KEY_USAGE_VERIFY_MESSAGE |
1459 PSA_KEY_USAGE_SIGN_HASH |
1460 PSA_KEY_USAGE_VERIFY_HASH |
1461 PSA_KEY_USAGE_VERIFY_DERIVATION |
1462 PSA_KEY_USAGE_DERIVE)) != 0) {
1463 return PSA_ERROR_INVALID_ARGUMENT;
1464 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001465
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001467}
1468
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001469/** Validate the internal consistency of key attributes.
1470 *
1471 * This function only rejects invalid attribute values. If does not
1472 * validate the consistency of the attributes with any key data that may
1473 * be involved in the creation of the key.
1474 *
1475 * Call this function early in the key creation process.
1476 *
1477 * \param[in] attributes Key attributes for the new key.
1478 * \param[out] p_drv On any return, the driver for the key, if any.
1479 * NULL for a transparent key.
1480 *
1481 */
1482static psa_status_t psa_validate_key_attributes(
1483 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001485{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001486 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1488 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 status = psa_validate_key_location(lifetime, p_drv);
1491 if (status != PSA_SUCCESS) {
1492 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001493 }
1494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 status = psa_validate_key_persistence(lifetime);
1496 if (status != PSA_SUCCESS) {
1497 return status;
1498 }
1499
1500 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1501 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1502 return PSA_ERROR_INVALID_ARGUMENT;
1503 }
1504 } else {
1505 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1506 return PSA_ERROR_INVALID_ARGUMENT;
1507 }
1508 }
1509
1510 status = psa_validate_key_policy(&attributes->core.policy);
1511 if (status != PSA_SUCCESS) {
1512 return status;
1513 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001514
1515 /* Refuse to create overly large keys.
1516 * Note that this doesn't trigger on import if the attributes don't
1517 * explicitly specify a size (so psa_get_key_bits returns 0), so
1518 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1520 return PSA_ERROR_NOT_SUPPORTED;
1521 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001522
Gilles Peskine91e8c332019-08-02 19:19:39 +02001523 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1525 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1526 return PSA_ERROR_INVALID_ARGUMENT;
1527 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001530}
1531
Gilles Peskine4747d192019-04-17 15:05:45 +02001532/** Prepare a key slot to receive key material.
1533 *
1534 * This function allocates a key slot and sets its metadata.
1535 *
1536 * If this function fails, call psa_fail_key_creation().
1537 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001538 * This function is intended to be used as follows:
1539 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001540 * it with the specified attributes, and in case of a volatile key assign it
1541 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001542 * -# Populate the slot with the key material.
1543 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1544 * In case of failure at any step, stop the sequence and call
1545 * psa_fail_key_creation().
1546 *
Ronald Cron5c522922020-11-14 16:35:34 +01001547 * On success, the key slot is locked. It is the responsibility of the caller
1548 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001549 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001550 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001551 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001552 * \param[out] p_slot On success, a pointer to the prepared slot.
1553 * \param[out] p_drv On any return, the driver for the key, if any.
1554 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001555 *
1556 * \retval #PSA_SUCCESS
1557 * The key slot is ready to receive key material.
1558 * \return If this function fails, the key slot is an invalid state.
1559 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001560 */
1561static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001562 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001563 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001564 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001566{
1567 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001568 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001569 psa_key_slot_t *slot;
1570
Gilles Peskinedf179142019-07-15 22:02:14 +02001571 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001572 *p_drv = NULL;
1573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 status = psa_validate_key_attributes(attributes, p_drv);
1575 if (status != PSA_SUCCESS) {
1576 return status;
1577 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001578
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1580 if (status != PSA_SUCCESS) {
1581 return status;
1582 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001583 slot = *p_slot;
1584
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001585 /* We're storing the declared bit-size of the key. It's up to each
1586 * creation mechanism to verify that this information is correct.
1587 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001588 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001589 * is optional (import, copy). In case of a volatile key, assign it the
1590 * volatile key identifier associated to the slot returned to contain its
1591 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001592
1593 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001595#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1596 slot->attr.id = volatile_key_id;
1597#else
1598 slot->attr.id.key_id = volatile_key_id;
1599#endif
1600 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001601
Gilles Peskine91e8c332019-08-02 19:19:39 +02001602 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001603 * external-only flags, query `attributes`. Thanks to the check
1604 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001605 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001606 * may have set. */
1607 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001608
Gilles Peskinecbaff462019-07-12 23:46:04 +02001609#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001610 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001611 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001612 * create the key file in internal storage, create the
1613 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001614 * persistent data. This is done by starting a transaction that will
1615 * encompass these three actions.
1616 * For registering a volatile key, we just need to find an appropriate
1617 * slot number inside the SE. Since the key is designated volatile, creating
1618 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001619 /* The first thing to do is to find a slot number for the new key.
1620 * We save the slot number in persistent storage as part of the
1621 * transaction data. It will be needed to recover if the power
1622 * fails during the key creation process, to clean up on the secure
1623 * element side after restarting. Obtaining a slot number from the
1624 * secure element driver updates its persistent state, but we do not yet
1625 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001626 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001628 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1630 &slot_number);
1631 if (status != PSA_SUCCESS) {
1632 return status;
1633 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001634
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1636 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001637 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001638 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001639 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 status = psa_crypto_save_transaction();
1641 if (status != PSA_SUCCESS) {
1642 (void) psa_crypto_stop_transaction();
1643 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001644 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001645 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001646
1647 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001649 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001650
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001652 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001654 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001655#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1656
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001658}
Gilles Peskine4747d192019-04-17 15:05:45 +02001659
1660/** Finalize the creation of a key once its key material has been set.
1661 *
1662 * This entails writing the key to persistent storage.
1663 *
1664 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001665 * See the documentation of psa_start_key_creation() for the intended use
1666 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001667 *
Ronald Cron5c522922020-11-14 16:35:34 +01001668 * If the finalization succeeds, the function unlocks the key slot (it was
1669 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1670 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001671 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001672 * \param[in,out] slot Pointer to the slot with key material.
1673 * \param[in] driver The secure element driver for the key,
1674 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001675 * \param[out] key On success, identifier of the key. Note that the
1676 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001677 *
1678 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001679 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001680 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1681 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1682 * \retval #PSA_ERROR_ALREADY_EXISTS
1683 * \retval #PSA_ERROR_DATA_INVALID
1684 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001685 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001686 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001687 * \return If this function fails, the key slot is an invalid state.
1688 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001689 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001690static psa_status_t psa_finish_key_creation(
1691 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001692 psa_se_drv_table_entry_t *driver,
1693 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001694{
1695 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001696 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001697 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001698
1699#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001701#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001703 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001704 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001706
Gilles Peskineb46bef22019-07-30 21:32:04 +02001707#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 static_assert(sizeof(slot_number) ==
1709 sizeof(data.slot_number),
1710 "Slot number size does not match psa_se_key_data_storage_t");
Gilles Peskineb46bef22019-07-30 21:32:04 +02001711#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1713 status = psa_save_persistent_key(&slot->attr,
1714 (uint8_t *) &data,
1715 sizeof(data));
1716 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001717#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1718 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001719 /* Key material is saved in export representation in the slot, so
1720 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 status = psa_save_persistent_key(&slot->attr,
1722 slot->key.data,
1723 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001724 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001725 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001726#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1727
Gilles Peskinecbaff462019-07-12 23:46:04 +02001728#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001729 /* Finish the transaction for a key creation. This does not
1730 * happen when registering an existing key. Detect this case
1731 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001732 * creation of a persistent key in a secure element requires a transaction,
1733 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 if (driver != NULL &&
1735 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1736 status = psa_save_se_persistent_data(driver);
1737 if (status != PSA_SUCCESS) {
1738 psa_destroy_persistent_key(slot->attr.id);
1739 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001740 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001742 }
1743#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1744
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001746 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 status = psa_unlock_key_slot(slot);
1748 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001749 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001751 }
Ronald Cron50972942020-11-14 11:28:25 +01001752
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001754}
1755
1756/** Abort the creation of a key.
1757 *
1758 * You may call this function after calling psa_start_key_creation(),
1759 * or after psa_finish_key_creation() fails. In other circumstances, this
1760 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001761 * See the documentation of psa_start_key_creation() for the intended use
1762 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001763 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001764 * \param[in,out] slot Pointer to the slot with key material.
1765 * \param[in] driver The secure element driver for the key,
1766 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001767 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001768static void psa_fail_key_creation(psa_key_slot_t *slot,
1769 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001770{
Gilles Peskine011e4282019-06-26 18:34:38 +02001771 (void) driver;
1772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001774 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001776
1777#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001778 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001779 * element, and the failure happened later (when saving metadata
1780 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001781 * element.
1782 * https://github.com/ARMmbed/mbed-crypto/issues/217
1783 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001784
Gilles Peskined7729582019-08-05 15:55:54 +02001785 /* Abort the ongoing transaction if any (there may not be one if
1786 * the creation process failed before starting one, or if the
1787 * key creation is a registration of a key in a secure element).
1788 * Earlier functions must already have done what it takes to undo any
1789 * partial creation. All that's left is to update the transaction data
1790 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001792#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1793
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001795}
1796
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001797/** Validate optional attributes during key creation.
1798 *
1799 * Some key attributes are optional during key creation. If they are
1800 * specified in the attributes structure, check that they are consistent
1801 * with the data in the slot.
1802 *
1803 * This function should be called near the end of key creation, after
1804 * the slot in memory is fully populated but before saving persistent data.
1805 */
1806static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001807 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001809{
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 if (attributes->core.type != 0) {
1811 if (attributes->core.type != slot->attr.type) {
1812 return PSA_ERROR_INVALID_ARGUMENT;
1813 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001814 }
1815
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001817#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1819 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001820 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001821 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001822 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001823
Ronald Cron90857082020-11-25 14:58:33 +01001824 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 slot->attr.type,
1826 slot->key.data,
1827 slot->key.bytes,
1828 &rsa);
1829 if (status != PSA_SUCCESS) {
1830 return status;
1831 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001832
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 mbedtls_mpi_init(&actual);
1834 mbedtls_mpi_init(&required);
1835 ret = mbedtls_rsa_export(rsa,
1836 NULL, NULL, NULL, NULL, &actual);
1837 mbedtls_rsa_free(rsa);
1838 mbedtls_free(rsa);
1839 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001840 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 }
1842 ret = mbedtls_mpi_read_binary(&required,
1843 attributes->domain_parameters,
1844 attributes->domain_parameters_size);
1845 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001846 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 }
1848 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001849 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 }
1851rsa_exit:
1852 mbedtls_mpi_free(&actual);
1853 mbedtls_mpi_free(&required);
1854 if (ret != 0) {
1855 return mbedtls_to_psa_error(ret);
1856 }
1857 } else
John Durkop9814fa22020-11-04 12:28:15 -08001858#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1859 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001860 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001862 }
1863 }
1864
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 if (attributes->core.bits != 0) {
1866 if (attributes->core.bits != slot->attr.bits) {
1867 return PSA_ERROR_INVALID_ARGUMENT;
1868 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001869 }
1870
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001872}
1873
Gilles Peskine449bd832023-01-11 14:50:10 +01001874psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1875 const uint8_t *data,
1876 size_t data_length,
1877 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001878{
1879 psa_status_t status;
1880 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001881 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001882 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301883 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001884
Ronald Cron81709fc2020-11-14 12:10:32 +01001885 *key = MBEDTLS_SVC_KEY_ID_INIT;
1886
Gilles Peskine0f84d622019-09-12 19:03:13 +02001887 /* Reject zero-length symmetric keys (including raw data key objects).
1888 * This also rejects any key which might be encoded as an empty string,
1889 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 if (data_length == 0) {
1891 return PSA_ERROR_INVALID_ARGUMENT;
1892 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001893
Archana449608b2021-09-08 15:36:05 +05301894 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 if (data_length > SIZE_MAX / 8) {
1896 return PSA_ERROR_NOT_SUPPORTED;
1897 }
Archana6ed4bda2021-08-04 10:47:15 +05301898
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1900 &slot, &driver);
1901 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001902 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001904
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001905 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301906 * storage ( thus not in the case of importing a key in a secure element
1907 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1908 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 if (slot->key.data == NULL) {
1910 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301911 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 attributes, data, data_length, &storage_size);
1913 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301914 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 }
Archanad8a83dc2021-06-14 10:04:16 +05301916 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 status = psa_allocate_buffer_to_slot(slot, storage_size);
1918 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001919 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001921 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001922
1923 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 status = psa_driver_wrapper_import_key(attributes,
1925 data, data_length,
1926 slot->key.data,
1927 slot->key.bytes,
1928 &slot->key.bytes, &bits);
1929 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001930 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001932
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001934 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001936 status = PSA_ERROR_INVALID_ARGUMENT;
1937 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001938 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001939
Archana6ed4bda2021-08-04 10:47:15 +05301940 /* Enforce a size limit, and in particular ensure that the bit
1941 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05301943 status = PSA_ERROR_NOT_SUPPORTED;
1944 goto exit;
1945 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 status = psa_validate_optional_attributes(slot, attributes);
1947 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02001948 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001950
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001952exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 if (status != PSA_SUCCESS) {
1954 psa_fail_key_creation(slot, driver);
1955 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001956
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001958}
1959
Gilles Peskined7729582019-08-05 15:55:54 +02001960#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1961psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02001963{
1964 psa_status_t status;
1965 psa_key_slot_t *slot = NULL;
1966 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001967 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02001968
1969 /* Leaving attributes unspecified is not currently supported.
1970 * It could make sense to query the key type and size from the
1971 * secure element, but not all secure elements support this
1972 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
1974 return PSA_ERROR_NOT_SUPPORTED;
1975 }
1976 if (psa_get_key_bits(attributes) == 0) {
1977 return PSA_ERROR_NOT_SUPPORTED;
1978 }
Gilles Peskined7729582019-08-05 15:55:54 +02001979
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
1981 &slot, &driver);
1982 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02001983 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 }
Gilles Peskined7729582019-08-05 15:55:54 +02001985
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02001987
1988exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 if (status != PSA_SUCCESS) {
1990 psa_fail_key_creation(slot, driver);
1991 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001992
Gilles Peskined7729582019-08-05 15:55:54 +02001993 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 psa_close_key(key);
1995 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02001996}
1997#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1998
Gilles Peskine449bd832023-01-11 14:50:10 +01001999psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2000 const psa_key_attributes_t *specified_attributes,
2001 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002002{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002003 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002004 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002005 psa_key_slot_t *source_slot = NULL;
2006 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002007 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002008 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302009 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002010
Ronald Cron81709fc2020-11-14 12:10:32 +01002011 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2012
Archana8a180362021-07-05 02:18:48 +05302013 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2015 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002016 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002018
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 status = psa_validate_optional_attributes(source_slot,
2020 specified_attributes);
2021 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002022 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002024
Archana9d17bf42021-09-10 06:22:44 +05302025 /* The target key type and number of bits have been validated by
2026 * psa_validate_optional_attributes() to be either equal to zero or
2027 * equal to the ones of the source key. So it is safe to inherit
2028 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302029 * */
Archana9d17bf42021-09-10 06:22:44 +05302030 actual_attributes.core.bits = source_slot->attr.bits;
2031 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302032
2033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 status = psa_restrict_key_policy(source_slot->attr.type,
2035 &actual_attributes.core.policy,
2036 &source_slot->attr.policy);
2037 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002038 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002040
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2042 &target_slot, &driver);
2043 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002044 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 }
2046 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2047 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302048 /*
Archana9d17bf42021-09-10 06:22:44 +05302049 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302050 * the source key would need to be exported as plaintext and re-imported
2051 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302052 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302053 * appropriate API invocations from the application, if needed.
2054 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002055 status = PSA_ERROR_NOT_SUPPORTED;
2056 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002057 }
Archana8a180362021-07-05 02:18:48 +05302058 /*
2059 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302060 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302061 * - For opaque keys this translates to an invocation of the drivers'
2062 * copy_key entry point through the dispatch layer.
2063 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2065 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2066 &storage_size);
2067 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302068 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 }
Archana374fe5b2021-09-08 15:50:28 +05302070
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2072 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302073 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 }
Archana374fe5b2021-09-08 15:50:28 +05302075
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 status = psa_driver_wrapper_copy_key(&actual_attributes,
2077 source_slot->key.data,
2078 source_slot->key.bytes,
2079 target_slot->key.data,
2080 target_slot->key.bytes,
2081 &target_slot->key.bytes);
2082 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302083 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 }
2085 } else {
2086 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302087 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 source_slot->key.bytes);
2089 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302090 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 }
Archana8a180362021-07-05 02:18:48 +05302092 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002094exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 if (status != PSA_SUCCESS) {
2096 psa_fail_key_creation(target_slot, driver);
2097 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002098
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002100
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002102}
2103
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002104
2105
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002106/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002107/* Message digests */
2108/****************************************************************/
2109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002111{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002112 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 if (operation->id == 0) {
2114 return PSA_SUCCESS;
2115 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002116
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002118 operation->id = 0;
2119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002121}
2122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2124 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002125{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002126 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002127
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002128 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002130 status = PSA_ERROR_BAD_STATE;
2131 goto exit;
2132 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002133
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002135 status = PSA_ERROR_INVALID_ARGUMENT;
2136 goto exit;
2137 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002138
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002139 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2140 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002142
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002144
2145exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 if (status != PSA_SUCCESS) {
2147 psa_hash_abort(operation);
2148 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002149
2150 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002151}
2152
Gilles Peskine449bd832023-01-11 14:50:10 +01002153psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2154 const uint8_t *input,
2155 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002156{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002157 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2158
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002160 status = PSA_ERROR_BAD_STATE;
2161 goto exit;
2162 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002163
Steven Cooremanc8288352021-03-04 14:02:19 +01002164 /* Don't require hash implementations to behave correctly on a
2165 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if (input_length == 0) {
2167 return PSA_SUCCESS;
2168 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002169
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002171
2172exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 if (status != PSA_SUCCESS) {
2174 psa_hash_abort(operation);
2175 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002176
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002178}
2179
Gilles Peskine449bd832023-01-11 14:50:10 +01002180psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2181 uint8_t *hash,
2182 size_t hash_size,
2183 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002184{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002185 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 if (operation->id == 0) {
2187 return PSA_ERROR_BAD_STATE;
2188 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002189
Steven Cooreman1e582352021-02-18 17:24:37 +01002190 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 operation, hash, hash_size, hash_length);
2192 psa_hash_abort(operation);
2193 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002194}
2195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2197 const uint8_t *hash,
2198 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002199{
Ronald Cron69a63422021-10-18 09:47:58 +02002200 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002201 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002202 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 operation,
2204 actual_hash, sizeof(actual_hash),
2205 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002208 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002212 status = PSA_ERROR_INVALID_SIGNATURE;
2213 goto exit;
2214 }
2215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002217 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002219
2220exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2222 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002223 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002225
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002227}
2228
Gilles Peskine449bd832023-01-11 14:50:10 +01002229psa_status_t psa_hash_compute(psa_algorithm_t alg,
2230 const uint8_t *input, size_t input_length,
2231 uint8_t *hash, size_t hash_size,
2232 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002233{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002234 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 if (!PSA_ALG_IS_HASH(alg)) {
2236 return PSA_ERROR_INVALID_ARGUMENT;
2237 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002238
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2240 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002241}
2242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243psa_status_t psa_hash_compare(psa_algorithm_t alg,
2244 const uint8_t *input, size_t input_length,
2245 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002246{
Ronald Cron69a63422021-10-18 09:47:58 +02002247 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002248 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 if (!PSA_ALG_IS_HASH(alg)) {
2251 return PSA_ERROR_INVALID_ARGUMENT;
2252 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002253
Steven Cooreman1e582352021-02-18 17:24:37 +01002254 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 alg, input, input_length,
2256 actual_hash, sizeof(actual_hash),
2257 &actual_hash_length);
2258 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002259 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 }
2261 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002262 status = PSA_ERROR_INVALID_SIGNATURE;
2263 goto exit;
2264 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002266 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002268
2269exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2271 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002272}
2273
Gilles Peskine449bd832023-01-11 14:50:10 +01002274psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2275 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002276{
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 if (source_operation->id == 0 ||
2278 target_operation->id != 0) {
2279 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002280 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002281
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2283 target_operation);
2284 if (status != PSA_SUCCESS) {
2285 psa_hash_abort(target_operation);
2286 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002287
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002289}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002290
2291
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002292/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002293/* MAC */
2294/****************************************************************/
2295
Gilles Peskine449bd832023-01-11 14:50:10 +01002296psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002297{
Steven Cooremane6804192021-03-19 18:28:56 +01002298 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 if (operation->id == 0) {
2300 return PSA_SUCCESS;
2301 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002304 operation->mac_size = 0;
2305 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002306 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002307
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002309}
2310
Ronald Cron2dff3b22021-06-17 16:33:22 +02002311static psa_status_t psa_mac_finalize_alg_and_key_validation(
2312 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002313 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002315{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002316 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 psa_key_type_t key_type = psa_get_key_type(attributes);
2318 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 if (!PSA_ALG_IS_MAC(alg)) {
2321 return PSA_ERROR_INVALID_ARGUMENT;
2322 }
Steven Cooremane6804192021-03-19 18:28:56 +01002323
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002324 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 status = psa_mac_key_can_do(alg, key_type);
2326 if (status != PSA_SUCCESS) {
2327 return status;
2328 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002329
Steven Cooremand1a68f12021-05-10 11:13:41 +02002330 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002334 /* A very short MAC is too short for security since it can be
2335 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2336 * so we make this our minimum, even though 32 bits is still
2337 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002339 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2342 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002343 /* It's impossible to "truncate" to a larger length than the full length
2344 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002346 }
2347
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002349 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2350 * that is disabled in the compile-time configuration. The result can
2351 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2352 * configuration into account. In this case, force a return of
2353 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2354 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2355 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2356 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2357 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002359 }
2360
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002362}
2363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2365 mbedtls_svc_key_id_t key,
2366 psa_algorithm_t alg,
2367 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002368{
2369 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2370 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002371 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002372
2373 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002375 status = PSA_ERROR_BAD_STATE;
2376 goto exit;
2377 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002378
2379 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 key,
2381 &slot,
2382 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2383 alg);
2384 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002385 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002387
2388 psa_key_attributes_t attributes = {
2389 .core = slot->attr
2390 };
2391
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2393 &operation->mac_size);
2394 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002395 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002397
2398 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002399 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 if (is_sign) {
2401 status = psa_driver_wrapper_mac_sign_setup(operation,
2402 &attributes,
2403 slot->key.data,
2404 slot->key.bytes,
2405 alg);
2406 } else {
2407 status = psa_driver_wrapper_mac_verify_setup(operation,
2408 &attributes,
2409 slot->key.data,
2410 slot->key.bytes,
2411 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002412 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002413
Gilles Peskinefbfac682018-07-08 20:51:54 +02002414exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 if (status != PSA_SUCCESS) {
2416 psa_mac_abort(operation);
2417 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002422}
2423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2425 mbedtls_svc_key_id_t key,
2426 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002427{
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002429}
2430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2432 mbedtls_svc_key_id_t key,
2433 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002434{
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002436}
2437
Gilles Peskine449bd832023-01-11 14:50:10 +01002438psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2439 const uint8_t *input,
2440 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002441{
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 if (operation->id == 0) {
2443 return PSA_ERROR_BAD_STATE;
2444 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002445
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002446 /* Don't require hash implementations to behave correctly on a
2447 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 if (input_length == 0) {
2449 return PSA_SUCCESS;
2450 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002451
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2453 input, input_length);
2454 if (status != PSA_SUCCESS) {
2455 psa_mac_abort(operation);
2456 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002457
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002459}
2460
Gilles Peskine449bd832023-01-11 14:50:10 +01002461psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2462 uint8_t *mac,
2463 size_t mac_size,
2464 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002465{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002466 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2467 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002470 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002471 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002472 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002473
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002475 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002476 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002477 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002478
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002479 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2480 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002482 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002483 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002484 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002487 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002488 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002489 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002490
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 status = psa_driver_wrapper_mac_sign_finish(operation,
2492 mac, operation->mac_size,
2493 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002494
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002495exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002496 /* In case of success, set the potential excess room in the output buffer
2497 * to an invalid value, to avoid potentially leaking a longer MAC.
2498 * In case of error, set the output length and content to a safe default,
2499 * such that in case the caller misses an error check, the output would be
2500 * an unachievable MAC.
2501 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002503 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002504 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002505 }
mohammad16036df908f2018-04-02 08:34:15 -07002506
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 if (mac_size > operation->mac_size) {
2508 memset(&mac[operation->mac_size], '!',
2509 mac_size - operation->mac_size);
2510 }
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002513
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002515}
2516
Gilles Peskine449bd832023-01-11 14:50:10 +01002517psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2518 const uint8_t *mac,
2519 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002520{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002521 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2522 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002525 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002526 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002527 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002528
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002530 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002531 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002532 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002533
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002535 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002536 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002537 }
2538
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 status = psa_driver_wrapper_mac_verify_finish(operation,
2540 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002541
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002542exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002546}
2547
Gilles Peskine449bd832023-01-11 14:50:10 +01002548static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2549 psa_algorithm_t alg,
2550 const uint8_t *input,
2551 size_t input_length,
2552 uint8_t *mac,
2553 size_t mac_size,
2554 size_t *mac_length,
2555 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002556{
2557 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002558 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2559 psa_key_slot_t *slot;
2560 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002561
Ronald Cron51131b52021-06-17 17:17:20 +02002562 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 key,
2564 &slot,
2565 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2566 alg);
2567 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002568 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002570
Ronald Cron51131b52021-06-17 17:17:20 +02002571 psa_key_attributes_t attributes = {
2572 .core = slot->attr
2573 };
2574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2576 &operation_mac_size);
2577 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002578 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002580
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002582 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002583 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002584 }
2585
2586 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 &attributes,
2588 slot->key.data, slot->key.bytes,
2589 alg,
2590 input, input_length,
2591 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002592
2593exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002594 /* In case of success, set the potential excess room in the output buffer
2595 * to an invalid value, to avoid potentially leaking a longer MAC.
2596 * In case of error, set the output length and content to a safe default,
2597 * such that in case the caller misses an error check, the output would be
2598 * an unachievable MAC.
2599 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002601 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002602 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002603 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 if (mac_size > operation_mac_size) {
2605 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2606 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002607
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002609
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002611}
2612
Gilles Peskine449bd832023-01-11 14:50:10 +01002613psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002614 psa_algorithm_t alg,
2615 const uint8_t *input,
2616 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 uint8_t *mac,
2618 size_t mac_size,
2619 size_t *mac_length)
2620{
2621 return psa_mac_compute_internal(key, alg,
2622 input, input_length,
2623 mac, mac_size, mac_length, 1);
2624}
2625
2626psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2627 psa_algorithm_t alg,
2628 const uint8_t *input,
2629 size_t input_length,
2630 const uint8_t *mac,
2631 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002632{
2633 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002634 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2635 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 status = psa_mac_compute_internal(key, alg,
2638 input, input_length,
2639 actual_mac, sizeof(actual_mac),
2640 &actual_mac_length, 0);
2641 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002642 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002644
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002646 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002647 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002648 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002650 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002651 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002652 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002653
2654exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002656
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002658}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002659
Gilles Peskinee59236f2018-01-27 23:32:46 +01002660/****************************************************************/
2661/* Asymmetric cryptography */
2662/****************************************************************/
2663
Gilles Peskine449bd832023-01-11 14:50:10 +01002664static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2665 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002666{
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 if (input_is_message) {
2668 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2669 return PSA_ERROR_INVALID_ARGUMENT;
2670 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2673 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2674 return PSA_ERROR_INVALID_ARGUMENT;
2675 }
2676 }
2677 } else {
2678 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2679 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002680 }
2681 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002684}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002685
Paul Elliott939bd942023-02-07 12:15:24 +00002686/**
Paul Elliott6d99f0c2023-02-10 12:58:09 +00002687 * \brief Fill the unused part of the output buffer (the
Paul Elliott939bd942023-02-07 12:15:24 +00002688 * whole buffer on error, the trailing part on
2689 * success) with something that isn't a valid
2690 * signature (barring an attack on the signature
2691 * and deliberately-crafted input), in case the
2692 * caller doesn't check the return status properly.
2693 *
2694 * \param output_buffer pointer to buffer to wipe. May not be NULL
Paul Elliott6d99f0c2023-02-10 12:58:09 +00002695 * unless \p output_buffer_size is zero.
Paul Elliott939bd942023-02-07 12:15:24 +00002696 * \param status status of function called to generate
2697 * output_buffer originally
Paul Elliott6d99f0c2023-02-10 12:58:09 +00002698 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
Paul Elliott939bd942023-02-07 12:15:24 +00002699 * could be NULL
2700 * \param output_buffer_length Length of data written to output_buffer, must be
Paul Elliott6d99f0c2023-02-10 12:58:09 +00002701 * less than \p output_buffer_size
Paul Elliott939bd942023-02-07 12:15:24 +00002702 */
2703static void psa_wipe_output_buffer(uint8_t *output_buffer, psa_status_t status,
2704 size_t output_buffer_size, size_t output_buffer_length)
2705{
2706 if (status == PSA_SUCCESS) {
2707 memset(output_buffer + output_buffer_length, '!',
2708 output_buffer_size - output_buffer_length);
2709 } else if (output_buffer_size > 0) {
2710 memset(output_buffer, '!', output_buffer_size);
2711 }
2712 /* If output_buffer_size is 0 then we have nothing to do. We must
2713 * not call memset because output_buffer may be NULL in this
2714 * case.*/
2715}
2716
Gilles Peskine449bd832023-01-11 14:50:10 +01002717static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2718 int input_is_message,
2719 psa_algorithm_t alg,
2720 const uint8_t *input,
2721 size_t input_length,
2722 uint8_t *signature,
2723 size_t signature_size,
2724 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002725{
2726 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2727 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2728 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002729
2730 *signature_length = 0;
2731
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 status = psa_sign_verify_check_alg(input_is_message, alg);
2733 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002734 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002736
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002737 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002738 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002739 * buffer can in principle be empty since it doesn't actually have
2740 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 if (signature_size == 0) {
2742 return PSA_ERROR_BUFFER_TOO_SMALL;
2743 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002744
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002745 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 key, &slot,
2747 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2748 PSA_KEY_USAGE_SIGN_HASH,
2749 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002750
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002752 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002754
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002756 status = PSA_ERROR_INVALID_ARGUMENT;
2757 goto exit;
2758 }
2759
2760 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002762 };
2763
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002765 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002766 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002767 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 signature, signature_size, signature_length);
2769 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002770
2771 status = psa_driver_wrapper_sign_hash(
2772 &attributes, slot->key.data, slot->key.bytes,
2773 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002775 }
2776
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002777
2778exit:
Paul Elliott939bd942023-02-07 12:15:24 +00002779 psa_wipe_output_buffer(signature, status, signature_size,
2780 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002781
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002783
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002785}
2786
Gilles Peskine449bd832023-01-11 14:50:10 +01002787static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2788 int input_is_message,
2789 psa_algorithm_t alg,
2790 const uint8_t *input,
2791 size_t input_length,
2792 const uint8_t *signature,
2793 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002794{
2795 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2796 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2797 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002798
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 status = psa_sign_verify_check_alg(input_is_message, alg);
2800 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002801 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002803
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002804 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 key, &slot,
2806 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2807 PSA_KEY_USAGE_VERIFY_HASH,
2808 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002809
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 if (status != PSA_SUCCESS) {
2811 return status;
2812 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002813
2814 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002816 };
2817
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002819 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002820 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002821 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 signature, signature_length);
2823 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002824 status = psa_driver_wrapper_verify_hash(
2825 &attributes, slot->key.data, slot->key.bytes,
2826 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002828 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002829
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002831
Gilles Peskine449bd832023-01-11 14:50:10 +01002832 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002833
2834}
2835
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002836psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002837 const psa_key_attributes_t *attributes,
2838 const uint8_t *key_buffer,
2839 size_t key_buffer_size,
2840 psa_algorithm_t alg,
2841 const uint8_t *input,
2842 size_t input_length,
2843 uint8_t *signature,
2844 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002846{
2847 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002848
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002850 size_t hash_length;
2851 uint8_t hash[PSA_HASH_MAX_SIZE];
2852
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002853 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 PSA_ALG_SIGN_GET_HASH(alg),
2855 input, input_length,
2856 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002857
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002859 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002861
2862 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 attributes, key_buffer, key_buffer_size,
2864 alg, hash, hash_length,
2865 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002866 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002867
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002869}
2870
Gilles Peskine449bd832023-01-11 14:50:10 +01002871psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2872 psa_algorithm_t alg,
2873 const uint8_t *input,
2874 size_t input_length,
2875 uint8_t *signature,
2876 size_t signature_size,
2877 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002878{
2879 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002880 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002881 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002882}
2883
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002884psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002885 const psa_key_attributes_t *attributes,
2886 const uint8_t *key_buffer,
2887 size_t key_buffer_size,
2888 psa_algorithm_t alg,
2889 const uint8_t *input,
2890 size_t input_length,
2891 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002892 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002893{
2894 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002895
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002897 size_t hash_length;
2898 uint8_t hash[PSA_HASH_MAX_SIZE];
2899
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002900 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 PSA_ALG_SIGN_GET_HASH(alg),
2902 input, input_length,
2903 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002904
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002906 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002908
2909 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 attributes, key_buffer, key_buffer_size,
2911 alg, hash, hash_length,
2912 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002913 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002914
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002916}
2917
Gilles Peskine449bd832023-01-11 14:50:10 +01002918psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2919 psa_algorithm_t alg,
2920 const uint8_t *input,
2921 size_t input_length,
2922 const uint8_t *signature,
2923 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002924{
2925 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002926 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002928}
2929
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002930psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002931 const psa_key_attributes_t *attributes,
2932 const uint8_t *key_buffer, size_t key_buffer_size,
2933 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002935{
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2937 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2938 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002939#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2941 return mbedtls_psa_rsa_sign_hash(
2942 attributes,
2943 key_buffer, key_buffer_size,
2944 alg, hash, hash_length,
2945 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002946#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2947 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 } else {
2949 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002950 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2952 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002953#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2955 return mbedtls_psa_ecdsa_sign_hash(
2956 attributes,
2957 key_buffer, key_buffer_size,
2958 alg, hash, hash_length,
2959 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002960#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2961 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 } else {
2963 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002964 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002965 }
Ronald Cron4501c982021-03-19 20:09:32 +01002966
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 (void) key_buffer;
2968 (void) key_buffer_size;
2969 (void) hash;
2970 (void) hash_length;
2971 (void) signature;
2972 (void) signature_size;
2973 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002974
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002976}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002977
Gilles Peskine449bd832023-01-11 14:50:10 +01002978psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2979 psa_algorithm_t alg,
2980 const uint8_t *hash,
2981 size_t hash_length,
2982 uint8_t *signature,
2983 size_t signature_size,
2984 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002985{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002986 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002987 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03002989}
2990
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002991psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002992 const psa_key_attributes_t *attributes,
2993 const uint8_t *key_buffer, size_t key_buffer_size,
2994 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03002996{
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
2998 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2999 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003000#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3002 return mbedtls_psa_rsa_verify_hash(
3003 attributes,
3004 key_buffer, key_buffer_size,
3005 alg, hash, hash_length,
3006 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003007#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3008 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 } else {
3010 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003011 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3013 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003014#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3016 return mbedtls_psa_ecdsa_verify_hash(
3017 attributes,
3018 key_buffer, key_buffer_size,
3019 alg, hash, hash_length,
3020 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003021#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3022 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 } else {
3024 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003025 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003026 }
Ronald Cron4501c982021-03-19 20:09:32 +01003027
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 (void) key_buffer;
3029 (void) key_buffer_size;
3030 (void) hash;
3031 (void) hash_length;
3032 (void) signature;
3033 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003034
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003036}
3037
Gilles Peskine449bd832023-01-11 14:50:10 +01003038psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3039 psa_algorithm_t alg,
3040 const uint8_t *hash,
3041 size_t hash_length,
3042 const uint8_t *signature,
3043 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003044{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003045 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003046 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003048}
3049
Gilles Peskine449bd832023-01-11 14:50:10 +01003050psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3051 psa_algorithm_t alg,
3052 const uint8_t *input,
3053 size_t input_length,
3054 const uint8_t *salt,
3055 size_t salt_length,
3056 uint8_t *output,
3057 size_t output_size,
3058 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003059{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003060 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003061 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003062 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003063
Darryl Green5cc689a2018-07-24 15:34:10 +01003064 (void) input;
3065 (void) input_length;
3066 (void) salt;
3067 (void) output;
3068 (void) output_size;
3069
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003070 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003071
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3073 return PSA_ERROR_INVALID_ARGUMENT;
3074 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003075
Ronald Cron5c522922020-11-14 16:35:34 +01003076 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3078 if (status != PSA_SUCCESS) {
3079 return status;
3080 }
3081 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3082 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003083 status = PSA_ERROR_INVALID_ARGUMENT;
3084 goto exit;
3085 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003086
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003087 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003089 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003090
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003091 status = psa_driver_wrapper_asymmetric_encrypt(
3092 &attributes, slot->key.data, slot->key.bytes,
3093 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003095exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003097
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003099}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003100
Gilles Peskine449bd832023-01-11 14:50:10 +01003101psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3102 psa_algorithm_t alg,
3103 const uint8_t *input,
3104 size_t input_length,
3105 const uint8_t *salt,
3106 size_t salt_length,
3107 uint8_t *output,
3108 size_t output_size,
3109 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003110{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003111 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003112 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003113 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003114
Darryl Green5cc689a2018-07-24 15:34:10 +01003115 (void) input;
3116 (void) input_length;
3117 (void) salt;
3118 (void) output;
3119 (void) output_size;
3120
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003121 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003122
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3124 return PSA_ERROR_INVALID_ARGUMENT;
3125 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003126
Ronald Cron5c522922020-11-14 16:35:34 +01003127 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3129 if (status != PSA_SUCCESS) {
3130 return status;
3131 }
3132 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003133 status = PSA_ERROR_INVALID_ARGUMENT;
3134 goto exit;
3135 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003136
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003137 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003139 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003140
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003141 status = psa_driver_wrapper_asymmetric_decrypt(
3142 &attributes, slot->key.data, slot->key.bytes,
3143 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003145
3146exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003148
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003150}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003151
Paul Elliott9fe12f62022-11-30 19:16:02 +00003152/****************************************************************/
3153/* Asymmetric interruptible cryptography */
3154/****************************************************************/
3155
3156void psa_interruptible_set_max_ops(uint32_t max_ops)
3157{
3158 psa_driver_wrapper_interruptible_set_max_ops(max_ops);
3159}
3160
3161uint32_t psa_interruptible_get_max_ops(void)
3162{
3163 return psa_driver_wrapper_interruptible_get_max_ops();
3164}
3165
3166
3167uint32_t psa_sign_hash_get_num_ops(
3168 const psa_sign_hash_interruptible_operation_t *operation)
3169{
Paul Elliott296ede92022-12-15 17:00:30 +00003170 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003171}
3172
3173uint32_t psa_verify_hash_get_num_ops(
3174 const psa_verify_hash_interruptible_operation_t *operation)
3175{
Paul Elliott296ede92022-12-15 17:00:30 +00003176 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003177}
3178
Paul Elliott59ad9452022-12-18 15:09:02 +00003179static psa_status_t psa_sign_hash_abort_internal(
3180 psa_sign_hash_interruptible_operation_t *operation)
3181{
3182 if (operation->id == 0) {
3183 /* The object has (apparently) been initialized but it is not (yet)
3184 * in use. It's ok to call abort on such an object, and there's
3185 * nothing to do. */
3186 return PSA_SUCCESS;
3187 }
3188
3189 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3190
3191 status = psa_driver_wrapper_sign_hash_abort(operation);
3192
3193 operation->id = 0;
3194
Paul Elliotte6145dc2023-02-07 12:51:21 +00003195 /* Do not clear either the error_occurred or num_ops elements here as they
3196 * only want to be cleared by the application calling abort, not by abort
3197 * being called at completion of an operation. */
3198
Paul Elliott59ad9452022-12-18 15:09:02 +00003199 return status;
3200}
3201
Paul Elliott9fe12f62022-11-30 19:16:02 +00003202psa_status_t psa_sign_hash_start(
3203 psa_sign_hash_interruptible_operation_t *operation,
3204 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3205 const uint8_t *hash, size_t hash_length)
3206{
3207 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3208 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3209 psa_key_slot_t *slot;
3210
Paul Elliottc9774412023-02-06 15:14:07 +00003211 /* Check that start has not been previously called, or operation has not
3212 * previously errored. */
3213 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003214 return PSA_ERROR_BAD_STATE;
3215 }
3216
Paul Elliott9fe12f62022-11-30 19:16:02 +00003217 status = psa_sign_verify_check_alg(0, alg);
3218 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003219 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003220 return status;
3221 }
3222
3223 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3224 PSA_KEY_USAGE_SIGN_HASH,
3225 alg);
3226
3227 if (status != PSA_SUCCESS) {
3228 goto exit;
3229 }
3230
3231 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3232 status = PSA_ERROR_INVALID_ARGUMENT;
3233 goto exit;
3234 }
3235
3236 psa_key_attributes_t attributes = {
3237 .core = slot->attr
3238 };
3239
Paul Elliott296ede92022-12-15 17:00:30 +00003240 /* Ensure ops count gets reset, in case of operation re-use. */
3241 operation->num_ops = 0;
3242
Paul Elliott9fe12f62022-11-30 19:16:02 +00003243 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3244 slot->key.data,
3245 slot->key.bytes, alg,
3246 hash, hash_length);
3247exit:
3248
3249 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003250 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003251 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003252 }
3253
3254 unlock_status = psa_unlock_key_slot(slot);
3255
Paul Elliottc9774412023-02-06 15:14:07 +00003256 if (unlock_status != PSA_SUCCESS) {
3257 operation->error_occurred = 1;
3258 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003259
Paul Elliottc9774412023-02-06 15:14:07 +00003260 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003261}
3262
3263
3264psa_status_t psa_sign_hash_complete(
3265 psa_sign_hash_interruptible_operation_t *operation,
3266 uint8_t *signature, size_t signature_size,
3267 size_t *signature_length)
3268{
3269 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3270
3271 *signature_length = 0;
3272
Paul Elliottc9774412023-02-06 15:14:07 +00003273 /* Check that start has been called first, and that operation has not
3274 * previously errored. */
3275 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003276 status = PSA_ERROR_BAD_STATE;
3277 goto exit;
3278 }
3279
Paul Elliott096abc42023-02-03 18:33:23 +00003280 /* Immediately reject a zero-length signature buffer. This guarantees that
3281 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003282 if (signature_size == 0) {
3283 status = PSA_ERROR_BUFFER_TOO_SMALL;
3284 goto exit;
3285 }
3286
3287 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3288 signature_size,
3289 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003290
Paul Elliott296ede92022-12-15 17:00:30 +00003291 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003292 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003293
Paul Elliottebe225c2023-02-10 14:32:53 +00003294exit:
3295
Paul Elliott53bb3122023-02-10 14:22:22 +00003296 psa_wipe_output_buffer(signature, status, signature_size,
3297 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003298
Paul Elliott53bb3122023-02-10 14:22:22 +00003299 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003300 if (status != PSA_SUCCESS) {
3301 operation->error_occurred = 1;
3302 }
3303
Paul Elliott59ad9452022-12-18 15:09:02 +00003304 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003305 }
3306
3307 return status;
3308}
3309
3310psa_status_t psa_sign_hash_abort(
3311 psa_sign_hash_interruptible_operation_t *operation)
3312{
Paul Elliott59ad9452022-12-18 15:09:02 +00003313 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3314
3315 status = psa_sign_hash_abort_internal(operation);
3316
3317 /* We clear the number of ops done here, so that it is not cleared when
3318 * the operation fails or succeeds, only on manual abort. */
3319 operation->num_ops = 0;
3320
Paul Elliottc9774412023-02-06 15:14:07 +00003321 /* Likewise, failure state. */
3322 operation->error_occurred = 0;
3323
Paul Elliott59ad9452022-12-18 15:09:02 +00003324 return status;
3325}
3326
3327static psa_status_t psa_verify_hash_abort_internal(
3328 psa_verify_hash_interruptible_operation_t *operation)
3329{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003330 if (operation->id == 0) {
3331 /* The object has (apparently) been initialized but it is not (yet)
3332 * in use. It's ok to call abort on such an object, and there's
3333 * nothing to do. */
3334 return PSA_SUCCESS;
3335 }
3336
Paul Elliott59ad9452022-12-18 15:09:02 +00003337 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3338
3339 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003340
3341 operation->id = 0;
3342
Paul Elliotte6145dc2023-02-07 12:51:21 +00003343 /* Do not clear either the error_occurred or num_ops elements here as they
3344 * only want to be cleared by the application calling abort, not by abort
3345 * being called at completion of an operation. */
3346
Paul Elliott59ad9452022-12-18 15:09:02 +00003347 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003348}
3349
3350psa_status_t psa_verify_hash_start(
3351 psa_verify_hash_interruptible_operation_t *operation,
3352 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3353 const uint8_t *hash, size_t hash_length,
3354 const uint8_t *signature, size_t signature_length)
3355{
3356 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3357 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3358 psa_key_slot_t *slot;
3359
Paul Elliottc9774412023-02-06 15:14:07 +00003360 /* Check that start has not been previously called, or operation has not
3361 * previously errored. */
3362 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003363 return PSA_ERROR_BAD_STATE;
3364 }
3365
3366 status = psa_sign_verify_check_alg(0, alg);
3367 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003368 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003369 return status;
3370 }
3371
3372 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3373 PSA_KEY_USAGE_VERIFY_HASH,
3374 alg);
3375
3376 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003377 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003378 return status;
3379 }
3380
3381 psa_key_attributes_t attributes = {
3382 .core = slot->attr
3383 };
3384
Paul Elliott296ede92022-12-15 17:00:30 +00003385 /* Ensure ops count gets reset, in case of operation re-use. */
3386 operation->num_ops = 0;
3387
Paul Elliott9fe12f62022-11-30 19:16:02 +00003388 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3389 slot->key.data,
3390 slot->key.bytes,
3391 alg, hash, hash_length,
3392 signature, signature_length);
3393
3394 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003395 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003396 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003397 }
3398
3399 unlock_status = psa_unlock_key_slot(slot);
3400
Paul Elliottc9774412023-02-06 15:14:07 +00003401 if (unlock_status != PSA_SUCCESS) {
3402 operation->error_occurred = 1;
3403 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003404
Paul Elliottc9774412023-02-06 15:14:07 +00003405 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003406}
3407
3408psa_status_t psa_verify_hash_complete(
3409 psa_verify_hash_interruptible_operation_t *operation)
3410{
3411 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3412
Paul Elliottc9774412023-02-06 15:14:07 +00003413 /* Check that start has been called first, and that operation has not
3414 * previously errored. */
3415 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003416 status = PSA_ERROR_BAD_STATE;
3417 goto exit;
3418 }
3419
3420 status = psa_driver_wrapper_verify_hash_complete(operation);
3421
Paul Elliott296ede92022-12-15 17:00:30 +00003422 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003423 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003424 operation);
3425
Paul Elliottebe225c2023-02-10 14:32:53 +00003426exit:
3427
Paul Elliott9fe12f62022-11-30 19:16:02 +00003428 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003429 if (status != PSA_SUCCESS) {
3430 operation->error_occurred = 1;
3431 }
3432
Paul Elliott59ad9452022-12-18 15:09:02 +00003433 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003434 }
3435
3436 return status;
3437}
3438
3439psa_status_t psa_verify_hash_abort(
3440 psa_verify_hash_interruptible_operation_t *operation)
3441{
Paul Elliott59ad9452022-12-18 15:09:02 +00003442 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003443
Paul Elliott59ad9452022-12-18 15:09:02 +00003444 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003445
Paul Elliott59ad9452022-12-18 15:09:02 +00003446 /* We clear the number of ops done here, so that it is not cleared when
3447 * the operation fails or succeeds, only on manual abort. */
3448 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003449
Paul Elliottc9774412023-02-06 15:14:07 +00003450 /* Likewise, failure state. */
3451 operation->error_occurred = 0;
3452
Paul Elliott59ad9452022-12-18 15:09:02 +00003453 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003454}
3455
Paul Elliott588f8ed2022-12-02 18:10:26 +00003456/****************************************************************/
3457/* Asymmetric interruptible cryptography internal */
3458/* implementations */
3459/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003460
Paul Elliott588f8ed2022-12-02 18:10:26 +00003461static uint32_t mbedtls_psa_interruptible_max_ops =
3462 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3463
3464void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3465{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003466 mbedtls_psa_interruptible_max_ops = max_ops;
3467
Paul Elliott588f8ed2022-12-02 18:10:26 +00003468#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3469 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3470 defined(MBEDTLS_ECP_RESTARTABLE)
3471
3472 /* Internal implementation uses zero to indicate infinite number max ops,
3473 * therefore avoid this value, and set to minimum possible. */
3474 if (max_ops == 0) {
3475 max_ops = 1;
3476 }
3477
Paul Elliott588f8ed2022-12-02 18:10:26 +00003478 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003479#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3480 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3481 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3482}
3483
3484uint32_t mbedtls_psa_interruptible_get_max_ops(void)
3485{
3486 return mbedtls_psa_interruptible_max_ops;
3487}
3488
3489uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003490 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003491{
3492#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3493 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3494 defined(MBEDTLS_ECP_RESTARTABLE)
3495
Paul Elliott93d9ca82023-02-15 18:14:21 +00003496 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003497#else
3498 (void) operation;
3499 return 0;
3500#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3501 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3502 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3503}
3504
3505uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003506 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003507{
3508 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3509 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3510 defined(MBEDTLS_ECP_RESTARTABLE)
3511
Paul Elliott93d9ca82023-02-15 18:14:21 +00003512 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003513#else
3514 (void) operation;
3515 return 0;
3516#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3517 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3518 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3519}
3520
3521psa_status_t mbedtls_psa_sign_hash_start(
3522 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3523 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3524 size_t key_buffer_size, psa_algorithm_t alg,
3525 const uint8_t *hash, size_t hash_length)
3526{
3527 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003528 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003529
Paul Elliott068fe072023-01-16 13:59:15 +00003530 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3531 return PSA_ERROR_NOT_SUPPORTED;
3532 }
3533
3534 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003535 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003536 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003537
3538#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003539 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3540 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003541
Paul Elliotta1c94092023-02-15 16:38:04 +00003542 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3543
Paul Elliott93d9ca82023-02-15 18:14:21 +00003544 /* Ensure num_ops is zero'ed in case of context re-use. */
3545 operation->num_ops = 0;
3546
Paul Elliott068fe072023-01-16 13:59:15 +00003547 /* Ensure default is set even if
3548 * mbedtls_psa_interruptible_set_max_ops() has not been called. */
3549 mbedtls_psa_interruptible_set_max_ops(
3550 mbedtls_psa_interruptible_get_max_ops());
Paul Elliott588f8ed2022-12-02 18:10:26 +00003551
Paul Elliott068fe072023-01-16 13:59:15 +00003552 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3553 attributes->core.bits,
3554 key_buffer,
3555 key_buffer_size,
3556 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003557
Paul Elliott068fe072023-01-16 13:59:15 +00003558 if (status != PSA_SUCCESS) {
3559 return status;
3560 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003561
Paul Elliott1bc59df2023-02-05 13:41:57 +00003562 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003563 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003564
Paul Elliott068fe072023-01-16 13:59:15 +00003565 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3566 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3567 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003568
Paul Elliott0290a762023-02-09 14:30:24 +00003569 /* We only need to store the same length of hash as the private key size
3570 * here, it would be truncated by the internal implementation anyway. */
3571 required_hash_length = (hash_length < operation->coordinate_bytes ?
3572 hash_length : operation->coordinate_bytes);
3573
Paul Elliottba70ad42023-02-15 18:23:53 +00003574 if (required_hash_length > sizeof(operation->hash)) {
3575 /* Shouldn't happen, but better safe than sorry. */
3576 return PSA_ERROR_CORRUPTION_DETECTED;
3577 }
3578
Paul Elliott0290a762023-02-09 14:30:24 +00003579 memcpy(operation->hash, hash, required_hash_length);
3580 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003581
3582 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003583
3584#else
Paul Elliott068fe072023-01-16 13:59:15 +00003585 (void) operation;
3586 (void) key_buffer;
3587 (void) key_buffer_size;
3588 (void) alg;
3589 (void) hash;
3590 (void) hash_length;
3591 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003592 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003593
Paul Elliott068fe072023-01-16 13:59:15 +00003594 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003595#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3596 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3597 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003598}
3599
3600psa_status_t mbedtls_psa_sign_hash_complete(
3601 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3602 uint8_t *signature, size_t signature_size,
3603 size_t *signature_length)
3604{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003605#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3606 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3607 defined(MBEDTLS_ECP_RESTARTABLE)
3608
Paul Elliotta1c94092023-02-15 16:38:04 +00003609 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003610 mbedtls_mpi r;
3611 mbedtls_mpi s;
3612
Paul Elliott46845252023-02-03 14:59:11 +00003613 mbedtls_mpi_init(&r);
3614 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003615
Paul Elliotta1c94092023-02-15 16:38:04 +00003616 if (signature_size < 2 * operation->coordinate_bytes) {
3617 status = PSA_ERROR_BUFFER_TOO_SMALL;
3618 goto exit;
3619 }
3620
Paul Elliott588f8ed2022-12-02 18:10:26 +00003621 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003622
Paul Elliott588f8ed2022-12-02 18:10:26 +00003623#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3624 status = mbedtls_to_psa_error(
3625 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003626 &r,
3627 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003628 &operation->ctx->d,
3629 operation->hash,
3630 operation->hash_length,
3631 operation->md_alg,
3632 mbedtls_psa_get_random,
3633 MBEDTLS_PSA_RANDOM_STATE,
3634 &operation->restart_ctx));
3635#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003636 status = PSA_ERROR_NOT_SUPPORTED;
3637 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003638#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3639 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003640 status = mbedtls_to_psa_error(
3641 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003642 &r,
3643 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003644 &operation->ctx->d,
3645 operation->hash,
3646 operation->hash_length,
3647 mbedtls_psa_get_random,
3648 MBEDTLS_PSA_RANDOM_STATE,
3649 mbedtls_psa_get_random,
3650 MBEDTLS_PSA_RANDOM_STATE,
3651 &operation->restart_ctx));
3652 }
3653
Paul Elliottf8e5b562023-02-19 18:43:45 +00003654 /* Hide the fact that the restart context only holds a delta of number of
3655 * ops done during the last operation, not an absolute value. */
3656 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3657
Paul Elliott724bd252023-02-08 12:35:08 +00003658 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003659 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003660 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003661 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003662 operation->coordinate_bytes)
3663 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003664
3665 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003666 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003667 }
3668
3669 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003670 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003671 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003672 operation->coordinate_bytes,
3673 operation->coordinate_bytes)
3674 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003675
3676 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003677 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003678 }
3679
Paul Elliott1bc59df2023-02-05 13:41:57 +00003680 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003681
Paul Elliott724bd252023-02-08 12:35:08 +00003682 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003683 }
Paul Elliott724bd252023-02-08 12:35:08 +00003684
3685exit:
3686
3687 mbedtls_mpi_free(&r);
3688 mbedtls_mpi_free(&s);
3689 return status;
3690
Paul Elliott588f8ed2022-12-02 18:10:26 +00003691 #else
3692
3693 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003694 (void) signature;
3695 (void) signature_size;
3696 (void) signature_length;
3697
3698 return PSA_ERROR_NOT_SUPPORTED;
3699
3700#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3701 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3702 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3703}
3704
3705psa_status_t mbedtls_psa_sign_hash_abort(
3706 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3707{
3708
3709#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3710 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3711 defined(MBEDTLS_ECP_RESTARTABLE)
3712
3713 if (operation->ctx) {
3714 mbedtls_ecdsa_free(operation->ctx);
3715 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003716 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003717 }
3718
3719 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3720
Paul Elliott93d9ca82023-02-15 18:14:21 +00003721 operation->num_ops = 0;
3722
Paul Elliott588f8ed2022-12-02 18:10:26 +00003723 return PSA_SUCCESS;
3724
3725#else
3726
3727 (void) operation;
3728
3729 return PSA_ERROR_NOT_SUPPORTED;
3730
3731#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3732 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3733 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3734}
3735
3736psa_status_t mbedtls_psa_verify_hash_start(
3737 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3738 const psa_key_attributes_t *attributes,
3739 const uint8_t *key_buffer, size_t key_buffer_size,
3740 psa_algorithm_t alg,
3741 const uint8_t *hash, size_t hash_length,
3742 const uint8_t *signature, size_t signature_length)
3743{
3744 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003745 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003746 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003747
Paul Elliott068fe072023-01-16 13:59:15 +00003748 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3749 return PSA_ERROR_NOT_SUPPORTED;
3750 }
3751
3752 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003753 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003754 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003755
3756#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003757 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3758 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003759
Paul Elliotta1c94092023-02-15 16:38:04 +00003760 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3761 mbedtls_mpi_init(&operation->r);
3762 mbedtls_mpi_init(&operation->s);
3763
Paul Elliott93d9ca82023-02-15 18:14:21 +00003764 /* Ensure num_ops is zero'ed in case of context re-use. */
3765 operation->num_ops = 0;
3766
Paul Elliott068fe072023-01-16 13:59:15 +00003767 /* Ensure default is set even if
3768 * mbedtls_psa_interruptible_set_max_ops() has not been called. */
3769 mbedtls_psa_interruptible_set_max_ops(
3770 mbedtls_psa_interruptible_get_max_ops());
Paul Elliott588f8ed2022-12-02 18:10:26 +00003771
Paul Elliott068fe072023-01-16 13:59:15 +00003772 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3773 attributes->core.bits,
3774 key_buffer,
3775 key_buffer_size,
3776 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003777
Paul Elliott068fe072023-01-16 13:59:15 +00003778 if (status != PSA_SUCCESS) {
3779 return status;
3780 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003781
Paul Elliottc569fc22023-02-10 13:02:54 +00003782 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003783
Paul Elliott1bc59df2023-02-05 13:41:57 +00003784 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003785 return PSA_ERROR_INVALID_SIGNATURE;
3786 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003787
Paul Elliott068fe072023-01-16 13:59:15 +00003788 status = mbedtls_to_psa_error(
3789 mbedtls_mpi_read_binary(&operation->r,
3790 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003791 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003792
Paul Elliott068fe072023-01-16 13:59:15 +00003793 if (status != PSA_SUCCESS) {
3794 return status;
3795 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003796
Paul Elliott068fe072023-01-16 13:59:15 +00003797 status = mbedtls_to_psa_error(
3798 mbedtls_mpi_read_binary(&operation->s,
3799 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003800 coordinate_bytes,
3801 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003802
Paul Elliott068fe072023-01-16 13:59:15 +00003803 if (status != PSA_SUCCESS) {
3804 return status;
3805 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003806
Paul Elliott2c9843f2023-02-15 17:32:42 +00003807 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003808
Paul Elliott2c9843f2023-02-15 17:32:42 +00003809 if (status != PSA_SUCCESS) {
3810 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003811 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003812
Paul Elliott0290a762023-02-09 14:30:24 +00003813 /* We only need to store the same length of hash as the private key size
3814 * here, it would be truncated by the internal implementation anyway. */
3815 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3816 coordinate_bytes);
3817
Paul Elliottba70ad42023-02-15 18:23:53 +00003818 if (required_hash_length > sizeof(operation->hash)) {
3819 /* Shouldn't happen, but better safe than sorry. */
3820 return PSA_ERROR_CORRUPTION_DETECTED;
3821 }
3822
Paul Elliott0290a762023-02-09 14:30:24 +00003823 memcpy(operation->hash, hash, required_hash_length);
3824 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003825
3826 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003827#else
Paul Elliott068fe072023-01-16 13:59:15 +00003828 (void) operation;
3829 (void) key_buffer;
3830 (void) key_buffer_size;
3831 (void) alg;
3832 (void) hash;
3833 (void) hash_length;
3834 (void) signature;
3835 (void) signature_length;
3836 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003837 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003838 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003839
Paul Elliott068fe072023-01-16 13:59:15 +00003840 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003841#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3842 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3843 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003844}
3845
3846psa_status_t mbedtls_psa_verify_hash_complete(
3847 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3848{
3849
3850#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3851 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3852 defined(MBEDTLS_ECP_RESTARTABLE)
3853
Paul Elliottf8e5b562023-02-19 18:43:45 +00003854 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3855
3856 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003857 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3858 operation->hash,
3859 operation->hash_length,
3860 &operation->ctx->Q,
3861 &operation->r,
3862 &operation->s,
3863 &operation->restart_ctx));
3864
Paul Elliottf8e5b562023-02-19 18:43:45 +00003865 /* Hide the fact that the restart context only holds a delta of number of
3866 * ops done during the last operation, not an absolute value. */
3867 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3868
3869 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003870#else
3871 (void) operation;
3872
3873 return PSA_ERROR_NOT_SUPPORTED;
3874
3875#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3876 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3877 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3878}
3879
3880psa_status_t mbedtls_psa_verify_hash_abort(
3881 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3882{
3883
3884#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3885 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3886 defined(MBEDTLS_ECP_RESTARTABLE)
3887
3888 if (operation->ctx) {
3889 mbedtls_ecdsa_free(operation->ctx);
3890 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003891 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003892 }
3893
3894 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3895
Paul Elliott93d9ca82023-02-15 18:14:21 +00003896 operation->num_ops = 0;
3897
Paul Elliott588f8ed2022-12-02 18:10:26 +00003898 mbedtls_mpi_free(&operation->r);
3899 mbedtls_mpi_free(&operation->s);
3900
3901 return PSA_SUCCESS;
3902
3903#else
3904 (void) operation;
3905
3906 return PSA_ERROR_NOT_SUPPORTED;
3907
3908#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3909 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3910 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3911}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003912
mohammad1603503973b2018-03-12 15:59:30 +02003913/****************************************************************/
3914/* Symmetric cryptography */
3915/****************************************************************/
3916
Gilles Peskine449bd832023-01-11 14:50:10 +01003917static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3918 mbedtls_svc_key_id_t key,
3919 psa_algorithm_t alg,
3920 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003921{
3922 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3923 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003924 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003925 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3926 PSA_KEY_USAGE_ENCRYPT :
3927 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003928
3929 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003930 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003931 status = PSA_ERROR_BAD_STATE;
3932 goto exit;
3933 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003934
Gilles Peskine449bd832023-01-11 14:50:10 +01003935 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003936 status = PSA_ERROR_INVALID_ARGUMENT;
3937 goto exit;
3938 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003939
Gilles Peskine449bd832023-01-11 14:50:10 +01003940 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3941 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003942 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003943 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003944
Ronald Cron49fafa92021-03-10 08:34:23 +01003945 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003946 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003947 * so we only set it (in the driver wrapper) after resources have been
3948 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003949 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003950 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003951 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003952 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003953 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003954 }
3955 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003956
Ronald Crona4af55f2020-12-14 14:36:06 +01003957 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003958 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003959 };
3960
Ronald Cronab99ac22020-10-01 16:38:28 +02003961 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003962 if (cipher_operation == MBEDTLS_ENCRYPT) {
3963 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3964 &attributes,
3965 slot->key.data,
3966 slot->key.bytes,
3967 alg);
3968 } else {
3969 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3970 &attributes,
3971 slot->key.data,
3972 slot->key.bytes,
3973 alg);
3974 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003975
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003976exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003977 if (status != PSA_SUCCESS) {
3978 psa_cipher_abort(operation);
3979 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003980
Gilles Peskine449bd832023-01-11 14:50:10 +01003981 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003982
Gilles Peskine449bd832023-01-11 14:50:10 +01003983 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003984}
3985
Gilles Peskine449bd832023-01-11 14:50:10 +01003986psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3987 mbedtls_svc_key_id_t key,
3988 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003989{
Gilles Peskine449bd832023-01-11 14:50:10 +01003990 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003991}
3992
Gilles Peskine449bd832023-01-11 14:50:10 +01003993psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3994 mbedtls_svc_key_id_t key,
3995 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003996{
Gilles Peskine449bd832023-01-11 14:50:10 +01003997 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003998}
3999
Gilles Peskine449bd832023-01-11 14:50:10 +01004000psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4001 uint8_t *iv,
4002 size_t iv_size,
4003 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004004{
Ronald Cron6d051732020-10-01 14:10:20 +02004005 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004006 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4007 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004008
Gilles Peskine449bd832023-01-11 14:50:10 +01004009 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004010 status = PSA_ERROR_BAD_STATE;
4011 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004012 }
4013
Gilles Peskine449bd832023-01-11 14:50:10 +01004014 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004015 status = PSA_ERROR_BAD_STATE;
4016 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004017 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004018
Ronald Cron2fb90522021-07-05 12:31:44 +02004019 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004020 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004021 status = PSA_ERROR_BUFFER_TOO_SMALL;
4022 goto exit;
4023 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004024
Gilles Peskine449bd832023-01-11 14:50:10 +01004025 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004026 status = PSA_ERROR_GENERIC_ERROR;
4027 goto exit;
4028 }
4029
Gilles Peskine449bd832023-01-11 14:50:10 +01004030 status = psa_generate_random(local_iv, default_iv_length);
4031 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004032 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004033 }
Ronald Cron5618a392021-03-26 09:52:26 +01004034
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 status = psa_driver_wrapper_cipher_set_iv(operation,
4036 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004037
4038exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004039 if (status == PSA_SUCCESS) {
4040 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004041 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004042 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004043 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004044 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004045 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004046 }
Ronald Cron6d051732020-10-01 14:10:20 +02004047
Gilles Peskine449bd832023-01-11 14:50:10 +01004048 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004049}
4050
Gilles Peskine449bd832023-01-11 14:50:10 +01004051psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4052 const uint8_t *iv,
4053 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004054{
Ronald Cron6d051732020-10-01 14:10:20 +02004055 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004056
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004058 status = PSA_ERROR_BAD_STATE;
4059 goto exit;
4060 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004061
Gilles Peskine449bd832023-01-11 14:50:10 +01004062 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004063 status = PSA_ERROR_BAD_STATE;
4064 goto exit;
4065 }
Ronald Crona0d68172021-03-26 10:15:08 +01004066
Gilles Peskine449bd832023-01-11 14:50:10 +01004067 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004068 status = PSA_ERROR_INVALID_ARGUMENT;
4069 goto exit;
4070 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004071
Gilles Peskine449bd832023-01-11 14:50:10 +01004072 status = psa_driver_wrapper_cipher_set_iv(operation,
4073 iv,
4074 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004075
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004076exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004077 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004078 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004079 } else {
4080 psa_cipher_abort(operation);
4081 }
4082 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004083}
4084
Gilles Peskine449bd832023-01-11 14:50:10 +01004085psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4086 const uint8_t *input,
4087 size_t input_length,
4088 uint8_t *output,
4089 size_t output_size,
4090 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004091{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004092 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004093
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004095 status = PSA_ERROR_BAD_STATE;
4096 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004097 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004098
Gilles Peskine449bd832023-01-11 14:50:10 +01004099 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004100 status = PSA_ERROR_BAD_STATE;
4101 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004102 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004103
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 status = psa_driver_wrapper_cipher_update(operation,
4105 input,
4106 input_length,
4107 output,
4108 output_size,
4109 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004110
4111exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004112 if (status != PSA_SUCCESS) {
4113 psa_cipher_abort(operation);
4114 }
Ronald Cron6d051732020-10-01 14:10:20 +02004115
Gilles Peskine449bd832023-01-11 14:50:10 +01004116 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004117}
4118
Gilles Peskine449bd832023-01-11 14:50:10 +01004119psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4120 uint8_t *output,
4121 size_t output_size,
4122 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004123{
David Saadab4ecc272019-02-14 13:48:10 +02004124 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004125
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004127 status = PSA_ERROR_BAD_STATE;
4128 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004129 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004130
Gilles Peskine449bd832023-01-11 14:50:10 +01004131 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004132 status = PSA_ERROR_BAD_STATE;
4133 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004134 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004135
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 status = psa_driver_wrapper_cipher_finish(operation,
4137 output,
4138 output_size,
4139 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004140
4141exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004142 if (status == PSA_SUCCESS) {
4143 return psa_cipher_abort(operation);
4144 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004145 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004146 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004147
Gilles Peskine449bd832023-01-11 14:50:10 +01004148 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004149 }
mohammad1603503973b2018-03-12 15:59:30 +02004150}
4151
Gilles Peskine449bd832023-01-11 14:50:10 +01004152psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004153{
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004155 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004156 * in use. It's ok to call abort on such an object, and there's
4157 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004158 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004159 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004160
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004162
Ronald Cron49fafa92021-03-10 08:34:23 +01004163 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004164 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004165 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004166
Gilles Peskine449bd832023-01-11 14:50:10 +01004167 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004168}
4169
Gilles Peskine449bd832023-01-11 14:50:10 +01004170psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4171 psa_algorithm_t alg,
4172 const uint8_t *input,
4173 size_t input_length,
4174 uint8_t *output,
4175 size_t output_size,
4176 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004177{
4178 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004179 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004180 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004181 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4182 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004183
Gilles Peskine449bd832023-01-11 14:50:10 +01004184 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004185 status = PSA_ERROR_INVALID_ARGUMENT;
4186 goto exit;
4187 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004188
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4190 PSA_KEY_USAGE_ENCRYPT,
4191 alg);
4192 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004193 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004194 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004195
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004196 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004198 };
4199
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4201 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004202 status = PSA_ERROR_GENERIC_ERROR;
4203 goto exit;
4204 }
4205
Gilles Peskine449bd832023-01-11 14:50:10 +01004206 if (default_iv_length > 0) {
4207 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004208 status = PSA_ERROR_BUFFER_TOO_SMALL;
4209 goto exit;
4210 }
4211
Gilles Peskine449bd832023-01-11 14:50:10 +01004212 status = psa_generate_random(local_iv, default_iv_length);
4213 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004214 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004215 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004216 }
4217
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004218 status = psa_driver_wrapper_cipher_encrypt(
4219 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004220 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 mbedtls_buffer_offset(output, default_iv_length),
4222 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004223
4224exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004225 unlock_status = psa_unlock_key_slot(slot);
4226 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004227 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004228 }
Ronald Cron23919522021-07-08 19:00:07 +02004229
Gilles Peskine449bd832023-01-11 14:50:10 +01004230 if (status == PSA_SUCCESS) {
4231 if (default_iv_length > 0) {
4232 memcpy(output, local_iv, default_iv_length);
4233 }
4234 *output_length += default_iv_length;
4235 } else {
4236 *output_length = 0;
4237 }
4238
4239 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004240}
4241
Gilles Peskine449bd832023-01-11 14:50:10 +01004242psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4243 psa_algorithm_t alg,
4244 const uint8_t *input,
4245 size_t input_length,
4246 uint8_t *output,
4247 size_t output_size,
4248 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004249{
4250 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004251 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004252 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004253
Gilles Peskine449bd832023-01-11 14:50:10 +01004254 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004255 status = PSA_ERROR_INVALID_ARGUMENT;
4256 goto exit;
4257 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004258
Gilles Peskine449bd832023-01-11 14:50:10 +01004259 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4260 PSA_KEY_USAGE_DECRYPT,
4261 alg);
4262 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004263 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004264 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004265
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004266 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004267 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004268 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004269
Gilles Peskine449bd832023-01-11 14:50:10 +01004270 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4271 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004272 status = PSA_ERROR_INVALID_ARGUMENT;
4273 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004274 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004275 status = PSA_ERROR_INVALID_ARGUMENT;
4276 goto exit;
4277 }
4278
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004279 status = psa_driver_wrapper_cipher_decrypt(
4280 &attributes, slot->key.data, slot->key.bytes,
4281 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004282 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004283
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004284exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004285 unlock_status = psa_unlock_key_slot(slot);
4286 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004287 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004288 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004289
Gilles Peskine449bd832023-01-11 14:50:10 +01004290 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004291 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004292 }
Ronald Cron23919522021-07-08 19:00:07 +02004293
Gilles Peskine449bd832023-01-11 14:50:10 +01004294 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004295}
4296
4297
mohammad16035955c982018-04-26 00:53:03 +03004298/****************************************************************/
4299/* AEAD */
4300/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004301
Paul Elliottbaff51c2021-09-28 17:44:45 +01004302/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004303static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004304{
Gilles Peskine449bd832023-01-11 14:50:10 +01004305 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004306}
4307
4308/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004309static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4310 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004311{
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004313
Gilles Peskine449bd832023-01-11 14:50:10 +01004314 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004315#if defined(PSA_WANT_ALG_GCM)
4316 case PSA_ALG_GCM:
4317 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 * arbitrarily large nonces. Please note that we do not generally
4319 * recommend the usage of nonces of greater length than
4320 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4321 * size, which can then lead to collisions if you encrypt a very
4322 * large number of messages.*/
4323 if (nonce_length != 0) {
4324 return PSA_SUCCESS;
4325 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004326 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004327#endif /* PSA_WANT_ALG_GCM */
4328#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004329 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004330 if (nonce_length >= 7 && nonce_length <= 13) {
4331 return PSA_SUCCESS;
4332 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004333 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004334#endif /* PSA_WANT_ALG_CCM */
4335#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004336 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004337 if (nonce_length == 12) {
4338 return PSA_SUCCESS;
4339 } else if (nonce_length == 8) {
4340 return PSA_ERROR_NOT_SUPPORTED;
4341 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004342 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004343#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004344 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004345 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004346 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004347 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004348
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004350}
4351
Gilles Peskine449bd832023-01-11 14:50:10 +01004352static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004353{
Gilles Peskine449bd832023-01-11 14:50:10 +01004354 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4355 return PSA_ERROR_INVALID_ARGUMENT;
4356 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004357
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004359}
4360
Gilles Peskine449bd832023-01-11 14:50:10 +01004361psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4362 psa_algorithm_t alg,
4363 const uint8_t *nonce,
4364 size_t nonce_length,
4365 const uint8_t *additional_data,
4366 size_t additional_data_length,
4367 const uint8_t *plaintext,
4368 size_t plaintext_length,
4369 uint8_t *ciphertext,
4370 size_t ciphertext_size,
4371 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004372{
Ronald Cron215633c2021-03-16 17:15:37 +01004373 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4374 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004375
mohammad1603f08a5502018-06-03 15:05:47 +03004376 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004377
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 status = psa_aead_check_algorithm(alg);
4379 if (status != PSA_SUCCESS) {
4380 return status;
4381 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004382
Ronald Cron9a986162021-03-26 12:40:07 +01004383 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004384 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4385 if (status != PSA_SUCCESS) {
4386 return status;
4387 }
mohammad16035955c982018-04-26 00:53:03 +03004388
Ronald Cron215633c2021-03-16 17:15:37 +01004389 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004391 };
mohammad16035955c982018-04-26 00:53:03 +03004392
Gilles Peskine449bd832023-01-11 14:50:10 +01004393 status = psa_aead_check_nonce_length(alg, nonce_length);
4394 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004395 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004396 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004397
Ronald Cronde822812021-03-17 16:08:20 +01004398 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004399 &attributes, slot->key.data, slot->key.bytes,
4400 alg,
4401 nonce, nonce_length,
4402 additional_data, additional_data_length,
4403 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004404 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004405
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4407 memset(ciphertext, 0, ciphertext_size);
4408 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004409
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004410exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004411 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004412
Gilles Peskine449bd832023-01-11 14:50:10 +01004413 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004414}
4415
Gilles Peskine449bd832023-01-11 14:50:10 +01004416psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4417 psa_algorithm_t alg,
4418 const uint8_t *nonce,
4419 size_t nonce_length,
4420 const uint8_t *additional_data,
4421 size_t additional_data_length,
4422 const uint8_t *ciphertext,
4423 size_t ciphertext_length,
4424 uint8_t *plaintext,
4425 size_t plaintext_size,
4426 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004427{
Ronald Cron215633c2021-03-16 17:15:37 +01004428 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4429 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004430
Gilles Peskineee652a32018-06-01 19:23:52 +02004431 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004432
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 status = psa_aead_check_algorithm(alg);
4434 if (status != PSA_SUCCESS) {
4435 return status;
4436 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004437
Ronald Cron9a986162021-03-26 12:40:07 +01004438 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4440 if (status != PSA_SUCCESS) {
4441 return status;
4442 }
mohammad16035955c982018-04-26 00:53:03 +03004443
Ronald Cron215633c2021-03-16 17:15:37 +01004444 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004446 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004447
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 status = psa_aead_check_nonce_length(alg, nonce_length);
4449 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004450 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004452
Ronald Cronde822812021-03-17 16:08:20 +01004453 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004454 &attributes, slot->key.data, slot->key.bytes,
4455 alg,
4456 nonce, nonce_length,
4457 additional_data, additional_data_length,
4458 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004460
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 if (status != PSA_SUCCESS && plaintext_size != 0) {
4462 memset(plaintext, 0, plaintext_size);
4463 }
mohammad160360a64d02018-06-03 17:20:42 +03004464
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004465exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004466 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004467
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 return status;
mohammad16035955c982018-04-26 00:53:03 +03004469}
4470
Gilles Peskine449bd832023-01-11 14:50:10 +01004471static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4472{
4473 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004474
Gilles Peskine449bd832023-01-11 14:50:10 +01004475 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004476#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004477 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004478 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4480 return PSA_ERROR_INVALID_ARGUMENT;
4481 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004482 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004483#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004484
Przemek Stekiel86679c72022-10-06 17:06:56 +02004485#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004486 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004487 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4489 return PSA_ERROR_INVALID_ARGUMENT;
4490 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004491 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004492#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004493
Przemek Stekiel86679c72022-10-06 17:06:56 +02004494#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004495 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004496 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 if (tag_len != 16) {
4498 return PSA_ERROR_INVALID_ARGUMENT;
4499 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004500 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004501#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004502
4503 default:
4504 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004506 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004508}
4509
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004510/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004511static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4512 int is_encrypt,
4513 mbedtls_svc_key_id_t key,
4514 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004515{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004516 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4517 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4518 psa_key_slot_t *slot = NULL;
4519 psa_key_usage_t key_usage = 0;
4520
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 status = psa_aead_check_algorithm(alg);
4522 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004523 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004525
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004527 status = PSA_ERROR_BAD_STATE;
4528 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004529 }
4530
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 if (operation->nonce_set || operation->lengths_set ||
4532 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004533 status = PSA_ERROR_BAD_STATE;
4534 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004535 }
4536
Gilles Peskine449bd832023-01-11 14:50:10 +01004537 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004538 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004540 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004542
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4544 alg);
4545 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004546 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004548
4549 psa_key_attributes_t attributes = {
4550 .core = slot->attr
4551 };
4552
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004554 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004556
Gilles Peskine449bd832023-01-11 14:50:10 +01004557 if (is_encrypt) {
4558 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4559 &attributes,
4560 slot->key.data,
4561 slot->key.bytes,
4562 alg);
4563 } else {
4564 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4565 &attributes,
4566 slot->key.data,
4567 slot->key.bytes,
4568 alg);
4569 }
4570 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004571 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004573
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004575
4576exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004578
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004580 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004582 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 } else {
4584 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004585 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004586
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004588}
4589
Paul Elliott302ff6b2021-04-20 18:10:30 +01004590/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004591psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4592 mbedtls_svc_key_id_t key,
4593 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004594{
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004596}
4597
4598/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004599psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4600 mbedtls_svc_key_id_t key,
4601 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004602{
Gilles Peskine449bd832023-01-11 14:50:10 +01004603 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004604}
4605
4606/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004607psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4608 uint8_t *nonce,
4609 size_t nonce_size,
4610 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004611{
4612 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004613 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004614 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004615
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004616 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004617
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004619 status = PSA_ERROR_BAD_STATE;
4620 goto exit;
4621 }
4622
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004624 status = PSA_ERROR_BAD_STATE;
4625 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004626 }
4627
Paul Elliotte193ea82021-10-01 13:00:16 +01004628 /* For CCM, this size may not be correct according to the PSA
4629 * specification. The PSA Crypto 1.0.1 specification states:
4630 *
4631 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4632 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4633 *
4634 * However this restriction that L has to be the smallest integer is not
4635 * applied in practice, and it is not implementable here since the
4636 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004637 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4638 operation->alg);
4639 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004640 status = PSA_ERROR_BUFFER_TOO_SMALL;
4641 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004642 }
4643
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 status = psa_generate_random(local_nonce, required_nonce_size);
4645 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004646 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004648
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004650
Paul Elliott39dc6b82021-05-11 19:16:09 +01004651exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 if (status == PSA_SUCCESS) {
4653 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004654 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 } else {
4656 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004657 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004658
Gilles Peskine449bd832023-01-11 14:50:10 +01004659 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004660}
4661
4662/* Set the nonce for a multipart authenticated encryption or decryption
4663 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004664psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4665 const uint8_t *nonce,
4666 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004667{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004668 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4669
Gilles Peskine449bd832023-01-11 14:50:10 +01004670 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004671 status = PSA_ERROR_BAD_STATE;
4672 goto exit;
4673 }
4674
Gilles Peskine449bd832023-01-11 14:50:10 +01004675 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004676 status = PSA_ERROR_BAD_STATE;
4677 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004678 }
4679
Gilles Peskine449bd832023-01-11 14:50:10 +01004680 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4681 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004682 status = PSA_ERROR_INVALID_ARGUMENT;
4683 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004684 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004685
Gilles Peskine449bd832023-01-11 14:50:10 +01004686 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4687 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004688
Paul Elliott39dc6b82021-05-11 19:16:09 +01004689exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004691 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 } else {
4693 psa_aead_abort(operation);
4694 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004695
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004697}
4698
4699/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004700psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4701 size_t ad_length,
4702 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004703{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004704 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4705
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004707 status = PSA_ERROR_BAD_STATE;
4708 goto exit;
4709 }
4710
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 if (operation->lengths_set || operation->ad_started ||
4712 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004713 status = PSA_ERROR_BAD_STATE;
4714 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004715 }
4716
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004718#if defined(PSA_WANT_ALG_GCM)
4719 case PSA_ALG_GCM:
4720 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 * bits. Without the guard this code will generate warnings on 32bit
4722 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004723#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 if (((uint64_t) ad_length) >> 61 != 0 ||
4725 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004726 status = PSA_ERROR_INVALID_ARGUMENT;
4727 goto exit;
4728 }
Paul Elliott325d3742021-09-27 17:56:28 +01004729#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004730 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004731#endif /* PSA_WANT_ALG_GCM */
4732#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004733 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004735 status = PSA_ERROR_INVALID_ARGUMENT;
4736 goto exit;
4737 }
4738 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004739#endif /* PSA_WANT_ALG_CCM */
4740#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004741 case PSA_ALG_CHACHA20_POLY1305:
4742 /* No length restrictions for ChaChaPoly. */
4743 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004744#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004745 default:
4746 break;
4747 }
Paul Elliott325d3742021-09-27 17:56:28 +01004748
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4750 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004751
Paul Elliott39dc6b82021-05-11 19:16:09 +01004752exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004754 operation->ad_remaining = ad_length;
4755 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004756 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004757 } else {
4758 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004759 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004760
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004762}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004763
4764/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004765psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4766 const uint8_t *input,
4767 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004768{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004769 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4770
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004772 status = PSA_ERROR_BAD_STATE;
4773 goto exit;
4774 }
4775
Gilles Peskine449bd832023-01-11 14:50:10 +01004776 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004777 status = PSA_ERROR_BAD_STATE;
4778 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004779 }
4780
Gilles Peskine449bd832023-01-11 14:50:10 +01004781 if (operation->lengths_set) {
4782 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004783 status = PSA_ERROR_INVALID_ARGUMENT;
4784 goto exit;
4785 }
4786
4787 operation->ad_remaining -= input_length;
4788 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004789#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004790 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004791 status = PSA_ERROR_BAD_STATE;
4792 goto exit;
4793 }
4794#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004795
Gilles Peskine449bd832023-01-11 14:50:10 +01004796 status = psa_driver_wrapper_aead_update_ad(operation, input,
4797 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004798
Paul Elliott39dc6b82021-05-11 19:16:09 +01004799exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004800 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004801 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004802 } else {
4803 psa_aead_abort(operation);
4804 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004805
Gilles Peskine449bd832023-01-11 14:50:10 +01004806 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004807}
4808
4809/* Encrypt or decrypt a message fragment in an active multipart AEAD
4810 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004811psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4812 const uint8_t *input,
4813 size_t input_length,
4814 uint8_t *output,
4815 size_t output_size,
4816 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004817{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004818 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004819
4820 *output_length = 0;
4821
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004823 status = PSA_ERROR_BAD_STATE;
4824 goto exit;
4825 }
4826
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004828 status = PSA_ERROR_BAD_STATE;
4829 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004830 }
4831
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004833 /* Additional data length was supplied, but not all the additional
4834 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004836 status = PSA_ERROR_INVALID_ARGUMENT;
4837 goto exit;
4838 }
4839
4840 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004841 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004842 status = PSA_ERROR_INVALID_ARGUMENT;
4843 goto exit;
4844 }
4845
4846 operation->body_remaining -= input_length;
4847 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004848#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004850 status = PSA_ERROR_BAD_STATE;
4851 goto exit;
4852 }
4853#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004854
Gilles Peskine449bd832023-01-11 14:50:10 +01004855 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4856 output, output_size,
4857 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004858
Paul Elliott39dc6b82021-05-11 19:16:09 +01004859exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004860 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004861 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004862 } else {
4863 psa_aead_abort(operation);
4864 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004865
Gilles Peskine449bd832023-01-11 14:50:10 +01004866 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004867}
4868
Gilles Peskine449bd832023-01-11 14:50:10 +01004869static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004870{
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 if (operation->id == 0 || !operation->nonce_set) {
4872 return PSA_ERROR_BAD_STATE;
4873 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004874
Gilles Peskine449bd832023-01-11 14:50:10 +01004875 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4876 operation->body_remaining != 0)) {
4877 return PSA_ERROR_INVALID_ARGUMENT;
4878 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004879
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004881}
4882
Paul Elliott302ff6b2021-04-20 18:10:30 +01004883/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004884psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4885 uint8_t *ciphertext,
4886 size_t ciphertext_size,
4887 size_t *ciphertext_length,
4888 uint8_t *tag,
4889 size_t tag_size,
4890 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004891{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004892 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4893
Paul Elliott302ff6b2021-04-20 18:10:30 +01004894 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004895 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004896
Gilles Peskine449bd832023-01-11 14:50:10 +01004897 status = psa_aead_final_checks(operation);
4898 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004899 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004900 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004901
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004903 status = PSA_ERROR_BAD_STATE;
4904 goto exit;
4905 }
4906
Gilles Peskine449bd832023-01-11 14:50:10 +01004907 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4908 ciphertext_size,
4909 ciphertext_length,
4910 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004911
Paul Elliott39dc6b82021-05-11 19:16:09 +01004912exit:
Paul Elliottf47b0952021-05-21 18:02:33 +01004913 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004914 * the zero tag size, make sure the tag is set to something implausible.
4915 * Even if the operation succeeds, make sure we clear the rest of the
4916 * buffer to prevent potential leakage of anything previously placed in
4917 * the same buffer.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004918 if (tag != NULL) {
4919 if (status != PSA_SUCCESS) {
4920 memset(tag, '!', tag_size);
4921 } else if (*tag_length < tag_size) {
4922 memset(tag + *tag_length, '!', (tag_size - *tag_length));
4923 }
Paul Elliottec95cc92021-09-19 22:33:09 +01004924 }
Paul Elliottf47b0952021-05-21 18:02:33 +01004925
Gilles Peskine449bd832023-01-11 14:50:10 +01004926 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004927
Gilles Peskine449bd832023-01-11 14:50:10 +01004928 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004929}
4930
4931/* Finish authenticating and decrypting a message in a multipart AEAD
4932 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004933psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4934 uint8_t *plaintext,
4935 size_t plaintext_size,
4936 size_t *plaintext_length,
4937 const uint8_t *tag,
4938 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004939{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004940 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4941
Paul Elliott302ff6b2021-04-20 18:10:30 +01004942 *plaintext_length = 0;
4943
Gilles Peskine449bd832023-01-11 14:50:10 +01004944 status = psa_aead_final_checks(operation);
4945 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004946 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004947 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004948
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004950 status = PSA_ERROR_BAD_STATE;
4951 goto exit;
4952 }
4953
Gilles Peskine449bd832023-01-11 14:50:10 +01004954 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4955 plaintext_size,
4956 plaintext_length,
4957 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004958
4959exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004960 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004961
Gilles Peskine449bd832023-01-11 14:50:10 +01004962 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004963}
4964
4965/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004966psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004967{
4968 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4969
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004971 /* The object has (apparently) been initialized but it is not (yet)
4972 * in use. It's ok to call abort on such an object, and there's
4973 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004974 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004975 }
4976
Gilles Peskine449bd832023-01-11 14:50:10 +01004977 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004978
Gilles Peskine449bd832023-01-11 14:50:10 +01004979 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004980
Gilles Peskine449bd832023-01-11 14:50:10 +01004981 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004982}
4983
Gilles Peskinee59236f2018-01-27 23:32:46 +01004984/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004985/* Generators */
4986/****************************************************************/
4987
Przemek Stekiel69c46792022-06-10 12:59:51 +02004988#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004989 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004990 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
4991 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08004992#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004993#endif /* At least one builtin KDF */
4994
Przemek Stekiel69c46792022-06-10 12:59:51 +02004995#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02004996 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4997 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4998static psa_status_t psa_key_derivation_start_hmac(
4999 psa_mac_operation_t *operation,
5000 psa_algorithm_t hash_alg,
5001 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005003{
5004 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5005 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005006 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5007 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5008 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005009
Steven Cooreman72f736a2021-05-07 14:14:37 +02005010 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005011 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005012
Gilles Peskine449bd832023-01-11 14:50:10 +01005013 status = psa_driver_wrapper_mac_sign_setup(operation,
5014 &attributes,
5015 hmac_key, hmac_key_length,
5016 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005017
Gilles Peskine449bd832023-01-11 14:50:10 +01005018 psa_reset_key_attributes(&attributes);
5019 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005020}
5021#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005022
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005023#define HKDF_STATE_INIT 0 /* no input yet */
5024#define HKDF_STATE_STARTED 1 /* got salt */
5025#define HKDF_STATE_KEYED 2 /* got key */
5026#define HKDF_STATE_OUTPUT 3 /* output started */
5027
Gilles Peskinecbe66502019-05-16 16:59:18 +02005028static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005029 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005030{
Gilles Peskine449bd832023-01-11 14:50:10 +01005031 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5032 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5033 } else {
5034 return operation->alg;
5035 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005036}
5037
Gilles Peskine449bd832023-01-11 14:50:10 +01005038psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005039{
5040 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005041 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5042 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005043 /* The object has (apparently) been initialized but it is not
5044 * in use. It's ok to call abort on such an object, and there's
5045 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005047#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5049 mbedtls_free(operation->ctx.hkdf.info);
5050 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5051 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005052#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005053#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5054 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5056 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5057 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5058 if (operation->ctx.tls12_prf.secret != NULL) {
5059 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5060 operation->ctx.tls12_prf.secret_length);
5061 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005062 }
5063
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 if (operation->ctx.tls12_prf.seed != NULL) {
5065 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5066 operation->ctx.tls12_prf.seed_length);
5067 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005068 }
5069
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 if (operation->ctx.tls12_prf.label != NULL) {
5071 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5072 operation->ctx.tls12_prf.label_length);
5073 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005074 }
5075
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 if (operation->ctx.tls12_prf.other_secret != NULL) {
5077 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5078 operation->ctx.tls12_prf.other_secret_length);
5079 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005080 }
5081
Steven Cooremana6df6042021-04-29 19:32:25 +02005082 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005083
5084 /* We leave the fields Ai and output_block to be erased safely by the
5085 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005087#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5088 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005089#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5091 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5092 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5093 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005094#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005095 {
5096 status = PSA_ERROR_BAD_STATE;
5097 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005098 mbedtls_platform_zeroize(operation, sizeof(*operation));
5099 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005100}
5101
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005102psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005103 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005104{
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005106 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005108 }
5109
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005110 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005112}
5113
Gilles Peskine449bd832023-01-11 14:50:10 +01005114psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5115 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005116{
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 if (operation->alg == 0) {
5118 return PSA_ERROR_BAD_STATE;
5119 }
5120 if (capacity > operation->capacity) {
5121 return PSA_ERROR_INVALID_ARGUMENT;
5122 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005123 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005124 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005125}
5126
Przemek Stekiel69c46792022-06-10 12:59:51 +02005127#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005128/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005129static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5130 psa_algorithm_t kdf_alg,
5131 uint8_t *output,
5132 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005133{
Gilles Peskine449bd832023-01-11 14:50:10 +01005134 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5135 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005136 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005137 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005138#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005140#else
5141 const uint8_t last_block = 0xff;
5142#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005143
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 if (hkdf->state < HKDF_STATE_KEYED ||
5145 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005146#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005148#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 )) {
5150 return PSA_ERROR_BAD_STATE;
5151 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005152 hkdf->state = HKDF_STATE_OUTPUT;
5153
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005155 /* Copy what remains of the current block */
5156 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005158 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 }
5160 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005161 output += n;
5162 output_length -= n;
5163 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005165 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005167 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005168 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005169 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005170 * object was corrupted or if this function is called directly
5171 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 if (hkdf->block_number == last_block) {
5173 return PSA_ERROR_BAD_STATE;
5174 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005175
5176 /* We need a new block */
5177 ++hkdf->block_number;
5178 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005179
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5181 hash_alg,
5182 hkdf->prk,
5183 hash_length);
5184 if (status != PSA_SUCCESS) {
5185 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005186 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005187
5188 if (hkdf->block_number != 1) {
5189 status = psa_mac_update(&hkdf->hmac,
5190 hkdf->output_block,
5191 hash_length);
5192 if (status != PSA_SUCCESS) {
5193 return status;
5194 }
5195 }
5196 status = psa_mac_update(&hkdf->hmac,
5197 hkdf->info,
5198 hkdf->info_length);
5199 if (status != PSA_SUCCESS) {
5200 return status;
5201 }
5202 status = psa_mac_update(&hkdf->hmac,
5203 &hkdf->block_number, 1);
5204 if (status != PSA_SUCCESS) {
5205 return status;
5206 }
5207 status = psa_mac_sign_finish(&hkdf->hmac,
5208 hkdf->output_block,
5209 sizeof(hkdf->output_block),
5210 &hmac_output_length);
5211 if (status != PSA_SUCCESS) {
5212 return status;
5213 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005214 }
5215
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005217}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005218#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005219
John Durkop07cc04a2020-11-16 22:08:34 -08005220#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5221 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005222static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5223 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005224 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005225{
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5227 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005228 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5229 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005230 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005231
Janos Follath7742fee2019-06-17 12:58:10 +01005232 /* We can't be wanting more output after block 0xff, otherwise
5233 * the capacity check in psa_key_derivation_output_bytes() would have
5234 * prevented this call. It could happen only if the operation
5235 * object was corrupted or if this function is called directly
5236 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 if (tls12_prf->block_number == 0xff) {
5238 return PSA_ERROR_CORRUPTION_DETECTED;
5239 }
Janos Follath7742fee2019-06-17 12:58:10 +01005240
5241 /* We need a new block */
5242 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005243 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005244
5245 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5246 *
5247 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5248 *
5249 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5250 * HMAC_hash(secret, A(2) + seed) +
5251 * HMAC_hash(secret, A(3) + seed) + ...
5252 *
5253 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005254 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005255 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005256 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005257 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005258 * is currently extracted as `output_block` and where i is
5259 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005260 */
5261
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 status = psa_key_derivation_start_hmac(&hmac,
5263 hash_alg,
5264 tls12_prf->secret,
5265 tls12_prf->secret_length);
5266 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005267 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005269
5270 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005272 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5273 * the variable seed and in this instance means it in the context of the
5274 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 status = psa_mac_update(&hmac,
5276 tls12_prf->label,
5277 tls12_prf->label_length);
5278 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005279 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005280 }
5281 status = psa_mac_update(&hmac,
5282 tls12_prf->seed,
5283 tls12_prf->seed_length);
5284 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005285 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 }
5287 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005288 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5290 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005291 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005293 }
5294
Gilles Peskine449bd832023-01-11 14:50:10 +01005295 status = psa_mac_sign_finish(&hmac,
5296 tls12_prf->Ai, hash_length,
5297 &hmac_output_length);
5298 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005299 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 }
5301 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005302 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005303 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005304
5305 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 status = psa_key_derivation_start_hmac(&hmac,
5307 hash_alg,
5308 tls12_prf->secret,
5309 tls12_prf->secret_length);
5310 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005311 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005312 }
5313 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5314 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005315 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 }
5317 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5318 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005319 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 }
5321 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5322 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005323 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 }
5325 status = psa_mac_sign_finish(&hmac,
5326 tls12_prf->output_block, hash_length,
5327 &hmac_output_length);
5328 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005329 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005331
Janos Follath7742fee2019-06-17 12:58:10 +01005332
5333cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 cleanup_status = psa_mac_abort(&hmac);
5335 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005336 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005338
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005340}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005341
Janos Follath844eb0e2019-06-19 12:10:49 +01005342static psa_status_t psa_key_derivation_tls12_prf_read(
5343 psa_tls12_prf_key_derivation_t *tls12_prf,
5344 psa_algorithm_t alg,
5345 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005347{
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5349 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005350 psa_status_t status;
5351 uint8_t offset, length;
5352
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005354 case PSA_TLS12_PRF_STATE_LABEL_SET:
5355 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5356 break;
5357 case PSA_TLS12_PRF_STATE_OUTPUT:
5358 break;
5359 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005360 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005361 }
5362
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005364 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 if (tls12_prf->left_in_block == 0) {
5366 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5367 alg);
5368 if (status != PSA_SUCCESS) {
5369 return status;
5370 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005371
5372 continue;
5373 }
5374
Gilles Peskine449bd832023-01-11 14:50:10 +01005375 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005376 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005378 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005380
5381 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005383 output += length;
5384 output_length -= length;
5385 tls12_prf->left_in_block -= length;
5386 }
5387
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005389}
John Durkop07cc04a2020-11-16 22:08:34 -08005390#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5391 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005392
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005393#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5394static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5395 psa_tls12_ecjpake_to_pms_t *ecjpake,
5396 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005398{
5399 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005400 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005401
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 if (output_length != 32) {
5403 return PSA_ERROR_INVALID_ARGUMENT;
5404 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005405
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5407 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5408 &output_size);
5409 if (status != PSA_SUCCESS) {
5410 return status;
5411 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005412
Gilles Peskine449bd832023-01-11 14:50:10 +01005413 if (output_size != output_length) {
5414 return PSA_ERROR_GENERIC_ERROR;
5415 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005416
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005418}
5419#endif
5420
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005421psa_status_t psa_key_derivation_output_bytes(
5422 psa_key_derivation_operation_t *operation,
5423 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005424 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005425{
5426 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005428
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005430 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005432 }
5433
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005435 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005436 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005437 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005438 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005439 goto exit;
5440 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005442 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005443 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005444 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5445 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005446 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005447 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005449 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005450 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005451
Przemek Stekiel69c46792022-06-10 12:59:51 +02005452#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5454 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5455 output, output_length);
5456 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005457#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005458#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5459 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5461 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5462 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5463 kdf_alg, output,
5464 output_length);
5465 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005466#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5467 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005468#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005470 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5472 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005473#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5474
Gilles Peskineeab56e42018-07-12 17:12:33 +02005475 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005476 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005478 }
5479
5480exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005482 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005483 * This allows us to differentiate between exhausted operations and
5484 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5485 * operations. */
5486 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005488 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005490 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005492}
5493
David Brown7807bf72021-02-09 16:28:23 -07005494#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005495static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005496{
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 if (data_size >= 8) {
5498 mbedtls_des_key_set_parity(data);
5499 }
5500 if (data_size >= 16) {
5501 mbedtls_des_key_set_parity(data + 8);
5502 }
5503 if (data_size >= 24) {
5504 mbedtls_des_key_set_parity(data + 16);
5505 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005506}
David Brown7807bf72021-02-09 16:28:23 -07005507#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005508
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005509/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 * ECC keys on a Weierstrass elliptic curve require the generation
5511 * of a private key which is an integer
5512 * in the range [1, N - 1], where N is the boundary of the private key domain:
5513 * N is the prime p for Diffie-Hellman, or the order of the
5514 * curve’s base point for ECC.
5515 *
5516 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5517 * This function generates the private key using the following process:
5518 *
5519 * 1. Draw a byte string of length ceiling(m/8) bytes.
5520 * 2. If m is not a multiple of 8, set the most significant
5521 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5522 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5523 * 4. If k > N - 2, discard the result and return to step 1.
5524 * 5. Output k + 1 as the private key.
5525 *
5526 * This method allows compliance to NIST standards, specifically the methods titled
5527 * Key-Pair Generation by Testing Candidates in the following publications:
5528 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5529 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5530 * Diffie-Hellman keys.
5531 *
5532 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5533 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5534 *
5535 * Note: Function allocates memory for *data buffer, so given *data should be
5536 * always NULL.
5537 */
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005538#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
5539 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
5540 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5541 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5542 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005543static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005544 psa_key_slot_t *slot,
5545 size_t bits,
5546 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005547 uint8_t **data
5548 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005549{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005550 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005551 mbedtls_mpi k;
5552 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005554 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005555
Gilles Peskine449bd832023-01-11 14:50:10 +01005556 mbedtls_mpi_init(&k);
5557 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005558
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005559 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005561 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005563
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005565 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005566 goto cleanup;
5567 }
5568
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005569 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005570 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005571
Gilles Peskine449bd832023-01-11 14:50:10 +01005572 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005573
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005574 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005575 /* Let m be the bit size of N. */
5576 size_t m = ecp_group.nbits;
5577
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005579
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005580 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005581 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005582
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005583 /* Note: This function is always called with *data == NULL and it
5584 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005585 *data = mbedtls_calloc(1, m_bytes);
5586 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005587 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005588 goto cleanup;
5589 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005590
Gilles Peskine449bd832023-01-11 14:50:10 +01005591 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005592 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005593 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005594 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005595 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005596
5597 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005599 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005600 * (8 * ceiling(m/8) - m) bits of the first byte in
5601 * the string to zero.
5602 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005603 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005604 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005605 }
5606
5607 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005608 * big-endian byte string.
5609 */
5610 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005611
5612 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 * Result of comparison is returned. When it indicates error
5614 * then this function is called again.
5615 */
5616 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005617 }
5618
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005619 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5621 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005622cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005623 if (ret != 0) {
5624 status = mbedtls_to_psa_error(ret);
5625 }
5626 if (status != PSA_SUCCESS) {
5627 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005628 *data = NULL;
5629 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005630 mbedtls_mpi_free(&k);
5631 mbedtls_mpi_free(&diff_N_2);
5632 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005633}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005634
5635/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5636 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5637 *
5638 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5639 * draw a 32-byte string and process it as specified in
5640 * Elliptic Curves for Security [RFC7748] §5.
5641 *
5642 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5643 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5644 *
5645 * Note: Function allocates memory for *data buffer, so given *data should be
5646 * always NULL.
5647 */
5648
5649static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5650 size_t bits,
5651 psa_key_derivation_operation_t *operation,
5652 uint8_t **data
5653 )
5654{
5655 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005656 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005657
Gilles Peskine449bd832023-01-11 14:50:10 +01005658 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005659 case 255:
5660 output_length = 32;
5661 break;
5662 case 448:
5663 output_length = 56;
5664 break;
5665 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005666 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005667 break;
5668 }
5669
Gilles Peskine449bd832023-01-11 14:50:10 +01005670 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005671
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 if (*data == NULL) {
5673 return PSA_ERROR_INSUFFICIENT_MEMORY;
5674 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005675
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005677
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005679 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005681
Gilles Peskine449bd832023-01-11 14:50:10 +01005682 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005683 case 255:
5684 (*data)[0] &= 248;
5685 (*data)[31] &= 127;
5686 (*data)[31] |= 64;
5687 break;
5688 case 448:
5689 (*data)[0] &= 252;
5690 (*data)[55] |= 128;
5691 break;
5692 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005693 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005694 break;
5695 }
5696
5697 return status;
5698}
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005699#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
5700 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
5701 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5702 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5703 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005704
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005705static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005706 psa_key_slot_t *slot,
5707 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005708 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005709{
5710 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005711 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305712 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005713 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005714
Gilles Peskine449bd832023-01-11 14:50:10 +01005715 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5716 return PSA_ERROR_INVALID_ARGUMENT;
5717 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005718
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005719#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
5720 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
5721 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5722 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5723 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5725 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5726 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005727 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5729 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005730 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005731 }
5732 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005733 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5735 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005736 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005738 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005739 } else
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005740#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
5741 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
5742 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5743 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5744 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 if (key_type_is_raw_bytes(slot->attr.type)) {
5746 if (bits % 8 != 0) {
5747 return PSA_ERROR_INVALID_ARGUMENT;
5748 }
5749 data = mbedtls_calloc(1, bytes);
5750 if (data == NULL) {
5751 return PSA_ERROR_INSUFFICIENT_MEMORY;
5752 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005753
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 status = psa_key_derivation_output_bytes(operation, data, bytes);
5755 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005756 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 }
David Brown12ca5032021-02-11 11:02:00 -07005758#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005759 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5760 psa_des_set_key_parity(data, bytes);
5761 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005762#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005763 } else {
5764 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005765 }
Ronald Crondd04d422020-11-28 15:14:42 +01005766
Ronald Cronbf33c932020-11-28 18:06:53 +01005767 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005768 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005770 };
5771
Gilles Peskine449bd832023-01-11 14:50:10 +01005772 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5773 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5774 &storage_size);
5775 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305776 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 }
Archanad8a83dc2021-06-14 10:04:16 +05305778 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 status = psa_allocate_buffer_to_slot(slot, storage_size);
5780 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305781 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 }
Archanad8a83dc2021-06-14 10:04:16 +05305783
Gilles Peskine449bd832023-01-11 14:50:10 +01005784 status = psa_driver_wrapper_import_key(&attributes,
5785 data, bytes,
5786 slot->key.data,
5787 slot->key.bytes,
5788 &slot->key.bytes, &bits);
5789 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005790 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005792
5793exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005794 mbedtls_free(data);
5795 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005796}
5797
Gilles Peskine449bd832023-01-11 14:50:10 +01005798psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5799 psa_key_derivation_operation_t *operation,
5800 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005801{
5802 psa_status_t status;
5803 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005804 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005805
Ronald Cron81709fc2020-11-14 12:10:32 +01005806 *key = MBEDTLS_SVC_KEY_ID_INIT;
5807
Gilles Peskine0f84d622019-09-12 19:03:13 +02005808 /* Reject any attempt to create a zero-length key so that we don't
5809 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005810 if (psa_get_key_bits(attributes) == 0) {
5811 return PSA_ERROR_INVALID_ARGUMENT;
5812 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005813
Gilles Peskine449bd832023-01-11 14:50:10 +01005814 if (operation->alg == PSA_ALG_NONE) {
5815 return PSA_ERROR_BAD_STATE;
5816 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005817
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 if (!operation->can_output_key) {
5819 return PSA_ERROR_NOT_PERMITTED;
5820 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005821
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5823 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005824#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005826 /* Deriving a key in a secure element is not implemented yet. */
5827 status = PSA_ERROR_NOT_SUPPORTED;
5828 }
5829#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005830 if (status == PSA_SUCCESS) {
5831 status = psa_generate_derived_key_internal(slot,
5832 attributes->core.bits,
5833 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005834 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005835 if (status == PSA_SUCCESS) {
5836 status = psa_finish_key_creation(slot, driver, key);
5837 }
5838 if (status != PSA_SUCCESS) {
5839 psa_fail_key_creation(slot, driver);
5840 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005841
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005843}
5844
Gilles Peskineeab56e42018-07-12 17:12:33 +02005845
5846
5847/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005848/* Key derivation */
5849/****************************************************************/
5850
Steven Cooreman932ffb72021-02-15 12:14:32 +01005851#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005852static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005853{
5854#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005855 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5856 return 1;
5857 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005858#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005859#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5861 return 1;
5862 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005863#endif
5864#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5866 return 1;
5867 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005868#endif
5869#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005870 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5871 return 1;
5872 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005873#endif
5874#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5876 return 1;
5877 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005878#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005879#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5881 return 1;
5882 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005883#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005884 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005885}
5886
Gilles Peskine449bd832023-01-11 14:50:10 +01005887static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005888{
5889 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005890 psa_status_t status = psa_hash_setup(&operation, alg);
5891 psa_hash_abort(&operation);
5892 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005893}
5894
Gilles Peskine969c5d62019-01-16 15:53:06 +01005895static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005896 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005897 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005898{
Janos Follath5fe19732019-06-20 15:09:30 +01005899 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5900 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005901 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005902
Gilles Peskine969c5d62019-01-16 15:53:06 +01005903 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005904 if (!is_kdf_alg_supported(kdf_alg)) {
5905 return PSA_ERROR_NOT_SUPPORTED;
5906 }
John Durkop07cc04a2020-11-16 22:08:34 -08005907
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005908 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005909 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005910 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5911 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5912 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5913 if (hash_size == 0) {
5914 return PSA_ERROR_NOT_SUPPORTED;
5915 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005916
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005917 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5918 * we might fail later, which is somewhat unfriendly and potentially
5919 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 psa_status_t status = psa_hash_try_support(hash_alg);
5921 if (status != PSA_SUCCESS) {
5922 return status;
5923 }
5924 } else {
5925 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005926 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005927
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5929 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
5930 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
5931 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005932 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005933#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005934 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005935 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
5936 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005937 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005939#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5940 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 operation->capacity = 255 * hash_size;
5942 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005943}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005944
Gilles Peskine449bd832023-01-11 14:50:10 +01005945static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005946{
5947#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005948 if (alg == PSA_ALG_ECDH) {
5949 return PSA_SUCCESS;
5950 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005951#endif
5952 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005954}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005955
5956static int psa_key_derivation_allows_free_form_secret_input(
5957 psa_algorithm_t kdf_alg)
5958{
5959#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
5960 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5961 return 0;
5962 }
5963#endif
5964 (void) kdf_alg;
5965 return 1;
5966}
John Durkop07cc04a2020-11-16 22:08:34 -08005967#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005968
Gilles Peskine449bd832023-01-11 14:50:10 +01005969psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
5970 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005971{
5972 psa_status_t status;
5973
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 if (operation->alg != 0) {
5975 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005976 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005977
Gilles Peskine449bd832023-01-11 14:50:10 +01005978 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
5979 return PSA_ERROR_INVALID_ARGUMENT;
5980 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
5981#if defined(AT_LEAST_ONE_BUILTIN_KDF)
5982 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
5983 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
5984 status = psa_key_agreement_try_support(ka_alg);
5985 if (status != PSA_SUCCESS) {
5986 return status;
5987 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005988 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
5989 return PSA_ERROR_INVALID_ARGUMENT;
5990 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
5992#else
5993 return PSA_ERROR_NOT_SUPPORTED;
5994#endif /* AT_LEAST_ONE_BUILTIN_KDF */
5995 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
5996#if defined(AT_LEAST_ONE_BUILTIN_KDF)
5997 status = psa_key_derivation_setup_kdf(operation, alg);
5998#else
5999 return PSA_ERROR_NOT_SUPPORTED;
6000#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6001 } else {
6002 return PSA_ERROR_INVALID_ARGUMENT;
6003 }
6004
6005 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006006 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 }
6008 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006009}
6010
Przemek Stekiel69c46792022-06-10 12:59:51 +02006011#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006012static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6013 psa_algorithm_t kdf_alg,
6014 psa_key_derivation_step_t step,
6015 const uint8_t *data,
6016 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006017{
Gilles Peskine449bd832023-01-11 14:50:10 +01006018 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006019 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006021 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006022#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006023 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6024 return PSA_ERROR_INVALID_ARGUMENT;
6025 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006026#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006027 if (hkdf->state != HKDF_STATE_INIT) {
6028 return PSA_ERROR_BAD_STATE;
6029 } else {
6030 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6031 hash_alg,
6032 data, data_length);
6033 if (status != PSA_SUCCESS) {
6034 return status;
6035 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006036 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006037 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006038 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006039 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006040#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006041 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006042 /* We shouldn't be in different state as HKDF_EXPAND only allows
6043 * two inputs: SECRET (this case) and INFO which does not modify
6044 * the state. It could happen only if the hkdf
6045 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 if (hkdf->state != HKDF_STATE_INIT) {
6047 return PSA_ERROR_BAD_STATE;
6048 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006049
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006050 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6052 return PSA_ERROR_INVALID_ARGUMENT;
6053 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006054
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 memcpy(hkdf->prk, data, data_length);
6056 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006057#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006058 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006059 /* HKDF: If no salt was provided, use an empty salt.
6060 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006061 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006062#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6064 return PSA_ERROR_BAD_STATE;
6065 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006066#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6068 hash_alg,
6069 NULL, 0);
6070 if (status != PSA_SUCCESS) {
6071 return status;
6072 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006073 hkdf->state = HKDF_STATE_STARTED;
6074 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 if (hkdf->state != HKDF_STATE_STARTED) {
6076 return PSA_ERROR_BAD_STATE;
6077 }
6078 status = psa_mac_update(&hkdf->hmac,
6079 data, data_length);
6080 if (status != PSA_SUCCESS) {
6081 return status;
6082 }
6083 status = psa_mac_sign_finish(&hkdf->hmac,
6084 hkdf->prk,
6085 sizeof(hkdf->prk),
6086 &data_length);
6087 if (status != PSA_SUCCESS) {
6088 return status;
6089 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006090 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006091
Gilles Peskine2b522db2019-04-12 15:11:49 +02006092 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006093 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006094#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006096 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006098 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006100#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006101 {
6102 /* Block 0 is empty, and the next block will be
6103 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006105 }
6106
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006108 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006109#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6111 return PSA_ERROR_INVALID_ARGUMENT;
6112 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006113#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006114#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6116 hkdf->state == HKDF_STATE_INIT) {
6117 return PSA_ERROR_BAD_STATE;
6118 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006119#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 if (hkdf->state == HKDF_STATE_OUTPUT) {
6121 return PSA_ERROR_BAD_STATE;
6122 }
6123 if (hkdf->info_set) {
6124 return PSA_ERROR_BAD_STATE;
6125 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006126 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006127 if (data_length != 0) {
6128 hkdf->info = mbedtls_calloc(1, data_length);
6129 if (hkdf->info == NULL) {
6130 return PSA_ERROR_INSUFFICIENT_MEMORY;
6131 }
6132 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006133 }
6134 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006136 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006137 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006138 }
6139}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006140#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006141
John Durkop07cc04a2020-11-16 22:08:34 -08006142#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6143 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006144static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6145 const uint8_t *data,
6146 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006147{
Gilles Peskine449bd832023-01-11 14:50:10 +01006148 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6149 return PSA_ERROR_BAD_STATE;
6150 }
Janos Follathf08e2652019-06-13 09:05:41 +01006151
Gilles Peskine449bd832023-01-11 14:50:10 +01006152 if (data_length != 0) {
6153 prf->seed = mbedtls_calloc(1, data_length);
6154 if (prf->seed == NULL) {
6155 return PSA_ERROR_INSUFFICIENT_MEMORY;
6156 }
Janos Follathf08e2652019-06-13 09:05:41 +01006157
Gilles Peskine449bd832023-01-11 14:50:10 +01006158 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006159 prf->seed_length = data_length;
6160 }
Janos Follathf08e2652019-06-13 09:05:41 +01006161
Gilles Peskine43f958b2020-12-13 14:55:14 +01006162 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006163
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006165}
6166
Gilles Peskine449bd832023-01-11 14:50:10 +01006167static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6168 const uint8_t *data,
6169 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006170{
Gilles Peskine449bd832023-01-11 14:50:10 +01006171 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6172 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6173 return PSA_ERROR_BAD_STATE;
6174 }
Janos Follath81550542019-06-13 14:26:34 +01006175
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 if (data_length != 0) {
6177 prf->secret = mbedtls_calloc(1, data_length);
6178 if (prf->secret == NULL) {
6179 return PSA_ERROR_INSUFFICIENT_MEMORY;
6180 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006181
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006183 prf->secret_length = data_length;
6184 }
Janos Follath81550542019-06-13 14:26:34 +01006185
Gilles Peskine43f958b2020-12-13 14:55:14 +01006186 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006187
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006189}
6190
Gilles Peskine449bd832023-01-11 14:50:10 +01006191static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6192 const uint8_t *data,
6193 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006194{
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6196 return PSA_ERROR_BAD_STATE;
6197 }
Janos Follath63028dd2019-06-13 09:15:47 +01006198
Gilles Peskine449bd832023-01-11 14:50:10 +01006199 if (data_length != 0) {
6200 prf->label = mbedtls_calloc(1, data_length);
6201 if (prf->label == NULL) {
6202 return PSA_ERROR_INSUFFICIENT_MEMORY;
6203 }
Janos Follath63028dd2019-06-13 09:15:47 +01006204
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006206 prf->label_length = data_length;
6207 }
Janos Follath63028dd2019-06-13 09:15:47 +01006208
Gilles Peskine43f958b2020-12-13 14:55:14 +01006209 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006210
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006212}
6213
Gilles Peskine449bd832023-01-11 14:50:10 +01006214static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6215 psa_key_derivation_step_t step,
6216 const uint8_t *data,
6217 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006218{
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006220 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006222 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006223 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006224 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006226 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006228 }
6229}
John Durkop07cc04a2020-11-16 22:08:34 -08006230#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6231 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6232
6233#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6234static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6235 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006236 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006238{
6239 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006240 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6241 4 + data_length + prf->other_secret_length :
6242 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006243
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6245 return PSA_ERROR_INVALID_ARGUMENT;
6246 }
John Durkop07cc04a2020-11-16 22:08:34 -08006247
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 uint8_t *pms = mbedtls_calloc(1, pms_len);
6249 if (pms == NULL) {
6250 return PSA_ERROR_INSUFFICIENT_MEMORY;
6251 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006252 uint8_t *cur = pms;
6253
6254 /* pure-PSK:
6255 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006256 *
6257 * The premaster secret is formed as follows: if the PSK is N octets
6258 * long, concatenate a uint16 with the value N, N zero octets, a second
6259 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006260 *
6261 * mixed-PSK:
6262 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6263 * follows: concatenate a uint16 with the length of the other secret,
6264 * the other secret itself, uint16 with the length of PSK, and the
6265 * PSK itself.
6266 * For details please check:
6267 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6268 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6269 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006270 */
6271
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6273 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6274 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6275 if (prf->other_secret_length != 0) {
6276 memcpy(cur, prf->other_secret, prf->other_secret_length);
6277 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006278 cur += prf->other_secret_length;
6279 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 } else {
6281 *cur++ = MBEDTLS_BYTE_1(data_length);
6282 *cur++ = MBEDTLS_BYTE_0(data_length);
6283 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006284 cur += data_length;
6285 }
6286
Gilles Peskine449bd832023-01-11 14:50:10 +01006287 *cur++ = MBEDTLS_BYTE_1(data_length);
6288 *cur++ = MBEDTLS_BYTE_0(data_length);
6289 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006290 cur += data_length;
6291
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006293
Gilles Peskine449bd832023-01-11 14:50:10 +01006294 mbedtls_platform_zeroize(pms, pms_len);
6295 mbedtls_free(pms);
6296 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006297}
Janos Follath6660f0e2019-06-17 08:44:03 +01006298
Przemek Stekiele47201b2022-04-19 13:53:28 +02006299static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6300 psa_tls12_prf_key_derivation_t *prf,
6301 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006303{
Gilles Peskine449bd832023-01-11 14:50:10 +01006304 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6305 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006306 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006307
6308 if (data_length != 0) {
6309 prf->other_secret = mbedtls_calloc(1, data_length);
6310 if (prf->other_secret == NULL) {
6311 return PSA_ERROR_INSUFFICIENT_MEMORY;
6312 }
6313
6314 memcpy(prf->other_secret, data, data_length);
6315 prf->other_secret_length = data_length;
6316 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006317 prf->other_secret_length = 0;
6318 }
6319
6320 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6321
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006323}
6324
Janos Follath6660f0e2019-06-17 08:44:03 +01006325static psa_status_t psa_tls12_prf_psk_to_ms_input(
6326 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006327 psa_key_derivation_step_t step,
6328 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006330{
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006332 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 return psa_tls12_prf_psk_to_ms_set_key(prf,
6334 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006335 break;
6336 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6338 data,
6339 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006340 break;
6341 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006343 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006344
Przemek Stekiele47201b2022-04-19 13:53:28 +02006345 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006346}
John Durkop07cc04a2020-11-16 22:08:34 -08006347#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006348
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006349#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6350static psa_status_t psa_tls12_ecjpake_to_pms_input(
6351 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006352 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006353 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006355{
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6357 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6358 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006359 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006360
6361 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 if (data[0] != 0x04) {
6363 return PSA_ERROR_INVALID_ARGUMENT;
6364 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006365
6366 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006368
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006370}
6371#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006372/** Check whether the given key type is acceptable for the given
6373 * input step of a key derivation.
6374 *
6375 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6376 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6377 * Both secret and non-secret inputs can alternatively have the type
6378 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6379 * that the input was passed as a buffer rather than via a key object.
6380 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006381static int psa_key_derivation_check_input_type(
6382 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006384{
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006386 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 if (key_type == PSA_KEY_TYPE_DERIVE) {
6388 return PSA_SUCCESS;
6389 }
6390 if (key_type == PSA_KEY_TYPE_NONE) {
6391 return PSA_SUCCESS;
6392 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006393 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006394 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 if (key_type == PSA_KEY_TYPE_DERIVE) {
6396 return PSA_SUCCESS;
6397 }
6398 if (key_type == PSA_KEY_TYPE_NONE) {
6399 return PSA_SUCCESS;
6400 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006401 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006402 case PSA_KEY_DERIVATION_INPUT_LABEL:
6403 case PSA_KEY_DERIVATION_INPUT_SALT:
6404 case PSA_KEY_DERIVATION_INPUT_INFO:
6405 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6407 return PSA_SUCCESS;
6408 }
6409 if (key_type == PSA_KEY_TYPE_NONE) {
6410 return PSA_SUCCESS;
6411 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006412 break;
6413 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006415}
6416
Janos Follathb80a94e2019-06-12 15:54:46 +01006417static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006418 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006419 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006420 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006421 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006423{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006424 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006426
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 status = psa_key_derivation_check_input_type(step, key_type);
6428 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006429 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006431
Przemek Stekiel69c46792022-06-10 12:59:51 +02006432#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6434 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6435 step, data, data_length);
6436 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006437#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006438#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006439 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6440 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6441 step, data, data_length);
6442 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006443#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6444#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006445 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6446 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6447 step, data, data_length);
6448 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006449#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006450#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006452 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006453 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6454 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006455#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006456 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006457 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006458 (void) data;
6459 (void) data_length;
6460 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006461 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006462 }
6463
Gilles Peskine224b0d62019-09-23 18:13:17 +02006464exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 if (status != PSA_SUCCESS) {
6466 psa_key_derivation_abort(operation);
6467 }
6468 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006469}
6470
Janos Follath51f4a0f2019-06-14 11:35:55 +01006471psa_status_t psa_key_derivation_input_bytes(
6472 psa_key_derivation_operation_t *operation,
6473 psa_key_derivation_step_t step,
6474 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006476{
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 return psa_key_derivation_input_internal(operation, step,
6478 PSA_KEY_TYPE_NONE,
6479 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006480}
6481
Janos Follath51f4a0f2019-06-14 11:35:55 +01006482psa_status_t psa_key_derivation_input_key(
6483 psa_key_derivation_operation_t *operation,
6484 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006486{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006487 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006488 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006489 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006490
Ronald Cron5c522922020-11-14 16:35:34 +01006491 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006492 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6493 if (status != PSA_SUCCESS) {
6494 psa_key_derivation_abort(operation);
6495 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006496 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006497
6498 /* Passing a key object as a SECRET input unlocks the permission
6499 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006501 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006502 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006503
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 status = psa_key_derivation_input_internal(operation,
6505 step, slot->attr.type,
6506 slot->key.data,
6507 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006508
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006510
Gilles Peskine449bd832023-01-11 14:50:10 +01006511 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006512}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006513
6514
6515
6516/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006517/* Key agreement */
6518/****************************************************************/
6519
Gilles Peskine449bd832023-01-11 14:50:10 +01006520psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6521 const uint8_t *key_buffer,
6522 size_t key_buffer_size,
6523 psa_algorithm_t alg,
6524 const uint8_t *peer_key,
6525 size_t peer_key_length,
6526 uint8_t *shared_secret,
6527 size_t shared_secret_size,
6528 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006529{
Gilles Peskine449bd832023-01-11 14:50:10 +01006530 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006531#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006532 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006533 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6534 key_buffer_size, alg,
6535 peer_key, peer_key_length,
6536 shared_secret,
6537 shared_secret_size,
6538 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006539#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006540 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006541 (void) attributes;
6542 (void) key_buffer;
6543 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006544 (void) peer_key;
6545 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006546 (void) shared_secret;
6547 (void) shared_secret_size;
6548 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006549 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006550 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006551}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006552
Aditya Deshpande17845b82022-10-13 17:21:01 +01006553/** Internal function for raw key agreement
6554 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006555 * to the driver's implementation if a driver is present.
6556 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006557 * (psa_key_agreement_raw_builtin).
6558 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006559static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6560 psa_key_slot_t *private_key,
6561 const uint8_t *peer_key,
6562 size_t peer_key_length,
6563 uint8_t *shared_secret,
6564 size_t shared_secret_size,
6565 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006566{
Gilles Peskine449bd832023-01-11 14:50:10 +01006567 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6568 return PSA_ERROR_NOT_SUPPORTED;
6569 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006570
6571 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006572 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006573 };
6574
Gilles Peskine449bd832023-01-11 14:50:10 +01006575 return psa_driver_wrapper_key_agreement(&attributes,
6576 private_key->key.data,
6577 private_key->key.bytes, alg,
6578 peer_key, peer_key_length,
6579 shared_secret,
6580 shared_secret_size,
6581 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006582}
6583
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006584/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006585 * to potentially free embedded data structures and wipe confidential data.
6586 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006587static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6588 psa_key_derivation_step_t step,
6589 psa_key_slot_t *private_key,
6590 const uint8_t *peer_key,
6591 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006592{
6593 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006594 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006595 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006596 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006597
6598 /* Step 1: run the secret agreement algorithm to generate the shared
6599 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006600 status = psa_key_agreement_raw_internal(ka_alg,
6601 private_key,
6602 peer_key, peer_key_length,
6603 shared_secret,
6604 sizeof(shared_secret),
6605 &shared_secret_length);
6606 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006607 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006608 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006609
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006610 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006611 * the shared secret. A shared secret is permitted wherever a key
6612 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006613 status = psa_key_derivation_input_internal(operation, step,
6614 PSA_KEY_TYPE_DERIVE,
6615 shared_secret,
6616 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006617exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006618 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6619 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006620}
6621
Gilles Peskine449bd832023-01-11 14:50:10 +01006622psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6623 psa_key_derivation_step_t step,
6624 mbedtls_svc_key_id_t private_key,
6625 const uint8_t *peer_key,
6626 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006627{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006628 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006629 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006630 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006631
Gilles Peskine449bd832023-01-11 14:50:10 +01006632 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6633 return PSA_ERROR_INVALID_ARGUMENT;
6634 }
Ronald Cron5c522922020-11-14 16:35:34 +01006635 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006636 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6637 if (status != PSA_SUCCESS) {
6638 return status;
6639 }
6640 status = psa_key_agreement_internal(operation, step,
6641 slot,
6642 peer_key, peer_key_length);
6643 if (status != PSA_SUCCESS) {
6644 psa_key_derivation_abort(operation);
6645 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006646 /* If a private key has been added as SECRET, we allow the derived
6647 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006648 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006649 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006650 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006651 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006652
Gilles Peskine449bd832023-01-11 14:50:10 +01006653 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006654
Gilles Peskine449bd832023-01-11 14:50:10 +01006655 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006656}
6657
Gilles Peskine449bd832023-01-11 14:50:10 +01006658psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6659 mbedtls_svc_key_id_t private_key,
6660 const uint8_t *peer_key,
6661 size_t peer_key_length,
6662 uint8_t *output,
6663 size_t output_size,
6664 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006665{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006666 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006667 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006668 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006669
Gilles Peskine449bd832023-01-11 14:50:10 +01006670 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006671 status = PSA_ERROR_INVALID_ARGUMENT;
6672 goto exit;
6673 }
Ronald Cron5c522922020-11-14 16:35:34 +01006674 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006675 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6676 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006677 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006678 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006679
Gilles Peskine3e561302022-04-14 00:17:15 +02006680 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6681 * for the output size. The PSA specification only guarantees that this
6682 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6683 * but it might be nice to allow smaller buffers if the output fits.
6684 * At the time of writing this comment, with only ECDH implemented,
6685 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6686 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6687 * be exact for it as well. */
6688 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006689 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6690 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006691 status = PSA_ERROR_BUFFER_TOO_SMALL;
6692 goto exit;
6693 }
6694
Gilles Peskine449bd832023-01-11 14:50:10 +01006695 status = psa_key_agreement_raw_internal(alg, slot,
6696 peer_key, peer_key_length,
6697 output, output_size,
6698 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006699
6700exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006701 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006702 /* If an error happens and is not handled properly, the output
6703 * may be used as a key to protect sensitive data. Arrange for such
6704 * a key to be random, which is likely to result in decryption or
6705 * verification errors. This is better than filling the buffer with
6706 * some constant data such as zeros, which would result in the data
6707 * being protected with a reproducible, easily knowable key.
6708 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006709 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006710 *output_length = output_size;
6711 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006712
Gilles Peskine449bd832023-01-11 14:50:10 +01006713 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006714
Gilles Peskine449bd832023-01-11 14:50:10 +01006715 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006716}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006717
6718
Gilles Peskine30524eb2020-11-13 17:02:26 +01006719
Gilles Peskine86a440b2018-11-12 18:39:40 +01006720/****************************************************************/
6721/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006722/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006723
Gilles Peskine30524eb2020-11-13 17:02:26 +01006724/** Initialize the PSA random generator.
6725 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006726static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006727{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006728#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006729 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006730#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6731
Gilles Peskine30524eb2020-11-13 17:02:26 +01006732 /* Set default configuration if
6733 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006734 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006735 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006736 }
6737 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006738 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006739 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006740
Gilles Peskine449bd832023-01-11 14:50:10 +01006741 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006742#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6743 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6744 /* The PSA entropy injection feature depends on using NV seed as an entropy
6745 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006746 mbedtls_entropy_add_source(&rng->entropy,
6747 mbedtls_nv_seed_poll, NULL,
6748 MBEDTLS_ENTROPY_BLOCK_SIZE,
6749 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006750#endif
6751
Gilles Peskine449bd832023-01-11 14:50:10 +01006752 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006753#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006754}
6755
6756/** Deinitialize the PSA random generator.
6757 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006758static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006759{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006760#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006761 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006762#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01006763 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
6764 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006765#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006766}
6767
6768/** Seed the PSA random generator.
6769 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006770static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006771{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006772#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6773 /* Do nothing: the external RNG seeds itself. */
6774 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006775 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006776#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006777 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01006778 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
6779 drbg_seed, sizeof(drbg_seed) - 1);
6780 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006781#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006782}
6783
Gilles Peskine449bd832023-01-11 14:50:10 +01006784psa_status_t psa_generate_random(uint8_t *output,
6785 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006786{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006787 GUARD_MODULE_INITIALIZED;
6788
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006789#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6790
6791 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006792 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6793 output, output_size,
6794 &output_length);
6795 if (status != PSA_SUCCESS) {
6796 return status;
6797 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006798 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006799 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006800 if (output_length != output_size) {
6801 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6802 }
6803 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006804
6805#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6806
Gilles Peskine449bd832023-01-11 14:50:10 +01006807 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006808 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006809 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6810 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6811 output_size);
6812 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6813 output, request_size);
6814 if (ret != 0) {
6815 return mbedtls_to_psa_error(ret);
6816 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006817 output_size -= request_size;
6818 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006819 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006820 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006821#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006822}
6823
Gilles Peskine8814fc42020-12-14 15:33:44 +01006824/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006825 *
6826 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6827 * `psa_generate_random(...)`. The state parameter is ignored since the
6828 * PSA API doesn't support passing an explicit state.
6829 *
6830 * In the non-external case, psa_generate_random() calls an
6831 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6832 * and semantics as mbedtls_psa_get_random(). As an optimization,
6833 * instead of doing this back-and-forth between the PSA API and the
6834 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6835 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006836 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006837#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6838int mbedtls_psa_get_random(void *p_rng,
6839 unsigned char *output,
6840 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01006841{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006842 /* This function takes a pointer to the RNG state because that's what
6843 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6844 * its own state internally and doesn't let the caller access that state.
6845 * So we just ignore the state parameter, and in practice we'll pass
6846 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006847 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006848 psa_status_t status = psa_generate_random(output, output_size);
6849 if (status == PSA_SUCCESS) {
6850 return 0;
6851 } else {
6852 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
6853 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01006854}
6855#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6856
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006857#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006858#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006859
Gilles Peskine449bd832023-01-11 14:50:10 +01006860psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
6861 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006862{
Gilles Peskine449bd832023-01-11 14:50:10 +01006863 if (global_data.initialized) {
6864 return PSA_ERROR_NOT_PERMITTED;
6865 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006866
Gilles Peskine449bd832023-01-11 14:50:10 +01006867 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
6868 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
6869 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
6870 return PSA_ERROR_INVALID_ARGUMENT;
6871 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006872
Gilles Peskine449bd832023-01-11 14:50:10 +01006873 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006874}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006875#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006876
Ronald Cron3772afe2021-02-08 16:10:05 +01006877/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006878 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006879 * \param type The key type
6880 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006881 *
6882 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006883 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006884 * \retval #PSA_ERROR_INVALID_ARGUMENT
6885 * The size in bits of the key is not valid.
6886 * \retval #PSA_ERROR_NOT_SUPPORTED
6887 * The type and/or the size in bits of the key or the combination of
6888 * the two is not supported.
6889 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006890static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006891 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006892{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006893 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006894
Gilles Peskine449bd832023-01-11 14:50:10 +01006895 if (key_type_is_raw_bytes(type)) {
6896 status = psa_validate_unstructured_key_bit_size(type, bits);
6897 if (status != PSA_SUCCESS) {
6898 return status;
6899 }
6900 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006901#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6903 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
6904 return PSA_ERROR_NOT_SUPPORTED;
6905 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006906
Ronald Cron01b2aba2020-10-05 09:42:02 +02006907 /* Accept only byte-aligned keys, for the same reasons as
6908 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006909 if (bits % 8 != 0) {
6910 return PSA_ERROR_NOT_SUPPORTED;
6911 }
6912 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006913#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006914
Ronald Cron5c4d3862020-12-07 11:07:24 +01006915#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006916 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01006917 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006918 return PSA_SUCCESS;
6919 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006920#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006921 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006922 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006923 }
6924
Gilles Peskine449bd832023-01-11 14:50:10 +01006925 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006926}
6927
Ronald Cron55ed0592020-10-05 10:30:40 +02006928psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006929 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01006930 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006931{
6932 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006933 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006934
Gilles Peskine449bd832023-01-11 14:50:10 +01006935 if ((attributes->domain_parameters == NULL) &&
6936 (attributes->domain_parameters_size != 0)) {
6937 return PSA_ERROR_INVALID_ARGUMENT;
6938 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02006939
Gilles Peskine449bd832023-01-11 14:50:10 +01006940 if (key_type_is_raw_bytes(type)) {
6941 status = psa_generate_random(key_buffer, key_buffer_size);
6942 if (status != PSA_SUCCESS) {
6943 return status;
6944 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006945
David Brown12ca5032021-02-11 11:02:00 -07006946#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006947 if (type == PSA_KEY_TYPE_DES) {
6948 psa_des_set_key_parity(key_buffer, key_buffer_size);
6949 }
David Brown8107e312021-02-12 08:08:46 -07006950#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01006951 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01006952
Jaeden Amero424fa932021-05-14 08:34:32 +01006953#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
6954 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01006955 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
6956 return mbedtls_psa_rsa_generate_key(attributes,
6957 key_buffer,
6958 key_buffer_size,
6959 key_buffer_length);
6960 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01006961#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
6962 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006963
John Durkop9814fa22020-11-04 12:28:15 -08006964#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006965 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6966 return mbedtls_psa_ecp_generate_key(attributes,
6967 key_buffer,
6968 key_buffer_size,
6969 key_buffer_length);
6970 } else
John Durkop9814fa22020-11-04 12:28:15 -08006971#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006972 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006973 (void) key_buffer_length;
6974 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02006975 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006976
Gilles Peskine449bd832023-01-11 14:50:10 +01006977 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006978}
Darryl Green0c6575a2018-11-07 16:05:30 +00006979
Gilles Peskine449bd832023-01-11 14:50:10 +01006980psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
6981 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006982{
6983 psa_status_t status;
6984 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006985 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006986 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006987
Ronald Cron81709fc2020-11-14 12:10:32 +01006988 *key = MBEDTLS_SVC_KEY_ID_INIT;
6989
Gilles Peskine0f84d622019-09-12 19:03:13 +02006990 /* Reject any attempt to create a zero-length key so that we don't
6991 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006992 if (psa_get_key_bits(attributes) == 0) {
6993 return PSA_ERROR_INVALID_ARGUMENT;
6994 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006995
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02006996 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006997 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
6998 return PSA_ERROR_INVALID_ARGUMENT;
6999 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007000
Gilles Peskine449bd832023-01-11 14:50:10 +01007001 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7002 &slot, &driver);
7003 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007004 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007005 }
Gilles Peskine11792082019-08-06 18:36:36 +02007006
Ronald Cron977c2472020-10-13 08:32:21 +02007007 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307008 * storage ( thus not in the case of generating a key in a secure element
7009 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7010 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007011 if (slot->key.data == NULL) {
7012 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7013 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007014 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007015 attributes->core.type, attributes->core.bits);
7016 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007017 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007018 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007019
7020 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007021 attributes->core.type,
7022 attributes->core.bits);
7023 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007024 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007025 attributes, &key_buffer_size);
7026 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007027 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007028 }
Ronald Cron977c2472020-10-13 08:32:21 +02007029 }
Ronald Cron977c2472020-10-13 08:32:21 +02007030
Gilles Peskine449bd832023-01-11 14:50:10 +01007031 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7032 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007033 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 }
Ronald Cron977c2472020-10-13 08:32:21 +02007035 }
7036
Gilles Peskine449bd832023-01-11 14:50:10 +01007037 status = psa_driver_wrapper_generate_key(attributes,
7038 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007039
Gilles Peskine449bd832023-01-11 14:50:10 +01007040 if (status != PSA_SUCCESS) {
7041 psa_remove_key_data_from_memory(slot);
7042 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007043
Gilles Peskine11792082019-08-06 18:36:36 +02007044exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007045 if (status == PSA_SUCCESS) {
7046 status = psa_finish_key_creation(slot, driver, key);
7047 }
7048 if (status != PSA_SUCCESS) {
7049 psa_fail_key_creation(slot, driver);
7050 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007051
Gilles Peskine449bd832023-01-11 14:50:10 +01007052 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007053}
7054
Gilles Peskinee59236f2018-01-27 23:32:46 +01007055/****************************************************************/
7056/* Module setup */
7057/****************************************************************/
7058
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007059#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007060psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007061 void (* entropy_init)(mbedtls_entropy_context *ctx),
7062 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007063{
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7065 return PSA_ERROR_BAD_STATE;
7066 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007067 global_data.rng.entropy_init = entropy_init;
7068 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007069 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007070}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007071#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007072
Gilles Peskine449bd832023-01-11 14:50:10 +01007073void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007074{
Gilles Peskine449bd832023-01-11 14:50:10 +01007075 psa_wipe_all_key_slots();
7076 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7077 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007078 }
7079 /* Wipe all remaining data, including configuration.
7080 * In particular, this sets all state indicator to the value
7081 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007082 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007083
7084 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007085 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007086}
7087
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007088#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7089/** Recover a transaction that was interrupted by a power failure.
7090 *
7091 * This function is called during initialization, before psa_crypto_init()
7092 * returns. If this function returns a failure status, the initialization
7093 * fails.
7094 */
7095static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007096 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007097{
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007099 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7100 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007101 /* TODO - fall through to the failure case until this
7102 * is implemented.
7103 * https://github.com/ARMmbed/mbed-crypto/issues/218
7104 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007105 default:
7106 /* We found an unsupported transaction in the storage.
7107 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007108 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007109 }
7110}
7111#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7112
Gilles Peskine449bd832023-01-11 14:50:10 +01007113psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007114{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007115 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007116
Gilles Peskinec6b69072018-11-20 21:42:52 +01007117 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007118 if (global_data.initialized != 0) {
7119 return PSA_SUCCESS;
7120 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007121
Gilles Peskine30524eb2020-11-13 17:02:26 +01007122 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007123 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007124 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007125 status = mbedtls_psa_random_seed(&global_data.rng);
7126 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007127 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007128 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007129 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007130
Gilles Peskine449bd832023-01-11 14:50:10 +01007131 status = psa_initialize_key_slots();
7132 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007133 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007134 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007135
Ronald Cron9ba76912021-04-10 16:57:30 +02007136 /* Init drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007137 status = psa_driver_wrapper_init();
7138 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02007139 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007140 }
Gilles Peskined9348f22019-10-01 15:22:29 +02007141
Gilles Peskine4b734222019-07-24 15:56:31 +02007142#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007143 status = psa_crypto_load_transaction();
7144 if (status == PSA_SUCCESS) {
7145 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7146 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007147 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007148 }
7149 status = psa_crypto_stop_transaction();
7150 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007151 /* There's no transaction to complete. It's all good. */
7152 status = PSA_SUCCESS;
7153 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007154#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007155
Gilles Peskinec6b69072018-11-20 21:42:52 +01007156 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007157 global_data.initialized = 1;
7158
7159exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007160 if (status != PSA_SUCCESS) {
7161 mbedtls_psa_crypto_free();
7162 }
7163 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007164}
7165
Neil Armstronga7d08c32022-06-01 18:21:20 +02007166#if defined(MBEDTLS_PSA_BUILTIN_PAKE)
7167psa_status_t psa_pake_setup(
7168 psa_pake_operation_t *operation,
7169 const psa_pake_cipher_suite_t *cipher_suite)
7170{
7171 return psa_driver_wrapper_pake_setup(operation, cipher_suite);
7172}
7173
7174psa_status_t psa_pake_set_password_key(
7175 psa_pake_operation_t *operation,
7176 mbedtls_svc_key_id_t password)
7177{
7178 return psa_driver_wrapper_pake_set_password_key(operation, password);
7179}
7180
7181psa_status_t psa_pake_set_user(
7182 psa_pake_operation_t *operation,
7183 const uint8_t *user_id,
7184 size_t user_id_len)
7185{
7186 return psa_driver_wrapper_pake_set_user(operation, user_id,
7187 user_id_len);
7188}
7189
7190psa_status_t psa_pake_set_peer(
7191 psa_pake_operation_t *operation,
7192 const uint8_t *peer_id,
7193 size_t peer_id_len)
7194{
7195 return psa_driver_wrapper_pake_set_peer(operation, peer_id,
7196 peer_id_len);
7197}
7198
7199psa_status_t psa_pake_set_role(
7200 psa_pake_operation_t *operation,
7201 psa_pake_role_t role)
7202{
7203 return psa_driver_wrapper_pake_set_role(operation, role);
7204}
7205
7206psa_status_t psa_pake_output(
7207 psa_pake_operation_t *operation,
7208 psa_pake_step_t step,
7209 uint8_t *output,
7210 size_t output_size,
7211 size_t *output_length)
7212{
7213 return psa_driver_wrapper_pake_output(operation, step, output,
7214 output_size, output_length);
7215}
7216
7217psa_status_t psa_pake_input(
7218 psa_pake_operation_t *operation,
7219 psa_pake_step_t step,
7220 const uint8_t *input,
7221 size_t input_length)
7222{
7223 return psa_driver_wrapper_pake_input(operation, step, input,
7224 input_length);
7225}
7226
7227psa_status_t psa_pake_get_implicit_key(
7228 psa_pake_operation_t *operation,
7229 psa_key_derivation_operation_t *output)
7230{
7231 return psa_driver_wrapper_pake_get_implicit_key(operation, output);
7232}
7233
7234psa_status_t psa_pake_abort(
7235 psa_pake_operation_t *operation)
7236{
7237 return psa_driver_wrapper_pake_abort(operation);
7238}
7239#endif /* MBEDTLS_PSA_BUILTIN_PAKE */
7240
Gilles Peskinee59236f2018-01-27 23:32:46 +01007241#endif /* MBEDTLS_PSA_CRYPTO_C */