blob: ae35efc49ede13e15449190cd9190ae67e4bcaad [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
Dave Rodgman7ff79652023-11-03 12:04:52 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskinee59236f2018-01-27 23:32:46 +01007 */
8
Gilles Peskinedb09ef62020-06-03 01:43:33 +02009#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010010
11#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030012
John Durkop07cc04a2020-11-16 22:08:34 -080013#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
14#include "check_crypto_config.h"
15#endif
16
Gilles Peskinee59236f2018-01-27 23:32:46 +010017#include "psa/crypto.h"
18
Ronald Crond6d28882020-12-14 14:56:02 +010019#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010020#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010021#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020022#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010023#include "psa_crypto_ecp.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010024#include "psa_crypto_hash.h"
Steven Cooreman32d56942021-03-19 17:39:17 +010025#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010026#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010027#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020028#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020029#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020030#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010031#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010032/* Include internal declarations that are useful for implementing persistently
33 * stored keys. */
34#include "psa_crypto_storage.h"
35
Gilles Peskineb2b64d32020-12-14 16:43:58 +010036#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010037
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010038#include <stdlib.h>
39#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010041
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010042#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010043#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020044#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000045#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020046#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010047#include "mbedtls/blowfish.h"
48#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020049#include "mbedtls/chacha20.h"
50#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010051#include "mbedtls/cipher.h"
52#include "mbedtls/ccm.h"
53#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010054#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020055#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010056#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010057#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010058#include "mbedtls/error.h"
59#include "mbedtls/gcm.h"
60#include "mbedtls/md2.h"
61#include "mbedtls/md4.h"
62#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010063#include "mbedtls/md.h"
64#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010065#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010067#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000068#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010069#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010070#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/sha1.h"
72#include "mbedtls/sha256.h"
73#include "mbedtls/sha512.h"
74#include "mbedtls/xtea.h"
75
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020077
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010078/****************************************************************/
79/* Global data, support functions and library management */
80/****************************************************************/
81
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020083{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020085}
86
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010087/* Values for psa_global_data_t::rng_state */
88#define RNG_NOT_INITIALIZED 0
89#define RNG_INITIALIZED 1
90#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +010091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +010093 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010094 unsigned rng_state : 2;
Gilles Peskineb8006a62021-11-08 22:20:03 +010095 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +010096} psa_global_data_t;
97
98static psa_global_data_t global_data;
99
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100100#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
101mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
102 &global_data.rng.drbg;
103#endif
104
itayzafrir0adf0fc2018-09-06 16:24:41 +0300105#define GUARD_MODULE_INITIALIZED \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 if (global_data.initialized == 0) \
107 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100110{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100111 /* Mbed TLS error codes can combine a high-level error code and a
112 * low-level error code. The low-level error usually reflects the
113 * root cause better, so dispatch on that preferably. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114 int low_level_ret = -(-ret & 0x007f);
115 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100116 case 0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100117 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100118
119 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
120 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
121 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100123 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100125
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200126 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200128
Gilles Peskine9a944802018-06-21 09:35:35 +0200129 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
130 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
131 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
132 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
133 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200135 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200137 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200139
Jaeden Amero93e21112019-02-20 13:57:28 +0000140#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000141 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000142#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
143 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
144#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100145 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100146 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100147 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100149
Jaeden Amero93e21112019-02-20 13:57:28 +0000150#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000151 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000152#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
153 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
154#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100155 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100157 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100158 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100159
160 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100162 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100164 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100166
Gilles Peskine26869f22019-05-06 15:25:00 +0200167 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200169
170 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100171 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200172 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100173 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200174
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100177 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100179 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100181 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100183 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100185 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100187 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100188 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100189 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100191
192 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
196 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100197 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
198 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100199 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100200 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100201 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
202 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100204 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100206#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100207
208 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100212
Gilles Peskinee59236f2018-01-27 23:32:46 +0100213 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
214 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
215 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100217
218 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100219 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100220 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100222 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100224
Gilles Peskine82e57d12020-11-13 21:31:17 +0100225#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100227 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
228 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100229 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100230 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100231 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
232 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100234 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100236#endif
237
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200238 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
239 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
240 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200242
Gilles Peskinea5905292018-02-07 20:59:33 +0100243 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100245 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100247 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100249 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250 return PSA_ERROR_STORAGE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200251 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100253
Gilles Peskinef76aa772018-10-29 19:24:33 +0100254 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100255 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100256 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100258 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100260 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100261 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100262 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100263 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100264 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100266 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100267 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100268 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100270
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
274 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100276 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
279 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100283 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
284 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100285 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100286 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100288 case MBEDTLS_ERR_PK_INVALID_ALG:
289 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
290 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100292 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100296
Gilles Peskineff2d2002019-05-06 15:26:23 +0200297 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100298 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200299 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200301
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200302 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200304
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100307 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100308 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100309 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100311 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100312 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100313 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
314 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100315 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100316 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100317 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100318 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100319 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100320 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100322 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100324 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100326
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200327 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
328 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
329 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200331
Gilles Peskinea5905292018-02-07 20:59:33 +0100332 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100334 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100335 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100336
itayzafrir5c753392018-05-08 11:18:38 +0300337 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300338 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300340 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300342 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300344 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
345 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300347 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100349 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 return PSA_ERROR_INSUFFICIENT_ENTROPY;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300351 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100352 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskine40d81602020-11-25 00:09:47 +0100353
Janos Follatha13b9052019-11-22 12:48:59 +0000354 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300356
Gilles Peskinee59236f2018-01-27 23:32:46 +0100357 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100358 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100359 }
360}
361
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200362
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200363
364
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100365/****************************************************************/
366/* Key management */
367/****************************************************************/
368
John Durkop9814fa22020-11-04 12:28:15 -0800369#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800370 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cron9ecdd972021-11-19 18:02:34 +0100371 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
372 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
373 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
375 size_t bits,
376 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200377{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100379 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100381#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100382 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100384#endif
385#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100386 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100387 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100388#endif
389#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100390 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100392#endif
393#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100394 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100396#endif
397#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100398 case 521:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100399 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100400 case 528:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100401 if (bits_is_sloppy) {
402 return MBEDTLS_ECP_DP_SECP521R1;
403 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100404 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100405#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100406 }
407 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100408
Paul Elliott8ff510a2020-06-02 17:19:28 +0100409 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100410 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100411#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100412 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100413 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100414#endif
415#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100416 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100418#endif
419#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100420 case 512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100421 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100422#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100423 }
424 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100425
Paul Elliott8ff510a2020-06-02 17:19:28 +0100426 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100427 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100428#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100429 case 255:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100431 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432 if (bits_is_sloppy) {
433 return MBEDTLS_ECP_DP_CURVE25519;
434 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100435 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100436#endif
437#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100438 case 448:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100439 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100440#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100441 }
442 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100443
Paul Elliott8ff510a2020-06-02 17:19:28 +0100444 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100445 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100446#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100447 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100448 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100449#endif
450#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100451 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100452 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100453#endif
454#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100455 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100456 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100457#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100458 }
459 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200460 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100461
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100462 (void) bits_is_sloppy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100463 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200464}
John Durkop9814fa22020-11-04 12:28:15 -0800465#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cron9ecdd972021-11-19 18:02:34 +0100466 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
467 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
468 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
469 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100471static psa_status_t validate_unstructured_key_bit_size(psa_key_type_t type,
472 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200473{
474 /* Check that the bit size is acceptable for the key type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100475 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200476 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200477 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200478 case PSA_KEY_TYPE_DERIVE:
479 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100480#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200481 case PSA_KEY_TYPE_AES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100482 if (bits != 128 && bits != 192 && bits != 256) {
483 return PSA_ERROR_INVALID_ARGUMENT;
484 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200485 break;
486#endif
Gilles Peskine8890f642021-09-21 11:59:39 +0200487#if defined(PSA_WANT_KEY_TYPE_ARIA)
488 case PSA_KEY_TYPE_ARIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100489 if (bits != 128 && bits != 192 && bits != 256) {
490 return PSA_ERROR_INVALID_ARGUMENT;
491 }
Gilles Peskine8890f642021-09-21 11:59:39 +0200492 break;
493#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100494#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100496 if (bits != 128 && bits != 192 && bits != 256) {
497 return PSA_ERROR_INVALID_ARGUMENT;
498 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200499 break;
500#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100501#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200502 case PSA_KEY_TYPE_DES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100503 if (bits != 64 && bits != 128 && bits != 192) {
504 return PSA_ERROR_INVALID_ARGUMENT;
505 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200506 break;
507#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100508#if defined(PSA_WANT_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200509 case PSA_KEY_TYPE_ARC4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100510 if (bits < 8 || bits > 2048) {
511 return PSA_ERROR_INVALID_ARGUMENT;
512 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200513 break;
514#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100515#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200516 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100517 if (bits != 256) {
518 return PSA_ERROR_INVALID_ARGUMENT;
519 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200520 break;
521#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200522 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100523 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200524 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100525 if (bits % 8 != 0) {
526 return PSA_ERROR_INVALID_ARGUMENT;
527 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200528
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100529 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200530}
531
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100532/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100533 *
Steven Cooremancd640932021-03-08 12:00:27 +0100534 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
535 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100536 *
537 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100538 * \param[in] key_type The key type of the key to be used with the
539 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100540 *
541 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100542 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100543 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100544 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100545 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100546MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100547 psa_algorithm_t algorithm,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100548 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100549{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100550 if (PSA_ALG_IS_HMAC(algorithm)) {
551 if (key_type == PSA_KEY_TYPE_HMAC) {
552 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100553 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100554 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100556 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
557 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
558 * key. */
559 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
560 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
561 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
562 * the block length (larger than 1) for block ciphers. */
563 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
564 return PSA_SUCCESS;
565 }
566 }
567 }
568
569 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100570}
571
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100572psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
573 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200574{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100575 if (slot->key.data != NULL) {
576 return PSA_ERROR_ALREADY_EXISTS;
577 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200578
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100579 slot->key.data = mbedtls_calloc(1, buffer_length);
580 if (slot->key.data == NULL) {
581 return PSA_ERROR_INSUFFICIENT_MEMORY;
582 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200583
Ronald Cronea0f8a62020-11-25 17:52:23 +0100584 slot->key.bytes = buffer_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100585 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200586}
587
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100588psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
589 const uint8_t *data,
590 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200591{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100592 psa_status_t status = psa_allocate_buffer_to_slot(slot,
593 data_length);
594 if (status != PSA_SUCCESS) {
595 return status;
596 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200597
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100598 memcpy(slot->key.data, data, data_length);
599 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200600}
601
Ronald Crona0fe59f2020-11-29 11:14:53 +0100602psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100603 const psa_key_attributes_t *attributes,
604 const uint8_t *data, size_t data_length,
605 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100606 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100607{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100608 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
609 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100610
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200611 /* zero-length keys are never supported. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100612 if (data_length == 0) {
613 return PSA_ERROR_NOT_SUPPORTED;
614 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200615
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100616 if (key_type_is_raw_bytes(type)) {
617 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200618
Steven Cooreman75b74362020-07-28 14:30:13 +0200619 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100620 if (data_length > SIZE_MAX / 8) {
621 return PSA_ERROR_NOT_SUPPORTED;
622 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200623
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200624 /* Enforce a size limit, and in particular ensure that the bit
625 * size fits in its representation type. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100626 if ((*bits) > PSA_MAX_KEY_BITS) {
627 return PSA_ERROR_NOT_SUPPORTED;
628 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200629
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100630 status = validate_unstructured_key_bit_size(type, *bits);
631 if (status != PSA_SUCCESS) {
632 return status;
633 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200634
Ronald Crondd04d422020-11-28 15:14:42 +0100635 /* Copy the key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100636 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100637 *key_buffer_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100638 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200639
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100640 return PSA_SUCCESS;
641 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800642#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100643 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
644 if (PSA_KEY_TYPE_IS_ECC(type)) {
645 return mbedtls_psa_ecp_import_key(attributes,
646 data, data_length,
647 key_buffer, key_buffer_size,
648 key_buffer_length,
649 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100650 }
John Durkop9814fa22020-11-04 12:28:15 -0800651#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
652 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700653#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100654 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
655 if (PSA_KEY_TYPE_IS_RSA(type)) {
656 return mbedtls_psa_rsa_import_key(attributes,
657 data, data_length,
658 key_buffer, key_buffer_size,
659 key_buffer_length,
660 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200661 }
John Durkop9814fa22020-11-04 12:28:15 -0800662#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
663 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100664 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100666 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100667}
668
Gilles Peskinef603c712019-01-19 13:40:11 +0100669/** Calculate the intersection of two algorithm usage policies.
670 *
671 * Return 0 (which allows no operation) on incompatibility.
672 */
673static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100674 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100675 psa_algorithm_t alg1,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100676 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100677{
Gilles Peskine549ea862019-05-22 11:45:59 +0200678 /* Common case: both sides actually specify the same policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100679 if (alg1 == alg2) {
680 return alg1;
681 }
Steven Cooreman03488022021-02-18 12:07:20 +0100682 /* If the policies are from the same hash-and-sign family, check
683 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100684 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
685 PSA_ALG_IS_SIGN_HASH(alg2) &&
686 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
687 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
688 return alg2;
689 }
690 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
691 return alg1;
692 }
Steven Cooreman03488022021-02-18 12:07:20 +0100693 }
694 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100695 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100696 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100697 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
698 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
699 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
700 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
701 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100702 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100703
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100704 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100705 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
706 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
707 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
708 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100709 }
710 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100711 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
712 (alg1_len <= alg2_len)) {
713 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100714 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100715 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
716 (alg2_len <= alg1_len)) {
717 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100718 }
719 }
720 /* If the policies are from the same MAC family, check whether one
721 * of them is a minimum-MAC-length policy. Calculate the most
722 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100723 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
724 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
725 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100726 /* Validate the combination of key type and algorithm. Since the base
727 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100728 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
729 return 0;
730 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100731
Steven Cooreman31a876d2021-03-03 20:47:40 +0100732 /* Get the (exact or at-least) output lengths for both sides of the
733 * requested intersection. None of the currently supported algorithms
734 * have an output length dependent on the actual key size, so setting it
735 * to a bogus value of 0 is currently OK.
736 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100737 * Note that for at-least-this-length wildcard algorithms, the output
738 * length is set to the shortest allowed length, which allows us to
739 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100740 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
741 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100742 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100743
Steven Cooremana1d83222021-02-25 10:20:29 +0100744 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100745 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
746 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
747 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100748 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100749
750 /* If only one is an at-least-this-length policy, the intersection would
751 * be the other (fixed-length) policy as long as said fixed length is
752 * equal to or larger than the shortest allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100753 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
754 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100755 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100756 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
757 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100758 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100759
Steven Cooremancd640932021-03-08 12:00:27 +0100760 /* If none of them are wildcards, check whether they define the same tag
761 * length. This is still possible here when one is default-length and
762 * the other specific-length. Ensure to always return the
763 * specific-length version for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100764 if (alg1_len == alg2_len) {
765 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
766 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100767 }
768 /* If the policies are incompatible, allow nothing. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100769 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100770}
771
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100772static int psa_key_algorithm_permits(psa_key_type_t key_type,
773 psa_algorithm_t policy_alg,
774 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200775{
Gilles Peskine549ea862019-05-22 11:45:59 +0200776 /* Common case: the policy only allows requested_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100777 if (requested_alg == policy_alg) {
778 return 1;
779 }
Steven Cooreman03488022021-02-18 12:07:20 +0100780 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
781 * and requested_alg is the same hash-and-sign family with any hash,
782 * then requested_alg is compliant with policy_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100783 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
784 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
785 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
786 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100787 }
788 /* If policy_alg is a wildcard AEAD algorithm of the same base as
789 * the requested algorithm, check the requested tag length to be
790 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100791 if (PSA_ALG_IS_AEAD(policy_alg) &&
792 PSA_ALG_IS_AEAD(requested_alg) &&
793 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
794 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
795 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
796 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
797 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100798 }
Steven Cooremancd640932021-03-08 12:00:27 +0100799 /* If policy_alg is a MAC algorithm of the same base as the requested
800 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100801 if (PSA_ALG_IS_MAC(policy_alg) &&
802 PSA_ALG_IS_MAC(requested_alg) &&
803 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
804 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100805 /* Validate the combination of key type and algorithm. Since the policy
806 * and requested algorithms are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100807 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
808 return 0;
809 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100810
Steven Cooreman31a876d2021-03-03 20:47:40 +0100811 /* Get both the requested output length for the algorithm which is to be
812 * verified, and the default output length for the base algorithm.
813 * Note that none of the currently supported algorithms have an output
814 * length dependent on actual key size, so setting it to a bogus value
815 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100816 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100817 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100818 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100819 key_type, 0,
820 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100821
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100822 /* If the policy is default-length, only allow an algorithm with
823 * a declared exact-length matching the default. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100824 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
825 return requested_output_length == default_output_length;
826 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100827
828 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100829 * length exactly matches the default length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100830 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
831 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
832 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100833 }
834
Steven Cooremancd640932021-03-08 12:00:27 +0100835 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
836 * check for the requested MAC length to be equal to or longer than the
837 * minimum allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100838 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
839 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
840 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100841 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200842 }
Steven Cooremance48e852020-10-05 16:02:45 +0200843 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200844 * a key derivation with that key agreement should also be allowed. This
845 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100846 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
847 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
848 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
849 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200850 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100851 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100852 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200853}
854
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100855/** Test whether a policy permits an algorithm.
856 *
857 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100858 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100859 * \note This function requires providing the key type for which the policy is
860 * being validated, since some algorithm policy definitions (e.g. MAC)
861 * have different properties depending on what kind of cipher it is
862 * combined with.
863 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100864 * \retval PSA_SUCCESS When \p alg is a specific algorithm
865 * allowed by the \p policy.
866 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
867 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
868 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100869 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100870static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
871 psa_key_type_t key_type,
872 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100873{
Steven Cooremand788fab2021-02-25 11:29:17 +0100874 /* '0' is not a valid algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100875 if (alg == 0) {
876 return PSA_ERROR_INVALID_ARGUMENT;
877 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100878
Steven Cooreman7e39f052021-02-23 14:18:32 +0100879 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100880 if (PSA_ALG_IS_WILDCARD(alg)) {
881 return PSA_ERROR_INVALID_ARGUMENT;
882 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100884 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
885 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
886 return PSA_SUCCESS;
887 } else {
888 return PSA_ERROR_NOT_PERMITTED;
889 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100890}
891
Gilles Peskinef603c712019-01-19 13:40:11 +0100892/** Restrict a key policy based on a constraint.
893 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100894 * \note This function requires providing the key type for which the policy is
895 * being restricted, since some algorithm policy definitions (e.g. MAC)
896 * have different properties depending on what kind of cipher it is
897 * combined with.
898 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100899 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100900 * \param[in,out] policy The policy to restrict.
901 * \param[in] constraint The policy constraint to apply.
902 *
903 * \retval #PSA_SUCCESS
904 * \c *policy contains the intersection of the original value of
905 * \c *policy and \c *constraint.
906 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100907 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100908 * \c *policy is unchanged.
909 */
910static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100911 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100912 psa_key_policy_t *policy,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100913 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100914{
915 psa_algorithm_t intersection_alg =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100916 psa_key_policy_algorithm_intersection(key_type, policy->alg,
917 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200918 psa_algorithm_t intersection_alg2 =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100919 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
920 constraint->alg2);
921 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
922 return PSA_ERROR_INVALID_ARGUMENT;
923 }
924 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
925 return PSA_ERROR_INVALID_ARGUMENT;
926 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100927 policy->usage &= constraint->usage;
928 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200929 policy->alg2 = intersection_alg2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100930 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100931}
932
Ronald Cron5c522922020-11-14 16:35:34 +0100933/** Get the description of a key given its identifier and policy constraints
934 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200935 *
Ronald Cron5c522922020-11-14 16:35:34 +0100936 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100937 * nonzero, the key must allow operations with this algorithm. If \p alg is
938 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100939 *
Ronald Cron5c522922020-11-14 16:35:34 +0100940 * In case of a persistent key, the function loads the description of the key
941 * into a key slot if not already done.
942 *
943 * On success, the returned key slot is locked. It is the responsibility of
944 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200945 */
Ronald Cron5c522922020-11-14 16:35:34 +0100946static psa_status_t psa_get_and_lock_key_slot_with_policy(
947 mbedtls_svc_key_id_t key,
948 psa_key_slot_t **p_slot,
949 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100950 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100951{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200952 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
953 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100954
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100955 status = psa_get_and_lock_key_slot(key, p_slot);
956 if (status != PSA_SUCCESS) {
957 return status;
958 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200959 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100960
961 /* Enforce that usage policy for the key slot contains all the flags
962 * required by the usage parameter. There is one exception: public
963 * keys can always be exported, so we treat public key objects as
964 * if they had the export flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100965 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100966 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100967 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200968
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100969 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100970 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200971 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100972 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100973
Shaun Case0e7791f2021-12-20 21:14:10 -0800974 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100975 if (alg != 0) {
976 status = psa_key_policy_permits(&slot->attr.policy,
977 slot->attr.type,
978 alg);
979 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100980 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100981 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100982 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100983
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100984 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200985
986error:
987 *p_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100988 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200989
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100990 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100991}
Darryl Green940d72c2018-07-13 13:18:51 +0100992
Ronald Cron5c522922020-11-14 16:35:34 +0100993/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200994 *
995 * A transparent key is a key for which the key material is directly
Ronald Cron8a0466a2021-08-24 15:39:44 +0200996 * available, as opposed to a key in a secure element and/or to be used
997 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200998 *
Ronald Cron8a0466a2021-08-24 15:39:44 +0200999 * This is a temporary function that may be used instead of
1000 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1001 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001002 *
Ronald Cron5c522922020-11-14 16:35:34 +01001003 * On success, the returned key slot is locked. It is the responsibility of the
1004 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001005 */
Ronald Cron5c522922020-11-14 16:35:34 +01001006static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1007 mbedtls_svc_key_id_t key,
1008 psa_key_slot_t **p_slot,
1009 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001010 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001011{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001012 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1013 usage, alg);
1014 if (status != PSA_SUCCESS) {
1015 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001016 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001017
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001018 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1019 psa_unlock_key_slot(*p_slot);
1020 *p_slot = NULL;
1021 return PSA_ERROR_NOT_SUPPORTED;
1022 }
1023
1024 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001025}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001026
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001027psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001028{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001029 /* Data pointer will always be either a valid pointer or NULL in an
1030 * initialized slot, so we can just free it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001031 if (slot->key.data != NULL) {
1032 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1033 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001034
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001035 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001036 slot->key.data = NULL;
1037 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001038
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001039 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001040}
1041
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001042/** Completely wipe a slot in memory, including its policy.
1043 * Persistent storage is not affected. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001044psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001045{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001046 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001047
1048 /*
1049 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001050 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001051 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1052 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001053 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001054 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001055 if (slot->lock_count != 1) {
Ronald Cronddd3d052020-10-30 14:07:07 +01001056#ifdef MBEDTLS_CHECK_PARAMS
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001057 MBEDTLS_PARAM_FAILED(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001058#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001059 status = PSA_ERROR_CORRUPTION_DETECTED;
1060 }
1061
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001062 /* Multipart operations may still be using the key. This is safe
1063 * because all multipart operation objects are independent from
1064 * the key slot: if they need to access the key after the setup
1065 * phase, they have a copy of the key. Note that this means that
1066 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001067 /* At this point, key material and other type-specific content has
1068 * been wiped. Clear remaining metadata. We can call memset and not
1069 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001070 memset(slot, 0, sizeof(*slot));
1071 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001072}
1073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001074psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001075{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001076 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001077 psa_status_t status; /* status of the last operation */
1078 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001079#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1080 psa_se_drv_table_entry_t *driver;
1081#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001083 if (mbedtls_svc_key_id_is_null(key)) {
1084 return PSA_SUCCESS;
1085 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001086
Ronald Cronf2911112020-10-29 17:51:10 +01001087 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001088 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001089 * key, this will load the key description from persistent memory if not
1090 * done yet. We cannot avoid this loading as without it we don't know if
1091 * the key is operated by an SE or not and this information is needed by
1092 * the current implementation.
1093 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001094 status = psa_get_and_lock_key_slot(key, &slot);
1095 if (status != PSA_SUCCESS) {
1096 return status;
1097 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001098
Ronald Cronf2911112020-10-29 17:51:10 +01001099 /*
1100 * If the key slot containing the key description is under access by the
1101 * library (apart from the present access), the key cannot be destroyed
1102 * yet. For the time being, just return in error. Eventually (to be
1103 * implemented), the key should be destroyed when all accesses have
1104 * stopped.
1105 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001106 if (slot->lock_count > 1) {
1107 psa_unlock_key_slot(slot);
1108 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001109 }
1110
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001111 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine251c7742021-04-21 22:32:05 +02001112 /* Refuse the destruction of a read-only key (which may or may not work
1113 * if we attempt it, depending on whether the key is merely read-only
1114 * by policy or actually physically read-only).
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001115 * Just do the best we can, which is to wipe the copy in memory
1116 * (done in this function's cleanup code). */
1117 overall_status = PSA_ERROR_NOT_PERMITTED;
1118 goto exit;
Gilles Peskine251c7742021-04-21 22:32:05 +02001119 }
1120
Gilles Peskine354f7672019-07-12 23:46:38 +02001121#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001122 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1123 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001124 /* For a key in a secure element, we need to do three things:
1125 * remove the key file in internal storage, destroy the
1126 * key inside the secure element, and update the driver's
1127 * persistent data. Start a transaction that will encompass these
1128 * three actions. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001129 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001130 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001131 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001132 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001133 status = psa_crypto_save_transaction();
1134 if (status != PSA_SUCCESS) {
1135 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001136 /* We should still try to destroy the key in the secure
1137 * element and the key metadata in storage. This is especially
1138 * important if the error is that the storage is full.
1139 * But how to do it exactly without risking an inconsistent
1140 * state after a reset?
1141 * https://github.com/ARMmbed/mbed-crypto/issues/215
1142 */
1143 overall_status = status;
1144 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001145 }
1146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001147 status = psa_destroy_se_key(driver,
1148 psa_key_slot_get_slot_number(slot));
1149 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001150 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001151 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001152 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001153#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1154
Darryl Greend49a4992018-06-18 17:27:26 +01001155#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001156 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1157 status = psa_destroy_persistent_key(slot->attr.id);
1158 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001159 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001160 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001161
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001162 /* TODO: other slots may have a copy of the same key. We should
1163 * invalidate them.
1164 * https://github.com/ARMmbed/mbed-crypto/issues/214
1165 */
Darryl Greend49a4992018-06-18 17:27:26 +01001166 }
1167#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001168
Gilles Peskinefc762652019-07-22 19:30:34 +02001169#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001170 if (driver != NULL) {
1171 status = psa_save_se_persistent_data(driver);
1172 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001173 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001174 }
1175 status = psa_crypto_stop_transaction();
1176 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001177 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001178 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001179 }
1180#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1181
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001182exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001183 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001184 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001185 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001186 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001187 }
1188 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001189}
1190
John Durkop07cc04a2020-11-16 22:08:34 -08001191#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001192 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001193static psa_status_t psa_get_rsa_public_exponent(
1194 const mbedtls_rsa_context *rsa,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001195 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001196{
1197 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001198 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001199 uint8_t *buffer = NULL;
1200 size_t buflen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001201 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001203 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1204 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001205 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001206 }
1207 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001208 /* It's the default value, which is reported as an empty string,
1209 * so there's nothing to do. */
1210 goto exit;
1211 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001212
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001213 buflen = mbedtls_mpi_size(&mpi);
1214 buffer = mbedtls_calloc(1, buflen);
1215 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001216 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1217 goto exit;
1218 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001219 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1220 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001221 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001222 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001223 attributes->domain_parameters = buffer;
1224 attributes->domain_parameters_size = buflen;
1225
1226exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001227 mbedtls_mpi_free(&mpi);
1228 if (ret != 0) {
1229 mbedtls_free(buffer);
1230 }
1231 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001232}
John Durkop07cc04a2020-11-16 22:08:34 -08001233#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001234 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001235
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001236/** Retrieve all the publicly-accessible attributes of a key.
1237 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001238psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1239 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001240{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001241 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001242 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001243 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001244
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001245 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001246
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001247 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1248 if (status != PSA_SUCCESS) {
1249 return status;
1250 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001251
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001252 attributes->core = slot->attr;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001253 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1254 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001255
1256#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001257 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1258 psa_set_key_slot_number(attributes,
1259 psa_key_slot_get_slot_number(slot));
1260 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001261#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001262
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001263 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001264#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001265 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001266 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001267 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001268 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001269 * is not yet implemented.
1270 * https://github.com/ARMmbed/mbed-crypto/issues/216
1271 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001272 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001273 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001274
Ronald Cron90857082020-11-25 14:58:33 +01001275 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001276 slot->attr.type,
1277 slot->key.data,
1278 slot->key.bytes,
1279 &rsa);
1280 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001281 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001282 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001283
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001284 status = psa_get_rsa_public_exponent(rsa,
1285 attributes);
1286 mbedtls_rsa_free(rsa);
1287 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001288 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001289 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001290#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001291 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001292 default:
1293 /* Nothing else to do. */
1294 break;
1295 }
1296
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001297 if (status != PSA_SUCCESS) {
1298 psa_reset_key_attributes(attributes);
1299 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001301 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001302
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001303 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001304}
1305
Gilles Peskinec8000c02019-08-02 20:15:51 +02001306#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1307psa_status_t psa_get_key_slot_number(
1308 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001309 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001310{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001311 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001312 *slot_number = attributes->slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001313 return PSA_SUCCESS;
1314 } else {
1315 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001316 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001317}
1318#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001320static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1321 size_t key_buffer_size,
1322 uint8_t *data,
1323 size_t data_size,
1324 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001325{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001326 if (key_buffer_size > data_size) {
1327 return PSA_ERROR_BUFFER_TOO_SMALL;
1328 }
1329 memcpy(data, key_buffer, key_buffer_size);
1330 memset(data + key_buffer_size, 0,
1331 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001332 *data_length = key_buffer_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001333 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001334}
1335
Ronald Cron7285cda2020-11-26 14:46:37 +01001336psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001337 const psa_key_attributes_t *attributes,
1338 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001339 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001340{
Ronald Crond18b5f82020-11-25 16:46:05 +01001341 psa_key_type_t type = attributes->core.type;
1342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001343 if (key_type_is_raw_bytes(type) ||
1344 PSA_KEY_TYPE_IS_RSA(type) ||
1345 PSA_KEY_TYPE_IS_ECC(type)) {
1346 return psa_export_key_buffer_internal(
1347 key_buffer, key_buffer_size,
1348 data, data_size, data_length);
1349 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001350 /* This shouldn't happen in the reference implementation, but
1351 it is valid for a special-purpose implementation to omit
1352 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001353 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001354 }
1355}
1356
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001357psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1358 uint8_t *data,
1359 size_t data_size,
1360 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001361{
1362 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1363 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1364 psa_key_slot_t *slot;
1365
Ronald Crone907e552021-01-18 13:22:38 +01001366 /* Reject a zero-length output buffer now, since this can never be a
1367 * valid key representation. This way we know that data must be a valid
1368 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001369 if (data_size == 0) {
1370 return PSA_ERROR_BUFFER_TOO_SMALL;
1371 }
Ronald Crone907e552021-01-18 13:22:38 +01001372
Ronald Cron9486f9d2020-11-26 13:36:23 +01001373 /* Set the key to empty now, so that even when there are errors, we always
1374 * set data_length to a value between 0 and data_size. On error, setting
1375 * the key to empty is a good choice because an empty key representation is
1376 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001377 *data_length = 0;
1378
Ronald Cron9486f9d2020-11-26 13:36:23 +01001379 /* Export requires the EXPORT flag. There is an exception for public keys,
1380 * which don't require any flag, but
1381 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1382 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001383 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1384 PSA_KEY_USAGE_EXPORT, 0);
1385 if (status != PSA_SUCCESS) {
1386 return status;
1387 }
mohammad160306e79202018-03-28 13:17:44 +03001388
Ronald Crond18b5f82020-11-25 16:46:05 +01001389 psa_key_attributes_t attributes = {
1390 .core = slot->attr
1391 };
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001392 status = psa_driver_wrapper_export_key(&attributes,
1393 slot->key.data, slot->key.bytes,
1394 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001396 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001398 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001399}
1400
Ronald Cron7285cda2020-11-26 14:46:37 +01001401psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001402 const psa_key_attributes_t *attributes,
1403 const uint8_t *key_buffer,
1404 size_t key_buffer_size,
1405 uint8_t *data,
1406 size_t data_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001407 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001408{
Ronald Crond18b5f82020-11-25 16:46:05 +01001409 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001411 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1412 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001413 /* Exporting public -> public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001414 return psa_export_key_buffer_internal(
1415 key_buffer, key_buffer_size,
1416 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001417 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001418
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001419 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001420#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001421 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1422 return mbedtls_psa_rsa_export_public_key(attributes,
1423 key_buffer,
1424 key_buffer_size,
1425 data,
1426 data_size,
1427 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001428#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001429 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001430 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001431#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1432 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001433 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001434#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001435 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1436 return mbedtls_psa_ecp_export_public_key(attributes,
1437 key_buffer,
1438 key_buffer_size,
1439 data,
1440 data_size,
1441 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001442#else
1443 /* We don't know how to convert a private ECC key to public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001444 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001445#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1446 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001447 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001448 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001449 /* This shouldn't happen in the reference implementation, but
1450 it is valid for a special-purpose implementation to omit
1451 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001452 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001453 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001454}
Ronald Crond18b5f82020-11-25 16:46:05 +01001455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001456psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1457 uint8_t *data,
1458 size_t data_size,
1459 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001460{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001461 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001462 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001463 psa_key_attributes_t attributes;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001464 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001465
Ronald Crone907e552021-01-18 13:22:38 +01001466 /* Reject a zero-length output buffer now, since this can never be a
1467 * valid key representation. This way we know that data must be a valid
1468 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001469 if (data_size == 0) {
1470 return PSA_ERROR_BUFFER_TOO_SMALL;
1471 }
Ronald Crone907e552021-01-18 13:22:38 +01001472
Darryl Greendd8fb772018-11-07 16:00:44 +00001473 /* Set the key to empty now, so that even when there are errors, we always
1474 * set data_length to a value between 0 and data_size. On error, setting
1475 * the key to empty is a good choice because an empty key representation is
1476 * unlikely to be accepted anywhere. */
1477 *data_length = 0;
1478
1479 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001480 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1481 if (status != PSA_SUCCESS) {
1482 return status;
1483 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001485 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1486 status = PSA_ERROR_INVALID_ARGUMENT;
1487 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001488 }
1489
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001490 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001491 .core = slot->attr
1492 };
Ronald Cron67227982020-11-26 15:16:05 +01001493 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001494 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001495 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001496
1497exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001498 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001499
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001500 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001501}
1502
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001503MBEDTLS_STATIC_ASSERT((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1504 "One or more key attribute flag is listed as both external-only and dual-use")
1505MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1506 "One or more key attribute flag is listed as both internal-only and dual-use")
1507MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1508 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001509
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001510/** Validate that a key policy is internally well-formed.
1511 *
1512 * This function only rejects invalid policies. It does not validate the
1513 * consistency of the policy with respect to other attributes of the key
1514 * such as the key type.
1515 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001516static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001517{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001518 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1519 PSA_KEY_USAGE_COPY |
1520 PSA_KEY_USAGE_ENCRYPT |
1521 PSA_KEY_USAGE_DECRYPT |
1522 PSA_KEY_USAGE_SIGN_MESSAGE |
1523 PSA_KEY_USAGE_VERIFY_MESSAGE |
1524 PSA_KEY_USAGE_SIGN_HASH |
1525 PSA_KEY_USAGE_VERIFY_HASH |
1526 PSA_KEY_USAGE_DERIVE)) != 0) {
1527 return PSA_ERROR_INVALID_ARGUMENT;
1528 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001529
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001530 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001531}
1532
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001533/** Validate the internal consistency of key attributes.
1534 *
1535 * This function only rejects invalid attribute values. If does not
1536 * validate the consistency of the attributes with any key data that may
1537 * be involved in the creation of the key.
1538 *
1539 * Call this function early in the key creation process.
1540 *
1541 * \param[in] attributes Key attributes for the new key.
1542 * \param[out] p_drv On any return, the driver for the key, if any.
1543 * NULL for a transparent key.
1544 *
1545 */
1546static psa_status_t psa_validate_key_attributes(
1547 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001548 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001549{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001550 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001551 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1552 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001553
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001554 status = psa_validate_key_location(lifetime, p_drv);
1555 if (status != PSA_SUCCESS) {
1556 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001557 }
1558
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001559 status = psa_validate_key_persistence(lifetime);
1560 if (status != PSA_SUCCESS) {
1561 return status;
1562 }
1563
1564 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1565 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1566 return PSA_ERROR_INVALID_ARGUMENT;
1567 }
1568 } else {
1569 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1570 return PSA_ERROR_INVALID_ARGUMENT;
1571 }
1572 }
1573
1574 status = psa_validate_key_policy(&attributes->core.policy);
1575 if (status != PSA_SUCCESS) {
1576 return status;
1577 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001578
1579 /* Refuse to create overly large keys.
1580 * Note that this doesn't trigger on import if the attributes don't
1581 * explicitly specify a size (so psa_get_key_bits returns 0), so
1582 * psa_import_key() needs its own checks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001583 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1584 return PSA_ERROR_NOT_SUPPORTED;
1585 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001586
Gilles Peskine91e8c332019-08-02 19:19:39 +02001587 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001588 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1589 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1590 return PSA_ERROR_INVALID_ARGUMENT;
1591 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001592
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001593 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001594}
1595
Gilles Peskine4747d192019-04-17 15:05:45 +02001596/** Prepare a key slot to receive key material.
1597 *
1598 * This function allocates a key slot and sets its metadata.
1599 *
1600 * If this function fails, call psa_fail_key_creation().
1601 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001602 * This function is intended to be used as follows:
1603 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001604 * it with the specified attributes, and in case of a volatile key assign it
1605 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001606 * -# Populate the slot with the key material.
1607 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1608 * In case of failure at any step, stop the sequence and call
1609 * psa_fail_key_creation().
1610 *
Ronald Cron5c522922020-11-14 16:35:34 +01001611 * On success, the key slot is locked. It is the responsibility of the caller
1612 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001613 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001614 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001615 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001616 * \param[out] p_slot On success, a pointer to the prepared slot.
1617 * \param[out] p_drv On any return, the driver for the key, if any.
1618 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001619 *
1620 * \retval #PSA_SUCCESS
1621 * The key slot is ready to receive key material.
1622 * \return If this function fails, the key slot is an invalid state.
1623 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001624 */
1625static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001626 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001627 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001628 psa_key_slot_t **p_slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001629 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001630{
1631 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001632 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001633 psa_key_slot_t *slot;
1634
Gilles Peskinedf179142019-07-15 22:02:14 +02001635 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001636 *p_drv = NULL;
1637
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001638 status = psa_validate_key_attributes(attributes, p_drv);
1639 if (status != PSA_SUCCESS) {
1640 return status;
1641 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001643 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1644 if (status != PSA_SUCCESS) {
1645 return status;
1646 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001647 slot = *p_slot;
1648
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001649 /* We're storing the declared bit-size of the key. It's up to each
1650 * creation mechanism to verify that this information is correct.
1651 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001652 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001653 * is optional (import, copy). In case of a volatile key, assign it the
1654 * volatile key identifier associated to the slot returned to contain its
1655 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001656
1657 slot->attr = attributes->core;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001658 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001659#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1660 slot->attr.id = volatile_key_id;
1661#else
1662 slot->attr.id.key_id = volatile_key_id;
1663#endif
1664 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001665
Gilles Peskine91e8c332019-08-02 19:19:39 +02001666 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001667 * external-only flags, query `attributes`. Thanks to the check
1668 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001669 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001670 * may have set. */
1671 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001672
Gilles Peskinecbaff462019-07-12 23:46:04 +02001673#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001674 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001675 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001676 * create the key file in internal storage, create the
1677 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001678 * persistent data. This is done by starting a transaction that will
1679 * encompass these three actions.
1680 * For registering a volatile key, we just need to find an appropriate
1681 * slot number inside the SE. Since the key is designated volatile, creating
1682 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001683 /* The first thing to do is to find a slot number for the new key.
1684 * We save the slot number in persistent storage as part of the
1685 * transaction data. It will be needed to recover if the power
1686 * fails during the key creation process, to clean up on the secure
1687 * element side after restarting. Obtaining a slot number from the
1688 * secure element driver updates its persistent state, but we do not yet
1689 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001690 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001691 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001692 psa_key_slot_number_t slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001693 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1694 &slot_number);
1695 if (status != PSA_SUCCESS) {
1696 return status;
1697 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001699 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1700 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001701 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001702 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001703 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001704 status = psa_crypto_save_transaction();
1705 if (status != PSA_SUCCESS) {
1706 (void) psa_crypto_stop_transaction();
1707 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001708 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001709 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001710
1711 status = psa_copy_key_material_into_slot(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001712 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Ryan Everett68a85e22023-11-16 16:44:13 +00001713 if (status != PSA_SUCCESS) {
1714 return status;
1715 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001716 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001717
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001718 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001719 /* Key registration only makes sense with a secure element. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001720 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001721 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001722#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1723
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001724 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001725}
Gilles Peskine4747d192019-04-17 15:05:45 +02001726
1727/** Finalize the creation of a key once its key material has been set.
1728 *
1729 * This entails writing the key to persistent storage.
1730 *
1731 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001732 * See the documentation of psa_start_key_creation() for the intended use
1733 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001734 *
Ronald Cron5c522922020-11-14 16:35:34 +01001735 * If the finalization succeeds, the function unlocks the key slot (it was
1736 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1737 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001738 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001739 * \param[in,out] slot Pointer to the slot with key material.
1740 * \param[in] driver The secure element driver for the key,
1741 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001742 * \param[out] key On success, identifier of the key. Note that the
1743 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001744 *
1745 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001746 * The key was successfully created.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001747 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1748 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1749 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1750 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1751 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1752 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001753 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001754 * \return If this function fails, the key slot is an invalid state.
1755 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001756 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001757static psa_status_t psa_finish_key_creation(
1758 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001759 psa_se_drv_table_entry_t *driver,
1760 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001761{
1762 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001763 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001764 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001765
1766#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001767 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001768#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001769 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001770 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001771 psa_key_slot_number_t slot_number =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001772 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001773
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001774 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1775 sizeof(data.slot_number),
1776 "Slot number size does not match psa_se_key_data_storage_t");
1777
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001778 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1779 status = psa_save_persistent_key(&slot->attr,
1780 (uint8_t *) &data,
1781 sizeof(data));
1782 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001783#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1784 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001785 /* Key material is saved in export representation in the slot, so
1786 * just pass the slot buffer for storage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001787 status = psa_save_persistent_key(&slot->attr,
1788 slot->key.data,
1789 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001790 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001791 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001792#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1793
Gilles Peskinecbaff462019-07-12 23:46:04 +02001794#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001795 /* Finish the transaction for a key creation. This does not
1796 * happen when registering an existing key. Detect this case
1797 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001798 * creation of a persistent key in a secure element requires a transaction,
1799 * but registration or volatile key creation doesn't use one). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001800 if (driver != NULL &&
1801 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1802 status = psa_save_se_persistent_data(driver);
1803 if (status != PSA_SUCCESS) {
1804 psa_destroy_persistent_key(slot->attr.id);
1805 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001806 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001807 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001808 }
1809#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1810
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001811 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001812 *key = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001813 status = psa_unlock_key_slot(slot);
1814 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001815 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001816 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001817 }
Ronald Cron50972942020-11-14 11:28:25 +01001818
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001819 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001820}
1821
1822/** Abort the creation of a key.
1823 *
1824 * You may call this function after calling psa_start_key_creation(),
1825 * or after psa_finish_key_creation() fails. In other circumstances, this
1826 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001827 * See the documentation of psa_start_key_creation() for the intended use
1828 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001829 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001830 * \param[in,out] slot Pointer to the slot with key material.
1831 * \param[in] driver The secure element driver for the key,
1832 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001833 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001834static void psa_fail_key_creation(psa_key_slot_t *slot,
1835 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001836{
Gilles Peskine011e4282019-06-26 18:34:38 +02001837 (void) driver;
1838
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001839 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001840 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001841 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001842
1843#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001844 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001845 * element, and the failure happened later (when saving metadata
1846 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001847 * element.
1848 * https://github.com/ARMmbed/mbed-crypto/issues/217
1849 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001850
Gilles Peskined7729582019-08-05 15:55:54 +02001851 /* Abort the ongoing transaction if any (there may not be one if
1852 * the creation process failed before starting one, or if the
1853 * key creation is a registration of a key in a secure element).
1854 * Earlier functions must already have done what it takes to undo any
1855 * partial creation. All that's left is to update the transaction data
1856 * itself. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001857 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001858#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1859
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001860 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001861}
1862
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001863/** Validate optional attributes during key creation.
1864 *
1865 * Some key attributes are optional during key creation. If they are
1866 * specified in the attributes structure, check that they are consistent
1867 * with the data in the slot.
1868 *
1869 * This function should be called near the end of key creation, after
1870 * the slot in memory is fully populated but before saving persistent data.
1871 */
1872static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001873 const psa_key_slot_t *slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001874 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001875{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001876 if (attributes->core.type != 0) {
1877 if (attributes->core.type != slot->attr.type) {
1878 return PSA_ERROR_INVALID_ARGUMENT;
1879 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001880 }
1881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001882 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001883#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001884 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1885 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001886 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001887 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001888 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001889
Ronald Cron90857082020-11-25 14:58:33 +01001890 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001891 slot->attr.type,
1892 slot->key.data,
1893 slot->key.bytes,
1894 &rsa);
1895 if (status != PSA_SUCCESS) {
1896 return status;
1897 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001898
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001899 mbedtls_mpi_init(&actual);
1900 mbedtls_mpi_init(&required);
1901 ret = mbedtls_rsa_export(rsa,
1902 NULL, NULL, NULL, NULL, &actual);
1903 mbedtls_rsa_free(rsa);
1904 mbedtls_free(rsa);
1905 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001906 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001907 }
1908 ret = mbedtls_mpi_read_binary(&required,
1909 attributes->domain_parameters,
1910 attributes->domain_parameters_size);
1911 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001912 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001913 }
1914 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001915 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001916 }
1917rsa_exit:
1918 mbedtls_mpi_free(&actual);
1919 mbedtls_mpi_free(&required);
1920 if (ret != 0) {
1921 return mbedtls_to_psa_error(ret);
1922 }
1923 } else
John Durkop9814fa22020-11-04 12:28:15 -08001924#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1925 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001926 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001927 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001928 }
1929 }
1930
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001931 if (attributes->core.bits != 0) {
1932 if (attributes->core.bits != slot->attr.bits) {
1933 return PSA_ERROR_INVALID_ARGUMENT;
1934 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001935 }
1936
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001937 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001938}
1939
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001940psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1941 const uint8_t *data,
1942 size_t data_length,
1943 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001944{
1945 psa_status_t status;
1946 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001947 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001948 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001949
Ronald Cron81709fc2020-11-14 12:10:32 +01001950 *key = MBEDTLS_SVC_KEY_ID_INIT;
1951
Gilles Peskine0f84d622019-09-12 19:03:13 +02001952 /* Reject zero-length symmetric keys (including raw data key objects).
1953 * This also rejects any key which might be encoded as an empty string,
1954 * which is never valid. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001955 if (data_length == 0) {
1956 return PSA_ERROR_INVALID_ARGUMENT;
1957 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001958
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001959 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1960 &slot, &driver);
1961 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001962 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001963 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001964
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001965 /* In the case of a transparent key or an opaque key stored in local
1966 * storage (thus not in the case of generating a key in a secure element
1967 * or cryptoprocessor with storage), we have to allocate a buffer to
1968 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001969 if (slot->key.data == NULL) {
1970 status = psa_allocate_buffer_to_slot(slot, data_length);
1971 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001972 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001973 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001974 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001975
1976 bits = slot->attr.bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001977 status = psa_driver_wrapper_import_key(attributes,
1978 data, data_length,
1979 slot->key.data,
1980 slot->key.bytes,
1981 &slot->key.bytes, &bits);
1982 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001983 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001984 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001985
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001986 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001987 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001988 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001989 status = PSA_ERROR_INVALID_ARGUMENT;
1990 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001991 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001992
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001993 status = psa_validate_optional_attributes(slot, attributes);
1994 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02001995 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001996 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001997
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001998 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001999exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002000 if (status != PSA_SUCCESS) {
2001 psa_fail_key_creation(slot, driver);
2002 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002003
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002004 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002005}
2006
Gilles Peskined7729582019-08-05 15:55:54 +02002007#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2008psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002009 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002010{
2011 psa_status_t status;
2012 psa_key_slot_t *slot = NULL;
2013 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002014 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002015
2016 /* Leaving attributes unspecified is not currently supported.
2017 * It could make sense to query the key type and size from the
2018 * secure element, but not all secure elements support this
2019 * and the driver HAL doesn't currently support it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002020 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2021 return PSA_ERROR_NOT_SUPPORTED;
2022 }
2023 if (psa_get_key_bits(attributes) == 0) {
2024 return PSA_ERROR_NOT_SUPPORTED;
2025 }
Gilles Peskined7729582019-08-05 15:55:54 +02002026
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002027 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2028 &slot, &driver);
2029 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002030 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002031 }
Gilles Peskined7729582019-08-05 15:55:54 +02002032
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002033 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002034
2035exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002036 if (status != PSA_SUCCESS) {
2037 psa_fail_key_creation(slot, driver);
2038 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002039
Gilles Peskined7729582019-08-05 15:55:54 +02002040 /* Registration doesn't keep the key in RAM. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002041 psa_close_key(key);
2042 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002043}
2044#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2045
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002046static psa_status_t psa_copy_key_material(const psa_key_slot_t *source,
2047 psa_key_slot_t *target)
Gilles Peskinef603c712019-01-19 13:40:11 +01002048{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002049 psa_status_t status = psa_copy_key_material_into_slot(target,
2050 source->key.data,
2051 source->key.bytes);
2052 if (status != PSA_SUCCESS) {
2053 return status;
2054 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002055
Steven Cooreman398aee52020-10-13 14:35:45 +02002056 target->attr.type = source->attr.type;
2057 target->attr.bits = source->attr.bits;
2058
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002059 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01002060}
2061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002062psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2063 const psa_key_attributes_t *specified_attributes,
2064 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002065{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002066 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002067 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002068 psa_key_slot_t *source_slot = NULL;
2069 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002070 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002071 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002072
Ronald Cron81709fc2020-11-14 12:10:32 +01002073 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2074
Ronald Cron5c522922020-11-14 16:35:34 +01002075 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002076 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2077 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002078 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002079 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002080
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002081 status = psa_validate_optional_attributes(source_slot,
2082 specified_attributes);
2083 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002084 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002085 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002086
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002087 status = psa_restrict_key_policy(source_slot->attr.type,
2088 &actual_attributes.core.policy,
2089 &source_slot->attr.policy);
2090 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002091 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002092 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002094 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2095 &target_slot, &driver);
2096 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002097 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002098 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002099
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002100#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002101 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002102 /* Copying to a secure element is not implemented yet. */
2103 status = PSA_ERROR_NOT_SUPPORTED;
2104 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002105 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002106#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002108 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
Ronald Cron6cc66312021-04-02 12:27:47 +02002109 /*
2110 * Copying through an opaque driver is not implemented yet, consider
2111 * a lifetime with an external location as an invalid parameter for
2112 * now.
2113 */
2114 status = PSA_ERROR_INVALID_ARGUMENT;
2115 goto exit;
2116 }
2117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002118 status = psa_copy_key_material(source_slot, target_slot);
2119 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002120 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002121 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002123 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002124exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002125 if (status != PSA_SUCCESS) {
2126 psa_fail_key_creation(target_slot, driver);
2127 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002129 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002131 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002132}
2133
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002134
2135
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002136/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002137/* Message digests */
2138/****************************************************************/
2139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002140psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002141{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002142 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002143 if (operation->id == 0) {
2144 return PSA_SUCCESS;
2145 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002147 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002148 operation->id = 0;
2149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002150 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002151}
2152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002153psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2154 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002155{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002156 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002157
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002158 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002159 if (operation->id != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002160 status = PSA_ERROR_BAD_STATE;
2161 goto exit;
2162 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002164 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002165 status = PSA_ERROR_INVALID_ARGUMENT;
2166 goto exit;
2167 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002168
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002169 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2170 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002171 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002173 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002174
2175exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002176 if (status != PSA_SUCCESS) {
2177 psa_hash_abort(operation);
2178 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002179
2180 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002181}
2182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002183psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2184 const uint8_t *input,
2185 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002186{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002187 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002189 if (operation->id == 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002190 status = PSA_ERROR_BAD_STATE;
2191 goto exit;
2192 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002193
Steven Cooremanc8288352021-03-04 14:02:19 +01002194 /* Don't require hash implementations to behave correctly on a
2195 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002196 if (input_length == 0) {
2197 return PSA_SUCCESS;
2198 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002199
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002200 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002201
2202exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002203 if (status != PSA_SUCCESS) {
2204 psa_hash_abort(operation);
2205 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002207 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002208}
2209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002210psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2211 uint8_t *hash,
2212 size_t hash_size,
2213 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002214{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002215 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002216 if (operation->id == 0) {
2217 return PSA_ERROR_BAD_STATE;
2218 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002219
Steven Cooreman1e582352021-02-18 17:24:37 +01002220 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002221 operation, hash, hash_size, hash_length);
2222 psa_hash_abort(operation);
2223 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002224}
2225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002226psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2227 const uint8_t *hash,
2228 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002229{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002230 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002231 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002232 psa_status_t status = psa_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002233 operation,
2234 actual_hash, sizeof(actual_hash),
2235 &actual_hash_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002237 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002238 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002239 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002241 if (actual_hash_length != hash_length) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002242 status = PSA_ERROR_INVALID_SIGNATURE;
2243 goto exit;
2244 }
2245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002246 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002247 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002248 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002249
2250exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002251 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2252 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002253 psa_hash_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002254 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002256 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002257}
2258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002259psa_status_t psa_hash_compute(psa_algorithm_t alg,
2260 const uint8_t *input, size_t input_length,
2261 uint8_t *hash, size_t hash_size,
2262 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002263{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002264 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002265 if (!PSA_ALG_IS_HASH(alg)) {
2266 return PSA_ERROR_INVALID_ARGUMENT;
2267 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002269 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2270 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002271}
2272
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002273psa_status_t psa_hash_compare(psa_algorithm_t alg,
2274 const uint8_t *input, size_t input_length,
2275 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002276{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002277 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002278 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002280 if (!PSA_ALG_IS_HASH(alg)) {
2281 return PSA_ERROR_INVALID_ARGUMENT;
2282 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002283
Steven Cooreman1e582352021-02-18 17:24:37 +01002284 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002285 alg, input, input_length,
2286 actual_hash, sizeof(actual_hash),
2287 &actual_hash_length);
2288 if (status != PSA_SUCCESS) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002289 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002290 }
2291 if (actual_hash_length != hash_length) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002292 status = PSA_ERROR_INVALID_SIGNATURE;
2293 goto exit;
2294 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002295 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002296 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002297 }
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002298
2299exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002300 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2301 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002302}
2303
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002304psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2305 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002306{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002307 if (source_operation->id == 0 ||
2308 target_operation->id != 0) {
2309 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002310 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002311
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002312 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2313 target_operation);
2314 if (status != PSA_SUCCESS) {
2315 psa_hash_abort(target_operation);
2316 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002318 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002319}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002320
2321
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002322/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002323/* MAC */
2324/****************************************************************/
2325
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002326psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002327{
Steven Cooreman07897832021-03-19 18:28:56 +01002328 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002329 if (operation->id == 0) {
2330 return PSA_SUCCESS;
2331 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002332
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002333 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002334 operation->mac_size = 0;
2335 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002336 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002337
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002338 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002339}
2340
Ronald Cron1c650a12021-06-17 16:33:22 +02002341static psa_status_t psa_mac_finalize_alg_and_key_validation(
2342 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002343 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002344 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002345{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002346 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002347 psa_key_type_t key_type = psa_get_key_type(attributes);
2348 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002350 if (!PSA_ALG_IS_MAC(alg)) {
2351 return PSA_ERROR_INVALID_ARGUMENT;
2352 }
Steven Cooreman07897832021-03-19 18:28:56 +01002353
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002354 /* Validate the combination of key type and algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002355 status = psa_mac_key_can_do(alg, key_type);
2356 if (status != PSA_SUCCESS) {
2357 return status;
2358 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002359
Steven Cooremana6474de2021-05-10 11:13:41 +02002360 /* Get the output length for the algorithm and key combination */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002361 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002363 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002364 /* A very short MAC is too short for security since it can be
2365 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2366 * so we make this our minimum, even though 32 bits is still
2367 * too small for security. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002368 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002369 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002371 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2372 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002373 /* It's impossible to "truncate" to a larger length than the full length
2374 * of the algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002375 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002376 }
2377
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002378 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinebc795822022-03-16 13:54:49 +01002379 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2380 * that is disabled in the compile-time configuration. The result can
2381 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2382 * configuration into account. In this case, force a return of
2383 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2384 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2385 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2386 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2387 * systematically generated tests. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002388 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinebc795822022-03-16 13:54:49 +01002389 }
2390
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002391 return PSA_SUCCESS;
Ronald Cron1c650a12021-06-17 16:33:22 +02002392}
2393
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002394static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2395 mbedtls_svc_key_id_t key,
2396 psa_algorithm_t alg,
2397 int is_sign)
Ronald Cron1c650a12021-06-17 16:33:22 +02002398{
2399 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2400 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002401 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002402 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002403
2404 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002405 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002406 status = PSA_ERROR_BAD_STATE;
2407 goto exit;
2408 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002409
2410 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002411 key,
2412 &slot,
2413 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2414 alg);
2415 if (status != PSA_SUCCESS) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002416 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002417 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002418
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002419 attributes = (psa_key_attributes_t) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002420 .core = slot->attr
2421 };
2422
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002423 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2424 &operation->mac_size);
2425 if (status != PSA_SUCCESS) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002426 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002427 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002428
2429 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002430 /* Dispatch the MAC setup call with validated input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002431 if (is_sign) {
2432 status = psa_driver_wrapper_mac_sign_setup(operation,
2433 &attributes,
2434 slot->key.data,
2435 slot->key.bytes,
2436 alg);
2437 } else {
2438 status = psa_driver_wrapper_mac_verify_setup(operation,
2439 &attributes,
2440 slot->key.data,
2441 slot->key.bytes,
2442 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002443 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002444
Gilles Peskinefbfac682018-07-08 20:51:54 +02002445exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002446 if (status != PSA_SUCCESS) {
2447 psa_mac_abort(operation);
2448 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002449
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002450 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002451
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002452 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002453}
2454
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002455psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2456 mbedtls_svc_key_id_t key,
2457 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002458{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002459 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002460}
2461
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002462psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2463 mbedtls_svc_key_id_t key,
2464 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002465{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002466 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002467}
2468
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002469psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2470 const uint8_t *input,
2471 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002472{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002473 if (operation->id == 0) {
2474 return PSA_ERROR_BAD_STATE;
2475 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002476
Steven Cooreman11743f92021-03-19 18:38:46 +01002477 /* Don't require hash implementations to behave correctly on a
2478 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002479 if (input_length == 0) {
2480 return PSA_SUCCESS;
2481 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002483 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2484 input, input_length);
2485 if (status != PSA_SUCCESS) {
2486 psa_mac_abort(operation);
2487 }
Steven Cooreman11743f92021-03-19 18:38:46 +01002488
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002489 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002490}
2491
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002492psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2493 uint8_t *mac,
2494 size_t mac_size,
2495 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002496{
Steven Cooreman87885df2021-03-19 19:04:39 +01002497 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2498 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002499
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002500 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002501 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002502 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002503 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002504
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002505 if (!operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002506 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002507 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002508 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002509
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002510 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2511 * once all the error checks are done. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002512 if (operation->mac_size == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002513 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002514 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002515 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002516
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002517 if (mac_size < operation->mac_size) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002518 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002519 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002520 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002521
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002522 status = psa_driver_wrapper_mac_sign_finish(operation,
2523 mac, operation->mac_size,
2524 mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002525
Dave Rodgman478ab542021-06-25 09:09:02 +01002526exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002527 /* In case of success, set the potential excess room in the output buffer
2528 * to an invalid value, to avoid potentially leaking a longer MAC.
2529 * In case of error, set the output length and content to a safe default,
2530 * such that in case the caller misses an error check, the output would be
2531 * an unachievable MAC.
2532 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002533 if (status != PSA_SUCCESS) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002534 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002535 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002536 }
mohammad16036df908f2018-04-02 08:34:15 -07002537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002538 if (mac_size > operation->mac_size) {
2539 memset(&mac[operation->mac_size], '!',
2540 mac_size - operation->mac_size);
2541 }
Ronald Cron882eb782021-06-18 13:05:48 +02002542
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002543 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002544
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002545 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002546}
2547
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002548psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2549 const uint8_t *mac,
2550 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002551{
Steven Cooreman87885df2021-03-19 19:04:39 +01002552 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2553 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002555 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002556 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002557 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002558 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002559
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002560 if (operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002561 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002562 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002563 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002565 if (operation->mac_size != mac_length) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002566 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002567 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002568 }
2569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002570 status = psa_driver_wrapper_mac_verify_finish(operation,
2571 mac, mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002572
Dave Rodgman478ab542021-06-25 09:09:02 +01002573exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002574 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002575
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002576 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002577}
2578
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002579static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2580 psa_algorithm_t alg,
2581 const uint8_t *input,
2582 size_t input_length,
2583 uint8_t *mac,
2584 size_t mac_size,
2585 size_t *mac_length,
2586 int is_sign)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002587{
2588 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002589 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002590 psa_key_attributes_t attributes;
Ronald Crondbb86462021-06-17 17:17:20 +02002591 psa_key_slot_t *slot;
2592 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002593
Ronald Crondbb86462021-06-17 17:17:20 +02002594 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002595 key,
2596 &slot,
2597 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2598 alg);
2599 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002600 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002601 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002602
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002603 attributes = (psa_key_attributes_t) {
Ronald Crondbb86462021-06-17 17:17:20 +02002604 .core = slot->attr
2605 };
2606
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002607 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2608 &operation_mac_size);
2609 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002610 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002611 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002612
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002613 if (mac_size < operation_mac_size) {
Ronald Crondbb86462021-06-17 17:17:20 +02002614 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002615 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002616 }
2617
2618 status = psa_driver_wrapper_mac_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002619 &attributes,
2620 slot->key.data, slot->key.bytes,
2621 alg,
2622 input, input_length,
2623 mac, operation_mac_size, mac_length);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002624
2625exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002626 /* In case of success, set the potential excess room in the output buffer
2627 * to an invalid value, to avoid potentially leaking a longer MAC.
2628 * In case of error, set the output length and content to a safe default,
2629 * such that in case the caller misses an error check, the output would be
2630 * an unachievable MAC.
2631 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002632 if (status != PSA_SUCCESS) {
Ronald Crondbb86462021-06-17 17:17:20 +02002633 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002634 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002635 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002636 if (mac_size > operation_mac_size) {
2637 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2638 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002639
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002640 unlock_status = psa_unlock_key_slot(slot);
Ronald Crondbb86462021-06-17 17:17:20 +02002641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002642 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002643}
2644
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002645psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002646 psa_algorithm_t alg,
2647 const uint8_t *input,
2648 size_t input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002649 uint8_t *mac,
2650 size_t mac_size,
2651 size_t *mac_length)
2652{
2653 return psa_mac_compute_internal(key, alg,
2654 input, input_length,
2655 mac, mac_size, mac_length, 1);
2656}
2657
2658psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2659 psa_algorithm_t alg,
2660 const uint8_t *input,
2661 size_t input_length,
2662 const uint8_t *mac,
2663 size_t mac_length)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002664{
2665 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002666 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2667 size_t actual_mac_length;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002668
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002669 status = psa_mac_compute_internal(key, alg,
2670 input, input_length,
2671 actual_mac, sizeof(actual_mac),
2672 &actual_mac_length, 0);
2673 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002674 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002675 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002676
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002677 if (mac_length != actual_mac_length) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002678 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002679 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002680 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002681 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002682 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002683 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002684 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002685
2686exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002687 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002688
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002689 return status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002690}
Gilles Peskine20035e32018-02-03 22:44:14 +01002691
Gilles Peskine20035e32018-02-03 22:44:14 +01002692/****************************************************************/
2693/* Asymmetric cryptography */
2694/****************************************************************/
2695
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002696static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2697 psa_algorithm_t alg)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002698{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002699 if (input_is_message) {
2700 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2701 return PSA_ERROR_INVALID_ARGUMENT;
2702 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002703
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002704 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2705 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2706 return PSA_ERROR_INVALID_ARGUMENT;
2707 }
2708 }
2709 } else {
2710 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2711 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002712 }
2713 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002714
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002715 return PSA_SUCCESS;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002716}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002717
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002718static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2719 int input_is_message,
2720 psa_algorithm_t alg,
2721 const uint8_t *input,
2722 size_t input_length,
2723 uint8_t *signature,
2724 size_t signature_size,
2725 size_t *signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002726{
2727 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2728 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002729 psa_key_attributes_t attributes;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002730 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002731
2732 *signature_length = 0;
2733
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002734 status = psa_sign_verify_check_alg(input_is_message, alg);
2735 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002736 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002737 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002738
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002739 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002740 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002741 * buffer can in principle be empty since it doesn't actually have
2742 * to be a hash.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002743 if (signature_size == 0) {
2744 return PSA_ERROR_BUFFER_TOO_SMALL;
2745 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002746
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002747 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002748 key, &slot,
2749 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2750 PSA_KEY_USAGE_SIGN_HASH,
2751 alg);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002752
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002753 if (status != PSA_SUCCESS) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002754 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002755 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002756
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002757 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002758 status = PSA_ERROR_INVALID_ARGUMENT;
2759 goto exit;
2760 }
2761
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002762 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002763 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002764 };
2765
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002766 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002767 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002768 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002769 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002770 signature, signature_size, signature_length);
2771 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002772
2773 status = psa_driver_wrapper_sign_hash(
2774 &attributes, slot->key.data, slot->key.bytes,
2775 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002776 signature, signature_size, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002777 }
2778
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002779
2780exit:
2781 /* Fill the unused part of the output buffer (the whole buffer on error,
2782 * the trailing part on success) with something that isn't a valid signature
2783 * (barring an attack on the signature and deliberately-crafted input),
2784 * in case the caller doesn't check the return status properly. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002785 if (status == PSA_SUCCESS) {
2786 memset(signature + *signature_length, '!',
2787 signature_size - *signature_length);
2788 } else {
2789 memset(signature, '!', signature_size);
2790 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002791 /* If signature_size is 0 then we have nothing to do. We must not call
2792 * memset because signature may be NULL in this case. */
2793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002794 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002795
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002796 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002797}
2798
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002799static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2800 int input_is_message,
2801 psa_algorithm_t alg,
2802 const uint8_t *input,
2803 size_t input_length,
2804 const uint8_t *signature,
2805 size_t signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002806{
2807 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2808 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2809 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002810
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002811 status = psa_sign_verify_check_alg(input_is_message, alg);
2812 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002813 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002814 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002815
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002816 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002817 key, &slot,
2818 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2819 PSA_KEY_USAGE_VERIFY_HASH,
2820 alg);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002821
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002822 if (status != PSA_SUCCESS) {
2823 return status;
2824 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002825
2826 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002827 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002828 };
2829
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002830 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002831 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002832 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002833 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002834 signature, signature_length);
2835 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002836 status = psa_driver_wrapper_verify_hash(
2837 &attributes, slot->key.data, slot->key.bytes,
2838 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002839 signature, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002840 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002841
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002842 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002843
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002844 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002845
2846}
2847
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002848psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002849 const psa_key_attributes_t *attributes,
2850 const uint8_t *key_buffer,
2851 size_t key_buffer_size,
2852 psa_algorithm_t alg,
2853 const uint8_t *input,
2854 size_t input_length,
2855 uint8_t *signature,
2856 size_t signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002857 size_t *signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002858{
2859 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002861 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002862 size_t hash_length;
2863 uint8_t hash[PSA_HASH_MAX_SIZE];
2864
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002865 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002866 PSA_ALG_SIGN_GET_HASH(alg),
2867 input, input_length,
2868 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002869
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002870 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002871 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002872 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002873
2874 return psa_driver_wrapper_sign_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002875 attributes, key_buffer, key_buffer_size,
2876 alg, hash, hash_length,
2877 signature, signature_size, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002878 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002880 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002881}
2882
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002883psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2884 psa_algorithm_t alg,
2885 const uint8_t *input,
2886 size_t input_length,
2887 uint8_t *signature,
2888 size_t signature_size,
2889 size_t *signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002890{
2891 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002892 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002893 signature, signature_size, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002894}
2895
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002896psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002897 const psa_key_attributes_t *attributes,
2898 const uint8_t *key_buffer,
2899 size_t key_buffer_size,
2900 psa_algorithm_t alg,
2901 const uint8_t *input,
2902 size_t input_length,
2903 const uint8_t *signature,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002904 size_t signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002905{
2906 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002907
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002908 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002909 size_t hash_length;
2910 uint8_t hash[PSA_HASH_MAX_SIZE];
2911
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002912 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002913 PSA_ALG_SIGN_GET_HASH(alg),
2914 input, input_length,
2915 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002916
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002917 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002918 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002919 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002920
2921 return psa_driver_wrapper_verify_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002922 attributes, key_buffer, key_buffer_size,
2923 alg, hash, hash_length,
2924 signature, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002925 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002926
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002927 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002928}
2929
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002930psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2931 psa_algorithm_t alg,
2932 const uint8_t *input,
2933 size_t input_length,
2934 const uint8_t *signature,
2935 size_t signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002936{
2937 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002938 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002939 signature, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002940}
2941
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002942psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002943 const psa_key_attributes_t *attributes,
2944 const uint8_t *key_buffer, size_t key_buffer_size,
2945 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002946 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskine20035e32018-02-03 22:44:14 +01002947{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002948 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2949 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2950 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002951#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002952 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2953 return mbedtls_psa_rsa_sign_hash(
2954 attributes,
2955 key_buffer, key_buffer_size,
2956 alg, hash, hash_length,
2957 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002958#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2959 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002960 } else {
2961 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002962 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002963 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2964 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002965#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002966 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2967 return mbedtls_psa_ecdsa_sign_hash(
2968 attributes,
2969 key_buffer, key_buffer_size,
2970 alg, hash, hash_length,
2971 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002972#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2973 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002974 } else {
2975 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002976 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002977 }
Ronald Cron4501c982021-03-19 20:09:32 +01002978
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002979 (void) key_buffer;
2980 (void) key_buffer_size;
2981 (void) hash;
2982 (void) hash_length;
2983 (void) signature;
2984 (void) signature_size;
2985 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002986
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002987 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002988}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002989
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002990psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2991 psa_algorithm_t alg,
2992 const uint8_t *hash,
2993 size_t hash_length,
2994 uint8_t *signature,
2995 size_t signature_size,
2996 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002997{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002998 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002999 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003000 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003001}
3002
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003003psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003004 const psa_key_attributes_t *attributes,
3005 const uint8_t *key_buffer, size_t key_buffer_size,
3006 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003007 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003008{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003009 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3010 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3011 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003012#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003013 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3014 return mbedtls_psa_rsa_verify_hash(
3015 attributes,
3016 key_buffer, key_buffer_size,
3017 alg, hash, hash_length,
3018 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003019#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3020 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003021 } else {
3022 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003023 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003024 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3025 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003026#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003027 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3028 return mbedtls_psa_ecdsa_verify_hash(
3029 attributes,
3030 key_buffer, key_buffer_size,
3031 alg, hash, hash_length,
3032 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003033#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3034 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003035 } else {
3036 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003037 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003038 }
Ronald Cron4501c982021-03-19 20:09:32 +01003039
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003040 (void) key_buffer;
3041 (void) key_buffer_size;
3042 (void) hash;
3043 (void) hash_length;
3044 (void) signature;
3045 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003046
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003047 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003048}
3049
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003050psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3051 psa_algorithm_t alg,
3052 const uint8_t *hash,
3053 size_t hash_length,
3054 const uint8_t *signature,
3055 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003056{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003057 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003058 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003059 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003060}
3061
John Durkop0e005192020-10-31 22:06:54 -07003062#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003063static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
3064 mbedtls_rsa_context *rsa)
Gilles Peskine072ac562018-06-30 00:21:29 +02003065{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003066 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
3067 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
3068 mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info);
3069 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Gilles Peskine072ac562018-06-30 00:21:29 +02003070}
John Durkop0e005192020-10-31 22:06:54 -07003071#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003072
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003073psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3074 psa_algorithm_t alg,
3075 const uint8_t *input,
3076 size_t input_length,
3077 const uint8_t *salt,
3078 size_t salt_length,
3079 uint8_t *output,
3080 size_t output_size,
3081 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003082{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003083 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003084 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003085 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003086
Darryl Green5cc689a2018-07-24 15:34:10 +01003087 (void) input;
3088 (void) input_length;
3089 (void) salt;
3090 (void) output;
3091 (void) output_size;
3092
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003093 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003095 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3096 return PSA_ERROR_INVALID_ARGUMENT;
3097 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003098
Ronald Cron5c522922020-11-14 16:35:34 +01003099 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003100 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3101 if (status != PSA_SUCCESS) {
3102 return status;
3103 }
3104 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3105 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003106 status = PSA_ERROR_INVALID_ARGUMENT;
3107 goto exit;
3108 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003110 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003111#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003112 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003113 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003114 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3115 slot->key.data,
3116 slot->key.bytes,
3117 &rsa);
3118 if (status != PSA_SUCCESS) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003119 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003120 }
Steven Cooremana2371e52020-07-28 14:30:39 +02003121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003122 if (output_size < mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003123 status = PSA_ERROR_BUFFER_TOO_SMALL;
3124 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003125 }
Ronald Cron7207d572021-09-08 14:28:35 +02003126#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3127 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003128 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003129#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003130 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003131 mbedtls_rsa_pkcs1_encrypt(rsa,
3132 mbedtls_psa_get_random,
3133 MBEDTLS_PSA_RANDOM_STATE,
3134 MBEDTLS_RSA_PUBLIC,
3135 input_length,
3136 input,
3137 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003138#else
3139 status = PSA_ERROR_NOT_SUPPORTED;
3140#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003141 } else
3142 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003143#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003144 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003145 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003146 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
3147 mbedtls_psa_get_random,
3148 MBEDTLS_PSA_RANDOM_STATE,
3149 MBEDTLS_RSA_PUBLIC,
3150 salt, salt_length,
3151 input_length,
3152 input,
3153 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003154#else
3155 status = PSA_ERROR_NOT_SUPPORTED;
3156#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003157 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003158 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003159 }
Ronald Cron7207d572021-09-08 14:28:35 +02003160#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003161 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003162rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003163 if (status == PSA_SUCCESS) {
3164 *output_length = mbedtls_rsa_get_len(rsa);
3165 }
Steven Cooremana01795d2020-07-24 22:48:15 +02003166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003167 mbedtls_rsa_free(rsa);
3168 mbedtls_free(rsa);
John Durkop9814fa22020-11-04 12:28:15 -08003169#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003170 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003171 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003172 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003173 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003174
3175exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003176 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003178 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003179}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003181psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3182 psa_algorithm_t alg,
3183 const uint8_t *input,
3184 size_t input_length,
3185 const uint8_t *salt,
3186 size_t salt_length,
3187 uint8_t *output,
3188 size_t output_size,
3189 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003190{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003191 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003192 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003193 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003194
Darryl Green5cc689a2018-07-24 15:34:10 +01003195 (void) input;
3196 (void) input_length;
3197 (void) salt;
3198 (void) output;
3199 (void) output_size;
3200
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003201 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003203 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3204 return PSA_ERROR_INVALID_ARGUMENT;
3205 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003206
Ronald Cron5c522922020-11-14 16:35:34 +01003207 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003208 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3209 if (status != PSA_SUCCESS) {
3210 return status;
3211 }
3212 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003213 status = PSA_ERROR_INVALID_ARGUMENT;
3214 goto exit;
3215 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003216
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003217 if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Ronald Cron7207d572021-09-08 14:28:35 +02003218#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003219 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003220 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003221 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3222 slot->key.data,
3223 slot->key.bytes,
3224 &rsa);
3225 if (status != PSA_SUCCESS) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003226 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003227 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003229 if (input_length != mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003230 status = PSA_ERROR_INVALID_ARGUMENT;
3231 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003232 }
Ronald Cron7207d572021-09-08 14:28:35 +02003233#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3234 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003235
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003236 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003237#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003238 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003239 mbedtls_rsa_pkcs1_decrypt(rsa,
3240 mbedtls_psa_get_random,
3241 MBEDTLS_PSA_RANDOM_STATE,
3242 MBEDTLS_RSA_PRIVATE,
3243 output_length,
3244 input,
3245 output,
3246 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003247#else
3248 status = PSA_ERROR_NOT_SUPPORTED;
3249#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003250 } else
3251 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003252#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003253 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003254 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003255 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
3256 mbedtls_psa_get_random,
3257 MBEDTLS_PSA_RANDOM_STATE,
3258 MBEDTLS_RSA_PRIVATE,
3259 salt, salt_length,
3260 output_length,
3261 input,
3262 output,
3263 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003264#else
3265 status = PSA_ERROR_NOT_SUPPORTED;
3266#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003267 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003268 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003269 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003270
Ronald Cron7207d572021-09-08 14:28:35 +02003271#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003272 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003273rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003274 mbedtls_rsa_free(rsa);
3275 mbedtls_free(rsa);
John Durkop6ba40d12020-11-10 08:50:04 -08003276#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3277 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003278 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003279 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003280 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003281
3282exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003283 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003284
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003285 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003286}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003287
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003288
3289
mohammad1603503973b2018-03-12 15:59:30 +02003290/****************************************************************/
3291/* Symmetric cryptography */
3292/****************************************************************/
3293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003294static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3295 mbedtls_svc_key_id_t key,
3296 psa_algorithm_t alg,
3297 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003298{
3299 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3300 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003301 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003302 psa_key_slot_t *slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003303 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3304 PSA_KEY_USAGE_ENCRYPT :
3305 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003306
3307 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003308 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003309 status = PSA_ERROR_BAD_STATE;
3310 goto exit;
3311 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003313 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003314 status = PSA_ERROR_INVALID_ARGUMENT;
3315 goto exit;
3316 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003318 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3319 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003320 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003321 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003322
Ronald Cron49fafa92021-03-10 08:34:23 +01003323 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003324 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003325 * so we only set it (in the driver wrapper) after resources have been
3326 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003327 operation->iv_set = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003328 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003329 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003330 } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) {
Gilles Peskine4a83c102022-04-20 17:00:22 +02003331 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003332 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003333 operation->iv_required = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003334 }
3335 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003336
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003337 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003338 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003339 };
3340
Ronald Cronab99ac22020-10-01 16:38:28 +02003341 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003342 if (cipher_operation == MBEDTLS_ENCRYPT) {
3343 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3344 &attributes,
3345 slot->key.data,
3346 slot->key.bytes,
3347 alg);
3348 } else {
3349 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3350 &attributes,
3351 slot->key.data,
3352 slot->key.bytes,
3353 alg);
3354 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003355
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003356exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003357 if (status != PSA_SUCCESS) {
3358 psa_cipher_abort(operation);
3359 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003360
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003361 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003363 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003364}
3365
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003366psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3367 mbedtls_svc_key_id_t key,
3368 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003369{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003370 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003371}
3372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003373psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3374 mbedtls_svc_key_id_t key,
3375 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003376{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003377 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003378}
3379
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003380psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3381 uint8_t *iv,
3382 size_t iv_size,
3383 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003384{
Ronald Cron6d051732020-10-01 14:10:20 +02003385 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc423acb2021-07-05 12:31:44 +02003386 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergey04eb7c02023-03-06 15:37:23 -07003387 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003388
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003389 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003390 status = PSA_ERROR_BAD_STATE;
3391 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003392 }
3393
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003394 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003395 status = PSA_ERROR_BAD_STATE;
3396 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003397 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003398
Ronald Cronc423acb2021-07-05 12:31:44 +02003399 default_iv_length = operation->default_iv_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003400 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003401 status = PSA_ERROR_BUFFER_TOO_SMALL;
3402 goto exit;
3403 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003404
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003405 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003406 status = PSA_ERROR_GENERIC_ERROR;
3407 goto exit;
3408 }
3409
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003410 status = psa_generate_random(local_iv, default_iv_length);
3411 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003412 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003413 }
Ronald Cron5618a392021-03-26 09:52:26 +01003414
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003415 status = psa_driver_wrapper_cipher_set_iv(operation,
3416 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003417
3418exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003419 if (status == PSA_SUCCESS) {
3420 memcpy(iv, local_iv, default_iv_length);
Ronald Cronc423acb2021-07-05 12:31:44 +02003421 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003422 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003423 } else {
Ronald Cronc423acb2021-07-05 12:31:44 +02003424 *iv_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003425 psa_cipher_abort(operation);
Ronald Cronc423acb2021-07-05 12:31:44 +02003426 }
Ronald Cron6d051732020-10-01 14:10:20 +02003427
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003428 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003429}
3430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003431psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
3432 const uint8_t *iv,
3433 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003434{
Ronald Cron6d051732020-10-01 14:10:20 +02003435 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003436
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003437 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003438 status = PSA_ERROR_BAD_STATE;
3439 goto exit;
3440 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003441
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003442 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003443 status = PSA_ERROR_BAD_STATE;
3444 goto exit;
3445 }
Ronald Crona0d68172021-03-26 10:15:08 +01003446
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003447 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003448 status = PSA_ERROR_INVALID_ARGUMENT;
3449 goto exit;
3450 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003451
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003452 status = psa_driver_wrapper_cipher_set_iv(operation,
3453 iv,
3454 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003455
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003456exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003457 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003458 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003459 } else {
3460 psa_cipher_abort(operation);
3461 }
3462 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003463}
3464
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003465psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
3466 const uint8_t *input,
3467 size_t input_length,
3468 uint8_t *output,
3469 size_t output_size,
3470 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003471{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003472 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003473
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003474 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003475 status = PSA_ERROR_BAD_STATE;
3476 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003477 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003478
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003479 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003480 status = PSA_ERROR_BAD_STATE;
3481 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003482 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003483
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003484 status = psa_driver_wrapper_cipher_update(operation,
3485 input,
3486 input_length,
3487 output,
3488 output_size,
3489 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003490
3491exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003492 if (status != PSA_SUCCESS) {
3493 psa_cipher_abort(operation);
3494 }
Ronald Cron6d051732020-10-01 14:10:20 +02003495
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003496 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003497}
3498
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003499psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
3500 uint8_t *output,
3501 size_t output_size,
3502 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003503{
David Saadab4ecc272019-02-14 13:48:10 +02003504 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003505
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003506 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003507 status = PSA_ERROR_BAD_STATE;
3508 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003509 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003510
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003511 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003512 status = PSA_ERROR_BAD_STATE;
3513 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003514 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003516 status = psa_driver_wrapper_cipher_finish(operation,
3517 output,
3518 output_size,
3519 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003520
3521exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003522 if (status == PSA_SUCCESS) {
3523 return psa_cipher_abort(operation);
3524 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003525 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003526 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01003527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003528 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003529 }
mohammad1603503973b2018-03-12 15:59:30 +02003530}
3531
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003532psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003533{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003534 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003535 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003536 * in use. It's ok to call abort on such an object, and there's
3537 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003538 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003539 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003541 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003542
Ronald Cron49fafa92021-03-10 08:34:23 +01003543 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003544 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003545 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003546
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003547 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003548}
3549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003550psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3551 psa_algorithm_t alg,
3552 const uint8_t *input,
3553 size_t input_length,
3554 uint8_t *output,
3555 size_t output_size,
3556 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003557{
3558 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003559 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003560 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003561 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003562 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3563 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003565 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003566 status = PSA_ERROR_INVALID_ARGUMENT;
3567 goto exit;
3568 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003570 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3571 PSA_KEY_USAGE_ENCRYPT,
3572 alg);
3573 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003574 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003575 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003576
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003577 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003578 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003579 };
3580
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003581 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
3582 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron4c224fe2021-07-09 18:44:58 +02003583 status = PSA_ERROR_GENERIC_ERROR;
3584 goto exit;
3585 }
3586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003587 if (default_iv_length > 0) {
3588 if (output_size < default_iv_length) {
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003589 status = PSA_ERROR_BUFFER_TOO_SMALL;
3590 goto exit;
3591 }
3592
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003593 status = psa_generate_random(local_iv, default_iv_length);
3594 if (status != PSA_SUCCESS) {
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003595 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003596 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003597 }
3598
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003599 status = psa_driver_wrapper_cipher_encrypt(
3600 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02003601 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003602 mbedtls_buffer_offset(output, default_iv_length),
3603 output_size - default_iv_length, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003604
3605exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003606 unlock_status = psa_unlock_key_slot(slot);
3607 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003608 status = unlock_status;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003609 }
Ronald Cron16377072021-07-08 19:00:07 +02003610
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003611 if (status == PSA_SUCCESS) {
3612 if (default_iv_length > 0) {
3613 memcpy(output, local_iv, default_iv_length);
3614 }
3615 *output_length += default_iv_length;
3616 } else {
3617 *output_length = 0;
3618 }
3619
3620 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003621}
3622
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003623psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
3624 psa_algorithm_t alg,
3625 const uint8_t *input,
3626 size_t input_length,
3627 uint8_t *output,
3628 size_t output_size,
3629 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003630{
3631 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003632 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003633 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003634 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003635
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003636 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003637 status = PSA_ERROR_INVALID_ARGUMENT;
3638 goto exit;
3639 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003640
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003641 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3642 PSA_KEY_USAGE_DECRYPT,
3643 alg);
3644 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003645 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003646 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003647
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003648 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003649 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003650 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003651
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003652 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003653 status = PSA_ERROR_INVALID_ARGUMENT;
3654 goto exit;
3655 }
3656
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003657 status = psa_driver_wrapper_cipher_decrypt(
3658 &attributes, slot->key.data, slot->key.bytes,
3659 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003660 output, output_size, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003661
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003662exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003663 unlock_status = psa_unlock_key_slot(slot);
3664 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003665 status = unlock_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003666 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003668 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003669 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003670 }
Ronald Cron16377072021-07-08 19:00:07 +02003671
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003672 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003673}
3674
3675
mohammad16035955c982018-04-26 00:53:03 +03003676/****************************************************************/
3677/* AEAD */
3678/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003679
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003680psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
3681 psa_algorithm_t alg,
3682 const uint8_t *nonce,
3683 size_t nonce_length,
3684 const uint8_t *additional_data,
3685 size_t additional_data_length,
3686 const uint8_t *plaintext,
3687 size_t plaintext_length,
3688 uint8_t *ciphertext,
3689 size_t ciphertext_size,
3690 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03003691{
Ronald Cron215633c2021-03-16 17:15:37 +01003692 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3693 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003694
mohammad1603f08a5502018-06-03 15:05:47 +03003695 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003696
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003697 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3698 return PSA_ERROR_NOT_SUPPORTED;
3699 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003700
Ronald Cron9a986162021-03-26 12:40:07 +01003701 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003702 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3703 if (status != PSA_SUCCESS) {
3704 return status;
3705 }
mohammad16035955c982018-04-26 00:53:03 +03003706
Ronald Cron215633c2021-03-16 17:15:37 +01003707 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003708 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003709 };
mohammad16035955c982018-04-26 00:53:03 +03003710
Ronald Cronde822812021-03-17 16:08:20 +01003711 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003712 &attributes, slot->key.data, slot->key.bytes,
3713 alg,
3714 nonce, nonce_length,
3715 additional_data, additional_data_length,
3716 plaintext, plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003717 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02003718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003719 if (status != PSA_SUCCESS && ciphertext_size != 0) {
3720 memset(ciphertext, 0, ciphertext_size);
3721 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003722
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003723 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03003724
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003725 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02003726}
3727
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003728psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
3729 psa_algorithm_t alg,
3730 const uint8_t *nonce,
3731 size_t nonce_length,
3732 const uint8_t *additional_data,
3733 size_t additional_data_length,
3734 const uint8_t *ciphertext,
3735 size_t ciphertext_length,
3736 uint8_t *plaintext,
3737 size_t plaintext_size,
3738 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03003739{
Ronald Cron215633c2021-03-16 17:15:37 +01003740 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3741 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003742
Gilles Peskineee652a32018-06-01 19:23:52 +02003743 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003744
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003745 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3746 return PSA_ERROR_NOT_SUPPORTED;
3747 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003748
Ronald Cron9a986162021-03-26 12:40:07 +01003749 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003750 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3751 if (status != PSA_SUCCESS) {
3752 return status;
3753 }
mohammad16035955c982018-04-26 00:53:03 +03003754
Ronald Cron215633c2021-03-16 17:15:37 +01003755 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003756 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003757 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003758
Ronald Cronde822812021-03-17 16:08:20 +01003759 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003760 &attributes, slot->key.data, slot->key.bytes,
3761 alg,
3762 nonce, nonce_length,
3763 additional_data, additional_data_length,
3764 ciphertext, ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003765 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02003766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003767 if (status != PSA_SUCCESS && plaintext_size != 0) {
3768 memset(plaintext, 0, plaintext_size);
3769 }
mohammad160360a64d02018-06-03 17:20:42 +03003770
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003771 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01003772
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003773 return status;
mohammad16035955c982018-04-26 00:53:03 +03003774}
3775
Gilles Peskinee59236f2018-01-27 23:32:46 +01003776/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003777/* Generators */
3778/****************************************************************/
3779
John Durkop07cc04a2020-11-16 22:08:34 -08003780#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3781 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3782 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3783#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02003784#endif /* At least one builtin KDF */
3785
3786#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3787 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3788 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3789static psa_status_t psa_key_derivation_start_hmac(
3790 psa_mac_operation_t *operation,
3791 psa_algorithm_t hash_alg,
3792 const uint8_t *hmac_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003793 size_t hmac_key_length)
Steven Cooreman9878a162021-05-06 17:58:36 +02003794{
3795 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3796 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003797 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
3798 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
3799 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman9878a162021-05-06 17:58:36 +02003800
Steven Cooreman15f0d922021-05-07 14:14:37 +02003801 operation->is_sign = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003802 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman15f0d922021-05-07 14:14:37 +02003803
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003804 status = psa_driver_wrapper_mac_sign_setup(operation,
3805 &attributes,
3806 hmac_key, hmac_key_length,
3807 PSA_ALG_HMAC(hash_alg));
Steven Cooreman9878a162021-05-06 17:58:36 +02003808
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003809 psa_reset_key_attributes(&attributes);
3810 return status;
Steven Cooreman9878a162021-05-06 17:58:36 +02003811}
3812#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08003813
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003814#define HKDF_STATE_INIT 0 /* no input yet */
3815#define HKDF_STATE_STARTED 1 /* got salt */
3816#define HKDF_STATE_KEYED 2 /* got key */
3817#define HKDF_STATE_OUTPUT 3 /* output started */
3818
Gilles Peskinecbe66502019-05-16 16:59:18 +02003819static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003820 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003821{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003822 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
3823 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
3824 } else {
3825 return operation->alg;
3826 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003827}
3828
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003829psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003830{
3831 psa_status_t status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003832 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
3833 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02003834 /* The object has (apparently) been initialized but it is not
3835 * in use. It's ok to call abort on such an object, and there's
3836 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003837 } else
John Durkopd0321952020-10-29 21:37:36 -07003838#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003839 if (PSA_ALG_IS_HKDF(kdf_alg)) {
3840 mbedtls_free(operation->ctx.hkdf.info);
3841 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
3842 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003843#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
3844#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3845 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003846 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
3847 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
3848 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
3849 if (operation->ctx.tls12_prf.secret != NULL) {
3850 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
3851 operation->ctx.tls12_prf.secret_length);
3852 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003853 }
3854
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003855 if (operation->ctx.tls12_prf.seed != NULL) {
3856 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
3857 operation->ctx.tls12_prf.seed_length);
3858 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01003859 }
3860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003861 if (operation->ctx.tls12_prf.label != NULL) {
3862 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
3863 operation->ctx.tls12_prf.label_length);
3864 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01003865 }
3866
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003867 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01003868
3869 /* We leave the fields Ai and output_block to be erased safely by the
3870 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003871 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003872#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
3873 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003874 {
3875 status = PSA_ERROR_BAD_STATE;
3876 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003877 mbedtls_platform_zeroize(operation, sizeof(*operation));
3878 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003879}
3880
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003881psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003882 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003883{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003884 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003885 /* This is a blank key derivation operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003886 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003887 }
3888
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003889 *capacity = operation->capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003890 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003891}
3892
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003893psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
3894 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003895{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003896 if (operation->alg == 0) {
3897 return PSA_ERROR_BAD_STATE;
3898 }
3899 if (capacity > operation->capacity) {
3900 return PSA_ERROR_INVALID_ARGUMENT;
3901 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003902 operation->capacity = capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003903 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003904}
3905
John Durkopd0321952020-10-29 21:37:36 -07003906#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003907/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02003908 * of the expand phase of the HKDF algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003909static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
3910 psa_algorithm_t hash_alg,
3911 uint8_t *output,
3912 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003913{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003914 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremanb27e3502021-04-29 19:11:25 +02003915 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003916 psa_status_t status;
3917
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003918 if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) {
3919 return PSA_ERROR_BAD_STATE;
3920 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003921 hkdf->state = HKDF_STATE_OUTPUT;
3922
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003923 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003924 /* Copy what remains of the current block */
3925 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003926 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003927 n = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003928 }
3929 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02003930 output += n;
3931 output_length -= n;
3932 hkdf->offset_in_block += n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003933 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003934 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003935 }
Gilles Peskined54931c2018-07-17 21:06:59 +02003936 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003937 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003938 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02003939 * object was corrupted or if this function is called directly
3940 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003941 if (hkdf->block_number == 0xff) {
3942 return PSA_ERROR_BAD_STATE;
3943 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003944
3945 /* We need a new block */
3946 ++hkdf->block_number;
3947 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02003948
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003949 status = psa_key_derivation_start_hmac(&hkdf->hmac,
3950 hash_alg,
3951 hkdf->prk,
3952 hash_length);
3953 if (status != PSA_SUCCESS) {
3954 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003955 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003956
3957 if (hkdf->block_number != 1) {
3958 status = psa_mac_update(&hkdf->hmac,
3959 hkdf->output_block,
3960 hash_length);
3961 if (status != PSA_SUCCESS) {
3962 return status;
3963 }
3964 }
3965 status = psa_mac_update(&hkdf->hmac,
3966 hkdf->info,
3967 hkdf->info_length);
3968 if (status != PSA_SUCCESS) {
3969 return status;
3970 }
3971 status = psa_mac_update(&hkdf->hmac,
3972 &hkdf->block_number, 1);
3973 if (status != PSA_SUCCESS) {
3974 return status;
3975 }
3976 status = psa_mac_sign_finish(&hkdf->hmac,
3977 hkdf->output_block,
3978 sizeof(hkdf->output_block),
3979 &hmac_output_length);
3980 if (status != PSA_SUCCESS) {
3981 return status;
3982 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003983 }
3984
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003985 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003986}
John Durkop07cc04a2020-11-16 22:08:34 -08003987#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003988
John Durkop07cc04a2020-11-16 22:08:34 -08003989#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3990 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01003991static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
3992 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003993 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01003994{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003995 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
3996 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003997 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
3998 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01003999 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004000
Janos Follath7742fee2019-06-17 12:58:10 +01004001 /* We can't be wanting more output after block 0xff, otherwise
4002 * the capacity check in psa_key_derivation_output_bytes() would have
4003 * prevented this call. It could happen only if the operation
4004 * object was corrupted or if this function is called directly
4005 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004006 if (tls12_prf->block_number == 0xff) {
4007 return PSA_ERROR_CORRUPTION_DETECTED;
4008 }
Janos Follath7742fee2019-06-17 12:58:10 +01004009
4010 /* We need a new block */
4011 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004012 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004013
4014 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4015 *
4016 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4017 *
4018 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4019 * HMAC_hash(secret, A(2) + seed) +
4020 * HMAC_hash(secret, A(3) + seed) + ...
4021 *
4022 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004023 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004024 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004025 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004026 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004027 * is currently extracted as `output_block` and where i is
4028 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004029 */
4030
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004031 status = psa_key_derivation_start_hmac(&hmac,
4032 hash_alg,
4033 tls12_prf->secret,
4034 tls12_prf->secret_length);
4035 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004036 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004037 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004038
4039 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004040 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004041 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4042 * the variable seed and in this instance means it in the context of the
4043 * P_hash function, where seed = label + seed.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004044 status = psa_mac_update(&hmac,
4045 tls12_prf->label,
4046 tls12_prf->label_length);
4047 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004048 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004049 }
4050 status = psa_mac_update(&hmac,
4051 tls12_prf->seed,
4052 tls12_prf->seed_length);
4053 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004054 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004055 }
4056 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004057 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004058 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4059 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004060 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004061 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004062 }
4063
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004064 status = psa_mac_sign_finish(&hmac,
4065 tls12_prf->Ai, hash_length,
4066 &hmac_output_length);
4067 if (hmac_output_length != hash_length) {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004068 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004069 }
4070 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004071 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004072 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004073
4074 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004075 status = psa_key_derivation_start_hmac(&hmac,
4076 hash_alg,
4077 tls12_prf->secret,
4078 tls12_prf->secret_length);
4079 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004080 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004081 }
4082 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4083 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004084 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004085 }
4086 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
4087 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004088 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004089 }
4090 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
4091 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004092 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004093 }
4094 status = psa_mac_sign_finish(&hmac,
4095 tls12_prf->output_block, hash_length,
4096 &hmac_output_length);
4097 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004098 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004099 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004100
Janos Follath7742fee2019-06-17 12:58:10 +01004101
4102cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004103 cleanup_status = psa_mac_abort(&hmac);
4104 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004105 status = cleanup_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004106 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004108 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004109}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004110
Janos Follath844eb0e2019-06-19 12:10:49 +01004111static psa_status_t psa_key_derivation_tls12_prf_read(
4112 psa_tls12_prf_key_derivation_t *tls12_prf,
4113 psa_algorithm_t alg,
4114 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004115 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004116{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004117 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4118 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004119 psa_status_t status;
4120 uint8_t offset, length;
4121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004122 switch (tls12_prf->state) {
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004123 case PSA_TLS12_PRF_STATE_LABEL_SET:
4124 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4125 break;
4126 case PSA_TLS12_PRF_STATE_OUTPUT:
4127 break;
4128 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004129 return PSA_ERROR_BAD_STATE;
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004130 }
4131
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004132 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004133 /* Check if we have fully processed the current block. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004134 if (tls12_prf->left_in_block == 0) {
4135 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4136 alg);
4137 if (status != PSA_SUCCESS) {
4138 return status;
4139 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004140
4141 continue;
4142 }
4143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004144 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004145 length = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004146 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004147 length = tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004148 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004149
4150 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004151 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004152 output += length;
4153 output_length -= length;
4154 tls12_prf->left_in_block -= length;
4155 }
4156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004157 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004158}
John Durkop07cc04a2020-11-16 22:08:34 -08004159#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4160 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004161
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004162psa_status_t psa_key_derivation_output_bytes(
4163 psa_key_derivation_operation_t *operation,
4164 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004165 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004166{
4167 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004168 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004170 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004171 /* This is a blank operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004172 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004173 }
4174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004175 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004176 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004177 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004178 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004179 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004180 goto exit;
4181 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004182 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004183 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004184 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004185 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4186 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004187 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004188 * output_length > 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004189 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004190 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004191 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004192
John Durkopd0321952020-10-29 21:37:36 -07004193#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004194 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4195 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4196 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg,
4197 output, output_length);
4198 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004199#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4200#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4201 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004202 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4203 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4204 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4205 kdf_alg, output,
4206 output_length);
4207 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004208#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4209 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004210 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004211 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004212 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004213 }
4214
4215exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004216 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004217 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004218 * This allows us to differentiate between exhausted operations and
4219 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4220 * operations. */
4221 psa_algorithm_t alg = operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004222 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004223 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004224 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004225 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004226 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004227}
4228
David Brown7807bf72021-02-09 16:28:23 -07004229#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004230static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004231{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004232 if (data_size >= 8) {
4233 mbedtls_des_key_set_parity(data);
4234 }
4235 if (data_size >= 16) {
4236 mbedtls_des_key_set_parity(data + 8);
4237 }
4238 if (data_size >= 24) {
4239 mbedtls_des_key_set_parity(data + 16);
4240 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004241}
David Brown7807bf72021-02-09 16:28:23 -07004242#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004243
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004244static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004245 psa_key_slot_t *slot,
4246 size_t bits,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004247 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004248{
4249 uint8_t *data = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004250 size_t bytes = PSA_BITS_TO_BYTES(bits);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004251 psa_status_t status;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004252 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004254 if (!key_type_is_raw_bytes(slot->attr.type)) {
4255 return PSA_ERROR_INVALID_ARGUMENT;
4256 }
4257 if (bits % 8 != 0) {
4258 return PSA_ERROR_INVALID_ARGUMENT;
4259 }
4260 data = mbedtls_calloc(1, bytes);
4261 if (data == NULL) {
4262 return PSA_ERROR_INSUFFICIENT_MEMORY;
4263 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004265 status = psa_key_derivation_output_bytes(operation, data, bytes);
4266 if (status != PSA_SUCCESS) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004267 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004268 }
David Brown12ca5032021-02-11 11:02:00 -07004269#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004270 if (slot->attr.type == PSA_KEY_TYPE_DES) {
4271 psa_des_set_key_parity(data, bytes);
4272 }
David Brown8107e312021-02-12 08:08:46 -07004273#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004275 status = psa_allocate_buffer_to_slot(slot, bytes);
4276 if (status != PSA_SUCCESS) {
Paul Elliottda3e7db2021-02-09 18:58:20 +00004277 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004278 }
Ronald Crondd04d422020-11-28 15:14:42 +01004279
Ronald Cronbf33c932020-11-28 18:06:53 +01004280 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004281 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004282 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004283 };
4284
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004285 status = psa_driver_wrapper_import_key(&attributes,
4286 data, bytes,
4287 slot->key.data,
4288 slot->key.bytes,
4289 &slot->key.bytes, &bits);
4290 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01004291 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004292 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004293
4294exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004295 mbedtls_free(data);
4296 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004297}
4298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004299psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
4300 psa_key_derivation_operation_t *operation,
4301 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004302{
4303 psa_status_t status;
4304 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004305 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004306
Ronald Cron81709fc2020-11-14 12:10:32 +01004307 *key = MBEDTLS_SVC_KEY_ID_INIT;
4308
Gilles Peskine0f84d622019-09-12 19:03:13 +02004309 /* Reject any attempt to create a zero-length key so that we don't
4310 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004311 if (psa_get_key_bits(attributes) == 0) {
4312 return PSA_ERROR_INVALID_ARGUMENT;
4313 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02004314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004315 if (operation->alg == PSA_ALG_NONE) {
4316 return PSA_ERROR_BAD_STATE;
4317 }
Dave Rodgman021e7242021-11-16 10:32:48 +00004318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004319 if (!operation->can_output_key) {
4320 return PSA_ERROR_NOT_PERMITTED;
4321 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004322
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004323 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
4324 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004325#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004326 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004327 /* Deriving a key in a secure element is not implemented yet. */
4328 status = PSA_ERROR_NOT_SUPPORTED;
4329 }
4330#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004331 if (status == PSA_SUCCESS) {
4332 status = psa_generate_derived_key_internal(slot,
4333 attributes->core.bits,
4334 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004335 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004336 if (status == PSA_SUCCESS) {
4337 status = psa_finish_key_creation(slot, driver, key);
4338 }
4339 if (status != PSA_SUCCESS) {
4340 psa_fail_key_creation(slot, driver);
4341 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004343 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004344}
4345
Gilles Peskineeab56e42018-07-12 17:12:33 +02004346
4347
4348/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004349/* Key derivation */
4350/****************************************************************/
4351
Steven Cooreman932ffb72021-02-15 12:14:32 +01004352#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004353static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004354{
4355#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004356 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4357 return 1;
4358 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004359#endif
4360#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004361 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4362 return 1;
4363 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004364#endif
4365#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004366 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4367 return 1;
4368 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004369#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004370 return 0;
Gilles Peskinecdacf042021-04-29 21:10:00 +02004371}
4372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004373static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004374{
4375 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004376 psa_status_t status = psa_hash_setup(&operation, alg);
4377 psa_hash_abort(&operation);
4378 return status;
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004379}
4380
Gilles Peskine969c5d62019-01-16 15:53:06 +01004381static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004382 psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004383 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004384{
Janos Follath5fe19732019-06-20 15:09:30 +01004385 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4386 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004387 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01004388
Gilles Peskine969c5d62019-01-16 15:53:06 +01004389 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004390 if (!is_kdf_alg_supported(kdf_alg)) {
4391 return PSA_ERROR_NOT_SUPPORTED;
4392 }
John Durkop07cc04a2020-11-16 22:08:34 -08004393
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004394 /* All currently supported key derivation algorithms are based on a
4395 * hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004396 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4397 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
4398 if (hash_size == 0) {
4399 return PSA_ERROR_NOT_SUPPORTED;
4400 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004401
4402 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4403 * we might fail later, which is somewhat unfriendly and potentially
4404 * risk-prone. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004405 psa_status_t status = psa_hash_try_support(hash_alg);
4406 if (status != PSA_SUCCESS) {
4407 return status;
4408 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004409
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004410 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4411 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
4412 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4413 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004414 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004415
Gilles Peskinecdacf042021-04-29 21:10:00 +02004416 operation->capacity = 255 * hash_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004417 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004418}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004419
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004420static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine739c98c2021-04-29 21:34:33 +02004421{
4422#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004423 if (alg == PSA_ALG_ECDH) {
4424 return PSA_SUCCESS;
4425 }
Gilles Peskine739c98c2021-04-29 21:34:33 +02004426#endif
4427 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004428 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine739c98c2021-04-29 21:34:33 +02004429}
John Durkop07cc04a2020-11-16 22:08:34 -08004430#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004431
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004432psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
4433 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004434{
4435 psa_status_t status;
4436
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004437 if (operation->alg != 0) {
4438 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004439 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004441 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
4442 return PSA_ERROR_INVALID_ARGUMENT;
4443 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
4444#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4445 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
4446 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
4447 status = psa_key_agreement_try_support(ka_alg);
4448 if (status != PSA_SUCCESS) {
4449 return status;
4450 }
4451 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
4452#else
4453 return PSA_ERROR_NOT_SUPPORTED;
4454#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4455 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
4456#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4457 status = psa_key_derivation_setup_kdf(operation, alg);
4458#else
4459 return PSA_ERROR_NOT_SUPPORTED;
4460#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4461 } else {
4462 return PSA_ERROR_INVALID_ARGUMENT;
4463 }
4464
4465 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004466 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004467 }
4468 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004469}
4470
John Durkopd0321952020-10-29 21:37:36 -07004471#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004472static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
4473 psa_algorithm_t hash_alg,
4474 psa_key_derivation_step_t step,
4475 const uint8_t *data,
4476 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004477{
4478 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004479 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02004480 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004481 if (hkdf->state != HKDF_STATE_INIT) {
4482 return PSA_ERROR_BAD_STATE;
4483 } else {
4484 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4485 hash_alg,
4486 data, data_length);
4487 if (status != PSA_SUCCESS) {
4488 return status;
4489 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02004490 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004491 return PSA_SUCCESS;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004492 }
Gilles Peskine03410b52019-05-16 16:05:19 +02004493 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004494 /* If no salt was provided, use an empty salt. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004495 if (hkdf->state == HKDF_STATE_INIT) {
4496 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4497 hash_alg,
4498 NULL, 0);
4499 if (status != PSA_SUCCESS) {
4500 return status;
4501 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004502 hkdf->state = HKDF_STATE_STARTED;
4503 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004504 if (hkdf->state != HKDF_STATE_STARTED) {
4505 return PSA_ERROR_BAD_STATE;
4506 }
4507 status = psa_mac_update(&hkdf->hmac,
4508 data, data_length);
4509 if (status != PSA_SUCCESS) {
4510 return status;
4511 }
4512 status = psa_mac_sign_finish(&hkdf->hmac,
4513 hkdf->prk,
4514 sizeof(hkdf->prk),
4515 &data_length);
4516 if (status != PSA_SUCCESS) {
4517 return status;
4518 }
4519 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Gilles Peskine2b522db2019-04-12 15:11:49 +02004520 hkdf->block_number = 0;
4521 hkdf->state = HKDF_STATE_KEYED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004522 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02004523 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004524 if (hkdf->state == HKDF_STATE_OUTPUT) {
4525 return PSA_ERROR_BAD_STATE;
4526 }
4527 if (hkdf->info_set) {
4528 return PSA_ERROR_BAD_STATE;
4529 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004530 hkdf->info_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004531 if (data_length != 0) {
4532 hkdf->info = mbedtls_calloc(1, data_length);
4533 if (hkdf->info == NULL) {
4534 return PSA_ERROR_INSUFFICIENT_MEMORY;
4535 }
4536 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004537 }
4538 hkdf->info_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004539 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004540 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004541 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004542 }
4543}
John Durkop07cc04a2020-11-16 22:08:34 -08004544#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004545
John Durkop07cc04a2020-11-16 22:08:34 -08004546#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4547 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004548static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
4549 const uint8_t *data,
4550 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01004551{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004552 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
4553 return PSA_ERROR_BAD_STATE;
4554 }
Janos Follathf08e2652019-06-13 09:05:41 +01004555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004556 if (data_length != 0) {
4557 prf->seed = mbedtls_calloc(1, data_length);
4558 if (prf->seed == NULL) {
4559 return PSA_ERROR_INSUFFICIENT_MEMORY;
4560 }
Janos Follathf08e2652019-06-13 09:05:41 +01004561
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004562 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004563 prf->seed_length = data_length;
4564 }
Janos Follathf08e2652019-06-13 09:05:41 +01004565
Gilles Peskine43f958b2020-12-13 14:55:14 +01004566 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01004567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004568 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01004569}
4570
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004571static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
4572 const uint8_t *data,
4573 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01004574{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004575 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
4576 return PSA_ERROR_BAD_STATE;
4577 }
Janos Follath81550542019-06-13 14:26:34 +01004578
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004579 if (data_length != 0) {
4580 prf->secret = mbedtls_calloc(1, data_length);
4581 if (prf->secret == NULL) {
4582 return PSA_ERROR_INSUFFICIENT_MEMORY;
4583 }
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004585 memcpy(prf->secret, data, data_length);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004586 prf->secret_length = data_length;
4587 }
Janos Follath81550542019-06-13 14:26:34 +01004588
Gilles Peskine43f958b2020-12-13 14:55:14 +01004589 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01004590
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004591 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01004592}
4593
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004594static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
4595 const uint8_t *data,
4596 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01004597{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004598 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
4599 return PSA_ERROR_BAD_STATE;
4600 }
Janos Follath63028dd2019-06-13 09:15:47 +01004601
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004602 if (data_length != 0) {
4603 prf->label = mbedtls_calloc(1, data_length);
4604 if (prf->label == NULL) {
4605 return PSA_ERROR_INSUFFICIENT_MEMORY;
4606 }
Janos Follath63028dd2019-06-13 09:15:47 +01004607
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004608 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004609 prf->label_length = data_length;
4610 }
Janos Follath63028dd2019-06-13 09:15:47 +01004611
Gilles Peskine43f958b2020-12-13 14:55:14 +01004612 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01004613
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004614 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01004615}
4616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004617static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
4618 psa_key_derivation_step_t step,
4619 const uint8_t *data,
4620 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01004621{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004622 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01004623 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004624 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01004625 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004626 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01004627 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004628 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01004629 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004630 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01004631 }
4632}
John Durkop07cc04a2020-11-16 22:08:34 -08004633#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4634 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
4635
4636#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4637static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4638 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08004639 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004640 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08004641{
4642 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004643 uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE];
John Durkop07cc04a2020-11-16 22:08:34 -08004644 uint8_t *cur = pms;
4645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004646 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
4647 return PSA_ERROR_INVALID_ARGUMENT;
4648 }
John Durkop07cc04a2020-11-16 22:08:34 -08004649
4650 /* Quoting RFC 4279, Section 2:
4651 *
4652 * The premaster secret is formed as follows: if the PSK is N octets
4653 * long, concatenate a uint16 with the value N, N zero octets, a second
4654 * uint16 with the value N, and the PSK itself.
4655 */
4656
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004657 *cur++ = MBEDTLS_BYTE_1(data_length);
4658 *cur++ = MBEDTLS_BYTE_0(data_length);
4659 memset(cur, 0, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004660 cur += data_length;
4661 *cur++ = pms[0];
4662 *cur++ = pms[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004663 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004664 cur += data_length;
4665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004666 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08004667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004668 mbedtls_platform_zeroize(pms, sizeof(pms));
4669 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08004670}
Janos Follath6660f0e2019-06-17 08:44:03 +01004671
4672static psa_status_t psa_tls12_prf_psk_to_ms_input(
4673 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004674 psa_key_derivation_step_t step,
4675 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004676 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01004677{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004678 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
4679 return psa_tls12_prf_psk_to_ms_set_key(prf,
4680 data, data_length);
Janos Follath0c1ed842019-06-28 13:35:36 +01004681 }
Janos Follath6660f0e2019-06-17 08:44:03 +01004682
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004683 return psa_tls12_prf_input(prf, step, data, data_length);
Janos Follath6660f0e2019-06-17 08:44:03 +01004684}
John Durkop07cc04a2020-11-16 22:08:34 -08004685#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004686
Gilles Peskineb8965192019-09-24 16:21:10 +02004687/** Check whether the given key type is acceptable for the given
4688 * input step of a key derivation.
4689 *
4690 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
4691 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
4692 * Both secret and non-secret inputs can alternatively have the type
4693 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
4694 * that the input was passed as a buffer rather than via a key object.
4695 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02004696static int psa_key_derivation_check_input_type(
4697 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004698 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02004699{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004700 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004701 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004702 if (key_type == PSA_KEY_TYPE_DERIVE) {
4703 return PSA_SUCCESS;
4704 }
4705 if (key_type == PSA_KEY_TYPE_NONE) {
4706 return PSA_SUCCESS;
4707 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004708 break;
4709 case PSA_KEY_DERIVATION_INPUT_LABEL:
4710 case PSA_KEY_DERIVATION_INPUT_SALT:
4711 case PSA_KEY_DERIVATION_INPUT_INFO:
4712 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004713 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
4714 return PSA_SUCCESS;
4715 }
4716 if (key_type == PSA_KEY_TYPE_NONE) {
4717 return PSA_SUCCESS;
4718 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004719 break;
4720 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004721 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02004722}
4723
Janos Follathb80a94e2019-06-12 15:54:46 +01004724static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004725 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004726 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004727 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004728 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004729 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004730{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004731 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004732 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004733
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004734 status = psa_key_derivation_check_input_type(step, key_type);
4735 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004736 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004737 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004738
John Durkopd0321952020-10-29 21:37:36 -07004739#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004740 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4741 status = psa_hkdf_input(&operation->ctx.hkdf,
4742 PSA_ALG_HKDF_GET_HASH(kdf_alg),
4743 step, data, data_length);
4744 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004745#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4746#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004747 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4748 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
4749 step, data, data_length);
4750 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004751#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
4752#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004753 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4754 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
4755 step, data, data_length);
4756 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004757#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004758 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004759 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01004760 (void) data;
4761 (void) data_length;
4762 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004763 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004764 }
4765
Gilles Peskine224b0d62019-09-23 18:13:17 +02004766exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004767 if (status != PSA_SUCCESS) {
4768 psa_key_derivation_abort(operation);
4769 }
4770 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004771}
4772
Janos Follath51f4a0f2019-06-14 11:35:55 +01004773psa_status_t psa_key_derivation_input_bytes(
4774 psa_key_derivation_operation_t *operation,
4775 psa_key_derivation_step_t step,
4776 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004777 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004778{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004779 return psa_key_derivation_input_internal(operation, step,
4780 PSA_KEY_TYPE_NONE,
4781 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004782}
4783
Janos Follath51f4a0f2019-06-14 11:35:55 +01004784psa_status_t psa_key_derivation_input_key(
4785 psa_key_derivation_operation_t *operation,
4786 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004787 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004788{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004789 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004790 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004791 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004792
Ronald Cron5c522922020-11-14 16:35:34 +01004793 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004794 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
4795 if (status != PSA_SUCCESS) {
4796 psa_key_derivation_abort(operation);
4797 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02004798 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004799
4800 /* Passing a key object as a SECRET input unlocks the permission
4801 * to output to a key object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004802 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004803 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004804 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004805
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004806 status = psa_key_derivation_input_internal(operation,
4807 step, slot->attr.type,
4808 slot->key.data,
4809 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004810
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004811 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004812
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004813 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004814}
Gilles Peskineea0fb492018-07-12 17:17:20 +02004815
4816
4817
4818/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004819/* Key agreement */
4820/****************************************************************/
4821
John Durkop6ba40d12020-11-10 08:50:04 -08004822#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004823static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key,
4824 size_t peer_key_length,
4825 const mbedtls_ecp_keypair *our_key,
4826 uint8_t *shared_secret,
4827 size_t shared_secret_size,
4828 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004829{
Steven Cooremand4867872020-08-05 16:31:39 +02004830 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004831 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004832 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004833 size_t bits = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004834 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits);
4835 mbedtls_ecdh_init(&ecdh);
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004836
Ronald Cron90857082020-11-25 14:58:33 +01004837 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004838 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
4839 bits,
4840 peer_key,
4841 peer_key_length,
4842 &their_key);
4843 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004844 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004845 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004846
Jaeden Amero97271b32019-01-10 19:38:51 +00004847 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004848 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
4849 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004850 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004851 }
Jaeden Amero97271b32019-01-10 19:38:51 +00004852 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004853 mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS));
4854 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004855 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004856 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004857
Jaeden Amero97271b32019-01-10 19:38:51 +00004858 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004859 mbedtls_ecdh_calc_secret(&ecdh,
4860 shared_secret_length,
4861 shared_secret, shared_secret_size,
4862 mbedtls_psa_get_random,
4863 MBEDTLS_PSA_RANDOM_STATE));
4864 if (status != PSA_SUCCESS) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004865 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004866 }
4867 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004868 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004869 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004870
4871exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004872 if (status != PSA_SUCCESS) {
4873 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
4874 }
4875 mbedtls_ecdh_free(&ecdh);
4876 mbedtls_ecp_keypair_free(their_key);
4877 mbedtls_free(their_key);
Steven Cooremana2371e52020-07-28 14:30:39 +02004878
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004879 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004880}
John Durkop6ba40d12020-11-10 08:50:04 -08004881#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004882
Gilles Peskine01d718c2018-09-18 12:01:02 +02004883#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4884
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004885static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
4886 psa_key_slot_t *private_key,
4887 const uint8_t *peer_key,
4888 size_t peer_key_length,
4889 uint8_t *shared_secret,
4890 size_t shared_secret_size,
4891 size_t *shared_secret_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02004892{
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004893 mbedtls_ecp_keypair *ecp = NULL;
4894 psa_status_t status;
Agathiyan Bragadeeshe7eb8052023-07-31 16:16:38 +01004895
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004896 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08004897#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02004898 case PSA_ALG_ECDH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004899 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) {
4900 return PSA_ERROR_INVALID_ARGUMENT;
4901 }
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004902 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004903 private_key->attr.type,
4904 private_key->attr.bits,
4905 private_key->key.data,
4906 private_key->key.bytes,
4907 &ecp);
4908 if (status != PSA_SUCCESS) {
4909 return status;
4910 }
4911 status = psa_key_agreement_ecdh(peer_key, peer_key_length,
4912 ecp,
4913 shared_secret, shared_secret_size,
4914 shared_secret_length);
4915 mbedtls_ecp_keypair_free(ecp);
4916 mbedtls_free(ecp);
4917 return status;
John Durkop6ba40d12020-11-10 08:50:04 -08004918#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004919 default:
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004920 (void) ecp;
4921 (void) status;
Gilles Peskine93f85002018-11-16 16:43:31 +01004922 (void) private_key;
4923 (void) peer_key;
4924 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004925 (void) shared_secret;
4926 (void) shared_secret_size;
4927 (void) shared_secret_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004928 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004929 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02004930}
4931
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004932/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02004933 * to potentially free embedded data structures and wipe confidential data.
4934 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004935static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
4936 psa_key_derivation_step_t step,
4937 psa_key_slot_t *private_key,
4938 const uint8_t *peer_key,
4939 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02004940{
4941 psa_status_t status;
4942 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4943 size_t shared_secret_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004944 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02004945
4946 /* Step 1: run the secret agreement algorithm to generate the shared
4947 * secret. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004948 status = psa_key_agreement_raw_internal(ka_alg,
4949 private_key,
4950 peer_key, peer_key_length,
4951 shared_secret,
4952 sizeof(shared_secret),
4953 &shared_secret_length);
4954 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02004955 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004956 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02004957
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004958 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02004959 * the shared secret. A shared secret is permitted wherever a key
4960 * of type DERIVE is permitted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004961 status = psa_key_derivation_input_internal(operation, step,
4962 PSA_KEY_TYPE_DERIVE,
4963 shared_secret,
4964 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02004965exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004966 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
4967 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004968}
4969
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004970psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
4971 psa_key_derivation_step_t step,
4972 mbedtls_svc_key_id_t private_key,
4973 const uint8_t *peer_key,
4974 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02004975{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004976 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004977 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004978 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004979
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004980 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4981 return PSA_ERROR_INVALID_ARGUMENT;
4982 }
Ronald Cron5c522922020-11-14 16:35:34 +01004983 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004984 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
4985 if (status != PSA_SUCCESS) {
4986 return status;
4987 }
4988 status = psa_key_agreement_internal(operation, step,
4989 slot,
4990 peer_key, peer_key_length);
4991 if (status != PSA_SUCCESS) {
4992 psa_key_derivation_abort(operation);
4993 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004994 /* If a private key has been added as SECRET, we allow the derived
4995 * key material to be used as a key in PSA Crypto. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004996 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004997 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004998 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004999 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005000
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005001 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005002
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005003 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005004}
5005
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005006psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
5007 mbedtls_svc_key_id_t private_key,
5008 const uint8_t *peer_key,
5009 size_t peer_key_length,
5010 uint8_t *output,
5011 size_t output_size,
5012 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005013{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005014 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005015 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005016 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005017 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005018
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005019 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005020 status = PSA_ERROR_INVALID_ARGUMENT;
5021 goto exit;
5022 }
Ronald Cron5c522922020-11-14 16:35:34 +01005023 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005024 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
5025 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005026 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005027 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005028
Gilles Peskine42313fb2022-04-14 00:17:15 +02005029 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5030 * for the output size. The PSA specification only guarantees that this
5031 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5032 * but it might be nice to allow smaller buffers if the output fits.
5033 * At the time of writing this comment, with only ECDH implemented,
5034 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5035 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5036 * be exact for it as well. */
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005037 expected_length =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005038 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
5039 if (output_size < expected_length) {
Gilles Peskine42313fb2022-04-14 00:17:15 +02005040 status = PSA_ERROR_BUFFER_TOO_SMALL;
5041 goto exit;
5042 }
5043
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005044 status = psa_key_agreement_raw_internal(alg, slot,
5045 peer_key, peer_key_length,
5046 output, output_size,
5047 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005048
5049exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005050 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005051 /* If an error happens and is not handled properly, the output
5052 * may be used as a key to protect sensitive data. Arrange for such
5053 * a key to be random, which is likely to result in decryption or
5054 * verification errors. This is better than filling the buffer with
5055 * some constant data such as zeros, which would result in the data
5056 * being protected with a reproducible, easily knowable key.
5057 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005058 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005059 *output_length = output_size;
5060 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005062 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005063
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005064 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005065}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005066
5067
Gilles Peskine30524eb2020-11-13 17:02:26 +01005068
Gilles Peskine86a440b2018-11-12 18:39:40 +01005069/****************************************************************/
5070/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005071/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005072
Gilles Peskine9a9d5ee2023-04-28 21:00:28 +02005073#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5074#include "mbedtls/entropy_poll.h"
5075#endif
5076
Gilles Peskine30524eb2020-11-13 17:02:26 +01005077/** Initialize the PSA random generator.
5078 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005079static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005080{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005081#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005082 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005083#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5084
Gilles Peskine30524eb2020-11-13 17:02:26 +01005085 /* Set default configuration if
5086 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005087 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005088 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005089 }
5090 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005091 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005092 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005094 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005095#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5096 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5097 /* The PSA entropy injection feature depends on using NV seed as an entropy
5098 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005099 mbedtls_entropy_add_source(&rng->entropy,
5100 mbedtls_nv_seed_poll, NULL,
5101 MBEDTLS_ENTROPY_BLOCK_SIZE,
5102 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005103#endif
5104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005105 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005106#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005107}
5108
5109/** Deinitialize the PSA random generator.
5110 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005111static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005112{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005113#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005114 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005115#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005116 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
5117 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005118#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005119}
5120
5121/** Seed the PSA random generator.
5122 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005123static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005124{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005125#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5126 /* Do nothing: the external RNG seeds itself. */
5127 (void) rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005128 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005129#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005130 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005131 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
5132 drbg_seed, sizeof(drbg_seed) - 1);
5133 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005134#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005135}
5136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005137psa_status_t psa_generate_random(uint8_t *output,
5138 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005139{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005140 GUARD_MODULE_INITIALIZED;
5141
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005142#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5143
5144 size_t output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005145 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
5146 output, output_size,
5147 &output_length);
5148 if (status != PSA_SUCCESS) {
5149 return status;
5150 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005151 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00005152 * for the external RNG interface. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005153 if (output_length != output_size) {
5154 return PSA_ERROR_INSUFFICIENT_ENTROPY;
5155 }
5156 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005157
5158#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005160 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005161 size_t request_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005162 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5163 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5164 output_size);
5165 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
5166 output, request_size);
5167 if (ret != 0) {
5168 return mbedtls_to_psa_error(ret);
5169 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01005170 output_size -= request_size;
5171 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005172 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005173 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005174#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005175}
5176
Gilles Peskine8814fc42020-12-14 15:33:44 +01005177/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005178 *
5179 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5180 * `psa_generate_random(...)`. The state parameter is ignored since the
5181 * PSA API doesn't support passing an explicit state.
5182 *
5183 * In the non-external case, psa_generate_random() calls an
5184 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5185 * and semantics as mbedtls_psa_get_random(). As an optimization,
5186 * instead of doing this back-and-forth between the PSA API and the
5187 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5188 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005189 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005190#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5191int mbedtls_psa_get_random(void *p_rng,
5192 unsigned char *output,
5193 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01005194{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005195 /* This function takes a pointer to the RNG state because that's what
5196 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5197 * its own state internally and doesn't let the caller access that state.
5198 * So we just ignore the state parameter, and in practice we'll pass
5199 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005200 (void) p_rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005201 psa_status_t status = psa_generate_random(output, output_size);
5202 if (status == PSA_SUCCESS) {
5203 return 0;
5204 } else {
5205 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
5206 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01005207}
5208#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5209
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005210#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005211psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
5212 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005213{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005214 if (global_data.initialized) {
5215 return PSA_ERROR_NOT_PERMITTED;
5216 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005218 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
5219 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
5220 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
5221 return PSA_ERROR_INVALID_ARGUMENT;
5222 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005223
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005224 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005225}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005226#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005227
Ronald Cron3772afe2021-02-08 16:10:05 +01005228/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005229 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005230 * \param type The key type
5231 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005232 *
5233 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005234 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005235 * \retval #PSA_ERROR_INVALID_ARGUMENT
5236 * The size in bits of the key is not valid.
5237 * \retval #PSA_ERROR_NOT_SUPPORTED
5238 * The type and/or the size in bits of the key or the combination of
5239 * the two is not supported.
5240 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005241static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005242 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005243{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005244 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005246 if (key_type_is_raw_bytes(type)) {
5247 status = validate_unstructured_key_bit_size(type, bits);
5248 if (status != PSA_SUCCESS) {
5249 return status;
5250 }
5251 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005252#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005253 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5254 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
5255 return PSA_ERROR_NOT_SUPPORTED;
5256 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005257
Ronald Cron01b2aba2020-10-05 09:42:02 +02005258 /* Accept only byte-aligned keys, for the same reasons as
5259 * in psa_import_rsa_key(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005260 if (bits % 8 != 0) {
5261 return PSA_ERROR_NOT_SUPPORTED;
5262 }
5263 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005264#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005265
Ronald Cron5c4d3862020-12-07 11:07:24 +01005266#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005267 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01005268 /* To avoid empty block, return successfully here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005269 return PSA_SUCCESS;
5270 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005271#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005272 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005273 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005274 }
5275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005276 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005277}
5278
Ronald Cron55ed0592020-10-05 10:30:40 +02005279psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005280 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005281 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005282{
5283 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005284 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005286 if ((attributes->domain_parameters == NULL) &&
5287 (attributes->domain_parameters_size != 0)) {
5288 return PSA_ERROR_INVALID_ARGUMENT;
5289 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02005290
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005291 if (key_type_is_raw_bytes(type)) {
5292 status = psa_generate_random(key_buffer, key_buffer_size);
5293 if (status != PSA_SUCCESS) {
5294 return status;
5295 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005296
David Brown12ca5032021-02-11 11:02:00 -07005297#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005298 if (type == PSA_KEY_TYPE_DES) {
5299 psa_des_set_key_parity(key_buffer, key_buffer_size);
5300 }
David Brown8107e312021-02-12 08:08:46 -07005301#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005302 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01005303
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005304#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5305 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005306 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
5307 return mbedtls_psa_rsa_generate_key(attributes,
5308 key_buffer,
5309 key_buffer_size,
5310 key_buffer_length);
5311 } else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005312#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5313 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005314
John Durkop9814fa22020-11-04 12:28:15 -08005315#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005316 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5317 return mbedtls_psa_ecp_generate_key(attributes,
5318 key_buffer,
5319 key_buffer_size,
5320 key_buffer_length);
5321 } else
John Durkop9814fa22020-11-04 12:28:15 -08005322#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005323 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005324 (void) key_buffer_length;
5325 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02005326 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005327
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005328 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005329}
Darryl Green0c6575a2018-11-07 16:05:30 +00005330
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005331psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
5332 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005333{
5334 psa_status_t status;
5335 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005336 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005337 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005338
Ronald Cron81709fc2020-11-14 12:10:32 +01005339 *key = MBEDTLS_SVC_KEY_ID_INIT;
5340
Gilles Peskine0f84d622019-09-12 19:03:13 +02005341 /* Reject any attempt to create a zero-length key so that we don't
5342 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005343 if (psa_get_key_bits(attributes) == 0) {
5344 return PSA_ERROR_INVALID_ARGUMENT;
5345 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005346
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005347 /* Reject any attempt to create a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005348 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
5349 return PSA_ERROR_INVALID_ARGUMENT;
5350 }
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005351
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005352 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
5353 &slot, &driver);
5354 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02005355 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005356 }
Gilles Peskine11792082019-08-06 18:36:36 +02005357
Ronald Cron977c2472020-10-13 08:32:21 +02005358 /* In the case of a transparent key or an opaque key stored in local
5359 * storage (thus not in the case of generating a key in a secure element
5360 * or cryptoprocessor with storage), we have to allocate a buffer to
5361 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005362 if (slot->key.data == NULL) {
5363 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
5364 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005365 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005366 attributes->core.type, attributes->core.bits);
5367 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005368 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005369 }
Ronald Cron3772afe2021-02-08 16:10:05 +01005370
5371 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005372 attributes->core.type,
5373 attributes->core.bits);
5374 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02005375 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005376 attributes, &key_buffer_size);
5377 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005378 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005379 }
Ronald Cron977c2472020-10-13 08:32:21 +02005380 }
Ronald Cron977c2472020-10-13 08:32:21 +02005381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005382 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
5383 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02005384 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005385 }
Ronald Cron977c2472020-10-13 08:32:21 +02005386 }
5387
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005388 status = psa_driver_wrapper_generate_key(attributes,
5389 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02005390
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005391 if (status != PSA_SUCCESS) {
5392 psa_remove_key_data_from_memory(slot);
5393 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02005394
Gilles Peskine11792082019-08-06 18:36:36 +02005395exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005396 if (status == PSA_SUCCESS) {
5397 status = psa_finish_key_creation(slot, driver, key);
5398 }
5399 if (status != PSA_SUCCESS) {
5400 psa_fail_key_creation(slot, driver);
5401 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005402
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005403 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005404}
5405
Gilles Peskinee59236f2018-01-27 23:32:46 +01005406/****************************************************************/
5407/* Module setup */
5408/****************************************************************/
5409
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005410#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005411psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005412 void (* entropy_init)(mbedtls_entropy_context *ctx),
5413 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01005414{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005415 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5416 return PSA_ERROR_BAD_STATE;
5417 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005418 global_data.rng.entropy_init = entropy_init;
5419 global_data.rng.entropy_free = entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005420 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01005421}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005422#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005423
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005424void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005425{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005426 psa_wipe_all_key_slots();
5427 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5428 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005429 }
5430 /* Wipe all remaining data, including configuration.
5431 * In particular, this sets all state indicator to the value
5432 * indicating "uninitialized". */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005433 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron088d5d02021-04-10 16:57:30 +02005434
5435 /* Terminate drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005436 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01005437}
5438
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005439#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5440/** Recover a transaction that was interrupted by a power failure.
5441 *
5442 * This function is called during initialization, before psa_crypto_init()
5443 * returns. If this function returns a failure status, the initialization
5444 * fails.
5445 */
5446static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005447 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005448{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005449 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005450 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5451 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005452 /* TODO - fall through to the failure case until this
5453 * is implemented.
5454 * https://github.com/ARMmbed/mbed-crypto/issues/218
5455 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005456 default:
5457 /* We found an unsupported transaction in the storage.
5458 * We don't know what state the storage is in. Give up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005459 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005460 }
5461}
5462#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005464psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005465{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005466 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005467
Gilles Peskinec6b69072018-11-20 21:42:52 +01005468 /* Double initialization is explicitly allowed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005469 if (global_data.initialized != 0) {
5470 return PSA_SUCCESS;
5471 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005472
Gilles Peskine30524eb2020-11-13 17:02:26 +01005473 /* Initialize and seed the random generator. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005474 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005475 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005476 status = mbedtls_psa_random_seed(&global_data.rng);
5477 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01005478 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005479 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005480 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005482 status = psa_initialize_key_slots();
5483 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01005484 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005485 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005486
Ronald Cron088d5d02021-04-10 16:57:30 +02005487 /* Init drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005488 status = psa_driver_wrapper_init();
5489 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02005490 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005491 }
Gilles Peskined9348f22019-10-01 15:22:29 +02005492
Gilles Peskine4b734222019-07-24 15:56:31 +02005493#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005494 status = psa_crypto_load_transaction();
5495 if (status == PSA_SUCCESS) {
5496 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
5497 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005498 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005499 }
5500 status = psa_crypto_stop_transaction();
5501 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02005502 /* There's no transaction to complete. It's all good. */
5503 status = PSA_SUCCESS;
5504 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005505#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005506
Gilles Peskinec6b69072018-11-20 21:42:52 +01005507 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005508 global_data.initialized = 1;
5509
5510exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005511 if (status != PSA_SUCCESS) {
5512 mbedtls_psa_crypto_free();
5513 }
5514 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005515}
5516
5517#endif /* MBEDTLS_PSA_CRYPTO_C */