blob: 894167abdbdd7af32a990ecfed3dfb9b110d8e99 [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 Rodgman16799db2023-11-02 19:47:20 +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"
Ronald Cronafbc7ed2023-01-24 16:02:04 +010010#include "psa_crypto_core_common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010011
12#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030013
John Durkop07cc04a2020-11-16 22:08:34 -080014#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
15#include "check_crypto_config.h"
16#endif
17
Gilles Peskinee59236f2018-01-27 23:32:46 +010018#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010019#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010020
Ronald Crond6d28882020-12-14 14:56:02 +010021#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010022#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010023#include "psa_crypto_invasive.h"
Xiaokang Qiane9c39c42023-08-09 08:50:19 +000024#include "psa_crypto_driver_wrappers.h"
Xiaokang Qianfe9666b2023-09-11 10:36:20 +000025#include "psa_crypto_driver_wrappers_no_static.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010026#include "psa_crypto_ecp.h"
Przemek Stekiel359f4622022-12-05 14:11:55 +010027#include "psa_crypto_ffdh.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010028#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010029#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010030#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010031#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020032#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020033#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020034#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010035#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010036/* Include internal declarations that are useful for implementing persistently
37 * stored keys. */
38#include "psa_crypto_storage.h"
39
Gilles Peskineb2b64d32020-12-14 16:43:58 +010040#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010041
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010042#include <stdlib.h>
43#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010046#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020047#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000048#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020049#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010050#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020051#include "mbedtls/chacha20.h"
52#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/cipher.h"
54#include "mbedtls/ccm.h"
55#include "mbedtls/cmac.h"
Dave Rodgman78701152023-08-29 14:20:18 +010056#include "mbedtls/constant_time.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010057#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020058#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010059#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010060#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/error.h"
62#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/md5.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010064#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000065#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010066#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000067#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010069#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/sha1.h"
71#include "mbedtls/sha256.h"
72#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020073#include "md_psa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010074
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020075#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
76 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
77 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020078#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020079#endif
80
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010081/****************************************************************/
82/* Global data, support functions and library management */
83/****************************************************************/
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020086{
Gilles Peskine449bd832023-01-11 14:50:10 +010087 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020088}
89
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010090/* Values for psa_global_data_t::rng_state */
91#define RNG_NOT_INITIALIZED 0
92#define RNG_INITIALIZED 1
93#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +010094
Gilles Peskine449bd832023-01-11 14:50:10 +010095typedef struct {
Dave Rodgman864f5942023-08-16 18:04:44 +010096 uint8_t initialized;
97 uint8_t rng_state;
98 uint8_t drivers_initialized;
Gilles Peskine2d8a1822021-11-08 22:20:03 +010099 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100100} psa_global_data_t;
101
102static psa_global_data_t global_data;
103
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100104#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
105mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
106 &global_data.rng.drbg;
107#endif
108
itayzafrir0adf0fc2018-09-06 16:24:41 +0300109#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if (global_data.initialized == 0) \
111 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300112
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100113int psa_can_do_hash(psa_algorithm_t hash_alg)
114{
115 (void) hash_alg;
116 return global_data.drivers_initialized;
117}
Valerio Settia55f0422023-07-10 15:34:41 +0200118#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200119 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200120 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200121static int psa_is_dh_key_size_valid(size_t bits)
122{
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200123 if (bits != 2048 && bits != 3072 && bits != 4096 &&
124 bits != 6144 && bits != 8192) {
125 return 0;
126 }
127
128 return 1;
129}
Valerio Settia55f0422023-07-10 15:34:41 +0200130#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200131 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200132 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100135{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100136 /* Mbed TLS error codes can combine a high-level error code and a
137 * low-level error code. The low-level error usually reflects the
138 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 int low_level_ret = -(-ret & 0x007f);
140 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100141 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100143
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100144#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100145 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
146 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100148 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
149 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100150#endif
151
152#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200153 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
154 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
155 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
156 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
157 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200159 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200161 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100163#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200164
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100165#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000166 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100167 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100169#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100170
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100171#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100172 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100176#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100177
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100178#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200179 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100181#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200182
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100183#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200184 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200186 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100188#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200189
Dave Rodgman509b5672023-08-16 19:26:23 +0100190#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100191 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100199 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100201 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100203 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100205#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
208 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100209 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
210 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100211 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
214 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100216 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100218#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100219
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100220#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100221 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100223#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100224
Gilles Peskinee59236f2018-01-27 23:32:46 +0100225 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
226 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
227 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100229
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100230#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100231 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200233 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100235 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100237#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100238
Gilles Peskine82e57d12020-11-13 21:31:17 +0100239#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100241 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
242 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100243 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100245 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
246 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100248 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100250#endif
251
Dave Rodgmandea266f2023-08-31 11:52:43 +0100252#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100253 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100255 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100257 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100259#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100260 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100262#endif
263#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100264
Dave Rodgman509b5672023-08-16 19:26:23 +0100265#if defined(MBEDTLS_BIGNUM_C)
266#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100267 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100269#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100270 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100272 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100274 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100276 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100278 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100280 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100282 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100284#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100285
Dave Rodgman509b5672023-08-16 19:26:23 +0100286#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100287 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100289 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
290 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100292#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
293 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100294 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100296#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100297 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
298 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100300 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100302 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
303 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100305 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100307 case MBEDTLS_ERR_PK_INVALID_ALG:
308 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
309 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100311 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200313 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100315#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100316
Gilles Peskineff2d2002019-05-06 15:26:23 +0200317 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200319 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200321
Dave Rodgman509b5672023-08-16 19:26:23 +0100322#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100323 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100325 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100327 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100329 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100331 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
332 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100334 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100336 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100338 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100340#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200341
Dave Rodgman1dab4452023-09-01 09:59:51 +0100342#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300343 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300344 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300346 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300348 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300350 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
351 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300353 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100355 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100357
Dave Rodgman509b5672023-08-16 19:26:23 +0100358#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000359 case MBEDTLS_ERR_ECP_IN_PROGRESS:
360 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100361#endif
362#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000363
Janos Follatha13b9052019-11-22 12:48:59 +0000364 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300366
Gilles Peskinee59236f2018-01-27 23:32:46 +0100367 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100369 }
370}
371
Paul Elliottdc42ca82023-02-24 18:11:59 +0000372/**
373 * \brief For output buffers which contain "tags"
374 * (outputs that may be checked for validity like
375 * hashes, MACs and signatures), fill the unused
376 * part of the output buffer (the whole buffer on
377 * error, the trailing part on success) with
378 * something that isn't a valid tag (barring an
379 * attack on the tag and deliberately-crafted
380 * input), in case the caller doesn't check the
381 * return status properly.
382 *
383 * \param output_buffer Pointer to buffer to wipe. May not be NULL
384 * unless \p output_buffer_size is zero.
385 * \param status Status of function called to generate
386 * output_buffer originally
387 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
388 * could be NULL.
389 * \param output_buffer_length Length of data written to output_buffer, must be
390 * less than \p output_buffer_size
391 */
392static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000393 size_t output_buffer_size, size_t output_buffer_length)
394{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000395 size_t offset = 0;
396
397 if (output_buffer_size == 0) {
398 /* If output_buffer_size is 0 then we have nothing to do. We must not
399 call memset because output_buffer may be NULL in this case */
400 return;
401 }
402
403 if (status == PSA_SUCCESS) {
404 offset = output_buffer_length;
405 }
406
407 memset(output_buffer + offset, '!', output_buffer_size - offset);
408}
409
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200410
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200411
412
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100413/****************************************************************/
414/* Key management */
415/****************************************************************/
416
Valerio Settia9aab1a2023-06-19 13:39:54 +0200417#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200418psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
419 size_t *bits)
420{
421 switch (grpid) {
Valerio Settic437fae2023-09-08 14:58:03 +0200422#if defined(MBEDTLS_ECP_HAVE_SECP192R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200423 case MBEDTLS_ECP_DP_SECP192R1:
424 *bits = 192;
425 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100426#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200427#if defined(MBEDTLS_ECP_HAVE_SECP224R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200428 case MBEDTLS_ECP_DP_SECP224R1:
429 *bits = 224;
430 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100431#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200432#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200433 case MBEDTLS_ECP_DP_SECP256R1:
434 *bits = 256;
435 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100436#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200437#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200438 case MBEDTLS_ECP_DP_SECP384R1:
439 *bits = 384;
440 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100441#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200442#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200443 case MBEDTLS_ECP_DP_SECP521R1:
444 *bits = 521;
445 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100446#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200447#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200448 case MBEDTLS_ECP_DP_BP256R1:
449 *bits = 256;
450 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100451#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200452#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200453 case MBEDTLS_ECP_DP_BP384R1:
454 *bits = 384;
455 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100456#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200457#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200458 case MBEDTLS_ECP_DP_BP512R1:
459 *bits = 512;
460 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100461#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200462#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200463 case MBEDTLS_ECP_DP_CURVE25519:
464 *bits = 255;
465 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100466#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200467#if defined(MBEDTLS_ECP_HAVE_SECP192K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200468 case MBEDTLS_ECP_DP_SECP192K1:
469 *bits = 192;
470 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100471#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200472#if defined(MBEDTLS_ECP_HAVE_SECP224K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200473 case MBEDTLS_ECP_DP_SECP224K1:
474 *bits = 224;
475 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100476#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200477#if defined(MBEDTLS_ECP_HAVE_SECP256K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200478 case MBEDTLS_ECP_DP_SECP256K1:
479 *bits = 256;
480 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100481#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200482#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200483 case MBEDTLS_ECP_DP_CURVE448:
484 *bits = 448;
485 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100486#endif
Valerio Settibc2b1d32023-06-19 12:15:13 +0200487 default:
488 *bits = 0;
489 return 0;
490 }
491}
492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
494 size_t bits,
495 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200496{
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100498 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100500#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100501 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100503#endif
504#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100505 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100507#endif
508#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100509 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100511#endif
512#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100513 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100515#endif
516#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100517 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100519 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 if (bits_is_sloppy) {
521 return MBEDTLS_ECP_DP_SECP521R1;
522 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100523 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100524#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100525 }
526 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100527
Paul Elliott8ff510a2020-06-02 17:19:28 +0100528 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100530#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100531 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100533#endif
534#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100535 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100537#endif
538#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100539 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100541#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100542 }
543 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100544
Paul Elliott8ff510a2020-06-02 17:19:28 +0100545 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100547#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100548 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100550 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if (bits_is_sloppy) {
552 return MBEDTLS_ECP_DP_CURVE25519;
553 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100554 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100555#endif
556#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100557 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100559#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100560 }
561 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100562
Paul Elliott8ff510a2020-06-02 17:19:28 +0100563 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100565#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100566 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100568#endif
569#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100570 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100572#endif
573#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100574 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100576#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100577 }
578 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200579 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100580
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100581 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200583}
Valerio Settia9aab1a2023-06-19 13:39:54 +0200584#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
587 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200588{
589 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200591 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200592 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200593 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200594 case PSA_KEY_TYPE_PASSWORD:
595 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200596 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100597#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200598 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 if (bits != 128 && bits != 192 && bits != 256) {
600 return PSA_ERROR_INVALID_ARGUMENT;
601 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200602 break;
603#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200604#if defined(PSA_WANT_KEY_TYPE_ARIA)
605 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 if (bits != 128 && bits != 192 && bits != 256) {
607 return PSA_ERROR_INVALID_ARGUMENT;
608 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200609 break;
610#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100611#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200612 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 if (bits != 128 && bits != 192 && bits != 256) {
614 return PSA_ERROR_INVALID_ARGUMENT;
615 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200616 break;
617#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100618#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200619 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 if (bits != 64 && bits != 128 && bits != 192) {
621 return PSA_ERROR_INVALID_ARGUMENT;
622 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200623 break;
624#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100625#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200626 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 if (bits != 256) {
628 return PSA_ERROR_INVALID_ARGUMENT;
629 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200630 break;
631#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200632 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200634 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 if (bits % 8 != 0) {
636 return PSA_ERROR_INVALID_ARGUMENT;
637 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200640}
641
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100642/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100643 *
Steven Cooremancd640932021-03-08 12:00:27 +0100644 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
645 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100646 *
647 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100648 * \param[in] key_type The key type of the key to be used with the
649 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100650 *
651 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100652 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100653 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100654 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100655 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100656MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100657 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100659{
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 if (PSA_ALG_IS_HMAC(algorithm)) {
661 if (key_type == PSA_KEY_TYPE_HMAC) {
662 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100663 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100664 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
667 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
668 * key. */
669 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
670 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
671 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
672 * the block length (larger than 1) for block ciphers. */
673 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
674 return PSA_SUCCESS;
675 }
676 }
677 }
678
679 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100680}
681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
683 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200684{
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (slot->key.data != NULL) {
686 return PSA_ERROR_ALREADY_EXISTS;
687 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 slot->key.data = mbedtls_calloc(1, buffer_length);
690 if (slot->key.data == NULL) {
691 return PSA_ERROR_INSUFFICIENT_MEMORY;
692 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200693
Ronald Cronea0f8a62020-11-25 17:52:23 +0100694 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200696}
697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
699 const uint8_t *data,
700 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200701{
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 psa_status_t status = psa_allocate_buffer_to_slot(slot,
703 data_length);
704 if (status != PSA_SUCCESS) {
705 return status;
706 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200707
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 memcpy(slot->key.data, data, data_length);
709 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200710}
711
Ronald Crona0fe59f2020-11-29 11:14:53 +0100712psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100713 const psa_key_attributes_t *attributes,
714 const uint8_t *data, size_t data_length,
715 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100717{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100718 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
719 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100720
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200721 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 if (data_length == 0) {
723 return PSA_ERROR_NOT_SUPPORTED;
724 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200725
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 if (key_type_is_raw_bytes(type)) {
727 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
730 *bits);
731 if (status != PSA_SUCCESS) {
732 return status;
733 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200734
Ronald Crondd04d422020-11-28 15:14:42 +0100735 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100737 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 return PSA_SUCCESS;
741 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200742#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200743 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100744 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200745 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100746 return PSA_ERROR_INVALID_ARGUMENT;
747 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200748 return mbedtls_psa_ffdh_import_key(attributes,
749 data, data_length,
750 key_buffer, key_buffer_size,
751 key_buffer_length,
752 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100753 }
Valerio Settia55f0422023-07-10 15:34:41 +0200754#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200755 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200756#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
758 if (PSA_KEY_TYPE_IS_ECC(type)) {
759 return mbedtls_psa_ecp_import_key(attributes,
760 data, data_length,
761 key_buffer, key_buffer_size,
762 key_buffer_length,
763 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100764 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200765#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800766 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200767#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
768 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
770 if (PSA_KEY_TYPE_IS_RSA(type)) {
771 return mbedtls_psa_rsa_import_key(attributes,
772 data, data_length,
773 key_buffer, key_buffer_size,
774 key_buffer_length,
775 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200776 }
Valerio Settife478902023-07-25 12:27:19 +0200777#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
778 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800779 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100780 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100783}
784
Gilles Peskinef603c712019-01-19 13:40:11 +0100785/** Calculate the intersection of two algorithm usage policies.
786 *
787 * Return 0 (which allows no operation) on incompatibility.
788 */
789static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100790 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100791 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100793{
Gilles Peskine549ea862019-05-22 11:45:59 +0200794 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if (alg1 == alg2) {
796 return alg1;
797 }
Steven Cooreman03488022021-02-18 12:07:20 +0100798 /* If the policies are from the same hash-and-sign family, check
799 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
801 PSA_ALG_IS_SIGN_HASH(alg2) &&
802 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
803 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
804 return alg2;
805 }
806 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
807 return alg1;
808 }
Steven Cooreman03488022021-02-18 12:07:20 +0100809 }
810 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100811 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100812 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
814 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
815 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
816 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
817 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100818 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100819
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100820 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
822 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
823 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
824 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100825 }
826 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
828 (alg1_len <= alg2_len)) {
829 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100830 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
832 (alg2_len <= alg1_len)) {
833 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100834 }
835 }
836 /* If the policies are from the same MAC family, check whether one
837 * of them is a minimum-MAC-length policy. Calculate the most
838 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
840 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
841 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100842 /* Validate the combination of key type and algorithm. Since the base
843 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
845 return 0;
846 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100847
Steven Cooreman31a876d2021-03-03 20:47:40 +0100848 /* Get the (exact or at-least) output lengths for both sides of the
849 * requested intersection. None of the currently supported algorithms
850 * have an output length dependent on the actual key size, so setting it
851 * to a bogus value of 0 is currently OK.
852 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100853 * Note that for at-least-this-length wildcard algorithms, the output
854 * length is set to the shortest allowed length, which allows us to
855 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
857 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100858 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100859
Steven Cooremana1d83222021-02-25 10:20:29 +0100860 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
862 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
863 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100864 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100865
866 /* If only one is an at-least-this-length policy, the intersection would
867 * be the other (fixed-length) policy as long as said fixed length is
868 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
870 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100871 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
873 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100874 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100875
Steven Cooremancd640932021-03-08 12:00:27 +0100876 /* If none of them are wildcards, check whether they define the same tag
877 * length. This is still possible here when one is default-length and
878 * the other specific-length. Ensure to always return the
879 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 if (alg1_len == alg2_len) {
881 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
882 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100883 }
884 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100886}
887
Gilles Peskine449bd832023-01-11 14:50:10 +0100888static int psa_key_algorithm_permits(psa_key_type_t key_type,
889 psa_algorithm_t policy_alg,
890 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200891{
Gilles Peskine549ea862019-05-22 11:45:59 +0200892 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (requested_alg == policy_alg) {
894 return 1;
895 }
Steven Cooreman03488022021-02-18 12:07:20 +0100896 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
897 * and requested_alg is the same hash-and-sign family with any hash,
898 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
900 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
901 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
902 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100903 }
904 /* If policy_alg is a wildcard AEAD algorithm of the same base as
905 * the requested algorithm, check the requested tag length to be
906 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 if (PSA_ALG_IS_AEAD(policy_alg) &&
908 PSA_ALG_IS_AEAD(requested_alg) &&
909 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
910 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
911 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
912 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
913 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100914 }
Steven Cooremancd640932021-03-08 12:00:27 +0100915 /* If policy_alg is a MAC algorithm of the same base as the requested
916 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 if (PSA_ALG_IS_MAC(policy_alg) &&
918 PSA_ALG_IS_MAC(requested_alg) &&
919 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
920 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100921 /* Validate the combination of key type and algorithm. Since the policy
922 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
924 return 0;
925 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100926
Steven Cooreman31a876d2021-03-03 20:47:40 +0100927 /* Get both the requested output length for the algorithm which is to be
928 * verified, and the default output length for the base algorithm.
929 * Note that none of the currently supported algorithms have an output
930 * length dependent on actual key size, so setting it to a bogus value
931 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100932 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100934 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 key_type, 0,
936 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100937
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100938 /* If the policy is default-length, only allow an algorithm with
939 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
941 return requested_output_length == default_output_length;
942 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100943
944 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100945 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
947 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
948 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100949 }
950
Steven Cooremancd640932021-03-08 12:00:27 +0100951 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
952 * check for the requested MAC length to be equal to or longer than the
953 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
955 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
956 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100957 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200958 }
Steven Cooremance48e852020-10-05 16:02:45 +0200959 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200960 * a key derivation with that key agreement should also be allowed. This
961 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
963 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
964 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
965 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200966 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100967 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200969}
970
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100971/** Test whether a policy permits an algorithm.
972 *
973 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100974 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100975 * \note This function requires providing the key type for which the policy is
976 * being validated, since some algorithm policy definitions (e.g. MAC)
977 * have different properties depending on what kind of cipher it is
978 * combined with.
979 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100980 * \retval PSA_SUCCESS When \p alg is a specific algorithm
981 * allowed by the \p policy.
982 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
983 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
984 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100985 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100986static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
987 psa_key_type_t key_type,
988 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100989{
Steven Cooremand788fab2021-02-25 11:29:17 +0100990 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 if (alg == 0) {
992 return PSA_ERROR_INVALID_ARGUMENT;
993 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100994
Steven Cooreman7e39f052021-02-23 14:18:32 +0100995 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 if (PSA_ALG_IS_WILDCARD(alg)) {
997 return PSA_ERROR_INVALID_ARGUMENT;
998 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
1001 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
1002 return PSA_SUCCESS;
1003 } else {
1004 return PSA_ERROR_NOT_PERMITTED;
1005 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001006}
1007
Gilles Peskinef603c712019-01-19 13:40:11 +01001008/** Restrict a key policy based on a constraint.
1009 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001010 * \note This function requires providing the key type for which the policy is
1011 * being restricted, since some algorithm policy definitions (e.g. MAC)
1012 * have different properties depending on what kind of cipher it is
1013 * combined with.
1014 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001015 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +01001016 * \param[in,out] policy The policy to restrict.
1017 * \param[in] constraint The policy constraint to apply.
1018 *
1019 * \retval #PSA_SUCCESS
1020 * \c *policy contains the intersection of the original value of
1021 * \c *policy and \c *constraint.
1022 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001023 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001024 * \c *policy is unchanged.
1025 */
1026static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001027 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001028 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001030{
1031 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1033 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001034 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1036 constraint->alg2);
1037 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1038 return PSA_ERROR_INVALID_ARGUMENT;
1039 }
1040 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1041 return PSA_ERROR_INVALID_ARGUMENT;
1042 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001043 policy->usage &= constraint->usage;
1044 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001045 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001047}
1048
Przemek Stekiel061f6942022-11-30 10:51:35 +01001049/** Get the description of a key given its identifier and policy constraints
1050 * and lock it.
1051 *
1052 * The key must have allow all the usage flags set in \p usage. If \p alg is
1053 * nonzero, the key must allow operations with this algorithm. If \p alg is
1054 * zero, the algorithm is not checked.
1055 *
1056 * In case of a persistent key, the function loads the description of the key
1057 * into a key slot if not already done.
1058 *
1059 * On success, the returned key slot is locked. It is the responsibility of
1060 * the caller to unlock the key slot when it does not access it anymore.
1061 */
1062static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001063 mbedtls_svc_key_id_t key,
1064 psa_key_slot_t **p_slot,
1065 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001067{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001068 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001069 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 status = psa_get_and_lock_key_slot(key, p_slot);
1072 if (status != PSA_SUCCESS) {
1073 return status;
1074 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001075 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001076
1077 /* Enforce that usage policy for the key slot contains all the flags
1078 * required by the usage parameter. There is one exception: public
1079 * keys can always be exported, so we treat public key objects as
1080 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001082 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001086 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001087 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001088 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001089
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001090 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (alg != 0) {
1092 status = psa_key_policy_permits(&slot->attr.policy,
1093 slot->attr.type,
1094 alg);
1095 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001096 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001098 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001099
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001101
1102error:
1103 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001107}
Darryl Green940d72c2018-07-13 13:18:51 +01001108
Ronald Cron5c522922020-11-14 16:35:34 +01001109/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001110 *
1111 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001112 * available, as opposed to a key in a secure element and/or to be used
1113 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001114 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001115 * This is a temporary function that may be used instead of
1116 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1117 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001118 *
Ronald Cron5c522922020-11-14 16:35:34 +01001119 * On success, the returned key slot is locked. It is the responsibility of the
1120 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001121 */
Ronald Cron5c522922020-11-14 16:35:34 +01001122static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1123 mbedtls_svc_key_id_t key,
1124 psa_key_slot_t **p_slot,
1125 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001127{
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1129 usage, alg);
1130 if (status != PSA_SUCCESS) {
1131 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001132 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1135 psa_unlock_key_slot(*p_slot);
1136 *p_slot = NULL;
1137 return PSA_ERROR_NOT_SUPPORTED;
1138 }
1139
1140 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001141}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001144{
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001146 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001148
Ronald Cronea0f8a62020-11-25 17:52:23 +01001149 slot->key.data = NULL;
1150 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001153}
1154
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001155/** Completely wipe a slot in memory, including its policy.
1156 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001157psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001158{
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 /*
1162 * As the return error code may not be handled in case of multiple errors,
1163 * do our best to report an unexpected lock counter. Assert with
1164 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1165 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1166 * function is called as part of the execution of a test suite, the
1167 * execution of the test suite is stopped in error if the assertion fails.
1168 */
1169 if (slot->lock_count != 1) {
1170 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001171 status = PSA_ERROR_CORRUPTION_DETECTED;
1172 }
1173
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001174 /* Multipart operations may still be using the key. This is safe
1175 * because all multipart operation objects are independent from
1176 * the key slot: if they need to access the key after the setup
1177 * phase, they have a copy of the key. Note that this means that
1178 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001179 /* At this point, key material and other type-specific content has
1180 * been wiped. Clear remaining metadata. We can call memset and not
1181 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 memset(slot, 0, sizeof(*slot));
1183 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001184}
1185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001187{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001188 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001189 psa_status_t status; /* status of the last operation */
1190 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001191#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1192 psa_se_drv_table_entry_t *driver;
1193#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 if (mbedtls_svc_key_id_is_null(key)) {
1196 return PSA_SUCCESS;
1197 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001198
Ronald Cronf2911112020-10-29 17:51:10 +01001199 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001200 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001201 * key, this will load the key description from persistent memory if not
1202 * done yet. We cannot avoid this loading as without it we don't know if
1203 * the key is operated by an SE or not and this information is needed by
1204 * the current implementation.
1205 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 status = psa_get_and_lock_key_slot(key, &slot);
1207 if (status != PSA_SUCCESS) {
1208 return status;
1209 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001210
Ronald Cronf2911112020-10-29 17:51:10 +01001211 /*
1212 * If the key slot containing the key description is under access by the
1213 * library (apart from the present access), the key cannot be destroyed
1214 * yet. For the time being, just return in error. Eventually (to be
1215 * implemented), the key should be destroyed when all accesses have
1216 * stopped.
1217 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 if (slot->lock_count > 1) {
1219 psa_unlock_key_slot(slot);
1220 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001221 }
1222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001224 /* Refuse the destruction of a read-only key (which may or may not work
1225 * if we attempt it, depending on whether the key is merely read-only
1226 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001227 * Just do the best we can, which is to wipe the copy in memory
1228 * (done in this function's cleanup code). */
1229 overall_status = PSA_ERROR_NOT_PERMITTED;
1230 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001231 }
1232
Gilles Peskine354f7672019-07-12 23:46:38 +02001233#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1235 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001236 /* For a key in a secure element, we need to do three things:
1237 * remove the key file in internal storage, destroy the
1238 * key inside the secure element, and update the driver's
1239 * persistent data. Start a transaction that will encompass these
1240 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001242 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001244 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 status = psa_crypto_save_transaction();
1246 if (status != PSA_SUCCESS) {
1247 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001248 /* We should still try to destroy the key in the secure
1249 * element and the key metadata in storage. This is especially
1250 * important if the error is that the storage is full.
1251 * But how to do it exactly without risking an inconsistent
1252 * state after a reset?
1253 * https://github.com/ARMmbed/mbed-crypto/issues/215
1254 */
1255 overall_status = status;
1256 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001257 }
1258
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 status = psa_destroy_se_key(driver,
1260 psa_key_slot_get_slot_number(slot));
1261 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001262 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001264 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001265#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1266
Darryl Greend49a4992018-06-18 17:27:26 +01001267#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1269 status = psa_destroy_persistent_key(slot->attr.id);
1270 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001271 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001273
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001274 /* TODO: other slots may have a copy of the same key. We should
1275 * invalidate them.
1276 * https://github.com/ARMmbed/mbed-crypto/issues/214
1277 */
Darryl Greend49a4992018-06-18 17:27:26 +01001278 }
1279#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001280
Gilles Peskinefc762652019-07-22 19:30:34 +02001281#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 if (driver != NULL) {
1283 status = psa_save_se_persistent_data(driver);
1284 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001285 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 }
1287 status = psa_crypto_stop_transaction();
1288 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001289 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001291 }
1292#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1293
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001294exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001296 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001298 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 }
1300 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001301}
1302
Valerio Settib2bcedb2023-07-10 11:24:00 +02001303#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001304 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001305static psa_status_t psa_get_rsa_public_exponent(
1306 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001308{
1309 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001310 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001311 uint8_t *buffer = NULL;
1312 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1316 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001317 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 }
1319 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001320 /* It's the default value, which is reported as an empty string,
1321 * so there's nothing to do. */
1322 goto exit;
1323 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 buflen = mbedtls_mpi_size(&mpi);
1326 buffer = mbedtls_calloc(1, buflen);
1327 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001328 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1329 goto exit;
1330 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1332 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001333 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001335 attributes->domain_parameters = buffer;
1336 attributes->domain_parameters_size = buflen;
1337
1338exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 mbedtls_mpi_free(&mpi);
1340 if (ret != 0) {
1341 mbedtls_free(buffer);
1342 }
1343 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001344}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001345#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001346 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001347
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001348/** Retrieve all the publicly-accessible attributes of a key.
1349 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001350psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1351 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001352{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001353 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001354 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001355 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1360 if (status != PSA_SUCCESS) {
1361 return status;
1362 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001363
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001364 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1366 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001367
1368#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1370 psa_set_key_slot_number(attributes,
1371 psa_key_slot_get_slot_number(slot));
1372 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001373#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001376#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1377 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001378 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001379 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001380 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001381 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001382 * is not yet implemented.
1383 * https://github.com/ARMmbed/mbed-crypto/issues/216
1384 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001386 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001387
Ronald Cron90857082020-11-25 14:58:33 +01001388 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 slot->attr.type,
1390 slot->key.data,
1391 slot->key.bytes,
1392 &rsa);
1393 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001394 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 status = psa_get_rsa_public_exponent(rsa,
1398 attributes);
1399 mbedtls_rsa_free(rsa);
1400 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001401 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001402 break;
Pengyu Lve9efbc22023-12-08 16:59:08 +08001403#else
1404 case PSA_KEY_TYPE_RSA_KEY_PAIR:
1405 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1406 attributes->domain_parameters = NULL;
1407 attributes->domain_parameters_size = SIZE_MAX;
1408 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001409#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1410 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001411 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001412 default:
1413 /* Nothing else to do. */
1414 break;
1415 }
1416
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 if (status != PSA_SUCCESS) {
1418 psa_reset_key_attributes(attributes);
1419 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001424}
1425
Gilles Peskinec8000c02019-08-02 20:15:51 +02001426#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1427psa_status_t psa_get_key_slot_number(
1428 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001430{
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001432 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 return PSA_SUCCESS;
1434 } else {
1435 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001436 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001437}
1438#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1439
Gilles Peskine449bd832023-01-11 14:50:10 +01001440static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1441 size_t key_buffer_size,
1442 uint8_t *data,
1443 size_t data_size,
1444 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001445{
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 if (key_buffer_size > data_size) {
1447 return PSA_ERROR_BUFFER_TOO_SMALL;
1448 }
1449 memcpy(data, key_buffer, key_buffer_size);
1450 memset(data + key_buffer_size, 0,
1451 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001452 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001454}
1455
Ronald Cron7285cda2020-11-26 14:46:37 +01001456psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001457 const psa_key_attributes_t *attributes,
1458 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001460{
Ronald Crond18b5f82020-11-25 16:46:05 +01001461 psa_key_type_t type = attributes->core.type;
1462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 if (key_type_is_raw_bytes(type) ||
1464 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001465 PSA_KEY_TYPE_IS_ECC(type) ||
1466 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 return psa_export_key_buffer_internal(
1468 key_buffer, key_buffer_size,
1469 data, data_size, data_length);
1470 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001471 /* This shouldn't happen in the reference implementation, but
1472 it is valid for a special-purpose implementation to omit
1473 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001475 }
1476}
1477
Gilles Peskine449bd832023-01-11 14:50:10 +01001478psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1479 uint8_t *data,
1480 size_t data_size,
1481 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001482{
1483 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1484 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1485 psa_key_slot_t *slot;
1486
Ronald Crone907e552021-01-18 13:22:38 +01001487 /* Reject a zero-length output buffer now, since this can never be a
1488 * valid key representation. This way we know that data must be a valid
1489 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 if (data_size == 0) {
1491 return PSA_ERROR_BUFFER_TOO_SMALL;
1492 }
Ronald Crone907e552021-01-18 13:22:38 +01001493
Ronald Cron9486f9d2020-11-26 13:36:23 +01001494 /* Set the key to empty now, so that even when there are errors, we always
1495 * set data_length to a value between 0 and data_size. On error, setting
1496 * the key to empty is a good choice because an empty key representation is
1497 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001498 *data_length = 0;
1499
Ronald Cron9486f9d2020-11-26 13:36:23 +01001500 /* Export requires the EXPORT flag. There is an exception for public keys,
1501 * which don't require any flag, but
1502 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1503 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1505 PSA_KEY_USAGE_EXPORT, 0);
1506 if (status != PSA_SUCCESS) {
1507 return status;
1508 }
mohammad160306e79202018-03-28 13:17:44 +03001509
Ronald Crond18b5f82020-11-25 16:46:05 +01001510 psa_key_attributes_t attributes = {
1511 .core = slot->attr
1512 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 status = psa_driver_wrapper_export_key(&attributes,
1514 slot->key.data, slot->key.bytes,
1515 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001518
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001520}
1521
Ronald Cron7285cda2020-11-26 14:46:37 +01001522psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001523 const psa_key_attributes_t *attributes,
1524 const uint8_t *key_buffer,
1525 size_t key_buffer_size,
1526 uint8_t *data,
1527 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001529{
Ronald Crond18b5f82020-11-25 16:46:05 +01001530 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001531
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001532 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001533 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1534 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001535 /* Exporting public -> public */
1536 return psa_export_key_buffer_internal(
1537 key_buffer, key_buffer_size,
1538 data, data_size, data_length);
1539 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001540#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001541 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1542 return mbedtls_psa_rsa_export_public_key(attributes,
1543 key_buffer,
1544 key_buffer_size,
1545 data,
1546 data_size,
1547 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001548#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001549 /* We don't know how to convert a private RSA key to public. */
1550 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001551#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001552 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001553 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001554#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001555 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1556 return mbedtls_psa_ecp_export_public_key(attributes,
1557 key_buffer,
1558 key_buffer_size,
1559 data,
1560 data_size,
1561 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001562#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001563 /* We don't know how to convert a private ECC key to public */
1564 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001565#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001566 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001567 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001568#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001569 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001570 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001571 key_buffer,
1572 key_buffer_size,
1573 data, data_size,
1574 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001575#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001576 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001577#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001578 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001580 (void) key_buffer;
1581 (void) key_buffer_size;
1582 (void) data;
1583 (void) data_size;
1584 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001586 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001587}
Ronald Crond18b5f82020-11-25 16:46:05 +01001588
Gilles Peskine449bd832023-01-11 14:50:10 +01001589psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1590 uint8_t *data,
1591 size_t data_size,
1592 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001593{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001594 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001595 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001596 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001597 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001598
Ronald Crone907e552021-01-18 13:22:38 +01001599 /* Reject a zero-length output buffer now, since this can never be a
1600 * valid key representation. This way we know that data must be a valid
1601 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 if (data_size == 0) {
1603 return PSA_ERROR_BUFFER_TOO_SMALL;
1604 }
Ronald Crone907e552021-01-18 13:22:38 +01001605
Darryl Greendd8fb772018-11-07 16:00:44 +00001606 /* Set the key to empty now, so that even when there are errors, we always
1607 * set data_length to a value between 0 and data_size. On error, setting
1608 * the key to empty is a good choice because an empty key representation is
1609 * unlikely to be accepted anywhere. */
1610 *data_length = 0;
1611
1612 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1614 if (status != PSA_SUCCESS) {
1615 return status;
1616 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001617
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1619 status = PSA_ERROR_INVALID_ARGUMENT;
1620 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001621 }
1622
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001623 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001624 .core = slot->attr
1625 };
Ronald Cron67227982020-11-26 15:16:05 +01001626 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001627 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001629
1630exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001634}
1635
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001636MBEDTLS_STATIC_ASSERT(
1637 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1638 "One or more key attribute flag is listed as both external-only and dual-use")
1639MBEDTLS_STATIC_ASSERT(
1640 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1641 "One or more key attribute flag is listed as both internal-only and dual-use")
1642MBEDTLS_STATIC_ASSERT(
1643 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1644 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001645
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001646/** Validate that a key policy is internally well-formed.
1647 *
1648 * This function only rejects invalid policies. It does not validate the
1649 * consistency of the policy with respect to other attributes of the key
1650 * such as the key type.
1651 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001652static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001653{
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1655 PSA_KEY_USAGE_COPY |
1656 PSA_KEY_USAGE_ENCRYPT |
1657 PSA_KEY_USAGE_DECRYPT |
1658 PSA_KEY_USAGE_SIGN_MESSAGE |
1659 PSA_KEY_USAGE_VERIFY_MESSAGE |
1660 PSA_KEY_USAGE_SIGN_HASH |
1661 PSA_KEY_USAGE_VERIFY_HASH |
1662 PSA_KEY_USAGE_VERIFY_DERIVATION |
1663 PSA_KEY_USAGE_DERIVE)) != 0) {
1664 return PSA_ERROR_INVALID_ARGUMENT;
1665 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001666
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001668}
1669
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001670/** Validate the internal consistency of key attributes.
1671 *
1672 * This function only rejects invalid attribute values. If does not
1673 * validate the consistency of the attributes with any key data that may
1674 * be involved in the creation of the key.
1675 *
1676 * Call this function early in the key creation process.
1677 *
1678 * \param[in] attributes Key attributes for the new key.
1679 * \param[out] p_drv On any return, the driver for the key, if any.
1680 * NULL for a transparent key.
1681 *
1682 */
1683static psa_status_t psa_validate_key_attributes(
1684 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001686{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001687 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1689 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001690
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 status = psa_validate_key_location(lifetime, p_drv);
1692 if (status != PSA_SUCCESS) {
1693 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001694 }
1695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 status = psa_validate_key_persistence(lifetime);
1697 if (status != PSA_SUCCESS) {
1698 return status;
1699 }
1700
1701 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1702 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1703 return PSA_ERROR_INVALID_ARGUMENT;
1704 }
1705 } else {
1706 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1707 return PSA_ERROR_INVALID_ARGUMENT;
1708 }
1709 }
1710
1711 status = psa_validate_key_policy(&attributes->core.policy);
1712 if (status != PSA_SUCCESS) {
1713 return status;
1714 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001715
1716 /* Refuse to create overly large keys.
1717 * Note that this doesn't trigger on import if the attributes don't
1718 * explicitly specify a size (so psa_get_key_bits returns 0), so
1719 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1721 return PSA_ERROR_NOT_SUPPORTED;
1722 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001723
Gilles Peskine91e8c332019-08-02 19:19:39 +02001724 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1726 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1727 return PSA_ERROR_INVALID_ARGUMENT;
1728 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001729
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001731}
1732
Gilles Peskine4747d192019-04-17 15:05:45 +02001733/** Prepare a key slot to receive key material.
1734 *
1735 * This function allocates a key slot and sets its metadata.
1736 *
1737 * If this function fails, call psa_fail_key_creation().
1738 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001739 * This function is intended to be used as follows:
1740 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001741 * it with the specified attributes, and in case of a volatile key assign it
1742 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001743 * -# Populate the slot with the key material.
1744 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1745 * In case of failure at any step, stop the sequence and call
1746 * psa_fail_key_creation().
1747 *
Ronald Cron5c522922020-11-14 16:35:34 +01001748 * On success, the key slot is locked. It is the responsibility of the caller
1749 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001750 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001751 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001752 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001753 * \param[out] p_slot On success, a pointer to the prepared slot.
1754 * \param[out] p_drv On any return, the driver for the key, if any.
1755 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001756 *
1757 * \retval #PSA_SUCCESS
1758 * The key slot is ready to receive key material.
1759 * \return If this function fails, the key slot is an invalid state.
1760 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001761 */
1762static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001763 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001764 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001765 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001767{
1768 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001769 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001770 psa_key_slot_t *slot;
1771
Gilles Peskinedf179142019-07-15 22:02:14 +02001772 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001773 *p_drv = NULL;
1774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 status = psa_validate_key_attributes(attributes, p_drv);
1776 if (status != PSA_SUCCESS) {
1777 return status;
1778 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1781 if (status != PSA_SUCCESS) {
1782 return status;
1783 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001784 slot = *p_slot;
1785
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001786 /* We're storing the declared bit-size of the key. It's up to each
1787 * creation mechanism to verify that this information is correct.
1788 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001789 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001790 * is optional (import, copy). In case of a volatile key, assign it the
1791 * volatile key identifier associated to the slot returned to contain its
1792 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001793
1794 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001796#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1797 slot->attr.id = volatile_key_id;
1798#else
1799 slot->attr.id.key_id = volatile_key_id;
1800#endif
1801 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001802
Gilles Peskine91e8c332019-08-02 19:19:39 +02001803 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001804 * external-only flags, query `attributes`. Thanks to the check
1805 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001806 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001807 * may have set. */
1808 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001809
Gilles Peskinecbaff462019-07-12 23:46:04 +02001810#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001811 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001812 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001813 * create the key file in internal storage, create the
1814 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001815 * persistent data. This is done by starting a transaction that will
1816 * encompass these three actions.
1817 * For registering a volatile key, we just need to find an appropriate
1818 * slot number inside the SE. Since the key is designated volatile, creating
1819 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001820 /* The first thing to do is to find a slot number for the new key.
1821 * We save the slot number in persistent storage as part of the
1822 * transaction data. It will be needed to recover if the power
1823 * fails during the key creation process, to clean up on the secure
1824 * element side after restarting. Obtaining a slot number from the
1825 * secure element driver updates its persistent state, but we do not yet
1826 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001827 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001829 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1831 &slot_number);
1832 if (status != PSA_SUCCESS) {
1833 return status;
1834 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1837 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001838 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001839 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001840 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 status = psa_crypto_save_transaction();
1842 if (status != PSA_SUCCESS) {
1843 (void) psa_crypto_stop_transaction();
1844 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001845 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001846 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001847
1848 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001850 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001851
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001853 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001855 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001856#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1857
Ryan Everett975d4112023-11-16 13:37:51 +00001858 slot->status = PSA_SLOT_OCCUPIED;
1859
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001861}
Gilles Peskine4747d192019-04-17 15:05:45 +02001862
1863/** Finalize the creation of a key once its key material has been set.
1864 *
1865 * This entails writing the key to persistent storage.
1866 *
1867 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001868 * See the documentation of psa_start_key_creation() for the intended use
1869 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001870 *
Ronald Cron5c522922020-11-14 16:35:34 +01001871 * If the finalization succeeds, the function unlocks the key slot (it was
1872 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1873 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001874 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001875 * \param[in,out] slot Pointer to the slot with key material.
1876 * \param[in] driver The secure element driver for the key,
1877 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001878 * \param[out] key On success, identifier of the key. Note that the
1879 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001880 *
1881 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001882 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001883 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1884 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1885 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1886 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1887 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1888 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001889 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001890 * \return If this function fails, the key slot is an invalid state.
1891 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001892 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001893static psa_status_t psa_finish_key_creation(
1894 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001895 psa_se_drv_table_entry_t *driver,
1896 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001897{
1898 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001899 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001900 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001901
1902#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001904#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001906 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001907 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001909
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001910 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1911 sizeof(data.slot_number),
1912 "Slot number size does not match psa_se_key_data_storage_t");
1913
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1915 status = psa_save_persistent_key(&slot->attr,
1916 (uint8_t *) &data,
1917 sizeof(data));
1918 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001919#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1920 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001921 /* Key material is saved in export representation in the slot, so
1922 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 status = psa_save_persistent_key(&slot->attr,
1924 slot->key.data,
1925 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001926 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001927 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001928#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1929
Gilles Peskinecbaff462019-07-12 23:46:04 +02001930#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001931 /* Finish the transaction for a key creation. This does not
1932 * happen when registering an existing key. Detect this case
1933 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001934 * creation of a persistent key in a secure element requires a transaction,
1935 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 if (driver != NULL &&
1937 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1938 status = psa_save_se_persistent_data(driver);
1939 if (status != PSA_SUCCESS) {
1940 psa_destroy_persistent_key(slot->attr.id);
1941 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001942 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001944 }
1945#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1946
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001948 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 status = psa_unlock_key_slot(slot);
1950 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001951 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001953 }
Ronald Cron50972942020-11-14 11:28:25 +01001954
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001956}
1957
1958/** Abort the creation of a key.
1959 *
1960 * You may call this function after calling psa_start_key_creation(),
1961 * or after psa_finish_key_creation() fails. In other circumstances, this
1962 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001963 * See the documentation of psa_start_key_creation() for the intended use
1964 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001965 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001966 * \param[in,out] slot Pointer to the slot with key material.
1967 * \param[in] driver The secure element driver for the key,
1968 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001969 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001970static void psa_fail_key_creation(psa_key_slot_t *slot,
1971 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001972{
Gilles Peskine011e4282019-06-26 18:34:38 +02001973 (void) driver;
1974
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001976 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001978
1979#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001980 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001981 * element, and the failure happened later (when saving metadata
1982 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001983 * element.
1984 * https://github.com/ARMmbed/mbed-crypto/issues/217
1985 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001986
Gilles Peskined7729582019-08-05 15:55:54 +02001987 /* Abort the ongoing transaction if any (there may not be one if
1988 * the creation process failed before starting one, or if the
1989 * key creation is a registration of a key in a secure element).
1990 * Earlier functions must already have done what it takes to undo any
1991 * partial creation. All that's left is to update the transaction data
1992 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001994#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1995
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001997}
1998
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001999/** Validate optional attributes during key creation.
2000 *
2001 * Some key attributes are optional during key creation. If they are
2002 * specified in the attributes structure, check that they are consistent
2003 * with the data in the slot.
2004 *
2005 * This function should be called near the end of key creation, after
2006 * the slot in memory is fully populated but before saving persistent data.
2007 */
2008static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002009 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002011{
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 if (attributes->core.type != 0) {
2013 if (attributes->core.type != slot->attr.type) {
2014 return PSA_ERROR_INVALID_ARGUMENT;
2015 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002016 }
2017
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02002019#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
2020 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2022 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002023 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002024 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002025 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002026
Ronald Cron90857082020-11-25 14:58:33 +01002027 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 slot->attr.type,
2029 slot->key.data,
2030 slot->key.bytes,
2031 &rsa);
2032 if (status != PSA_SUCCESS) {
2033 return status;
2034 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002035
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 mbedtls_mpi_init(&actual);
2037 mbedtls_mpi_init(&required);
2038 ret = mbedtls_rsa_export(rsa,
2039 NULL, NULL, NULL, NULL, &actual);
2040 mbedtls_rsa_free(rsa);
2041 mbedtls_free(rsa);
2042 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002043 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 }
2045 ret = mbedtls_mpi_read_binary(&required,
2046 attributes->domain_parameters,
2047 attributes->domain_parameters_size);
2048 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002049 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 }
2051 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002052 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 }
2054rsa_exit:
2055 mbedtls_mpi_free(&actual);
2056 mbedtls_mpi_free(&required);
2057 if (ret != 0) {
2058 return mbedtls_to_psa_error(ret);
2059 }
2060 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02002061#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
2062 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08002063 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002064 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002066 }
2067 }
2068
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 if (attributes->core.bits != 0) {
2070 if (attributes->core.bits != slot->attr.bits) {
2071 return PSA_ERROR_INVALID_ARGUMENT;
2072 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002073 }
2074
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002076}
2077
Gilles Peskine449bd832023-01-11 14:50:10 +01002078psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2079 const uint8_t *data,
2080 size_t data_length,
2081 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002082{
2083 psa_status_t status;
2084 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002085 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002086 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302087 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002088
Ronald Cron81709fc2020-11-14 12:10:32 +01002089 *key = MBEDTLS_SVC_KEY_ID_INIT;
2090
Gilles Peskine0f84d622019-09-12 19:03:13 +02002091 /* Reject zero-length symmetric keys (including raw data key objects).
2092 * This also rejects any key which might be encoded as an empty string,
2093 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 if (data_length == 0) {
2095 return PSA_ERROR_INVALID_ARGUMENT;
2096 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002097
Archana449608b2021-09-08 15:36:05 +05302098 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 if (data_length > SIZE_MAX / 8) {
2100 return PSA_ERROR_NOT_SUPPORTED;
2101 }
Archana6ed4bda2021-08-04 10:47:15 +05302102
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2104 &slot, &driver);
2105 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002106 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002108
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002109 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302110 * storage ( thus not in the case of importing a key in a secure element
2111 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2112 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 if (slot->key.data == NULL) {
2114 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302115 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 attributes, data, data_length, &storage_size);
2117 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302118 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 }
Archanad8a83dc2021-06-14 10:04:16 +05302120 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 status = psa_allocate_buffer_to_slot(slot, storage_size);
2122 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002123 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002125 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002126
2127 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 status = psa_driver_wrapper_import_key(attributes,
2129 data, data_length,
2130 slot->key.data,
2131 slot->key.bytes,
2132 &slot->key.bytes, &bits);
2133 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002134 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002138 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002140 status = PSA_ERROR_INVALID_ARGUMENT;
2141 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002142 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002143
Archana6ed4bda2021-08-04 10:47:15 +05302144 /* Enforce a size limit, and in particular ensure that the bit
2145 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302147 status = PSA_ERROR_NOT_SUPPORTED;
2148 goto exit;
2149 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 status = psa_validate_optional_attributes(slot, attributes);
2151 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002152 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002156exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 if (status != PSA_SUCCESS) {
2158 psa_fail_key_creation(slot, driver);
2159 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002162}
2163
Gilles Peskined7729582019-08-05 15:55:54 +02002164#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2165psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002167{
2168 psa_status_t status;
2169 psa_key_slot_t *slot = NULL;
2170 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002171 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002172
2173 /* Leaving attributes unspecified is not currently supported.
2174 * It could make sense to query the key type and size from the
2175 * secure element, but not all secure elements support this
2176 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2178 return PSA_ERROR_NOT_SUPPORTED;
2179 }
2180 if (psa_get_key_bits(attributes) == 0) {
2181 return PSA_ERROR_NOT_SUPPORTED;
2182 }
Gilles Peskined7729582019-08-05 15:55:54 +02002183
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2185 &slot, &driver);
2186 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002187 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 }
Gilles Peskined7729582019-08-05 15:55:54 +02002189
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002191
2192exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 if (status != PSA_SUCCESS) {
2194 psa_fail_key_creation(slot, driver);
2195 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002196
Gilles Peskined7729582019-08-05 15:55:54 +02002197 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 psa_close_key(key);
2199 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002200}
2201#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2204 const psa_key_attributes_t *specified_attributes,
2205 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002206{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002207 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002208 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002209 psa_key_slot_t *source_slot = NULL;
2210 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002211 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002212 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302213 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002214
Ronald Cron81709fc2020-11-14 12:10:32 +01002215 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2216
Archana8a180362021-07-05 02:18:48 +05302217 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2219 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002220 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002222
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 status = psa_validate_optional_attributes(source_slot,
2224 specified_attributes);
2225 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002226 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002228
Archana9d17bf42021-09-10 06:22:44 +05302229 /* The target key type and number of bits have been validated by
2230 * psa_validate_optional_attributes() to be either equal to zero or
2231 * equal to the ones of the source key. So it is safe to inherit
2232 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302233 * */
Archana9d17bf42021-09-10 06:22:44 +05302234 actual_attributes.core.bits = source_slot->attr.bits;
2235 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302236
2237
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 status = psa_restrict_key_policy(source_slot->attr.type,
2239 &actual_attributes.core.policy,
2240 &source_slot->attr.policy);
2241 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002242 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2246 &target_slot, &driver);
2247 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002248 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 }
2250 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2251 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302252 /*
Archana9d17bf42021-09-10 06:22:44 +05302253 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302254 * the source key would need to be exported as plaintext and re-imported
2255 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302256 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302257 * appropriate API invocations from the application, if needed.
2258 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002259 status = PSA_ERROR_NOT_SUPPORTED;
2260 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002261 }
Archana8a180362021-07-05 02:18:48 +05302262 /*
2263 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302264 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302265 * - For opaque keys this translates to an invocation of the drivers'
2266 * copy_key entry point through the dispatch layer.
2267 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2269 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2270 &storage_size);
2271 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302272 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 }
Archana374fe5b2021-09-08 15:50:28 +05302274
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2276 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302277 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 }
Archana374fe5b2021-09-08 15:50:28 +05302279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 status = psa_driver_wrapper_copy_key(&actual_attributes,
2281 source_slot->key.data,
2282 source_slot->key.bytes,
2283 target_slot->key.data,
2284 target_slot->key.bytes,
2285 &target_slot->key.bytes);
2286 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302287 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 }
2289 } else {
2290 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302291 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 source_slot->key.bytes);
2293 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302294 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 }
Archana8a180362021-07-05 02:18:48 +05302296 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002298exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 if (status != PSA_SUCCESS) {
2300 psa_fail_key_creation(target_slot, driver);
2301 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002304
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002306}
2307
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002308
2309
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002310/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002311/* Message digests */
2312/****************************************************************/
2313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002315{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002316 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 if (operation->id == 0) {
2318 return PSA_SUCCESS;
2319 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002320
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002322 operation->id = 0;
2323
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002325}
2326
Gilles Peskine449bd832023-01-11 14:50:10 +01002327psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2328 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002329{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002330 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002331
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002332 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002334 status = PSA_ERROR_BAD_STATE;
2335 goto exit;
2336 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002339 status = PSA_ERROR_INVALID_ARGUMENT;
2340 goto exit;
2341 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002342
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002343 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2344 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002348
2349exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 if (status != PSA_SUCCESS) {
2351 psa_hash_abort(operation);
2352 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002353
2354 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002355}
2356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2358 const uint8_t *input,
2359 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002360{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002361 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2362
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002364 status = PSA_ERROR_BAD_STATE;
2365 goto exit;
2366 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002367
Steven Cooremanc8288352021-03-04 14:02:19 +01002368 /* Don't require hash implementations to behave correctly on a
2369 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 if (input_length == 0) {
2371 return PSA_SUCCESS;
2372 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002373
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002375
2376exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 if (status != PSA_SUCCESS) {
2378 psa_hash_abort(operation);
2379 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002382}
2383
Gilles Peskine449bd832023-01-11 14:50:10 +01002384psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2385 uint8_t *hash,
2386 size_t hash_size,
2387 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002388{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002389 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 if (operation->id == 0) {
2391 return PSA_ERROR_BAD_STATE;
2392 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002393
Steven Cooreman1e582352021-02-18 17:24:37 +01002394 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 operation, hash, hash_size, hash_length);
2396 psa_hash_abort(operation);
2397 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002398}
2399
Gilles Peskine449bd832023-01-11 14:50:10 +01002400psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2401 const uint8_t *hash,
2402 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002403{
Ronald Cron69a63422021-10-18 09:47:58 +02002404 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002405 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002406 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 operation,
2408 actual_hash, sizeof(actual_hash),
2409 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002412 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002416 status = PSA_ERROR_INVALID_SIGNATURE;
2417 goto exit;
2418 }
2419
Dave Rodgman78701152023-08-29 14:20:18 +01002420 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002421 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002423
2424exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2426 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002427 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002429
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002431}
2432
Gilles Peskine449bd832023-01-11 14:50:10 +01002433psa_status_t psa_hash_compute(psa_algorithm_t alg,
2434 const uint8_t *input, size_t input_length,
2435 uint8_t *hash, size_t hash_size,
2436 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002437{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002438 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 if (!PSA_ALG_IS_HASH(alg)) {
2440 return PSA_ERROR_INVALID_ARGUMENT;
2441 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002442
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2444 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002445}
2446
Gilles Peskine449bd832023-01-11 14:50:10 +01002447psa_status_t psa_hash_compare(psa_algorithm_t alg,
2448 const uint8_t *input, size_t input_length,
2449 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002450{
Ronald Cron69a63422021-10-18 09:47:58 +02002451 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002452 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002453
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 if (!PSA_ALG_IS_HASH(alg)) {
2455 return PSA_ERROR_INVALID_ARGUMENT;
2456 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002457
Steven Cooreman1e582352021-02-18 17:24:37 +01002458 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 alg, input, input_length,
2460 actual_hash, sizeof(actual_hash),
2461 &actual_hash_length);
2462 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002463 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 }
2465 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002466 status = PSA_ERROR_INVALID_SIGNATURE;
2467 goto exit;
2468 }
Dave Rodgman78701152023-08-29 14:20:18 +01002469 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002470 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002472
2473exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2475 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002476}
2477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2479 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002480{
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 if (source_operation->id == 0 ||
2482 target_operation->id != 0) {
2483 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002484 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2487 target_operation);
2488 if (status != PSA_SUCCESS) {
2489 psa_hash_abort(target_operation);
2490 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002491
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002493}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002494
2495
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002496/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002497/* MAC */
2498/****************************************************************/
2499
Gilles Peskine449bd832023-01-11 14:50:10 +01002500psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002501{
Steven Cooremane6804192021-03-19 18:28:56 +01002502 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 if (operation->id == 0) {
2504 return PSA_SUCCESS;
2505 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002506
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002508 operation->mac_size = 0;
2509 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002510 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002513}
2514
Ronald Cron2dff3b22021-06-17 16:33:22 +02002515static psa_status_t psa_mac_finalize_alg_and_key_validation(
2516 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002517 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002519{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002520 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 psa_key_type_t key_type = psa_get_key_type(attributes);
2522 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 if (!PSA_ALG_IS_MAC(alg)) {
2525 return PSA_ERROR_INVALID_ARGUMENT;
2526 }
Steven Cooremane6804192021-03-19 18:28:56 +01002527
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002528 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 status = psa_mac_key_can_do(alg, key_type);
2530 if (status != PSA_SUCCESS) {
2531 return status;
2532 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002533
Steven Cooremand1a68f12021-05-10 11:13:41 +02002534 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002536
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002538 /* A very short MAC is too short for security since it can be
2539 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2540 * so we make this our minimum, even though 32 bits is still
2541 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002543 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2546 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002547 /* It's impossible to "truncate" to a larger length than the full length
2548 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002550 }
2551
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002553 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2554 * that is disabled in the compile-time configuration. The result can
2555 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2556 * configuration into account. In this case, force a return of
2557 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2558 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2559 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2560 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2561 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002563 }
2564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002566}
2567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2569 mbedtls_svc_key_id_t key,
2570 psa_algorithm_t alg,
2571 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002572{
2573 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2574 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002575 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002576 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002577
2578 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002580 status = PSA_ERROR_BAD_STATE;
2581 goto exit;
2582 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002583
2584 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 key,
2586 &slot,
2587 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2588 alg);
2589 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002590 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002592
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002593 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002594 .core = slot->attr
2595 };
2596
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2598 &operation->mac_size);
2599 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002600 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002602
2603 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002604 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 if (is_sign) {
2606 status = psa_driver_wrapper_mac_sign_setup(operation,
2607 &attributes,
2608 slot->key.data,
2609 slot->key.bytes,
2610 alg);
2611 } else {
2612 status = psa_driver_wrapper_mac_verify_setup(operation,
2613 &attributes,
2614 slot->key.data,
2615 slot->key.bytes,
2616 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002617 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002618
Gilles Peskinefbfac682018-07-08 20:51:54 +02002619exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 if (status != PSA_SUCCESS) {
2621 psa_mac_abort(operation);
2622 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002623
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002625
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002627}
2628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2630 mbedtls_svc_key_id_t key,
2631 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002632{
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002634}
2635
Gilles Peskine449bd832023-01-11 14:50:10 +01002636psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2637 mbedtls_svc_key_id_t key,
2638 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002639{
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002641}
2642
Gilles Peskine449bd832023-01-11 14:50:10 +01002643psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2644 const uint8_t *input,
2645 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002646{
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 if (operation->id == 0) {
2648 return PSA_ERROR_BAD_STATE;
2649 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002650
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002651 /* Don't require hash implementations to behave correctly on a
2652 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 if (input_length == 0) {
2654 return PSA_SUCCESS;
2655 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002656
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2658 input, input_length);
2659 if (status != PSA_SUCCESS) {
2660 psa_mac_abort(operation);
2661 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002662
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002664}
2665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2667 uint8_t *mac,
2668 size_t mac_size,
2669 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002670{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002671 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2672 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002675 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002676 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002677 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002678
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002680 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002681 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002682 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002683
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002684 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2685 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002687 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002688 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002689 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002690
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002692 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002693 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002694 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 status = psa_driver_wrapper_mac_sign_finish(operation,
2697 mac, operation->mac_size,
2698 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002699
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002700exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002701 /* In case of success, set the potential excess room in the output buffer
2702 * to an invalid value, to avoid potentially leaking a longer MAC.
2703 * In case of error, set the output length and content to a safe default,
2704 * such that in case the caller misses an error check, the output would be
2705 * an unachievable MAC.
2706 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002708 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002709 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002710 }
mohammad16036df908f2018-04-02 08:34:15 -07002711
Paul Elliottdc42ca82023-02-24 18:11:59 +00002712 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002713
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002715
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002717}
2718
Gilles Peskine449bd832023-01-11 14:50:10 +01002719psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2720 const uint8_t *mac,
2721 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002722{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002723 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2724 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002725
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002727 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002728 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002729 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002730
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002732 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002733 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002734 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002735
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002737 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002738 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002739 }
2740
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 status = psa_driver_wrapper_mac_verify_finish(operation,
2742 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002743
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002744exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002746
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002748}
2749
Gilles Peskine449bd832023-01-11 14:50:10 +01002750static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2751 psa_algorithm_t alg,
2752 const uint8_t *input,
2753 size_t input_length,
2754 uint8_t *mac,
2755 size_t mac_size,
2756 size_t *mac_length,
2757 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002758{
2759 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002760 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2761 psa_key_slot_t *slot;
2762 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002763 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002764
Ronald Cron51131b52021-06-17 17:17:20 +02002765 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 key,
2767 &slot,
2768 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2769 alg);
2770 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002771 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002772 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002773
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002774 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002775 .core = slot->attr
2776 };
2777
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2779 &operation_mac_size);
2780 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002781 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002783
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002785 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002786 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002787 }
2788
2789 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 &attributes,
2791 slot->key.data, slot->key.bytes,
2792 alg,
2793 input, input_length,
2794 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002795
2796exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002797 /* In case of success, set the potential excess room in the output buffer
2798 * to an invalid value, to avoid potentially leaking a longer MAC.
2799 * In case of error, set the output length and content to a safe default,
2800 * such that in case the caller misses an error check, the output would be
2801 * an unachievable MAC.
2802 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002804 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002805 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002806 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002807
2808 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002809
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002811
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002813}
2814
Gilles Peskine449bd832023-01-11 14:50:10 +01002815psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002816 psa_algorithm_t alg,
2817 const uint8_t *input,
2818 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 uint8_t *mac,
2820 size_t mac_size,
2821 size_t *mac_length)
2822{
2823 return psa_mac_compute_internal(key, alg,
2824 input, input_length,
2825 mac, mac_size, mac_length, 1);
2826}
2827
2828psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2829 psa_algorithm_t alg,
2830 const uint8_t *input,
2831 size_t input_length,
2832 const uint8_t *mac,
2833 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002834{
2835 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002836 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2837 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002838
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 status = psa_mac_compute_internal(key, alg,
2840 input, input_length,
2841 actual_mac, sizeof(actual_mac),
2842 &actual_mac_length, 0);
2843 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002844 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002846
Gilles Peskine449bd832023-01-11 14:50:10 +01002847 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002848 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002849 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002850 }
Dave Rodgman78701152023-08-29 14:20:18 +01002851 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002852 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002853 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002854 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002855
2856exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002858
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002860}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002861
Gilles Peskinee59236f2018-01-27 23:32:46 +01002862/****************************************************************/
2863/* Asymmetric cryptography */
2864/****************************************************************/
2865
Gilles Peskine449bd832023-01-11 14:50:10 +01002866static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2867 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002868{
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 if (input_is_message) {
2870 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2871 return PSA_ERROR_INVALID_ARGUMENT;
2872 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002873
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2875 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2876 return PSA_ERROR_INVALID_ARGUMENT;
2877 }
2878 }
2879 } else {
2880 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2881 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002882 }
2883 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002884
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002886}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002887
Gilles Peskine449bd832023-01-11 14:50:10 +01002888static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2889 int input_is_message,
2890 psa_algorithm_t alg,
2891 const uint8_t *input,
2892 size_t input_length,
2893 uint8_t *signature,
2894 size_t signature_size,
2895 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002896{
2897 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2898 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2899 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002900 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002901
2902 *signature_length = 0;
2903
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 status = psa_sign_verify_check_alg(input_is_message, alg);
2905 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002906 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002908
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002909 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002910 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002911 * buffer can in principle be empty since it doesn't actually have
2912 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 if (signature_size == 0) {
2914 return PSA_ERROR_BUFFER_TOO_SMALL;
2915 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002916
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002917 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 key, &slot,
2919 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2920 PSA_KEY_USAGE_SIGN_HASH,
2921 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002922
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002924 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002926
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002928 status = PSA_ERROR_INVALID_ARGUMENT;
2929 goto exit;
2930 }
2931
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002932 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002934 };
2935
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002937 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002938 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002939 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 signature, signature_size, signature_length);
2941 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002942
2943 status = psa_driver_wrapper_sign_hash(
2944 &attributes, slot->key.data, slot->key.bytes,
2945 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002947 }
2948
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002949
2950exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002951 psa_wipe_tag_output_buffer(signature, status, signature_size,
2952 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002953
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002955
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002957}
2958
Gilles Peskine449bd832023-01-11 14:50:10 +01002959static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2960 int input_is_message,
2961 psa_algorithm_t alg,
2962 const uint8_t *input,
2963 size_t input_length,
2964 const uint8_t *signature,
2965 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002966{
2967 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2968 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2969 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002970
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 status = psa_sign_verify_check_alg(input_is_message, alg);
2972 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002973 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002975
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002976 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 key, &slot,
2978 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2979 PSA_KEY_USAGE_VERIFY_HASH,
2980 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002981
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 if (status != PSA_SUCCESS) {
2983 return status;
2984 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002985
2986 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002988 };
2989
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002991 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002992 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002993 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 signature, signature_length);
2995 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002996 status = psa_driver_wrapper_verify_hash(
2997 &attributes, slot->key.data, slot->key.bytes,
2998 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003000 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003001
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003003
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003005
3006}
3007
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003008psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003009 const psa_key_attributes_t *attributes,
3010 const uint8_t *key_buffer,
3011 size_t key_buffer_size,
3012 psa_algorithm_t alg,
3013 const uint8_t *input,
3014 size_t input_length,
3015 uint8_t *signature,
3016 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003018{
3019 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003020
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003022 size_t hash_length;
3023 uint8_t hash[PSA_HASH_MAX_SIZE];
3024
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003025 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 PSA_ALG_SIGN_GET_HASH(alg),
3027 input, input_length,
3028 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003029
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003031 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003033
3034 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 attributes, key_buffer, key_buffer_size,
3036 alg, hash, hash_length,
3037 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003038 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003039
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003041}
3042
Gilles Peskine449bd832023-01-11 14:50:10 +01003043psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3044 psa_algorithm_t alg,
3045 const uint8_t *input,
3046 size_t input_length,
3047 uint8_t *signature,
3048 size_t signature_size,
3049 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003050{
3051 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003052 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003054}
3055
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003056psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003057 const psa_key_attributes_t *attributes,
3058 const uint8_t *key_buffer,
3059 size_t key_buffer_size,
3060 psa_algorithm_t alg,
3061 const uint8_t *input,
3062 size_t input_length,
3063 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003065{
3066 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003067
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003069 size_t hash_length;
3070 uint8_t hash[PSA_HASH_MAX_SIZE];
3071
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003072 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 PSA_ALG_SIGN_GET_HASH(alg),
3074 input, input_length,
3075 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003076
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003078 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003080
3081 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003082 attributes, key_buffer, key_buffer_size,
3083 alg, hash, hash_length,
3084 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003085 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003086
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003088}
3089
Gilles Peskine449bd832023-01-11 14:50:10 +01003090psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3091 psa_algorithm_t alg,
3092 const uint8_t *input,
3093 size_t input_length,
3094 const uint8_t *signature,
3095 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003096{
3097 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003098 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003100}
3101
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003102psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003103 const psa_key_attributes_t *attributes,
3104 const uint8_t *key_buffer, size_t key_buffer_size,
3105 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003107{
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3109 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3110 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003111#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3113 return mbedtls_psa_rsa_sign_hash(
3114 attributes,
3115 key_buffer, key_buffer_size,
3116 alg, hash, hash_length,
3117 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003118#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3119 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 } else {
3121 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003122 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3124 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003125#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3127 return mbedtls_psa_ecdsa_sign_hash(
3128 attributes,
3129 key_buffer, key_buffer_size,
3130 alg, hash, hash_length,
3131 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003132#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3133 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 } else {
3135 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003136 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003137 }
Ronald Cron4501c982021-03-19 20:09:32 +01003138
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 (void) key_buffer;
3140 (void) key_buffer_size;
3141 (void) hash;
3142 (void) hash_length;
3143 (void) signature;
3144 (void) signature_size;
3145 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003146
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003148}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003149
Gilles Peskine449bd832023-01-11 14:50:10 +01003150psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3151 psa_algorithm_t alg,
3152 const uint8_t *hash,
3153 size_t hash_length,
3154 uint8_t *signature,
3155 size_t signature_size,
3156 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003157{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003158 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003159 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003161}
3162
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003163psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003164 const psa_key_attributes_t *attributes,
3165 const uint8_t *key_buffer, size_t key_buffer_size,
3166 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003168{
Gilles Peskine449bd832023-01-11 14:50:10 +01003169 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3170 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3171 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003172#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003173 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3174 return mbedtls_psa_rsa_verify_hash(
3175 attributes,
3176 key_buffer, key_buffer_size,
3177 alg, hash, hash_length,
3178 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003179#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3180 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 } else {
3182 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003183 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3185 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003186#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3188 return mbedtls_psa_ecdsa_verify_hash(
3189 attributes,
3190 key_buffer, key_buffer_size,
3191 alg, hash, hash_length,
3192 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003193#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3194 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 } else {
3196 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003197 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003198 }
Ronald Cron4501c982021-03-19 20:09:32 +01003199
Gilles Peskine449bd832023-01-11 14:50:10 +01003200 (void) key_buffer;
3201 (void) key_buffer_size;
3202 (void) hash;
3203 (void) hash_length;
3204 (void) signature;
3205 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003206
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003208}
3209
Gilles Peskine449bd832023-01-11 14:50:10 +01003210psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3211 psa_algorithm_t alg,
3212 const uint8_t *hash,
3213 size_t hash_length,
3214 const uint8_t *signature,
3215 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003216{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003217 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003218 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003219 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003220}
3221
Gilles Peskine449bd832023-01-11 14:50:10 +01003222psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3223 psa_algorithm_t alg,
3224 const uint8_t *input,
3225 size_t input_length,
3226 const uint8_t *salt,
3227 size_t salt_length,
3228 uint8_t *output,
3229 size_t output_size,
3230 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003231{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003232 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003233 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003234 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003235 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003236
Darryl Green5cc689a2018-07-24 15:34:10 +01003237 (void) input;
3238 (void) input_length;
3239 (void) salt;
3240 (void) output;
3241 (void) output_size;
3242
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003243 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003244
Gilles Peskine449bd832023-01-11 14:50:10 +01003245 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3246 return PSA_ERROR_INVALID_ARGUMENT;
3247 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003248
Ronald Cron5c522922020-11-14 16:35:34 +01003249 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3251 if (status != PSA_SUCCESS) {
3252 return status;
3253 }
3254 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3255 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003256 status = PSA_ERROR_INVALID_ARGUMENT;
3257 goto exit;
3258 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003259
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003260 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003262 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003263
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003264 status = psa_driver_wrapper_asymmetric_encrypt(
3265 &attributes, slot->key.data, slot->key.bytes,
3266 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003267 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003268exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003269 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003270
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003272}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003273
Gilles Peskine449bd832023-01-11 14:50:10 +01003274psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3275 psa_algorithm_t alg,
3276 const uint8_t *input,
3277 size_t input_length,
3278 const uint8_t *salt,
3279 size_t salt_length,
3280 uint8_t *output,
3281 size_t output_size,
3282 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003283{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003284 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003285 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003286 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003287 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003288
Darryl Green5cc689a2018-07-24 15:34:10 +01003289 (void) input;
3290 (void) input_length;
3291 (void) salt;
3292 (void) output;
3293 (void) output_size;
3294
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003295 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003296
Gilles Peskine449bd832023-01-11 14:50:10 +01003297 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3298 return PSA_ERROR_INVALID_ARGUMENT;
3299 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003300
Ronald Cron5c522922020-11-14 16:35:34 +01003301 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003302 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3303 if (status != PSA_SUCCESS) {
3304 return status;
3305 }
3306 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003307 status = PSA_ERROR_INVALID_ARGUMENT;
3308 goto exit;
3309 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003310
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003311 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003312 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003313 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003314
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003315 status = psa_driver_wrapper_asymmetric_decrypt(
3316 &attributes, slot->key.data, slot->key.bytes,
3317 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003318 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003319
3320exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003322
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003324}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003325
Paul Elliott9fe12f62022-11-30 19:16:02 +00003326/****************************************************************/
3327/* Asymmetric interruptible cryptography */
3328/****************************************************************/
3329
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003330static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3331
Paul Elliott9fe12f62022-11-30 19:16:02 +00003332void psa_interruptible_set_max_ops(uint32_t max_ops)
3333{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003334 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003335}
3336
3337uint32_t psa_interruptible_get_max_ops(void)
3338{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003339 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003340}
3341
Paul Elliott9fe12f62022-11-30 19:16:02 +00003342uint32_t psa_sign_hash_get_num_ops(
3343 const psa_sign_hash_interruptible_operation_t *operation)
3344{
Paul Elliott296ede92022-12-15 17:00:30 +00003345 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003346}
3347
3348uint32_t psa_verify_hash_get_num_ops(
3349 const psa_verify_hash_interruptible_operation_t *operation)
3350{
Paul Elliott296ede92022-12-15 17:00:30 +00003351 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003352}
3353
Paul Elliott59ad9452022-12-18 15:09:02 +00003354static psa_status_t psa_sign_hash_abort_internal(
3355 psa_sign_hash_interruptible_operation_t *operation)
3356{
3357 if (operation->id == 0) {
3358 /* The object has (apparently) been initialized but it is not (yet)
3359 * in use. It's ok to call abort on such an object, and there's
3360 * nothing to do. */
3361 return PSA_SUCCESS;
3362 }
3363
3364 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3365
3366 status = psa_driver_wrapper_sign_hash_abort(operation);
3367
3368 operation->id = 0;
3369
Paul Elliotte6145dc2023-02-07 12:51:21 +00003370 /* Do not clear either the error_occurred or num_ops elements here as they
3371 * only want to be cleared by the application calling abort, not by abort
3372 * being called at completion of an operation. */
3373
Paul Elliott59ad9452022-12-18 15:09:02 +00003374 return status;
3375}
3376
Paul Elliott9fe12f62022-11-30 19:16:02 +00003377psa_status_t psa_sign_hash_start(
3378 psa_sign_hash_interruptible_operation_t *operation,
3379 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3380 const uint8_t *hash, size_t hash_length)
3381{
3382 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3383 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3384 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003385 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003386
Paul Elliottc9774412023-02-06 15:14:07 +00003387 /* Check that start has not been previously called, or operation has not
3388 * previously errored. */
3389 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003390 return PSA_ERROR_BAD_STATE;
3391 }
3392
Paul Elliott9fe12f62022-11-30 19:16:02 +00003393 status = psa_sign_verify_check_alg(0, alg);
3394 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003395 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003396 return status;
3397 }
3398
3399 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3400 PSA_KEY_USAGE_SIGN_HASH,
3401 alg);
3402
3403 if (status != PSA_SUCCESS) {
3404 goto exit;
3405 }
3406
3407 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3408 status = PSA_ERROR_INVALID_ARGUMENT;
3409 goto exit;
3410 }
3411
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003412 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003413 .core = slot->attr
3414 };
3415
Paul Elliott296ede92022-12-15 17:00:30 +00003416 /* Ensure ops count gets reset, in case of operation re-use. */
3417 operation->num_ops = 0;
3418
Paul Elliott9fe12f62022-11-30 19:16:02 +00003419 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3420 slot->key.data,
3421 slot->key.bytes, alg,
3422 hash, hash_length);
3423exit:
3424
3425 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003426 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003427 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003428 }
3429
3430 unlock_status = psa_unlock_key_slot(slot);
3431
Paul Elliottc9774412023-02-06 15:14:07 +00003432 if (unlock_status != PSA_SUCCESS) {
3433 operation->error_occurred = 1;
3434 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003435
Paul Elliottc9774412023-02-06 15:14:07 +00003436 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003437}
3438
3439
3440psa_status_t psa_sign_hash_complete(
3441 psa_sign_hash_interruptible_operation_t *operation,
3442 uint8_t *signature, size_t signature_size,
3443 size_t *signature_length)
3444{
3445 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3446
3447 *signature_length = 0;
3448
Paul Elliottc9774412023-02-06 15:14:07 +00003449 /* Check that start has been called first, and that operation has not
3450 * previously errored. */
3451 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003452 status = PSA_ERROR_BAD_STATE;
3453 goto exit;
3454 }
3455
Paul Elliott096abc42023-02-03 18:33:23 +00003456 /* Immediately reject a zero-length signature buffer. This guarantees that
3457 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003458 if (signature_size == 0) {
3459 status = PSA_ERROR_BUFFER_TOO_SMALL;
3460 goto exit;
3461 }
3462
3463 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3464 signature_size,
3465 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003466
Paul Elliott296ede92022-12-15 17:00:30 +00003467 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003468 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003469
Paul Elliottebe225c2023-02-10 14:32:53 +00003470exit:
3471
Paul Elliott59200a22023-02-21 15:07:40 +00003472 psa_wipe_tag_output_buffer(signature, status, signature_size,
3473 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003474
Paul Elliott53bb3122023-02-10 14:22:22 +00003475 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003476 if (status != PSA_SUCCESS) {
3477 operation->error_occurred = 1;
3478 }
3479
Paul Elliott59ad9452022-12-18 15:09:02 +00003480 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003481 }
3482
3483 return status;
3484}
3485
3486psa_status_t psa_sign_hash_abort(
3487 psa_sign_hash_interruptible_operation_t *operation)
3488{
Paul Elliott59ad9452022-12-18 15:09:02 +00003489 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3490
3491 status = psa_sign_hash_abort_internal(operation);
3492
3493 /* We clear the number of ops done here, so that it is not cleared when
3494 * the operation fails or succeeds, only on manual abort. */
3495 operation->num_ops = 0;
3496
Paul Elliottc9774412023-02-06 15:14:07 +00003497 /* Likewise, failure state. */
3498 operation->error_occurred = 0;
3499
Paul Elliott59ad9452022-12-18 15:09:02 +00003500 return status;
3501}
3502
3503static psa_status_t psa_verify_hash_abort_internal(
3504 psa_verify_hash_interruptible_operation_t *operation)
3505{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003506 if (operation->id == 0) {
3507 /* The object has (apparently) been initialized but it is not (yet)
3508 * in use. It's ok to call abort on such an object, and there's
3509 * nothing to do. */
3510 return PSA_SUCCESS;
3511 }
3512
Paul Elliott59ad9452022-12-18 15:09:02 +00003513 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3514
3515 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003516
3517 operation->id = 0;
3518
Paul Elliotte6145dc2023-02-07 12:51:21 +00003519 /* Do not clear either the error_occurred or num_ops elements here as they
3520 * only want to be cleared by the application calling abort, not by abort
3521 * being called at completion of an operation. */
3522
Paul Elliott59ad9452022-12-18 15:09:02 +00003523 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003524}
3525
3526psa_status_t psa_verify_hash_start(
3527 psa_verify_hash_interruptible_operation_t *operation,
3528 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3529 const uint8_t *hash, size_t hash_length,
3530 const uint8_t *signature, size_t signature_length)
3531{
3532 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3533 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3534 psa_key_slot_t *slot;
3535
Paul Elliottc9774412023-02-06 15:14:07 +00003536 /* Check that start has not been previously called, or operation has not
3537 * previously errored. */
3538 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003539 return PSA_ERROR_BAD_STATE;
3540 }
3541
3542 status = psa_sign_verify_check_alg(0, alg);
3543 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003544 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003545 return status;
3546 }
3547
3548 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3549 PSA_KEY_USAGE_VERIFY_HASH,
3550 alg);
3551
3552 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003553 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003554 return status;
3555 }
3556
3557 psa_key_attributes_t attributes = {
3558 .core = slot->attr
3559 };
3560
Paul Elliott296ede92022-12-15 17:00:30 +00003561 /* Ensure ops count gets reset, in case of operation re-use. */
3562 operation->num_ops = 0;
3563
Paul Elliott9fe12f62022-11-30 19:16:02 +00003564 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3565 slot->key.data,
3566 slot->key.bytes,
3567 alg, hash, hash_length,
3568 signature, signature_length);
3569
3570 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003571 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003572 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003573 }
3574
3575 unlock_status = psa_unlock_key_slot(slot);
3576
Paul Elliottc9774412023-02-06 15:14:07 +00003577 if (unlock_status != PSA_SUCCESS) {
3578 operation->error_occurred = 1;
3579 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003580
Paul Elliottc9774412023-02-06 15:14:07 +00003581 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003582}
3583
3584psa_status_t psa_verify_hash_complete(
3585 psa_verify_hash_interruptible_operation_t *operation)
3586{
3587 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3588
Paul Elliottc9774412023-02-06 15:14:07 +00003589 /* Check that start has been called first, and that operation has not
3590 * previously errored. */
3591 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003592 status = PSA_ERROR_BAD_STATE;
3593 goto exit;
3594 }
3595
3596 status = psa_driver_wrapper_verify_hash_complete(operation);
3597
Paul Elliott296ede92022-12-15 17:00:30 +00003598 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003599 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003600 operation);
3601
Paul Elliottebe225c2023-02-10 14:32:53 +00003602exit:
3603
Paul Elliott9fe12f62022-11-30 19:16:02 +00003604 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003605 if (status != PSA_SUCCESS) {
3606 operation->error_occurred = 1;
3607 }
3608
Paul Elliott59ad9452022-12-18 15:09:02 +00003609 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003610 }
3611
3612 return status;
3613}
3614
3615psa_status_t psa_verify_hash_abort(
3616 psa_verify_hash_interruptible_operation_t *operation)
3617{
Paul Elliott59ad9452022-12-18 15:09:02 +00003618 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003619
Paul Elliott59ad9452022-12-18 15:09:02 +00003620 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003621
Paul Elliott59ad9452022-12-18 15:09:02 +00003622 /* We clear the number of ops done here, so that it is not cleared when
3623 * the operation fails or succeeds, only on manual abort. */
3624 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003625
Paul Elliottc9774412023-02-06 15:14:07 +00003626 /* Likewise, failure state. */
3627 operation->error_occurred = 0;
3628
Paul Elliott59ad9452022-12-18 15:09:02 +00003629 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003630}
3631
Paul Elliott588f8ed2022-12-02 18:10:26 +00003632/****************************************************************/
3633/* Asymmetric interruptible cryptography internal */
3634/* implementations */
3635/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003636
Paul Elliott588f8ed2022-12-02 18:10:26 +00003637void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3638{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003639
Paul Elliott588f8ed2022-12-02 18:10:26 +00003640#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3641 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3642 defined(MBEDTLS_ECP_RESTARTABLE)
3643
3644 /* Internal implementation uses zero to indicate infinite number max ops,
3645 * therefore avoid this value, and set to minimum possible. */
3646 if (max_ops == 0) {
3647 max_ops = 1;
3648 }
3649
Paul Elliott588f8ed2022-12-02 18:10:26 +00003650 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003651#else
3652 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003653#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3654 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3655 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3656}
3657
Paul Elliott588f8ed2022-12-02 18:10:26 +00003658uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003659 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003660{
3661#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3662 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3663 defined(MBEDTLS_ECP_RESTARTABLE)
3664
Paul Elliott93d9ca82023-02-15 18:14:21 +00003665 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003666#else
3667 (void) operation;
3668 return 0;
3669#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3670 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3671 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3672}
3673
3674uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003675 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003676{
3677 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3678 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3679 defined(MBEDTLS_ECP_RESTARTABLE)
3680
Paul Elliott93d9ca82023-02-15 18:14:21 +00003681 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003682#else
3683 (void) operation;
3684 return 0;
3685#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3686 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3687 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3688}
3689
3690psa_status_t mbedtls_psa_sign_hash_start(
3691 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3692 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3693 size_t key_buffer_size, psa_algorithm_t alg,
3694 const uint8_t *hash, size_t hash_length)
3695{
3696 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003697 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003698
Paul Elliott068fe072023-01-16 13:59:15 +00003699 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3700 return PSA_ERROR_NOT_SUPPORTED;
3701 }
3702
3703 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003704 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003705 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003706
3707#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003708 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3709 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003710
Paul Elliotta1c94092023-02-15 16:38:04 +00003711 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3712
Paul Elliott93d9ca82023-02-15 18:14:21 +00003713 /* Ensure num_ops is zero'ed in case of context re-use. */
3714 operation->num_ops = 0;
3715
Paul Elliott068fe072023-01-16 13:59:15 +00003716 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3717 attributes->core.bits,
3718 key_buffer,
3719 key_buffer_size,
3720 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003721
Paul Elliott068fe072023-01-16 13:59:15 +00003722 if (status != PSA_SUCCESS) {
3723 return status;
3724 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003725
Paul Elliott1bc59df2023-02-05 13:41:57 +00003726 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003727 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003728
Paul Elliott068fe072023-01-16 13:59:15 +00003729 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003730 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003731 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003732
Paul Elliott0290a762023-02-09 14:30:24 +00003733 /* We only need to store the same length of hash as the private key size
3734 * here, it would be truncated by the internal implementation anyway. */
3735 required_hash_length = (hash_length < operation->coordinate_bytes ?
3736 hash_length : operation->coordinate_bytes);
3737
Paul Elliottba70ad42023-02-15 18:23:53 +00003738 if (required_hash_length > sizeof(operation->hash)) {
3739 /* Shouldn't happen, but better safe than sorry. */
3740 return PSA_ERROR_CORRUPTION_DETECTED;
3741 }
3742
Paul Elliott0290a762023-02-09 14:30:24 +00003743 memcpy(operation->hash, hash, required_hash_length);
3744 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003745
3746 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003747
3748#else
Paul Elliott068fe072023-01-16 13:59:15 +00003749 (void) operation;
3750 (void) key_buffer;
3751 (void) key_buffer_size;
3752 (void) alg;
3753 (void) hash;
3754 (void) hash_length;
3755 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003756 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003757
Paul Elliott068fe072023-01-16 13:59:15 +00003758 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003759#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3760 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3761 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003762}
3763
3764psa_status_t mbedtls_psa_sign_hash_complete(
3765 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3766 uint8_t *signature, size_t signature_size,
3767 size_t *signature_length)
3768{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003769#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3770 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3771 defined(MBEDTLS_ECP_RESTARTABLE)
3772
Paul Elliotta1c94092023-02-15 16:38:04 +00003773 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003774 mbedtls_mpi r;
3775 mbedtls_mpi s;
3776
Paul Elliott46845252023-02-03 14:59:11 +00003777 mbedtls_mpi_init(&r);
3778 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003779
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003780 /* Ensure max_ops is set to the current value (or default). */
3781 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3782
Paul Elliotta1c94092023-02-15 16:38:04 +00003783 if (signature_size < 2 * operation->coordinate_bytes) {
3784 status = PSA_ERROR_BUFFER_TOO_SMALL;
3785 goto exit;
3786 }
3787
Paul Elliott588f8ed2022-12-02 18:10:26 +00003788 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003789
Paul Elliott588f8ed2022-12-02 18:10:26 +00003790#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3791 status = mbedtls_to_psa_error(
3792 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003793 &r,
3794 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003795 &operation->ctx->d,
3796 operation->hash,
3797 operation->hash_length,
3798 operation->md_alg,
3799 mbedtls_psa_get_random,
3800 MBEDTLS_PSA_RANDOM_STATE,
3801 &operation->restart_ctx));
3802#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003803 status = PSA_ERROR_NOT_SUPPORTED;
3804 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003805#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3806 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003807 status = mbedtls_to_psa_error(
3808 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003809 &r,
3810 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003811 &operation->ctx->d,
3812 operation->hash,
3813 operation->hash_length,
3814 mbedtls_psa_get_random,
3815 MBEDTLS_PSA_RANDOM_STATE,
3816 mbedtls_psa_get_random,
3817 MBEDTLS_PSA_RANDOM_STATE,
3818 &operation->restart_ctx));
3819 }
3820
Paul Elliottf8e5b562023-02-19 18:43:45 +00003821 /* Hide the fact that the restart context only holds a delta of number of
3822 * ops done during the last operation, not an absolute value. */
3823 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3824
Paul Elliott724bd252023-02-08 12:35:08 +00003825 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003826 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003827 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003828 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003829 operation->coordinate_bytes)
3830 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003831
3832 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003833 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003834 }
3835
3836 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003837 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003838 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003839 operation->coordinate_bytes,
3840 operation->coordinate_bytes)
3841 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003842
3843 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003844 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003845 }
3846
Paul Elliott1bc59df2023-02-05 13:41:57 +00003847 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003848
Paul Elliott724bd252023-02-08 12:35:08 +00003849 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003850 }
Paul Elliott724bd252023-02-08 12:35:08 +00003851
3852exit:
3853
3854 mbedtls_mpi_free(&r);
3855 mbedtls_mpi_free(&s);
3856 return status;
3857
Paul Elliott588f8ed2022-12-02 18:10:26 +00003858 #else
3859
3860 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003861 (void) signature;
3862 (void) signature_size;
3863 (void) signature_length;
3864
3865 return PSA_ERROR_NOT_SUPPORTED;
3866
3867#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3868 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3869 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3870}
3871
3872psa_status_t mbedtls_psa_sign_hash_abort(
3873 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3874{
3875
3876#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3877 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3878 defined(MBEDTLS_ECP_RESTARTABLE)
3879
3880 if (operation->ctx) {
3881 mbedtls_ecdsa_free(operation->ctx);
3882 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003883 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003884 }
3885
3886 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3887
Paul Elliott93d9ca82023-02-15 18:14:21 +00003888 operation->num_ops = 0;
3889
Paul Elliott588f8ed2022-12-02 18:10:26 +00003890 return PSA_SUCCESS;
3891
3892#else
3893
3894 (void) operation;
3895
3896 return PSA_ERROR_NOT_SUPPORTED;
3897
3898#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3899 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3900 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3901}
3902
3903psa_status_t mbedtls_psa_verify_hash_start(
3904 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3905 const psa_key_attributes_t *attributes,
3906 const uint8_t *key_buffer, size_t key_buffer_size,
3907 psa_algorithm_t alg,
3908 const uint8_t *hash, size_t hash_length,
3909 const uint8_t *signature, size_t signature_length)
3910{
3911 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003912 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003913 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003914
Paul Elliott068fe072023-01-16 13:59:15 +00003915 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3916 return PSA_ERROR_NOT_SUPPORTED;
3917 }
3918
3919 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003920 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003921 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003922
3923#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003924 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3925 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003926
Paul Elliotta1c94092023-02-15 16:38:04 +00003927 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3928 mbedtls_mpi_init(&operation->r);
3929 mbedtls_mpi_init(&operation->s);
3930
Paul Elliott93d9ca82023-02-15 18:14:21 +00003931 /* Ensure num_ops is zero'ed in case of context re-use. */
3932 operation->num_ops = 0;
3933
Paul Elliott068fe072023-01-16 13:59:15 +00003934 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3935 attributes->core.bits,
3936 key_buffer,
3937 key_buffer_size,
3938 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003939
Paul Elliott068fe072023-01-16 13:59:15 +00003940 if (status != PSA_SUCCESS) {
3941 return status;
3942 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003943
Paul Elliottc569fc22023-02-10 13:02:54 +00003944 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003945
Paul Elliott1bc59df2023-02-05 13:41:57 +00003946 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003947 return PSA_ERROR_INVALID_SIGNATURE;
3948 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003949
Paul Elliott068fe072023-01-16 13:59:15 +00003950 status = mbedtls_to_psa_error(
3951 mbedtls_mpi_read_binary(&operation->r,
3952 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003953 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003954
Paul Elliott068fe072023-01-16 13:59:15 +00003955 if (status != PSA_SUCCESS) {
3956 return status;
3957 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003958
Paul Elliott068fe072023-01-16 13:59:15 +00003959 status = mbedtls_to_psa_error(
3960 mbedtls_mpi_read_binary(&operation->s,
3961 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003962 coordinate_bytes,
3963 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003964
Paul Elliott068fe072023-01-16 13:59:15 +00003965 if (status != PSA_SUCCESS) {
3966 return status;
3967 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003968
Paul Elliott2c9843f2023-02-15 17:32:42 +00003969 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003970
Paul Elliott2c9843f2023-02-15 17:32:42 +00003971 if (status != PSA_SUCCESS) {
3972 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003973 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003974
Paul Elliott0290a762023-02-09 14:30:24 +00003975 /* We only need to store the same length of hash as the private key size
3976 * here, it would be truncated by the internal implementation anyway. */
3977 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3978 coordinate_bytes);
3979
Paul Elliottba70ad42023-02-15 18:23:53 +00003980 if (required_hash_length > sizeof(operation->hash)) {
3981 /* Shouldn't happen, but better safe than sorry. */
3982 return PSA_ERROR_CORRUPTION_DETECTED;
3983 }
3984
Paul Elliott0290a762023-02-09 14:30:24 +00003985 memcpy(operation->hash, hash, required_hash_length);
3986 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003987
3988 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003989#else
Paul Elliott068fe072023-01-16 13:59:15 +00003990 (void) operation;
3991 (void) key_buffer;
3992 (void) key_buffer_size;
3993 (void) alg;
3994 (void) hash;
3995 (void) hash_length;
3996 (void) signature;
3997 (void) signature_length;
3998 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003999 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00004000 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004001
Paul Elliott068fe072023-01-16 13:59:15 +00004002 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004003#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4004 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4005 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004006}
4007
4008psa_status_t mbedtls_psa_verify_hash_complete(
4009 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4010{
4011
4012#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4013 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4014 defined(MBEDTLS_ECP_RESTARTABLE)
4015
Paul Elliottf8e5b562023-02-19 18:43:45 +00004016 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4017
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004018 /* Ensure max_ops is set to the current value (or default). */
4019 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4020
Paul Elliottf8e5b562023-02-19 18:43:45 +00004021 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004022 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4023 operation->hash,
4024 operation->hash_length,
4025 &operation->ctx->Q,
4026 &operation->r,
4027 &operation->s,
4028 &operation->restart_ctx));
4029
Paul Elliottf8e5b562023-02-19 18:43:45 +00004030 /* Hide the fact that the restart context only holds a delta of number of
4031 * ops done during the last operation, not an absolute value. */
4032 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4033
4034 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004035#else
4036 (void) operation;
4037
4038 return PSA_ERROR_NOT_SUPPORTED;
4039
4040#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4041 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4042 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4043}
4044
4045psa_status_t mbedtls_psa_verify_hash_abort(
4046 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4047{
4048
4049#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4050 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4051 defined(MBEDTLS_ECP_RESTARTABLE)
4052
4053 if (operation->ctx) {
4054 mbedtls_ecdsa_free(operation->ctx);
4055 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004056 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004057 }
4058
4059 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4060
Paul Elliott93d9ca82023-02-15 18:14:21 +00004061 operation->num_ops = 0;
4062
Paul Elliott588f8ed2022-12-02 18:10:26 +00004063 mbedtls_mpi_free(&operation->r);
4064 mbedtls_mpi_free(&operation->s);
4065
4066 return PSA_SUCCESS;
4067
4068#else
4069 (void) operation;
4070
4071 return PSA_ERROR_NOT_SUPPORTED;
4072
4073#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4074 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4075 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4076}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004077
mohammad1603503973b2018-03-12 15:59:30 +02004078/****************************************************************/
4079/* Symmetric cryptography */
4080/****************************************************************/
4081
Gilles Peskine449bd832023-01-11 14:50:10 +01004082static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4083 mbedtls_svc_key_id_t key,
4084 psa_algorithm_t alg,
4085 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004086{
4087 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4088 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004089 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004090 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4091 PSA_KEY_USAGE_ENCRYPT :
4092 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004093 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004094
4095 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004096 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004097 status = PSA_ERROR_BAD_STATE;
4098 goto exit;
4099 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004100
Gilles Peskine449bd832023-01-11 14:50:10 +01004101 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004102 status = PSA_ERROR_INVALID_ARGUMENT;
4103 goto exit;
4104 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004105
Gilles Peskine449bd832023-01-11 14:50:10 +01004106 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4107 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004108 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004109 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004110
Ronald Cron49fafa92021-03-10 08:34:23 +01004111 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004112 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004113 * so we only set it (in the driver wrapper) after resources have been
4114 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004115 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004116 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004117 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004119 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004120 }
4121 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004122
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004123 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004125 };
4126
Ronald Cronab99ac22020-10-01 16:38:28 +02004127 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004128 if (cipher_operation == MBEDTLS_ENCRYPT) {
4129 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4130 &attributes,
4131 slot->key.data,
4132 slot->key.bytes,
4133 alg);
4134 } else {
4135 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4136 &attributes,
4137 slot->key.data,
4138 slot->key.bytes,
4139 alg);
4140 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004141
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004142exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004143 if (status != PSA_SUCCESS) {
4144 psa_cipher_abort(operation);
4145 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004146
Gilles Peskine449bd832023-01-11 14:50:10 +01004147 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004148
Gilles Peskine449bd832023-01-11 14:50:10 +01004149 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004150}
4151
Gilles Peskine449bd832023-01-11 14:50:10 +01004152psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4153 mbedtls_svc_key_id_t key,
4154 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004155{
Gilles Peskine449bd832023-01-11 14:50:10 +01004156 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004157}
4158
Gilles Peskine449bd832023-01-11 14:50:10 +01004159psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4160 mbedtls_svc_key_id_t key,
4161 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004162{
Gilles Peskine449bd832023-01-11 14:50:10 +01004163 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004164}
4165
Gilles Peskine449bd832023-01-11 14:50:10 +01004166psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4167 uint8_t *iv,
4168 size_t iv_size,
4169 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004170{
Ronald Cron6d051732020-10-01 14:10:20 +02004171 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004172 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004173 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004174
Gilles Peskine449bd832023-01-11 14:50:10 +01004175 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004176 status = PSA_ERROR_BAD_STATE;
4177 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004178 }
4179
Gilles Peskine449bd832023-01-11 14:50:10 +01004180 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004181 status = PSA_ERROR_BAD_STATE;
4182 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004183 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004184
Ronald Cron2fb90522021-07-05 12:31:44 +02004185 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004186 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004187 status = PSA_ERROR_BUFFER_TOO_SMALL;
4188 goto exit;
4189 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004190
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004192 status = PSA_ERROR_GENERIC_ERROR;
4193 goto exit;
4194 }
4195
Gilles Peskine449bd832023-01-11 14:50:10 +01004196 status = psa_generate_random(local_iv, default_iv_length);
4197 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004198 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 }
Ronald Cron5618a392021-03-26 09:52:26 +01004200
Gilles Peskine449bd832023-01-11 14:50:10 +01004201 status = psa_driver_wrapper_cipher_set_iv(operation,
4202 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004203
4204exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004205 if (status == PSA_SUCCESS) {
4206 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004207 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004208 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004210 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004211 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004212 }
Ronald Cron6d051732020-10-01 14:10:20 +02004213
Gilles Peskine449bd832023-01-11 14:50:10 +01004214 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004215}
4216
Gilles Peskine449bd832023-01-11 14:50:10 +01004217psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4218 const uint8_t *iv,
4219 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004220{
Ronald Cron6d051732020-10-01 14:10:20 +02004221 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004222
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004224 status = PSA_ERROR_BAD_STATE;
4225 goto exit;
4226 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004227
Gilles Peskine449bd832023-01-11 14:50:10 +01004228 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004229 status = PSA_ERROR_BAD_STATE;
4230 goto exit;
4231 }
Ronald Crona0d68172021-03-26 10:15:08 +01004232
Gilles Peskine449bd832023-01-11 14:50:10 +01004233 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004234 status = PSA_ERROR_INVALID_ARGUMENT;
4235 goto exit;
4236 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004237
Gilles Peskine449bd832023-01-11 14:50:10 +01004238 status = psa_driver_wrapper_cipher_set_iv(operation,
4239 iv,
4240 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004241
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004242exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004243 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004244 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004245 } else {
4246 psa_cipher_abort(operation);
4247 }
4248 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004249}
4250
Gilles Peskine449bd832023-01-11 14:50:10 +01004251psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4252 const uint8_t *input,
4253 size_t input_length,
4254 uint8_t *output,
4255 size_t output_size,
4256 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004257{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004258 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004259
Gilles Peskine449bd832023-01-11 14:50:10 +01004260 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004261 status = PSA_ERROR_BAD_STATE;
4262 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004263 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004264
Gilles Peskine449bd832023-01-11 14:50:10 +01004265 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004266 status = PSA_ERROR_BAD_STATE;
4267 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004268 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004269
Gilles Peskine449bd832023-01-11 14:50:10 +01004270 status = psa_driver_wrapper_cipher_update(operation,
4271 input,
4272 input_length,
4273 output,
4274 output_size,
4275 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004276
4277exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004278 if (status != PSA_SUCCESS) {
4279 psa_cipher_abort(operation);
4280 }
Ronald Cron6d051732020-10-01 14:10:20 +02004281
Gilles Peskine449bd832023-01-11 14:50:10 +01004282 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004283}
4284
Gilles Peskine449bd832023-01-11 14:50:10 +01004285psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4286 uint8_t *output,
4287 size_t output_size,
4288 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004289{
David Saadab4ecc272019-02-14 13:48:10 +02004290 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004291
Gilles Peskine449bd832023-01-11 14:50:10 +01004292 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004293 status = PSA_ERROR_BAD_STATE;
4294 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004295 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004296
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004298 status = PSA_ERROR_BAD_STATE;
4299 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004300 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004301
Gilles Peskine449bd832023-01-11 14:50:10 +01004302 status = psa_driver_wrapper_cipher_finish(operation,
4303 output,
4304 output_size,
4305 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004306
4307exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004308 if (status == PSA_SUCCESS) {
4309 return psa_cipher_abort(operation);
4310 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004311 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004313
Gilles Peskine449bd832023-01-11 14:50:10 +01004314 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004315 }
mohammad1603503973b2018-03-12 15:59:30 +02004316}
4317
Gilles Peskine449bd832023-01-11 14:50:10 +01004318psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004319{
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004321 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004322 * in use. It's ok to call abort on such an object, and there's
4323 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004324 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004325 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004326
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004328
Ronald Cron49fafa92021-03-10 08:34:23 +01004329 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004330 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004331 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004332
Gilles Peskine449bd832023-01-11 14:50:10 +01004333 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004334}
4335
Gilles Peskine449bd832023-01-11 14:50:10 +01004336psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4337 psa_algorithm_t alg,
4338 const uint8_t *input,
4339 size_t input_length,
4340 uint8_t *output,
4341 size_t output_size,
4342 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004343{
4344 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004345 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004346 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004347 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4348 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004349 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004350
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004352 status = PSA_ERROR_INVALID_ARGUMENT;
4353 goto exit;
4354 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004355
Gilles Peskine449bd832023-01-11 14:50:10 +01004356 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4357 PSA_KEY_USAGE_ENCRYPT,
4358 alg);
4359 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004360 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004361 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004362
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004363 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004364 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004365 };
4366
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4368 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004369 status = PSA_ERROR_GENERIC_ERROR;
4370 goto exit;
4371 }
4372
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 if (default_iv_length > 0) {
4374 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004375 status = PSA_ERROR_BUFFER_TOO_SMALL;
4376 goto exit;
4377 }
4378
Gilles Peskine449bd832023-01-11 14:50:10 +01004379 status = psa_generate_random(local_iv, default_iv_length);
4380 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004381 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004383 }
4384
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004385 status = psa_driver_wrapper_cipher_encrypt(
4386 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004387 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004388 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004389 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004390
4391exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 unlock_status = psa_unlock_key_slot(slot);
4393 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004394 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004395 }
Ronald Cron23919522021-07-08 19:00:07 +02004396
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 if (status == PSA_SUCCESS) {
4398 if (default_iv_length > 0) {
4399 memcpy(output, local_iv, default_iv_length);
4400 }
4401 *output_length += default_iv_length;
4402 } else {
4403 *output_length = 0;
4404 }
4405
4406 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004407}
4408
Gilles Peskine449bd832023-01-11 14:50:10 +01004409psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4410 psa_algorithm_t alg,
4411 const uint8_t *input,
4412 size_t input_length,
4413 uint8_t *output,
4414 size_t output_size,
4415 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004416{
4417 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004418 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004419 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004420 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004421
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004423 status = PSA_ERROR_INVALID_ARGUMENT;
4424 goto exit;
4425 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004426
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4428 PSA_KEY_USAGE_DECRYPT,
4429 alg);
4430 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004431 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004432 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004433
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004434 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004436 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004437
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4439 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004440 status = PSA_ERROR_INVALID_ARGUMENT;
4441 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004443 status = PSA_ERROR_INVALID_ARGUMENT;
4444 goto exit;
4445 }
4446
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004447 status = psa_driver_wrapper_cipher_decrypt(
4448 &attributes, slot->key.data, slot->key.bytes,
4449 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004451
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004452exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 unlock_status = psa_unlock_key_slot(slot);
4454 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004455 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004457
Gilles Peskine449bd832023-01-11 14:50:10 +01004458 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004459 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 }
Ronald Cron23919522021-07-08 19:00:07 +02004461
Gilles Peskine449bd832023-01-11 14:50:10 +01004462 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004463}
4464
4465
mohammad16035955c982018-04-26 00:53:03 +03004466/****************************************************************/
4467/* AEAD */
4468/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004469
Paul Elliottbaff51c2021-09-28 17:44:45 +01004470/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004471static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004472{
Gilles Peskine449bd832023-01-11 14:50:10 +01004473 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004474}
4475
4476/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004477static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4478 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004479{
Gilles Peskine449bd832023-01-11 14:50:10 +01004480 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004481
Gilles Peskine449bd832023-01-11 14:50:10 +01004482 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004483#if defined(PSA_WANT_ALG_GCM)
4484 case PSA_ALG_GCM:
4485 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004486 * arbitrarily large nonces. Please note that we do not generally
4487 * recommend the usage of nonces of greater length than
4488 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4489 * size, which can then lead to collisions if you encrypt a very
4490 * large number of messages.*/
4491 if (nonce_length != 0) {
4492 return PSA_SUCCESS;
4493 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004494 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004495#endif /* PSA_WANT_ALG_GCM */
4496#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004497 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004498 if (nonce_length >= 7 && nonce_length <= 13) {
4499 return PSA_SUCCESS;
4500 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004501 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004502#endif /* PSA_WANT_ALG_CCM */
4503#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004504 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 if (nonce_length == 12) {
4506 return PSA_SUCCESS;
4507 } else if (nonce_length == 8) {
4508 return PSA_ERROR_NOT_SUPPORTED;
4509 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004510 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004511#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004512 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004513 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004515 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004516
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004518}
4519
Gilles Peskine449bd832023-01-11 14:50:10 +01004520static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004521{
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4523 return PSA_ERROR_INVALID_ARGUMENT;
4524 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004525
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004527}
4528
Gilles Peskine449bd832023-01-11 14:50:10 +01004529psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4530 psa_algorithm_t alg,
4531 const uint8_t *nonce,
4532 size_t nonce_length,
4533 const uint8_t *additional_data,
4534 size_t additional_data_length,
4535 const uint8_t *plaintext,
4536 size_t plaintext_length,
4537 uint8_t *ciphertext,
4538 size_t ciphertext_size,
4539 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004540{
Ronald Cron215633c2021-03-16 17:15:37 +01004541 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4542 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004543
mohammad1603f08a5502018-06-03 15:05:47 +03004544 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004545
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 status = psa_aead_check_algorithm(alg);
4547 if (status != PSA_SUCCESS) {
4548 return status;
4549 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004550
Ronald Cron9a986162021-03-26 12:40:07 +01004551 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4553 if (status != PSA_SUCCESS) {
4554 return status;
4555 }
mohammad16035955c982018-04-26 00:53:03 +03004556
Ronald Cron215633c2021-03-16 17:15:37 +01004557 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004559 };
mohammad16035955c982018-04-26 00:53:03 +03004560
Gilles Peskine449bd832023-01-11 14:50:10 +01004561 status = psa_aead_check_nonce_length(alg, nonce_length);
4562 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004563 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004564 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004565
Ronald Cronde822812021-03-17 16:08:20 +01004566 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004567 &attributes, slot->key.data, slot->key.bytes,
4568 alg,
4569 nonce, nonce_length,
4570 additional_data, additional_data_length,
4571 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004573
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4575 memset(ciphertext, 0, ciphertext_size);
4576 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004577
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004578exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004580
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004582}
4583
Gilles Peskine449bd832023-01-11 14:50:10 +01004584psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4585 psa_algorithm_t alg,
4586 const uint8_t *nonce,
4587 size_t nonce_length,
4588 const uint8_t *additional_data,
4589 size_t additional_data_length,
4590 const uint8_t *ciphertext,
4591 size_t ciphertext_length,
4592 uint8_t *plaintext,
4593 size_t plaintext_size,
4594 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004595{
Ronald Cron215633c2021-03-16 17:15:37 +01004596 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4597 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004598
Gilles Peskineee652a32018-06-01 19:23:52 +02004599 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004600
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 status = psa_aead_check_algorithm(alg);
4602 if (status != PSA_SUCCESS) {
4603 return status;
4604 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004605
Ronald Cron9a986162021-03-26 12:40:07 +01004606 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4608 if (status != PSA_SUCCESS) {
4609 return status;
4610 }
mohammad16035955c982018-04-26 00:53:03 +03004611
Ronald Cron215633c2021-03-16 17:15:37 +01004612 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004613 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004614 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004615
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 status = psa_aead_check_nonce_length(alg, nonce_length);
4617 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004618 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004620
Ronald Cronde822812021-03-17 16:08:20 +01004621 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004622 &attributes, slot->key.data, slot->key.bytes,
4623 alg,
4624 nonce, nonce_length,
4625 additional_data, additional_data_length,
4626 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004628
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 if (status != PSA_SUCCESS && plaintext_size != 0) {
4630 memset(plaintext, 0, plaintext_size);
4631 }
mohammad160360a64d02018-06-03 17:20:42 +03004632
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004633exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004634 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004635
Gilles Peskine449bd832023-01-11 14:50:10 +01004636 return status;
mohammad16035955c982018-04-26 00:53:03 +03004637}
4638
Gilles Peskine449bd832023-01-11 14:50:10 +01004639static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4640{
4641 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004642
Gilles Peskine449bd832023-01-11 14:50:10 +01004643 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004644#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004645 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004646 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4648 return PSA_ERROR_INVALID_ARGUMENT;
4649 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004650 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004651#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004652
Przemek Stekiel86679c72022-10-06 17:06:56 +02004653#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004655 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004656 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4657 return PSA_ERROR_INVALID_ARGUMENT;
4658 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004659 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004660#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004661
Przemek Stekiel86679c72022-10-06 17:06:56 +02004662#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004663 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004664 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004665 if (tag_len != 16) {
4666 return PSA_ERROR_INVALID_ARGUMENT;
4667 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004668 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004669#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004670
4671 default:
4672 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004674 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004675 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004676}
4677
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004678/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004679static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4680 int is_encrypt,
4681 mbedtls_svc_key_id_t key,
4682 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004683{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004684 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4685 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4686 psa_key_slot_t *slot = NULL;
4687 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004688 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004689
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 status = psa_aead_check_algorithm(alg);
4691 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004692 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004694
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004696 status = PSA_ERROR_BAD_STATE;
4697 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004698 }
4699
Gilles Peskine449bd832023-01-11 14:50:10 +01004700 if (operation->nonce_set || operation->lengths_set ||
4701 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004702 status = PSA_ERROR_BAD_STATE;
4703 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004704 }
4705
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004707 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004708 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004709 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004710 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004711
Gilles Peskine449bd832023-01-11 14:50:10 +01004712 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4713 alg);
4714 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004715 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004717
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004718 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004719 .core = slot->attr
4720 };
4721
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004723 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004725
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 if (is_encrypt) {
4727 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4728 &attributes,
4729 slot->key.data,
4730 slot->key.bytes,
4731 alg);
4732 } else {
4733 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4734 &attributes,
4735 slot->key.data,
4736 slot->key.bytes,
4737 alg);
4738 }
4739 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004740 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004742
Gilles Peskine449bd832023-01-11 14:50:10 +01004743 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004744
4745exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004746 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004747
Gilles Peskine449bd832023-01-11 14:50:10 +01004748 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004749 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004750 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004751 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 } else {
4753 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004754 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004755
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004757}
4758
Paul Elliott302ff6b2021-04-20 18:10:30 +01004759/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004760psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4761 mbedtls_svc_key_id_t key,
4762 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004763{
Gilles Peskine449bd832023-01-11 14:50:10 +01004764 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004765}
4766
4767/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004768psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4769 mbedtls_svc_key_id_t key,
4770 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004771{
Gilles Peskine449bd832023-01-11 14:50:10 +01004772 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004773}
4774
4775/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004776psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4777 uint8_t *nonce,
4778 size_t nonce_size,
4779 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004780{
4781 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004782 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004783 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004784
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004785 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004786
Gilles Peskine449bd832023-01-11 14:50:10 +01004787 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004788 status = PSA_ERROR_BAD_STATE;
4789 goto exit;
4790 }
4791
Gilles Peskine449bd832023-01-11 14:50:10 +01004792 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004793 status = PSA_ERROR_BAD_STATE;
4794 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004795 }
4796
Paul Elliotte193ea82021-10-01 13:00:16 +01004797 /* For CCM, this size may not be correct according to the PSA
4798 * specification. The PSA Crypto 1.0.1 specification states:
4799 *
4800 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4801 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4802 *
4803 * However this restriction that L has to be the smallest integer is not
4804 * applied in practice, and it is not implementable here since the
4805 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004806 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4807 operation->alg);
4808 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004809 status = PSA_ERROR_BUFFER_TOO_SMALL;
4810 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004811 }
4812
Gilles Peskine449bd832023-01-11 14:50:10 +01004813 status = psa_generate_random(local_nonce, required_nonce_size);
4814 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004815 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004817
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004819
Paul Elliott39dc6b82021-05-11 19:16:09 +01004820exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 if (status == PSA_SUCCESS) {
4822 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004823 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004824 } else {
4825 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004826 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004827
Gilles Peskine449bd832023-01-11 14:50:10 +01004828 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004829}
4830
4831/* Set the nonce for a multipart authenticated encryption or decryption
4832 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004833psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4834 const uint8_t *nonce,
4835 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004836{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004837 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4838
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004840 status = PSA_ERROR_BAD_STATE;
4841 goto exit;
4842 }
4843
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004845 status = PSA_ERROR_BAD_STATE;
4846 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004847 }
4848
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4850 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004851 status = PSA_ERROR_INVALID_ARGUMENT;
4852 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004853 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004854
Gilles Peskine449bd832023-01-11 14:50:10 +01004855 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4856 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004857
Paul Elliott39dc6b82021-05-11 19:16:09 +01004858exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004859 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004860 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004861 } else {
4862 psa_aead_abort(operation);
4863 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004864
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004866}
4867
4868/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004869psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4870 size_t ad_length,
4871 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004872{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004873 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4874
Gilles Peskine449bd832023-01-11 14:50:10 +01004875 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004876 status = PSA_ERROR_BAD_STATE;
4877 goto exit;
4878 }
4879
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 if (operation->lengths_set || operation->ad_started ||
4881 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004882 status = PSA_ERROR_BAD_STATE;
4883 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004884 }
4885
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004887#if defined(PSA_WANT_ALG_GCM)
4888 case PSA_ALG_GCM:
4889 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004890 * bits. Without the guard this code will generate warnings on 32bit
4891 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004892#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004893 if (((uint64_t) ad_length) >> 61 != 0 ||
4894 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004895 status = PSA_ERROR_INVALID_ARGUMENT;
4896 goto exit;
4897 }
Paul Elliott325d3742021-09-27 17:56:28 +01004898#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004899 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004900#endif /* PSA_WANT_ALG_GCM */
4901#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004902 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004904 status = PSA_ERROR_INVALID_ARGUMENT;
4905 goto exit;
4906 }
4907 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004908#endif /* PSA_WANT_ALG_CCM */
4909#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004910 case PSA_ALG_CHACHA20_POLY1305:
4911 /* No length restrictions for ChaChaPoly. */
4912 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004913#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004914 default:
4915 break;
4916 }
Paul Elliott325d3742021-09-27 17:56:28 +01004917
Gilles Peskine449bd832023-01-11 14:50:10 +01004918 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4919 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004920
Paul Elliott39dc6b82021-05-11 19:16:09 +01004921exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004922 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004923 operation->ad_remaining = ad_length;
4924 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004925 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004926 } else {
4927 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004928 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004929
Gilles Peskine449bd832023-01-11 14:50:10 +01004930 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004931}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004932
4933/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004934psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4935 const uint8_t *input,
4936 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004937{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004938 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4939
Gilles Peskine449bd832023-01-11 14:50:10 +01004940 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004941 status = PSA_ERROR_BAD_STATE;
4942 goto exit;
4943 }
4944
Gilles Peskine449bd832023-01-11 14:50:10 +01004945 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004946 status = PSA_ERROR_BAD_STATE;
4947 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004948 }
4949
Gilles Peskine449bd832023-01-11 14:50:10 +01004950 if (operation->lengths_set) {
4951 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004952 status = PSA_ERROR_INVALID_ARGUMENT;
4953 goto exit;
4954 }
4955
4956 operation->ad_remaining -= input_length;
4957 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004958#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004960 status = PSA_ERROR_BAD_STATE;
4961 goto exit;
4962 }
4963#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004964
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 status = psa_driver_wrapper_aead_update_ad(operation, input,
4966 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004967
Paul Elliott39dc6b82021-05-11 19:16:09 +01004968exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004969 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004970 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004971 } else {
4972 psa_aead_abort(operation);
4973 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004974
Gilles Peskine449bd832023-01-11 14:50:10 +01004975 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004976}
4977
4978/* Encrypt or decrypt a message fragment in an active multipart AEAD
4979 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004980psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4981 const uint8_t *input,
4982 size_t input_length,
4983 uint8_t *output,
4984 size_t output_size,
4985 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004986{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004987 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004988
4989 *output_length = 0;
4990
Gilles Peskine449bd832023-01-11 14:50:10 +01004991 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004992 status = PSA_ERROR_BAD_STATE;
4993 goto exit;
4994 }
4995
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004997 status = PSA_ERROR_BAD_STATE;
4998 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004999 }
5000
Gilles Peskine449bd832023-01-11 14:50:10 +01005001 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005002 /* Additional data length was supplied, but not all the additional
5003 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005005 status = PSA_ERROR_INVALID_ARGUMENT;
5006 goto exit;
5007 }
5008
5009 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005010 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005011 status = PSA_ERROR_INVALID_ARGUMENT;
5012 goto exit;
5013 }
5014
5015 operation->body_remaining -= input_length;
5016 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005017#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005018 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005019 status = PSA_ERROR_BAD_STATE;
5020 goto exit;
5021 }
5022#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005023
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5025 output, output_size,
5026 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005027
Paul Elliott39dc6b82021-05-11 19:16:09 +01005028exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005029 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005030 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005031 } else {
5032 psa_aead_abort(operation);
5033 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005034
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005036}
5037
Gilles Peskine449bd832023-01-11 14:50:10 +01005038static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005039{
Gilles Peskine449bd832023-01-11 14:50:10 +01005040 if (operation->id == 0 || !operation->nonce_set) {
5041 return PSA_ERROR_BAD_STATE;
5042 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005043
Gilles Peskine449bd832023-01-11 14:50:10 +01005044 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5045 operation->body_remaining != 0)) {
5046 return PSA_ERROR_INVALID_ARGUMENT;
5047 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005048
Gilles Peskine449bd832023-01-11 14:50:10 +01005049 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005050}
5051
Paul Elliott302ff6b2021-04-20 18:10:30 +01005052/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005053psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5054 uint8_t *ciphertext,
5055 size_t ciphertext_size,
5056 size_t *ciphertext_length,
5057 uint8_t *tag,
5058 size_t tag_size,
5059 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005060{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005061 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5062
Paul Elliott302ff6b2021-04-20 18:10:30 +01005063 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005064 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005065
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 status = psa_aead_final_checks(operation);
5067 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005068 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005070
Gilles Peskine449bd832023-01-11 14:50:10 +01005071 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005072 status = PSA_ERROR_BAD_STATE;
5073 goto exit;
5074 }
5075
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5077 ciphertext_size,
5078 ciphertext_length,
5079 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005080
Paul Elliott39dc6b82021-05-11 19:16:09 +01005081exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005082
5083
Paul Elliottf47b0952021-05-21 18:02:33 +01005084 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005085 * the zero tag size, make sure the tag is set to something implausible.
5086 * Even if the operation succeeds, make sure we clear the rest of the
5087 * buffer to prevent potential leakage of anything previously placed in
5088 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005089 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005090
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005092
Gilles Peskine449bd832023-01-11 14:50:10 +01005093 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005094}
5095
5096/* Finish authenticating and decrypting a message in a multipart AEAD
5097 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005098psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5099 uint8_t *plaintext,
5100 size_t plaintext_size,
5101 size_t *plaintext_length,
5102 const uint8_t *tag,
5103 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005104{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005105 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5106
Paul Elliott302ff6b2021-04-20 18:10:30 +01005107 *plaintext_length = 0;
5108
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 status = psa_aead_final_checks(operation);
5110 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005111 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005112 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005113
Gilles Peskine449bd832023-01-11 14:50:10 +01005114 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005115 status = PSA_ERROR_BAD_STATE;
5116 goto exit;
5117 }
5118
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5120 plaintext_size,
5121 plaintext_length,
5122 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005123
5124exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005126
Gilles Peskine449bd832023-01-11 14:50:10 +01005127 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005128}
5129
5130/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005131psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005132{
5133 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5134
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005136 /* The object has (apparently) been initialized but it is not (yet)
5137 * in use. It's ok to call abort on such an object, and there's
5138 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005140 }
5141
Gilles Peskine449bd832023-01-11 14:50:10 +01005142 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005143
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005145
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005147}
5148
Gilles Peskinee59236f2018-01-27 23:32:46 +01005149/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005150/* Generators */
5151/****************************************************************/
5152
Przemek Stekiel69c46792022-06-10 12:59:51 +02005153#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005154 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005155 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305156 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305157 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005158#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005159#endif /* At least one builtin KDF */
5160
Przemek Stekiel69c46792022-06-10 12:59:51 +02005161#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005162 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5163 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5164static psa_status_t psa_key_derivation_start_hmac(
5165 psa_mac_operation_t *operation,
5166 psa_algorithm_t hash_alg,
5167 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005168 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005169{
5170 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5171 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5173 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5174 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005175
Steven Cooreman72f736a2021-05-07 14:14:37 +02005176 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005178
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 status = psa_driver_wrapper_mac_sign_setup(operation,
5180 &attributes,
5181 hmac_key, hmac_key_length,
5182 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005183
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 psa_reset_key_attributes(&attributes);
5185 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005186}
5187#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005188
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005189#define HKDF_STATE_INIT 0 /* no input yet */
5190#define HKDF_STATE_STARTED 1 /* got salt */
5191#define HKDF_STATE_KEYED 2 /* got key */
5192#define HKDF_STATE_OUTPUT 3 /* output started */
5193
Gilles Peskinecbe66502019-05-16 16:59:18 +02005194static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005195 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005196{
Gilles Peskine449bd832023-01-11 14:50:10 +01005197 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5198 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5199 } else {
5200 return operation->alg;
5201 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005202}
5203
Gilles Peskine449bd832023-01-11 14:50:10 +01005204psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005205{
5206 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005207 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5208 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005209 /* The object has (apparently) been initialized but it is not
5210 * in use. It's ok to call abort on such an object, and there's
5211 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005212 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005213#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5215 mbedtls_free(operation->ctx.hkdf.info);
5216 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5217 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005218#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005219#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5220 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5222 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5223 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5224 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005225 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005227 }
5228
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005230 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005232 }
5233
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005235 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005237 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005238#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005239 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005240 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005242 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005243#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005244 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005245
5246 /* We leave the fields Ai and output_block to be erased safely by the
5247 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005248 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005249#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5250 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005251#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005252 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5253 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5254 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5255 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005256#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305257#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305258 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305259 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005260 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305261 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305262 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305263
5264 status = PSA_SUCCESS;
5265 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305266#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005267 {
5268 status = PSA_ERROR_BAD_STATE;
5269 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 mbedtls_platform_zeroize(operation, sizeof(*operation));
5271 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005272}
5273
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005274psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005276{
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005278 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005280 }
5281
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005282 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005284}
5285
Gilles Peskine449bd832023-01-11 14:50:10 +01005286psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5287 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005288{
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 if (operation->alg == 0) {
5290 return PSA_ERROR_BAD_STATE;
5291 }
5292 if (capacity > operation->capacity) {
5293 return PSA_ERROR_INVALID_ARGUMENT;
5294 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005295 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005296 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005297}
5298
Przemek Stekiel69c46792022-06-10 12:59:51 +02005299#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005300/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005301static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5302 psa_algorithm_t kdf_alg,
5303 uint8_t *output,
5304 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005305{
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5307 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005308 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005309 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005310#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005312#else
5313 const uint8_t last_block = 0xff;
5314#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005315
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 if (hkdf->state < HKDF_STATE_KEYED ||
5317 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005318#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005320#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005321 )) {
5322 return PSA_ERROR_BAD_STATE;
5323 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005324 hkdf->state = HKDF_STATE_OUTPUT;
5325
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005327 /* Copy what remains of the current block */
5328 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005330 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 }
5332 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005333 output += n;
5334 output_length -= n;
5335 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005337 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005338 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005339 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005340 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005341 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005342 * object was corrupted or if this function is called directly
5343 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 if (hkdf->block_number == last_block) {
5345 return PSA_ERROR_BAD_STATE;
5346 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005347
5348 /* We need a new block */
5349 ++hkdf->block_number;
5350 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005351
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5353 hash_alg,
5354 hkdf->prk,
5355 hash_length);
5356 if (status != PSA_SUCCESS) {
5357 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005358 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005359
5360 if (hkdf->block_number != 1) {
5361 status = psa_mac_update(&hkdf->hmac,
5362 hkdf->output_block,
5363 hash_length);
5364 if (status != PSA_SUCCESS) {
5365 return status;
5366 }
5367 }
5368 status = psa_mac_update(&hkdf->hmac,
5369 hkdf->info,
5370 hkdf->info_length);
5371 if (status != PSA_SUCCESS) {
5372 return status;
5373 }
5374 status = psa_mac_update(&hkdf->hmac,
5375 &hkdf->block_number, 1);
5376 if (status != PSA_SUCCESS) {
5377 return status;
5378 }
5379 status = psa_mac_sign_finish(&hkdf->hmac,
5380 hkdf->output_block,
5381 sizeof(hkdf->output_block),
5382 &hmac_output_length);
5383 if (status != PSA_SUCCESS) {
5384 return status;
5385 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005386 }
5387
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005389}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005390#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005391
John Durkop07cc04a2020-11-16 22:08:34 -08005392#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5393 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005394static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5395 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005396 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005397{
Gilles Peskine449bd832023-01-11 14:50:10 +01005398 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5399 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005400 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5401 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005402 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005403
Janos Follath7742fee2019-06-17 12:58:10 +01005404 /* We can't be wanting more output after block 0xff, otherwise
5405 * the capacity check in psa_key_derivation_output_bytes() would have
5406 * prevented this call. It could happen only if the operation
5407 * object was corrupted or if this function is called directly
5408 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005409 if (tls12_prf->block_number == 0xff) {
5410 return PSA_ERROR_CORRUPTION_DETECTED;
5411 }
Janos Follath7742fee2019-06-17 12:58:10 +01005412
5413 /* We need a new block */
5414 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005415 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005416
5417 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5418 *
5419 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5420 *
5421 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5422 * HMAC_hash(secret, A(2) + seed) +
5423 * HMAC_hash(secret, A(3) + seed) + ...
5424 *
5425 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005426 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005427 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005428 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005429 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005430 * is currently extracted as `output_block` and where i is
5431 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005432 */
5433
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 status = psa_key_derivation_start_hmac(&hmac,
5435 hash_alg,
5436 tls12_prf->secret,
5437 tls12_prf->secret_length);
5438 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005439 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005440 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005441
5442 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005444 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5445 * the variable seed and in this instance means it in the context of the
5446 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 status = psa_mac_update(&hmac,
5448 tls12_prf->label,
5449 tls12_prf->label_length);
5450 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005451 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 }
5453 status = psa_mac_update(&hmac,
5454 tls12_prf->seed,
5455 tls12_prf->seed_length);
5456 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005457 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 }
5459 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005460 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005461 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5462 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005463 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005465 }
5466
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 status = psa_mac_sign_finish(&hmac,
5468 tls12_prf->Ai, hash_length,
5469 &hmac_output_length);
5470 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005471 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 }
5473 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005474 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005476
5477 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 status = psa_key_derivation_start_hmac(&hmac,
5479 hash_alg,
5480 tls12_prf->secret,
5481 tls12_prf->secret_length);
5482 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005483 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 }
5485 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5486 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005487 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 }
5489 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5490 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005491 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 }
5493 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5494 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005495 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 }
5497 status = psa_mac_sign_finish(&hmac,
5498 tls12_prf->output_block, hash_length,
5499 &hmac_output_length);
5500 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005501 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005503
Janos Follath7742fee2019-06-17 12:58:10 +01005504
5505cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005506 cleanup_status = psa_mac_abort(&hmac);
5507 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005508 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005509 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005510
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005512}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005513
Janos Follath844eb0e2019-06-19 12:10:49 +01005514static psa_status_t psa_key_derivation_tls12_prf_read(
5515 psa_tls12_prf_key_derivation_t *tls12_prf,
5516 psa_algorithm_t alg,
5517 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005518 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005519{
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5521 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005522 psa_status_t status;
5523 uint8_t offset, length;
5524
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005526 case PSA_TLS12_PRF_STATE_LABEL_SET:
5527 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5528 break;
5529 case PSA_TLS12_PRF_STATE_OUTPUT:
5530 break;
5531 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005532 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005533 }
5534
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005536 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005537 if (tls12_prf->left_in_block == 0) {
5538 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5539 alg);
5540 if (status != PSA_SUCCESS) {
5541 return status;
5542 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005543
5544 continue;
5545 }
5546
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005548 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005550 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005551 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005552
5553 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005555 output += length;
5556 output_length -= length;
5557 tls12_prf->left_in_block -= length;
5558 }
5559
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005561}
John Durkop07cc04a2020-11-16 22:08:34 -08005562#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5563 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005564
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005565#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5566static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5567 psa_tls12_ecjpake_to_pms_t *ecjpake,
5568 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005570{
5571 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005572 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005573
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 if (output_length != 32) {
5575 return PSA_ERROR_INVALID_ARGUMENT;
5576 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005577
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5579 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5580 &output_size);
5581 if (status != PSA_SUCCESS) {
5582 return status;
5583 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005584
Gilles Peskine449bd832023-01-11 14:50:10 +01005585 if (output_size != output_length) {
5586 return PSA_ERROR_GENERIC_ERROR;
5587 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005588
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005590}
5591#endif
5592
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305593#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305594static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5595 psa_pbkdf2_key_derivation_t *pbkdf2,
5596 psa_algorithm_t prf_alg,
5597 uint8_t prf_output_length,
5598 psa_key_attributes_t *attributes)
5599{
5600 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305601 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305602 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305603 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305604 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305605 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305606 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305607
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305608 mac_operation.is_sign = 1;
5609 mac_operation.mac_size = prf_output_length;
5610 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305611
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305612 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5613 attributes,
5614 pbkdf2->password,
5615 pbkdf2->password_length,
5616 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305617 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305618 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305619 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305620 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5621 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305622 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305623 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305624 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305625 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305626 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305627 }
5628 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5629 &mac_output_length);
5630 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305631 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305632 }
5633
5634 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305635 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305636 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305637 }
5638
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305639 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305640
5641 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305642 /* We are passing prf_output_length as mac_size because the driver
5643 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305644 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305645 status = psa_driver_wrapper_mac_compute(attributes,
5646 pbkdf2->password,
5647 pbkdf2->password_length,
5648 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305649 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305650 &mac_output_length);
5651 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305652 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305653 }
5654
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305655 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305656 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305657
5658cleanup:
5659 /* Zeroise buffers to clear sensitive data from memory. */
5660 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5661 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305662}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305663
5664static psa_status_t psa_key_derivation_pbkdf2_read(
5665 psa_pbkdf2_key_derivation_t *pbkdf2,
5666 psa_algorithm_t kdf_alg,
5667 uint8_t *output,
5668 size_t output_length)
5669{
5670 psa_status_t status;
5671 psa_algorithm_t prf_alg;
5672 uint8_t prf_output_length;
5673 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5674 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5675 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5676
5677 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5678 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5679 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5680 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305681 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5682 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305683 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305684 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305685 } else {
5686 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305687 }
5688
5689 switch (pbkdf2->state) {
5690 case PSA_PBKDF2_STATE_PASSWORD_SET:
5691 /* Initially we need a new block so bytes_used is equal to block size*/
5692 pbkdf2->bytes_used = prf_output_length;
5693 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5694 break;
5695 case PSA_PBKDF2_STATE_OUTPUT:
5696 break;
5697 default:
5698 return PSA_ERROR_BAD_STATE;
5699 }
5700
5701 while (output_length != 0) {
5702 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5703 if (n > output_length) {
5704 n = (uint8_t) output_length;
5705 }
5706 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5707 output += n;
5708 output_length -= n;
5709 pbkdf2->bytes_used += n;
5710
5711 if (output_length == 0) {
5712 break;
5713 }
5714
5715 /* We need a new block */
5716 pbkdf2->bytes_used = 0;
5717 pbkdf2->block_number++;
5718
5719 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5720 prf_output_length,
5721 &attributes);
5722 if (status != PSA_SUCCESS) {
5723 return status;
5724 }
5725 }
5726
5727 return PSA_SUCCESS;
5728}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305729#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305730
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005731psa_status_t psa_key_derivation_output_bytes(
5732 psa_key_derivation_operation_t *operation,
5733 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005735{
5736 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005738
Gilles Peskine449bd832023-01-11 14:50:10 +01005739 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005740 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005741 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005742 }
5743
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005745 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005746 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005747 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005748 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005749 goto exit;
5750 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005751 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005752 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005753 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005754 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5755 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005756 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005757 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005759 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005760 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005761
Przemek Stekiel69c46792022-06-10 12:59:51 +02005762#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005763 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5764 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5765 output, output_length);
5766 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005767#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005768#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5769 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5771 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5772 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5773 kdf_alg, output,
5774 output_length);
5775 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005776#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5777 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005778#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005780 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5782 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005783#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305784#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305785 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305786 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5787 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305788 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305789#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005790
Gilles Peskineeab56e42018-07-12 17:12:33 +02005791 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005792 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005793 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005794 }
5795
5796exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005797 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005798 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005799 * This allows us to differentiate between exhausted operations and
5800 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5801 * operations. */
5802 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005803 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005804 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005806 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005808}
5809
David Brown7807bf72021-02-09 16:28:23 -07005810#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005811static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005812{
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 if (data_size >= 8) {
5814 mbedtls_des_key_set_parity(data);
5815 }
5816 if (data_size >= 16) {
5817 mbedtls_des_key_set_parity(data + 8);
5818 }
5819 if (data_size >= 24) {
5820 mbedtls_des_key_set_parity(data + 16);
5821 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005822}
David Brown7807bf72021-02-09 16:28:23 -07005823#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005824
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005825/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005826 * ECC keys on a Weierstrass elliptic curve require the generation
5827 * of a private key which is an integer
5828 * in the range [1, N - 1], where N is the boundary of the private key domain:
5829 * N is the prime p for Diffie-Hellman, or the order of the
5830 * curve’s base point for ECC.
5831 *
5832 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5833 * This function generates the private key using the following process:
5834 *
5835 * 1. Draw a byte string of length ceiling(m/8) bytes.
5836 * 2. If m is not a multiple of 8, set the most significant
5837 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5838 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5839 * 4. If k > N - 2, discard the result and return to step 1.
5840 * 5. Output k + 1 as the private key.
5841 *
5842 * This method allows compliance to NIST standards, specifically the methods titled
5843 * Key-Pair Generation by Testing Candidates in the following publications:
5844 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5845 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5846 * Diffie-Hellman keys.
5847 *
5848 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5849 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5850 *
5851 * Note: Function allocates memory for *data buffer, so given *data should be
5852 * always NULL.
5853 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005854#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5855#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005856static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005857 psa_key_slot_t *slot,
5858 size_t bits,
5859 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005860 uint8_t **data
5861 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005862{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005863 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005864 mbedtls_mpi k;
5865 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005866 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005867 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005868 size_t m;
5869 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005870
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 mbedtls_mpi_init(&k);
5872 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005873
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005874 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005876 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005878
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005880 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005881 goto cleanup;
5882 }
5883
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005884 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005886
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005888
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005889 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005890 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005891 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005892
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005893 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005894
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005895 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005897
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005898 /* Note: This function is always called with *data == NULL and it
5899 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005900 *data = mbedtls_calloc(1, m_bytes);
5901 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005902 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005903 goto cleanup;
5904 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005905
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005907 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005908 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005909 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005910 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005911
5912 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005914 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005915 * (8 * ceiling(m/8) - m) bits of the first byte in
5916 * the string to zero.
5917 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005918 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005919 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005920 }
5921
5922 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005923 * big-endian byte string.
5924 */
5925 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005926
5927 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 * Result of comparison is returned. When it indicates error
5929 * then this function is called again.
5930 */
5931 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005932 }
5933
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005934 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005935 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5936 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005937cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 if (ret != 0) {
5939 status = mbedtls_to_psa_error(ret);
5940 }
5941 if (status != PSA_SUCCESS) {
5942 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005943 *data = NULL;
5944 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005945 mbedtls_mpi_free(&k);
5946 mbedtls_mpi_free(&diff_N_2);
5947 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005948}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005949
5950/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5951 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5952 *
5953 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5954 * draw a 32-byte string and process it as specified in
5955 * Elliptic Curves for Security [RFC7748] §5.
5956 *
5957 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5958 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5959 *
5960 * Note: Function allocates memory for *data buffer, so given *data should be
5961 * always NULL.
5962 */
5963
5964static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5965 size_t bits,
5966 psa_key_derivation_operation_t *operation,
5967 uint8_t **data
5968 )
5969{
5970 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005971 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005972
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005974 case 255:
5975 output_length = 32;
5976 break;
5977 case 448:
5978 output_length = 56;
5979 break;
5980 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005982 break;
5983 }
5984
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005986
Gilles Peskine449bd832023-01-11 14:50:10 +01005987 if (*data == NULL) {
5988 return PSA_ERROR_INSUFFICIENT_MEMORY;
5989 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005990
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005992
Gilles Peskine449bd832023-01-11 14:50:10 +01005993 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005994 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005996
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005998 case 255:
5999 (*data)[0] &= 248;
6000 (*data)[31] &= 127;
6001 (*data)[31] |= 64;
6002 break;
6003 case 448:
6004 (*data)[0] &= 252;
6005 (*data)[55] |= 128;
6006 break;
6007 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006008 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006009 break;
6010 }
6011
6012 return status;
6013}
Valerio Setti86587ab2023-06-27 13:09:30 +02006014#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6015static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6016 psa_key_slot_t *slot, size_t bits,
6017 psa_key_derivation_operation_t *operation, uint8_t **data)
6018{
6019 (void) slot;
6020 (void) bits;
6021 (void) operation;
6022 (void) data;
6023 return PSA_ERROR_NOT_SUPPORTED;
6024}
6025
6026static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6027 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6028{
6029 (void) bits;
6030 (void) operation;
6031 (void) data;
6032 return PSA_ERROR_NOT_SUPPORTED;
6033}
6034#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6035#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006036
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006037static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006038 psa_key_slot_t *slot,
6039 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006040 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006041{
6042 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306044 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006045 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006046 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006047
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6049 return PSA_ERROR_INVALID_ARGUMENT;
6050 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006051
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006052#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006053 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6055 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6056 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006057 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6059 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006060 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006061 }
6062 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006063 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6065 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006066 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006068 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006069 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006070#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006071 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 if (key_type_is_raw_bytes(slot->attr.type)) {
6073 if (bits % 8 != 0) {
6074 return PSA_ERROR_INVALID_ARGUMENT;
6075 }
6076 data = mbedtls_calloc(1, bytes);
6077 if (data == NULL) {
6078 return PSA_ERROR_INSUFFICIENT_MEMORY;
6079 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006080
Gilles Peskine449bd832023-01-11 14:50:10 +01006081 status = psa_key_derivation_output_bytes(operation, data, bytes);
6082 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006083 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006084 }
David Brown12ca5032021-02-11 11:02:00 -07006085#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006086 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6087 psa_des_set_key_parity(data, bytes);
6088 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006089#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006090 } else {
6091 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006092 }
Ronald Crondd04d422020-11-28 15:14:42 +01006093
Ronald Cronbf33c932020-11-28 18:06:53 +01006094 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006095 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006097 };
6098
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6100 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6101 &storage_size);
6102 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306103 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 }
Archanad8a83dc2021-06-14 10:04:16 +05306105 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006106 status = psa_allocate_buffer_to_slot(slot, storage_size);
6107 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306108 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 }
Archanad8a83dc2021-06-14 10:04:16 +05306110
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 status = psa_driver_wrapper_import_key(&attributes,
6112 data, bytes,
6113 slot->key.data,
6114 slot->key.bytes,
6115 &slot->key.bytes, &bits);
6116 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006117 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006119
6120exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 mbedtls_free(data);
6122 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006123}
6124
Gilles Peskine449bd832023-01-11 14:50:10 +01006125psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6126 psa_key_derivation_operation_t *operation,
6127 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006128{
6129 psa_status_t status;
6130 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006131 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006132
Ronald Cron81709fc2020-11-14 12:10:32 +01006133 *key = MBEDTLS_SVC_KEY_ID_INIT;
6134
Gilles Peskine0f84d622019-09-12 19:03:13 +02006135 /* Reject any attempt to create a zero-length key so that we don't
6136 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006137 if (psa_get_key_bits(attributes) == 0) {
6138 return PSA_ERROR_INVALID_ARGUMENT;
6139 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006140
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 if (operation->alg == PSA_ALG_NONE) {
6142 return PSA_ERROR_BAD_STATE;
6143 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006144
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 if (!operation->can_output_key) {
6146 return PSA_ERROR_NOT_PERMITTED;
6147 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006148
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6150 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006151#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006152 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006153 /* Deriving a key in a secure element is not implemented yet. */
6154 status = PSA_ERROR_NOT_SUPPORTED;
6155 }
6156#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 if (status == PSA_SUCCESS) {
6158 status = psa_generate_derived_key_internal(slot,
6159 attributes->core.bits,
6160 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006161 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 if (status == PSA_SUCCESS) {
6163 status = psa_finish_key_creation(slot, driver, key);
6164 }
6165 if (status != PSA_SUCCESS) {
6166 psa_fail_key_creation(slot, driver);
6167 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006168
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006170}
6171
Gilles Peskineeab56e42018-07-12 17:12:33 +02006172
6173
6174/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006175/* Key derivation */
6176/****************************************************************/
6177
Steven Cooreman932ffb72021-02-15 12:14:32 +01006178#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006179static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006180{
6181#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6183 return 1;
6184 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006185#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006186#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006187 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6188 return 1;
6189 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006190#endif
6191#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006192 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6193 return 1;
6194 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006195#endif
6196#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6198 return 1;
6199 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006200#endif
6201#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6203 return 1;
6204 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006205#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006206#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6208 return 1;
6209 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006210#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306211#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6212 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6213 return 1;
6214 }
6215#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306216#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6217 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6218 return 1;
6219 }
6220#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006222}
6223
Gilles Peskine449bd832023-01-11 14:50:10 +01006224static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006225{
6226 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 psa_status_t status = psa_hash_setup(&operation, alg);
6228 psa_hash_abort(&operation);
6229 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006230}
6231
Gilles Peskine969c5d62019-01-16 15:53:06 +01006232static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006233 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006235{
Janos Follath5fe19732019-06-20 15:09:30 +01006236 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6237 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006239
Gilles Peskine969c5d62019-01-16 15:53:06 +01006240 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 if (!is_kdf_alg_supported(kdf_alg)) {
6242 return PSA_ERROR_NOT_SUPPORTED;
6243 }
John Durkop07cc04a2020-11-16 22:08:34 -08006244
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006245 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306246 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6248 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306249 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6250 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6251 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306252 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306253 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006254 if (hash_size == 0) {
6255 return PSA_ERROR_NOT_SUPPORTED;
6256 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006257
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006258 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6259 * we might fail later, which is somewhat unfriendly and potentially
6260 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006261 psa_status_t status = psa_hash_try_support(hash_alg);
6262 if (status != PSA_SUCCESS) {
6263 return status;
6264 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006265 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006266
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6268 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6269 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6270 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006271 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006272#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006273 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6275 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006276 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006278#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6279 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 operation->capacity = 255 * hash_size;
6281 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006282}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006283
Gilles Peskine449bd832023-01-11 14:50:10 +01006284static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006285{
6286#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006287 if (alg == PSA_ALG_ECDH) {
6288 return PSA_SUCCESS;
6289 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006290#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006291#if defined(PSA_WANT_ALG_FFDH)
6292 if (alg == PSA_ALG_FFDH) {
6293 return PSA_SUCCESS;
6294 }
6295#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006296 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006298}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006299
6300static int psa_key_derivation_allows_free_form_secret_input(
6301 psa_algorithm_t kdf_alg)
6302{
6303#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6304 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6305 return 0;
6306 }
6307#endif
6308 (void) kdf_alg;
6309 return 1;
6310}
John Durkop07cc04a2020-11-16 22:08:34 -08006311#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006312
Gilles Peskine449bd832023-01-11 14:50:10 +01006313psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6314 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006315{
6316 psa_status_t status;
6317
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 if (operation->alg != 0) {
6319 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006320 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006321
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6323 return PSA_ERROR_INVALID_ARGUMENT;
6324 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6325#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6326 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6327 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6328 status = psa_key_agreement_try_support(ka_alg);
6329 if (status != PSA_SUCCESS) {
6330 return status;
6331 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006332 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6333 return PSA_ERROR_INVALID_ARGUMENT;
6334 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6336#else
6337 return PSA_ERROR_NOT_SUPPORTED;
6338#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6339 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6340#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6341 status = psa_key_derivation_setup_kdf(operation, alg);
6342#else
6343 return PSA_ERROR_NOT_SUPPORTED;
6344#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6345 } else {
6346 return PSA_ERROR_INVALID_ARGUMENT;
6347 }
6348
6349 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006350 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006351 }
6352 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006353}
6354
Przemek Stekiel69c46792022-06-10 12:59:51 +02006355#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006356static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6357 psa_algorithm_t kdf_alg,
6358 psa_key_derivation_step_t step,
6359 const uint8_t *data,
6360 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006361{
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006363 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006365 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006366#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6368 return PSA_ERROR_INVALID_ARGUMENT;
6369 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006370#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006371 if (hkdf->state != HKDF_STATE_INIT) {
6372 return PSA_ERROR_BAD_STATE;
6373 } else {
6374 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6375 hash_alg,
6376 data, data_length);
6377 if (status != PSA_SUCCESS) {
6378 return status;
6379 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006380 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006382 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006383 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006384#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006386 /* We shouldn't be in different state as HKDF_EXPAND only allows
6387 * two inputs: SECRET (this case) and INFO which does not modify
6388 * the state. It could happen only if the hkdf
6389 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006390 if (hkdf->state != HKDF_STATE_INIT) {
6391 return PSA_ERROR_BAD_STATE;
6392 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006393
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006394 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6396 return PSA_ERROR_INVALID_ARGUMENT;
6397 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006398
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 memcpy(hkdf->prk, data, data_length);
6400 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006401#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006402 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006403 /* HKDF: If no salt was provided, use an empty salt.
6404 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006406#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6408 return PSA_ERROR_BAD_STATE;
6409 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006410#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6412 hash_alg,
6413 NULL, 0);
6414 if (status != PSA_SUCCESS) {
6415 return status;
6416 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006417 hkdf->state = HKDF_STATE_STARTED;
6418 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006419 if (hkdf->state != HKDF_STATE_STARTED) {
6420 return PSA_ERROR_BAD_STATE;
6421 }
6422 status = psa_mac_update(&hkdf->hmac,
6423 data, data_length);
6424 if (status != PSA_SUCCESS) {
6425 return status;
6426 }
6427 status = psa_mac_sign_finish(&hkdf->hmac,
6428 hkdf->prk,
6429 sizeof(hkdf->prk),
6430 &data_length);
6431 if (status != PSA_SUCCESS) {
6432 return status;
6433 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006434 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006435
Gilles Peskine2b522db2019-04-12 15:11:49 +02006436 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006437 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006438#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006439 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006440 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006442 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006444#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006445 {
6446 /* Block 0 is empty, and the next block will be
6447 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006449 }
6450
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006452 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006453#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006454 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6455 return PSA_ERROR_INVALID_ARGUMENT;
6456 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006457#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006458#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6460 hkdf->state == HKDF_STATE_INIT) {
6461 return PSA_ERROR_BAD_STATE;
6462 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006463#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006464 if (hkdf->state == HKDF_STATE_OUTPUT) {
6465 return PSA_ERROR_BAD_STATE;
6466 }
6467 if (hkdf->info_set) {
6468 return PSA_ERROR_BAD_STATE;
6469 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006470 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 if (data_length != 0) {
6472 hkdf->info = mbedtls_calloc(1, data_length);
6473 if (hkdf->info == NULL) {
6474 return PSA_ERROR_INSUFFICIENT_MEMORY;
6475 }
6476 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006477 }
6478 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006479 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006480 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006481 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006482 }
6483}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006484#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006485
John Durkop07cc04a2020-11-16 22:08:34 -08006486#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6487 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006488static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6489 const uint8_t *data,
6490 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006491{
Gilles Peskine449bd832023-01-11 14:50:10 +01006492 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6493 return PSA_ERROR_BAD_STATE;
6494 }
Janos Follathf08e2652019-06-13 09:05:41 +01006495
Gilles Peskine449bd832023-01-11 14:50:10 +01006496 if (data_length != 0) {
6497 prf->seed = mbedtls_calloc(1, data_length);
6498 if (prf->seed == NULL) {
6499 return PSA_ERROR_INSUFFICIENT_MEMORY;
6500 }
Janos Follathf08e2652019-06-13 09:05:41 +01006501
Gilles Peskine449bd832023-01-11 14:50:10 +01006502 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006503 prf->seed_length = data_length;
6504 }
Janos Follathf08e2652019-06-13 09:05:41 +01006505
Gilles Peskine43f958b2020-12-13 14:55:14 +01006506 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006507
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006509}
6510
Gilles Peskine449bd832023-01-11 14:50:10 +01006511static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6512 const uint8_t *data,
6513 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006514{
Gilles Peskine449bd832023-01-11 14:50:10 +01006515 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6516 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6517 return PSA_ERROR_BAD_STATE;
6518 }
Janos Follath81550542019-06-13 14:26:34 +01006519
Gilles Peskine449bd832023-01-11 14:50:10 +01006520 if (data_length != 0) {
6521 prf->secret = mbedtls_calloc(1, data_length);
6522 if (prf->secret == NULL) {
6523 return PSA_ERROR_INSUFFICIENT_MEMORY;
6524 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006525
Gilles Peskine449bd832023-01-11 14:50:10 +01006526 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006527 prf->secret_length = data_length;
6528 }
Janos Follath81550542019-06-13 14:26:34 +01006529
Gilles Peskine43f958b2020-12-13 14:55:14 +01006530 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006531
Gilles Peskine449bd832023-01-11 14:50:10 +01006532 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006533}
6534
Gilles Peskine449bd832023-01-11 14:50:10 +01006535static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6536 const uint8_t *data,
6537 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006538{
Gilles Peskine449bd832023-01-11 14:50:10 +01006539 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6540 return PSA_ERROR_BAD_STATE;
6541 }
Janos Follath63028dd2019-06-13 09:15:47 +01006542
Gilles Peskine449bd832023-01-11 14:50:10 +01006543 if (data_length != 0) {
6544 prf->label = mbedtls_calloc(1, data_length);
6545 if (prf->label == NULL) {
6546 return PSA_ERROR_INSUFFICIENT_MEMORY;
6547 }
Janos Follath63028dd2019-06-13 09:15:47 +01006548
Gilles Peskine449bd832023-01-11 14:50:10 +01006549 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006550 prf->label_length = data_length;
6551 }
Janos Follath63028dd2019-06-13 09:15:47 +01006552
Gilles Peskine43f958b2020-12-13 14:55:14 +01006553 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006554
Gilles Peskine449bd832023-01-11 14:50:10 +01006555 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006556}
6557
Gilles Peskine449bd832023-01-11 14:50:10 +01006558static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6559 psa_key_derivation_step_t step,
6560 const uint8_t *data,
6561 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006562{
Gilles Peskine449bd832023-01-11 14:50:10 +01006563 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006564 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006565 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006566 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006567 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006568 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006569 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006570 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006571 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006572 }
6573}
John Durkop07cc04a2020-11-16 22:08:34 -08006574#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6575 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6576
6577#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6578static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6579 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006580 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006581 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006582{
6583 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006584 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6585 4 + data_length + prf->other_secret_length :
6586 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006587
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6589 return PSA_ERROR_INVALID_ARGUMENT;
6590 }
John Durkop07cc04a2020-11-16 22:08:34 -08006591
Gilles Peskine449bd832023-01-11 14:50:10 +01006592 uint8_t *pms = mbedtls_calloc(1, pms_len);
6593 if (pms == NULL) {
6594 return PSA_ERROR_INSUFFICIENT_MEMORY;
6595 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006596 uint8_t *cur = pms;
6597
6598 /* pure-PSK:
6599 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006600 *
6601 * The premaster secret is formed as follows: if the PSK is N octets
6602 * long, concatenate a uint16 with the value N, N zero octets, a second
6603 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006604 *
6605 * mixed-PSK:
6606 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6607 * follows: concatenate a uint16 with the length of the other secret,
6608 * the other secret itself, uint16 with the length of PSK, and the
6609 * PSK itself.
6610 * For details please check:
6611 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6612 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6613 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006614 */
6615
Gilles Peskine449bd832023-01-11 14:50:10 +01006616 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6617 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6618 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6619 if (prf->other_secret_length != 0) {
6620 memcpy(cur, prf->other_secret, prf->other_secret_length);
6621 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006622 cur += prf->other_secret_length;
6623 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006624 } else {
6625 *cur++ = MBEDTLS_BYTE_1(data_length);
6626 *cur++ = MBEDTLS_BYTE_0(data_length);
6627 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006628 cur += data_length;
6629 }
6630
Gilles Peskine449bd832023-01-11 14:50:10 +01006631 *cur++ = MBEDTLS_BYTE_1(data_length);
6632 *cur++ = MBEDTLS_BYTE_0(data_length);
6633 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006634 cur += data_length;
6635
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006636 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006637
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006638 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006639 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006640}
Janos Follath6660f0e2019-06-17 08:44:03 +01006641
Przemek Stekiele47201b2022-04-19 13:53:28 +02006642static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6643 psa_tls12_prf_key_derivation_t *prf,
6644 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006645 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006646{
Gilles Peskine449bd832023-01-11 14:50:10 +01006647 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6648 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006649 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006650
6651 if (data_length != 0) {
6652 prf->other_secret = mbedtls_calloc(1, data_length);
6653 if (prf->other_secret == NULL) {
6654 return PSA_ERROR_INSUFFICIENT_MEMORY;
6655 }
6656
6657 memcpy(prf->other_secret, data, data_length);
6658 prf->other_secret_length = data_length;
6659 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006660 prf->other_secret_length = 0;
6661 }
6662
6663 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6664
Gilles Peskine449bd832023-01-11 14:50:10 +01006665 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006666}
6667
Janos Follath6660f0e2019-06-17 08:44:03 +01006668static psa_status_t psa_tls12_prf_psk_to_ms_input(
6669 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006670 psa_key_derivation_step_t step,
6671 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006672 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006673{
Gilles Peskine449bd832023-01-11 14:50:10 +01006674 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006675 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006676 return psa_tls12_prf_psk_to_ms_set_key(prf,
6677 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006678 break;
6679 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006680 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6681 data,
6682 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006683 break;
6684 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006685 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006686 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006687
Przemek Stekiele47201b2022-04-19 13:53:28 +02006688 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006689}
John Durkop07cc04a2020-11-16 22:08:34 -08006690#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006691
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006692#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6693static psa_status_t psa_tls12_ecjpake_to_pms_input(
6694 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006695 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006696 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006697 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006698{
Gilles Peskine449bd832023-01-11 14:50:10 +01006699 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6700 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6701 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006702 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006703
6704 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006705 if (data[0] != 0x04) {
6706 return PSA_ERROR_INVALID_ARGUMENT;
6707 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006708
6709 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006710 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006711
Gilles Peskine449bd832023-01-11 14:50:10 +01006712 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006713}
6714#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306715
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306716#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306717static psa_status_t psa_pbkdf2_set_input_cost(
6718 psa_pbkdf2_key_derivation_t *pbkdf2,
6719 psa_key_derivation_step_t step,
6720 uint64_t data)
6721{
6722 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6723 return PSA_ERROR_INVALID_ARGUMENT;
6724 }
6725
6726 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6727 return PSA_ERROR_BAD_STATE;
6728 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306729
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306730 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306731 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306732 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306733
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306734 if (data == 0) {
6735 return PSA_ERROR_INVALID_ARGUMENT;
6736 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306737
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306738 pbkdf2->input_cost = data;
6739 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6740
6741 return PSA_SUCCESS;
6742}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306743
6744static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6745 const uint8_t *data,
6746 size_t data_length)
6747{
Gilles Peskineead17662023-08-20 22:02:51 +02006748 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6749 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6750 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6751 /* Appending to existing salt. No state change. */
6752 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306753 return PSA_ERROR_BAD_STATE;
6754 }
6755
Gilles Peskineca57d782023-07-20 19:08:06 +02006756 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006757 /* Appending an empty string, nothing to do. */
6758 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306759 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306760
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306761 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6762 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306763 return PSA_ERROR_INSUFFICIENT_MEMORY;
6764 }
6765
Gilles Peskineead17662023-08-20 22:02:51 +02006766 if (pbkdf2->salt_length != 0) {
6767 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6768 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306769 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306770 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306771 mbedtls_free(pbkdf2->salt);
6772 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306773 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306774 return PSA_SUCCESS;
6775}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306776
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306777#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306778static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306779 const uint8_t *input,
6780 size_t input_len,
6781 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306782 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306783{
6784 psa_status_t status = PSA_SUCCESS;
6785 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6786 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306787 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306788 } else {
6789 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306790 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306791 }
6792 return status;
6793}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306794#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306795
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306796#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306797static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6798 size_t input_len,
6799 uint8_t *output,
6800 size_t *output_len)
6801{
6802 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306803 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306804 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306805 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306806 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6807 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6808 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306809 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306810 * mac_size as the driver function sets mac_output_length = mac_size
6811 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306812 status = psa_driver_wrapper_mac_compute(&attributes,
6813 zeros, sizeof(zeros),
6814 PSA_ALG_CMAC, input, input_len,
6815 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306816 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6817 128U,
6818 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306819 output_len);
6820 } else {
6821 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306822 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306823 }
6824 return status;
6825}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306826#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306827
6828static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306829 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306830 const uint8_t *data,
6831 size_t data_length)
6832{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306833 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306834 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6835 return PSA_ERROR_BAD_STATE;
6836 }
6837
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306838#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306839 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6840 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6841 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6842 pbkdf2->password,
6843 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306844 } else
6845#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6846#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6847 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306848 status = psa_pbkdf2_cmac_set_password(data, data_length,
6849 pbkdf2->password,
6850 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306851 } else
6852#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6853 {
6854 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306855 }
6856
6857 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6858
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306859 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306860}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306861
6862static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306863 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306864 psa_key_derivation_step_t step,
6865 const uint8_t *data,
6866 size_t data_length)
6867{
6868 switch (step) {
6869 case PSA_KEY_DERIVATION_INPUT_SALT:
6870 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6871 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306872 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306873 default:
6874 return PSA_ERROR_INVALID_ARGUMENT;
6875 }
6876}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306877#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306878
Gilles Peskineb8965192019-09-24 16:21:10 +02006879/** Check whether the given key type is acceptable for the given
6880 * input step of a key derivation.
6881 *
6882 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6883 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6884 * Both secret and non-secret inputs can alternatively have the type
6885 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6886 * that the input was passed as a buffer rather than via a key object.
6887 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006888static int psa_key_derivation_check_input_type(
6889 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006890 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006891{
Gilles Peskine449bd832023-01-11 14:50:10 +01006892 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006893 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006894 if (key_type == PSA_KEY_TYPE_DERIVE) {
6895 return PSA_SUCCESS;
6896 }
6897 if (key_type == PSA_KEY_TYPE_NONE) {
6898 return PSA_SUCCESS;
6899 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006900 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006901 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 if (key_type == PSA_KEY_TYPE_DERIVE) {
6903 return PSA_SUCCESS;
6904 }
6905 if (key_type == PSA_KEY_TYPE_NONE) {
6906 return PSA_SUCCESS;
6907 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006908 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006909 case PSA_KEY_DERIVATION_INPUT_LABEL:
6910 case PSA_KEY_DERIVATION_INPUT_SALT:
6911 case PSA_KEY_DERIVATION_INPUT_INFO:
6912 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006913 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6914 return PSA_SUCCESS;
6915 }
6916 if (key_type == PSA_KEY_TYPE_NONE) {
6917 return PSA_SUCCESS;
6918 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006919 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306920 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6921 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6922 return PSA_SUCCESS;
6923 }
6924 if (key_type == PSA_KEY_TYPE_DERIVE) {
6925 return PSA_SUCCESS;
6926 }
6927 if (key_type == PSA_KEY_TYPE_NONE) {
6928 return PSA_SUCCESS;
6929 }
6930 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006931 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006932 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006933}
6934
Janos Follathb80a94e2019-06-12 15:54:46 +01006935static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006936 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006937 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006938 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006939 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006940 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006941{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006942 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006944
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 status = psa_key_derivation_check_input_type(step, key_type);
6946 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006947 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006948 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006949
Przemek Stekiel69c46792022-06-10 12:59:51 +02006950#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006951 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6952 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6953 step, data, data_length);
6954 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006955#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006956#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006957 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6958 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6959 step, data, data_length);
6960 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006961#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6962#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006963 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6964 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6965 step, data, data_length);
6966 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006967#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006968#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006969 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006970 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006971 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6972 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006973#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306974#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306975 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306976 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6977 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306978 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306979#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006980 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006981 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006982 (void) data;
6983 (void) data_length;
6984 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006985 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006986 }
6987
Gilles Peskine224b0d62019-09-23 18:13:17 +02006988exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006989 if (status != PSA_SUCCESS) {
6990 psa_key_derivation_abort(operation);
6991 }
6992 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006993}
6994
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306995static psa_status_t psa_key_derivation_input_integer_internal(
6996 psa_key_derivation_operation_t *operation,
6997 psa_key_derivation_step_t step,
6998 uint64_t value)
6999{
7000 psa_status_t status;
7001 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
7002
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307003#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307004 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307005 status = psa_pbkdf2_set_input_cost(
7006 &operation->ctx.pbkdf2, step, value);
7007 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307008#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307009 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307010 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307011 (void) value;
7012 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307013 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307014 }
7015
7016 if (status != PSA_SUCCESS) {
7017 psa_key_derivation_abort(operation);
7018 }
7019 return status;
7020}
7021
Janos Follath51f4a0f2019-06-14 11:35:55 +01007022psa_status_t psa_key_derivation_input_bytes(
7023 psa_key_derivation_operation_t *operation,
7024 psa_key_derivation_step_t step,
7025 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007026 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007027{
Gilles Peskine449bd832023-01-11 14:50:10 +01007028 return psa_key_derivation_input_internal(operation, step,
7029 PSA_KEY_TYPE_NONE,
7030 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007031}
7032
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307033psa_status_t psa_key_derivation_input_integer(
7034 psa_key_derivation_operation_t *operation,
7035 psa_key_derivation_step_t step,
7036 uint64_t value)
7037{
7038 return psa_key_derivation_input_integer_internal(operation, step, value);
7039}
7040
Janos Follath51f4a0f2019-06-14 11:35:55 +01007041psa_status_t psa_key_derivation_input_key(
7042 psa_key_derivation_operation_t *operation,
7043 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007044 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007045{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007046 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007047 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007048 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007049
Ronald Cron5c522922020-11-14 16:35:34 +01007050 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007051 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7052 if (status != PSA_SUCCESS) {
7053 psa_key_derivation_abort(operation);
7054 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007055 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007056
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307057 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7058 * permission to output to a key object. */
7059 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7060 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007061 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007062 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007063
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 status = psa_key_derivation_input_internal(operation,
7065 step, slot->attr.type,
7066 slot->key.data,
7067 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007068
Gilles Peskine449bd832023-01-11 14:50:10 +01007069 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007070
Gilles Peskine449bd832023-01-11 14:50:10 +01007071 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007072}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007073
7074
7075
7076/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007077/* Key agreement */
7078/****************************************************************/
7079
Gilles Peskine449bd832023-01-11 14:50:10 +01007080psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7081 const uint8_t *key_buffer,
7082 size_t key_buffer_size,
7083 psa_algorithm_t alg,
7084 const uint8_t *peer_key,
7085 size_t peer_key_length,
7086 uint8_t *shared_secret,
7087 size_t shared_secret_size,
7088 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007089{
Gilles Peskine449bd832023-01-11 14:50:10 +01007090 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007091#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007092 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007093 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7094 key_buffer_size, alg,
7095 peer_key, peer_key_length,
7096 shared_secret,
7097 shared_secret_size,
7098 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007099#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007100
7101#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7102 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007103 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007104 peer_key,
7105 peer_key_length,
7106 key_buffer,
7107 key_buffer_size,
7108 shared_secret,
7109 shared_secret_size,
7110 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007111#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7112
Gilles Peskine01d718c2018-09-18 12:01:02 +02007113 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007114 (void) attributes;
7115 (void) key_buffer;
7116 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007117 (void) peer_key;
7118 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007119 (void) shared_secret;
7120 (void) shared_secret_size;
7121 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007122 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007123 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007124}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007125
Aditya Deshpande17845b82022-10-13 17:21:01 +01007126/** Internal function for raw key agreement
7127 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007128 * to the driver's implementation if a driver is present.
7129 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007130 * (psa_key_agreement_raw_builtin).
7131 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007132static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7133 psa_key_slot_t *private_key,
7134 const uint8_t *peer_key,
7135 size_t peer_key_length,
7136 uint8_t *shared_secret,
7137 size_t shared_secret_size,
7138 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007139{
Gilles Peskine449bd832023-01-11 14:50:10 +01007140 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7141 return PSA_ERROR_NOT_SUPPORTED;
7142 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007143
7144 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007145 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007146 };
7147
Gilles Peskine449bd832023-01-11 14:50:10 +01007148 return psa_driver_wrapper_key_agreement(&attributes,
7149 private_key->key.data,
7150 private_key->key.bytes, alg,
7151 peer_key, peer_key_length,
7152 shared_secret,
7153 shared_secret_size,
7154 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007155}
7156
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007157/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007158 * to potentially free embedded data structures and wipe confidential data.
7159 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007160static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7161 psa_key_derivation_step_t step,
7162 psa_key_slot_t *private_key,
7163 const uint8_t *peer_key,
7164 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007165{
7166 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007167 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007168 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007169 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007170
7171 /* Step 1: run the secret agreement algorithm to generate the shared
7172 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007173 status = psa_key_agreement_raw_internal(ka_alg,
7174 private_key,
7175 peer_key, peer_key_length,
7176 shared_secret,
7177 sizeof(shared_secret),
7178 &shared_secret_length);
7179 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007180 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007181 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007182
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007183 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007184 * the shared secret. A shared secret is permitted wherever a key
7185 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007186 status = psa_key_derivation_input_internal(operation, step,
7187 PSA_KEY_TYPE_DERIVE,
7188 shared_secret,
7189 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007190exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007191 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7192 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007193}
7194
Gilles Peskine449bd832023-01-11 14:50:10 +01007195psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7196 psa_key_derivation_step_t step,
7197 mbedtls_svc_key_id_t private_key,
7198 const uint8_t *peer_key,
7199 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007200{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007201 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007202 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007203 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007204
Gilles Peskine449bd832023-01-11 14:50:10 +01007205 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7206 return PSA_ERROR_INVALID_ARGUMENT;
7207 }
Ronald Cron5c522922020-11-14 16:35:34 +01007208 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007209 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7210 if (status != PSA_SUCCESS) {
7211 return status;
7212 }
7213 status = psa_key_agreement_internal(operation, step,
7214 slot,
7215 peer_key, peer_key_length);
7216 if (status != PSA_SUCCESS) {
7217 psa_key_derivation_abort(operation);
7218 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007219 /* If a private key has been added as SECRET, we allow the derived
7220 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007222 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007223 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007224 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007225
Gilles Peskine449bd832023-01-11 14:50:10 +01007226 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007227
Gilles Peskine449bd832023-01-11 14:50:10 +01007228 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007229}
7230
Gilles Peskine449bd832023-01-11 14:50:10 +01007231psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7232 mbedtls_svc_key_id_t private_key,
7233 const uint8_t *peer_key,
7234 size_t peer_key_length,
7235 uint8_t *output,
7236 size_t output_size,
7237 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007238{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007239 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007240 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007241 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007242 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007243
Gilles Peskine449bd832023-01-11 14:50:10 +01007244 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007245 status = PSA_ERROR_INVALID_ARGUMENT;
7246 goto exit;
7247 }
Ronald Cron5c522922020-11-14 16:35:34 +01007248 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007249 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7250 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007251 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007252 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007253
Gilles Peskine3e561302022-04-14 00:17:15 +02007254 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7255 * for the output size. The PSA specification only guarantees that this
7256 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7257 * but it might be nice to allow smaller buffers if the output fits.
7258 * At the time of writing this comment, with only ECDH implemented,
7259 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7260 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7261 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007262 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007263 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7264 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007265 status = PSA_ERROR_BUFFER_TOO_SMALL;
7266 goto exit;
7267 }
7268
Gilles Peskine449bd832023-01-11 14:50:10 +01007269 status = psa_key_agreement_raw_internal(alg, slot,
7270 peer_key, peer_key_length,
7271 output, output_size,
7272 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007273
7274exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007275 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007276 /* If an error happens and is not handled properly, the output
7277 * may be used as a key to protect sensitive data. Arrange for such
7278 * a key to be random, which is likely to result in decryption or
7279 * verification errors. This is better than filling the buffer with
7280 * some constant data such as zeros, which would result in the data
7281 * being protected with a reproducible, easily knowable key.
7282 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007283 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007284 *output_length = output_size;
7285 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007286
Gilles Peskine449bd832023-01-11 14:50:10 +01007287 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007288
Gilles Peskine449bd832023-01-11 14:50:10 +01007289 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007290}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007291
7292
Gilles Peskine30524eb2020-11-13 17:02:26 +01007293
Gilles Peskine86a440b2018-11-12 18:39:40 +01007294/****************************************************************/
7295/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007296/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007297
Gilles Peskine672a7712023-04-28 21:00:28 +02007298#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7299#include "entropy_poll.h"
7300#endif
7301
Gilles Peskine30524eb2020-11-13 17:02:26 +01007302/** Initialize the PSA random generator.
7303 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007304static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007305{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007306#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007307 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007308#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7309
Gilles Peskine30524eb2020-11-13 17:02:26 +01007310 /* Set default configuration if
7311 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007312 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007313 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007314 }
7315 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007316 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007317 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007318
Gilles Peskine449bd832023-01-11 14:50:10 +01007319 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007320#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7321 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7322 /* The PSA entropy injection feature depends on using NV seed as an entropy
7323 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007324 mbedtls_entropy_add_source(&rng->entropy,
7325 mbedtls_nv_seed_poll, NULL,
7326 MBEDTLS_ENTROPY_BLOCK_SIZE,
7327 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007328#endif
7329
Gilles Peskine449bd832023-01-11 14:50:10 +01007330 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007331#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007332}
7333
7334/** Deinitialize the PSA random generator.
7335 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007336static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007337{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007338#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007339 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007340#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007341 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7342 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007343#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007344}
7345
7346/** Seed the PSA random generator.
7347 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007348static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007349{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007350#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7351 /* Do nothing: the external RNG seeds itself. */
7352 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007353 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007354#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007355 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007356 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7357 drbg_seed, sizeof(drbg_seed) - 1);
7358 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007359#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007360}
7361
Gilles Peskine449bd832023-01-11 14:50:10 +01007362psa_status_t psa_generate_random(uint8_t *output,
7363 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007364{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007365 GUARD_MODULE_INITIALIZED;
7366
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007367#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7368
7369 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007370 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7371 output, output_size,
7372 &output_length);
7373 if (status != PSA_SUCCESS) {
7374 return status;
7375 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007376 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007377 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007378 if (output_length != output_size) {
7379 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7380 }
7381 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007382
7383#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7384
Gilles Peskine449bd832023-01-11 14:50:10 +01007385 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007386 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007387 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7388 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7389 output_size);
7390 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7391 output, request_size);
7392 if (ret != 0) {
7393 return mbedtls_to_psa_error(ret);
7394 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007395 output_size -= request_size;
7396 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007397 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007398 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007399#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007400}
7401
Gilles Peskine8814fc42020-12-14 15:33:44 +01007402/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007403 *
7404 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7405 * `psa_generate_random(...)`. The state parameter is ignored since the
7406 * PSA API doesn't support passing an explicit state.
7407 *
7408 * In the non-external case, psa_generate_random() calls an
7409 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7410 * and semantics as mbedtls_psa_get_random(). As an optimization,
7411 * instead of doing this back-and-forth between the PSA API and the
7412 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7413 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007414 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007415#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7416int mbedtls_psa_get_random(void *p_rng,
7417 unsigned char *output,
7418 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007419{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007420 /* This function takes a pointer to the RNG state because that's what
7421 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7422 * its own state internally and doesn't let the caller access that state.
7423 * So we just ignore the state parameter, and in practice we'll pass
7424 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007425 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007426 psa_status_t status = psa_generate_random(output, output_size);
7427 if (status == PSA_SUCCESS) {
7428 return 0;
7429 } else {
7430 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7431 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007432}
7433#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7434
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007435#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007436psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7437 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007438{
Gilles Peskine449bd832023-01-11 14:50:10 +01007439 if (global_data.initialized) {
7440 return PSA_ERROR_NOT_PERMITTED;
7441 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007442
Gilles Peskine449bd832023-01-11 14:50:10 +01007443 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7444 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7445 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7446 return PSA_ERROR_INVALID_ARGUMENT;
7447 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007448
Gilles Peskine449bd832023-01-11 14:50:10 +01007449 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007450}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007451#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007452
Ronald Cron3772afe2021-02-08 16:10:05 +01007453/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007454 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007455 * \param type The key type
7456 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007457 *
7458 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007459 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007460 * \retval #PSA_ERROR_INVALID_ARGUMENT
7461 * The size in bits of the key is not valid.
7462 * \retval #PSA_ERROR_NOT_SUPPORTED
7463 * The type and/or the size in bits of the key or the combination of
7464 * the two is not supported.
7465 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007466static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007467 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007468{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007469 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007470
Gilles Peskine449bd832023-01-11 14:50:10 +01007471 if (key_type_is_raw_bytes(type)) {
7472 status = psa_validate_unstructured_key_bit_size(type, bits);
7473 if (status != PSA_SUCCESS) {
7474 return status;
7475 }
7476 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007477#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007478 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7479 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7480 return PSA_ERROR_NOT_SUPPORTED;
7481 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007482 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007483 return PSA_ERROR_NOT_SUPPORTED;
7484 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007485
Ronald Cron01b2aba2020-10-05 09:42:02 +02007486 /* Accept only byte-aligned keys, for the same reasons as
7487 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007488 if (bits % 8 != 0) {
7489 return PSA_ERROR_NOT_SUPPORTED;
7490 }
7491 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007492#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007493
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007494#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007495 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007496 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007497 return PSA_SUCCESS;
7498 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007499#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007500
Valerio Settia55f0422023-07-10 15:34:41 +02007501#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007502 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007503 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007504 return PSA_ERROR_NOT_SUPPORTED;
7505 }
7506 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007507#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007508 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007509 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007510 }
7511
Gilles Peskine449bd832023-01-11 14:50:10 +01007512 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007513}
7514
Ronald Cron55ed0592020-10-05 10:30:40 +02007515psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007516 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007517 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007518{
7519 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007520 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007521
Gilles Peskine449bd832023-01-11 14:50:10 +01007522 if ((attributes->domain_parameters == NULL) &&
7523 (attributes->domain_parameters_size != 0)) {
7524 return PSA_ERROR_INVALID_ARGUMENT;
7525 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007526
Gilles Peskine449bd832023-01-11 14:50:10 +01007527 if (key_type_is_raw_bytes(type)) {
7528 status = psa_generate_random(key_buffer, key_buffer_size);
7529 if (status != PSA_SUCCESS) {
7530 return status;
7531 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007532
David Brown12ca5032021-02-11 11:02:00 -07007533#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007534 if (type == PSA_KEY_TYPE_DES) {
7535 psa_des_set_key_parity(key_buffer, key_buffer_size);
7536 }
David Brown8107e312021-02-12 08:08:46 -07007537#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007538 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007539
Valerio Setti76df8c12023-07-11 14:11:28 +02007540#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007541 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7542 return mbedtls_psa_rsa_generate_key(attributes,
7543 key_buffer,
7544 key_buffer_size,
7545 key_buffer_length);
7546 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007547#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007548
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007549#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007550 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7551 return mbedtls_psa_ecp_generate_key(attributes,
7552 key_buffer,
7553 key_buffer_size,
7554 key_buffer_length);
7555 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007556#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007557
Valerio Settia55f0422023-07-10 15:34:41 +02007558#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007559 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7560 return mbedtls_psa_ffdh_generate_key(attributes,
7561 key_buffer,
7562 key_buffer_size,
7563 key_buffer_length);
7564 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007565#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007566 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007567 (void) key_buffer_length;
7568 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007569 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007570
Gilles Peskine449bd832023-01-11 14:50:10 +01007571 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007572}
Darryl Green0c6575a2018-11-07 16:05:30 +00007573
Gilles Peskine449bd832023-01-11 14:50:10 +01007574psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7575 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007576{
7577 psa_status_t status;
7578 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007579 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007580 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007581
Ronald Cron81709fc2020-11-14 12:10:32 +01007582 *key = MBEDTLS_SVC_KEY_ID_INIT;
7583
Gilles Peskine0f84d622019-09-12 19:03:13 +02007584 /* Reject any attempt to create a zero-length key so that we don't
7585 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007586 if (psa_get_key_bits(attributes) == 0) {
7587 return PSA_ERROR_INVALID_ARGUMENT;
7588 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007589
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007590 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007591 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7592 return PSA_ERROR_INVALID_ARGUMENT;
7593 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007594
Gilles Peskine449bd832023-01-11 14:50:10 +01007595 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7596 &slot, &driver);
7597 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007598 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007599 }
Gilles Peskine11792082019-08-06 18:36:36 +02007600
Ronald Cron977c2472020-10-13 08:32:21 +02007601 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307602 * storage ( thus not in the case of generating a key in a secure element
7603 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7604 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007605 if (slot->key.data == NULL) {
7606 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7607 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007608 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007609 attributes->core.type, attributes->core.bits);
7610 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007611 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007612 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007613
7614 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007615 attributes->core.type,
7616 attributes->core.bits);
7617 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007618 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007619 attributes, &key_buffer_size);
7620 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007621 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007622 }
Ronald Cron977c2472020-10-13 08:32:21 +02007623 }
Ronald Cron977c2472020-10-13 08:32:21 +02007624
Gilles Peskine449bd832023-01-11 14:50:10 +01007625 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7626 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007627 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007628 }
Ronald Cron977c2472020-10-13 08:32:21 +02007629 }
7630
Gilles Peskine449bd832023-01-11 14:50:10 +01007631 status = psa_driver_wrapper_generate_key(attributes,
7632 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007633
Gilles Peskine449bd832023-01-11 14:50:10 +01007634 if (status != PSA_SUCCESS) {
7635 psa_remove_key_data_from_memory(slot);
7636 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007637
Gilles Peskine11792082019-08-06 18:36:36 +02007638exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007639 if (status == PSA_SUCCESS) {
7640 status = psa_finish_key_creation(slot, driver, key);
7641 }
7642 if (status != PSA_SUCCESS) {
7643 psa_fail_key_creation(slot, driver);
7644 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007645
Gilles Peskine449bd832023-01-11 14:50:10 +01007646 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007647}
7648
Gilles Peskinee59236f2018-01-27 23:32:46 +01007649/****************************************************************/
7650/* Module setup */
7651/****************************************************************/
7652
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007653#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007654psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007655 void (* entropy_init)(mbedtls_entropy_context *ctx),
7656 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007657{
Gilles Peskine449bd832023-01-11 14:50:10 +01007658 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7659 return PSA_ERROR_BAD_STATE;
7660 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007661 global_data.rng.entropy_init = entropy_init;
7662 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007663 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007664}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007665#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007666
Gilles Peskine449bd832023-01-11 14:50:10 +01007667void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007668{
Gilles Peskine449bd832023-01-11 14:50:10 +01007669 psa_wipe_all_key_slots();
7670 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7671 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007672 }
7673 /* Wipe all remaining data, including configuration.
7674 * In particular, this sets all state indicator to the value
7675 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007676 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007677
7678 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007679 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007680}
7681
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007682#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7683/** Recover a transaction that was interrupted by a power failure.
7684 *
7685 * This function is called during initialization, before psa_crypto_init()
7686 * returns. If this function returns a failure status, the initialization
7687 * fails.
7688 */
7689static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007690 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007691{
Gilles Peskine449bd832023-01-11 14:50:10 +01007692 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007693 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7694 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007695 /* TODO - fall through to the failure case until this
7696 * is implemented.
7697 * https://github.com/ARMmbed/mbed-crypto/issues/218
7698 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007699 default:
7700 /* We found an unsupported transaction in the storage.
7701 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007702 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007703 }
7704}
7705#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7706
Gilles Peskine449bd832023-01-11 14:50:10 +01007707psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007708{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007709 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007710
Gilles Peskinec6b69072018-11-20 21:42:52 +01007711 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007712 if (global_data.initialized != 0) {
7713 return PSA_SUCCESS;
7714 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007715
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007716 /* Init drivers */
7717 status = psa_driver_wrapper_init();
7718 if (status != PSA_SUCCESS) {
7719 goto exit;
7720 }
7721 global_data.drivers_initialized = 1;
7722
Gilles Peskine30524eb2020-11-13 17:02:26 +01007723 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007724 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007725 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007726 status = mbedtls_psa_random_seed(&global_data.rng);
7727 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007728 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007729 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007730 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007731
Gilles Peskine449bd832023-01-11 14:50:10 +01007732 status = psa_initialize_key_slots();
7733 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007734 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007735 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007736
Gilles Peskine4b734222019-07-24 15:56:31 +02007737#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007738 status = psa_crypto_load_transaction();
7739 if (status == PSA_SUCCESS) {
7740 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7741 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007742 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007743 }
7744 status = psa_crypto_stop_transaction();
7745 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007746 /* There's no transaction to complete. It's all good. */
7747 status = PSA_SUCCESS;
7748 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007749#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007750
Gilles Peskinec6b69072018-11-20 21:42:52 +01007751 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007752 global_data.initialized = 1;
7753
7754exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007755 if (status != PSA_SUCCESS) {
7756 mbedtls_psa_crypto_free();
7757 }
7758 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007759}
7760
Yanray Wangffc3c482023-07-11 12:01:04 +08007761#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007762psa_status_t psa_crypto_driver_pake_get_password_len(
7763 const psa_crypto_driver_pake_inputs_t *inputs,
7764 size_t *password_len)
7765{
7766 if (inputs->password_len == 0) {
7767 return PSA_ERROR_BAD_STATE;
7768 }
7769
7770 *password_len = inputs->password_len;
7771
7772 return PSA_SUCCESS;
7773}
7774
7775psa_status_t psa_crypto_driver_pake_get_password(
7776 const psa_crypto_driver_pake_inputs_t *inputs,
7777 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7778{
7779 if (inputs->password_len == 0) {
7780 return PSA_ERROR_BAD_STATE;
7781 }
7782
7783 if (buffer_size < inputs->password_len) {
7784 return PSA_ERROR_BUFFER_TOO_SMALL;
7785 }
7786
7787 memcpy(buffer, inputs->password, inputs->password_len);
7788 *buffer_length = inputs->password_len;
7789
7790 return PSA_SUCCESS;
7791}
7792
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007793psa_status_t psa_crypto_driver_pake_get_user_len(
7794 const psa_crypto_driver_pake_inputs_t *inputs,
7795 size_t *user_len)
7796{
7797 if (inputs->user_len == 0) {
7798 return PSA_ERROR_BAD_STATE;
7799 }
7800
7801 *user_len = inputs->user_len;
7802
7803 return PSA_SUCCESS;
7804}
7805
7806psa_status_t psa_crypto_driver_pake_get_user(
7807 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007808 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007809{
7810 if (inputs->user_len == 0) {
7811 return PSA_ERROR_BAD_STATE;
7812 }
7813
Przemek Stekielc0e62502023-03-14 11:49:36 +01007814 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007815 return PSA_ERROR_BUFFER_TOO_SMALL;
7816 }
7817
Przemek Stekielc0e62502023-03-14 11:49:36 +01007818 memcpy(user_id, inputs->user, inputs->user_len);
7819 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007820
7821 return PSA_SUCCESS;
7822}
7823
7824psa_status_t psa_crypto_driver_pake_get_peer_len(
7825 const psa_crypto_driver_pake_inputs_t *inputs,
7826 size_t *peer_len)
7827{
7828 if (inputs->peer_len == 0) {
7829 return PSA_ERROR_BAD_STATE;
7830 }
7831
7832 *peer_len = inputs->peer_len;
7833
7834 return PSA_SUCCESS;
7835}
7836
7837psa_status_t psa_crypto_driver_pake_get_peer(
7838 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007839 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007840{
7841 if (inputs->peer_len == 0) {
7842 return PSA_ERROR_BAD_STATE;
7843 }
7844
Przemek Stekielc0e62502023-03-14 11:49:36 +01007845 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007846 return PSA_ERROR_BUFFER_TOO_SMALL;
7847 }
7848
Przemek Stekielc0e62502023-03-14 11:49:36 +01007849 memcpy(peer_id, inputs->peer, inputs->peer_len);
7850 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007851
7852 return PSA_SUCCESS;
7853}
7854
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007855psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7856 const psa_crypto_driver_pake_inputs_t *inputs,
7857 psa_pake_cipher_suite_t *cipher_suite)
7858{
7859 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7860 return PSA_ERROR_BAD_STATE;
7861 }
7862
7863 *cipher_suite = inputs->cipher_suite;
7864
7865 return PSA_SUCCESS;
7866}
7867
Neil Armstronga7d08c32022-06-01 18:21:20 +02007868psa_status_t psa_pake_setup(
7869 psa_pake_operation_t *operation,
7870 const psa_pake_cipher_suite_t *cipher_suite)
7871{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007872 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007873
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007874 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007875 status = PSA_ERROR_BAD_STATE;
7876 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007877 }
7878
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007879 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007880 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007881 status = PSA_ERROR_INVALID_ARGUMENT;
7882 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007883 }
7884
Przemek Stekiel51eac532022-12-07 11:04:51 +01007885 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7886
Przemek Stekiele12ed362022-12-21 12:54:46 +01007887 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007888 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7889 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007890 operation->data.inputs.cipher_suite = *cipher_suite;
7891
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007892#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007893 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007894 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007895 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007896
David Horstmann16f01512023-06-14 17:21:07 +01007897 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007898 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007899 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007900#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007901 {
7902 status = PSA_ERROR_NOT_SUPPORTED;
7903 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007904 }
7905
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007906 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7907
Przemek Stekiel51eac532022-12-07 11:04:51 +01007908 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007909exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007910 psa_pake_abort(operation);
7911 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007912}
7913
7914psa_status_t psa_pake_set_password_key(
7915 psa_pake_operation_t *operation,
7916 mbedtls_svc_key_id_t password)
7917{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007918 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007919 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7920 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007921 psa_key_attributes_t attributes;
7922 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007923
Przemek Stekiel51eac532022-12-07 11:04:51 +01007924 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007925 status = PSA_ERROR_BAD_STATE;
7926 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007927 }
7928
Przemek Stekiel6c764412022-11-22 14:05:12 +01007929 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7930 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007931 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007932 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007933 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007934 }
7935
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007936 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007937 .core = slot->attr
7938 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007939
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007940 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007941
Przemek Stekiel51eac532022-12-07 11:04:51 +01007942 if (type != PSA_KEY_TYPE_PASSWORD &&
7943 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7944 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007945 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007946 }
7947
Przemek Stekiel51eac532022-12-07 11:04:51 +01007948 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7949 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007950 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007951 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007952 }
7953
7954 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7955 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007956 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007957exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007958 if (status != PSA_SUCCESS) {
7959 psa_pake_abort(operation);
7960 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007961 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007962 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007963}
7964
7965psa_status_t psa_pake_set_user(
7966 psa_pake_operation_t *operation,
7967 const uint8_t *user_id,
7968 size_t user_id_len)
7969{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007970 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007971
7972 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007973 status = PSA_ERROR_BAD_STATE;
7974 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007975 }
7976
Przemek Stekiel51eac532022-12-07 11:04:51 +01007977 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007978 status = PSA_ERROR_INVALID_ARGUMENT;
7979 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007980 }
7981
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007982 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007983 status = PSA_ERROR_BAD_STATE;
7984 goto exit;
7985 }
7986
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007987 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7988 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007989 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7990 goto exit;
7991 }
7992
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007993 memcpy(operation->data.inputs.user, user_id, user_id_len);
7994 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007995
7996 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007997exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007998 psa_pake_abort(operation);
7999 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008000}
8001
8002psa_status_t psa_pake_set_peer(
8003 psa_pake_operation_t *operation,
8004 const uint8_t *peer_id,
8005 size_t peer_id_len)
8006{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008007 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008008
8009 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008010 status = PSA_ERROR_BAD_STATE;
8011 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008012 }
8013
Przemek Stekiel51eac532022-12-07 11:04:51 +01008014 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008015 status = PSA_ERROR_INVALID_ARGUMENT;
8016 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008017 }
8018
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008019 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008020 status = PSA_ERROR_BAD_STATE;
8021 goto exit;
8022 }
8023
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008024 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8025 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008026 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8027 goto exit;
8028 }
8029
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008030 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8031 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008032
8033 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008034exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008035 psa_pake_abort(operation);
8036 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008037}
8038
8039psa_status_t psa_pake_set_role(
8040 psa_pake_operation_t *operation,
8041 psa_pake_role_t role)
8042{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008043 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008044
Przemek Stekiel51eac532022-12-07 11:04:51 +01008045 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008046 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008047 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008048 }
8049
Przemek Stekiel09104b82023-03-06 14:11:51 +01008050 switch (operation->alg) {
8051#if defined(PSA_WANT_ALG_JPAKE)
8052 case PSA_ALG_JPAKE:
8053 if (role == PSA_PAKE_ROLE_NONE) {
8054 return PSA_SUCCESS;
8055 }
8056 status = PSA_ERROR_INVALID_ARGUMENT;
8057 break;
8058#endif
8059 default:
8060 (void) role;
8061 status = PSA_ERROR_NOT_SUPPORTED;
8062 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008063 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008064exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008065 psa_pake_abort(operation);
8066 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008067}
8068
David Horstmanne7f21e62023-05-12 18:17:21 +01008069/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008070#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008071static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008072 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008073{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008074 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008075 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008076 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008077
David Horstmann024e5c52023-06-14 15:48:21 +01008078 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008079 is_x1 = (stage->outputs < 1);
8080 } else {
8081 is_x1 = (stage->inputs < 1);
8082 }
8083
David Horstmann74a3d8c2023-06-14 18:28:19 +01008084 key_share_step = is_x1 ?
8085 PSA_JPAKE_X1_STEP_KEY_SHARE :
8086 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008087 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008088 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8089 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8090 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8091 } else {
8092 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008093 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008094 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008095}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008096#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008097
Przemek Stekiel2797d372022-12-22 11:19:22 +01008098static psa_status_t psa_pake_complete_inputs(
8099 psa_pake_operation_t *operation)
8100{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008101 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008102 /* Create copy of the inputs on stack as inputs share memory
8103 with the driver context which will be setup by the driver. */
8104 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008105
Przemek Stekielfde11282023-03-13 16:06:09 +01008106 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008107 return PSA_ERROR_BAD_STATE;
8108 }
8109
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008110 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008111 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8112 return PSA_ERROR_BAD_STATE;
8113 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008114 }
8115
Przemek Stekiel18620a32023-01-17 16:34:52 +01008116 /* Clear driver context */
8117 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8118
8119 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008120
8121 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008122 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008123
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008124 /* User and peer are translated to role. */
8125 mbedtls_free(inputs.user);
8126 mbedtls_free(inputs.peer);
8127
Przemek Stekiel2797d372022-12-22 11:19:22 +01008128 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008129#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008130 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008131 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008132 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008133#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008134 {
8135 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008136 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008137 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008138 return status;
8139}
8140
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008141#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008142static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008143 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008144 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008145 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008146{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008147 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8148 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8149 step != PSA_PAKE_STEP_ZK_PROOF) {
8150 return PSA_ERROR_INVALID_ARGUMENT;
8151 }
8152
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008153 psa_jpake_computation_stage_t *computation_stage =
8154 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008155
David Horstmann5da95602023-06-08 15:37:12 +01008156 if (computation_stage->round != PSA_JPAKE_FIRST &&
8157 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008158 return PSA_ERROR_BAD_STATE;
8159 }
8160
David Horstmanne7f21e62023-05-12 18:17:21 +01008161 /* Check that the step we are given is the one we were expecting */
8162 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008163 return PSA_ERROR_BAD_STATE;
8164 }
8165
David Horstmanne7f21e62023-05-12 18:17:21 +01008166 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8167 computation_stage->inputs == 0 &&
8168 computation_stage->outputs == 0) {
8169 /* Start of the round, so function decides whether we are inputting
8170 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008171 computation_stage->io_mode = io_mode;
8172 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008173 /* Middle of the round so the mode we are in must match the function
8174 * called by the user */
8175 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008176 }
8177
8178 return PSA_SUCCESS;
8179}
8180
David Horstmanne7f21e62023-05-12 18:17:21 +01008181static psa_status_t psa_jpake_epilogue(
8182 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008183 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008184{
David Horstmanne7f21e62023-05-12 18:17:21 +01008185 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008186 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008187
David Horstmanne7f21e62023-05-12 18:17:21 +01008188 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8189 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008190 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008191 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008192 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008193 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008194 }
8195 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008196 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008197 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008198 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008199 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008200 }
8201 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008202 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8203 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008204 /* End of a round, move to the next round */
8205 stage->inputs = 0;
8206 stage->outputs = 0;
8207 stage->round++;
8208 }
8209 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008210 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008211 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008212 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008213 return PSA_SUCCESS;
8214}
David Horstmanne7f21e62023-05-12 18:17:21 +01008215
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008216#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008217
Neil Armstronga7d08c32022-06-01 18:21:20 +02008218psa_status_t psa_pake_output(
8219 psa_pake_operation_t *operation,
8220 psa_pake_step_t step,
8221 uint8_t *output,
8222 size_t output_size,
8223 size_t *output_length)
8224{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008225 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008226 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008227 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008228
8229 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008230 status = psa_pake_complete_inputs(operation);
8231 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008232 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008233 }
8234 }
8235
8236 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008237 status = PSA_ERROR_BAD_STATE;
8238 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008239 }
8240
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008241 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008242 status = PSA_ERROR_INVALID_ARGUMENT;
8243 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008244 }
8245
Przemek Stekiele12ed362022-12-21 12:54:46 +01008246 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008247#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008248 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008249 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008250 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008251 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008252 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008253 driver_step = convert_jpake_computation_stage_to_driver_step(
8254 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008255 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008256#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008257 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008258 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008259 status = PSA_ERROR_NOT_SUPPORTED;
8260 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008261 }
8262
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008263 status = psa_driver_wrapper_pake_output(operation, driver_step,
8264 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008265
8266 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008267 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008268 }
8269
8270 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008271#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008272 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008273 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008274 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008275 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008276 }
8277 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008278#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008279 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008280 status = PSA_ERROR_NOT_SUPPORTED;
8281 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008282 }
8283
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008284 return PSA_SUCCESS;
8285exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008286 psa_pake_abort(operation);
8287 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008288}
8289
8290psa_status_t psa_pake_input(
8291 psa_pake_operation_t *operation,
8292 psa_pake_step_t step,
8293 const uint8_t *input,
8294 size_t input_length)
8295{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008296 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008297 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008298 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8299 operation->primitive,
8300 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008301
8302 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008303 status = psa_pake_complete_inputs(operation);
8304 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008305 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008306 }
8307 }
8308
8309 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008310 status = PSA_ERROR_BAD_STATE;
8311 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008312 }
8313
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008314 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008315 status = PSA_ERROR_INVALID_ARGUMENT;
8316 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008317 }
8318
Przemek Stekiele12ed362022-12-21 12:54:46 +01008319 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008320#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008321 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008322 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008323 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008324 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008325 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008326 driver_step = convert_jpake_computation_stage_to_driver_step(
8327 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008328 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008329#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008330 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008331 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008332 status = PSA_ERROR_NOT_SUPPORTED;
8333 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008334 }
8335
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008336 status = psa_driver_wrapper_pake_input(operation, driver_step,
8337 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008338
8339 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008340 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008341 }
8342
8343 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008344#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008345 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008346 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008347 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008348 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008349 }
8350 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008351#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008352 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008353 status = PSA_ERROR_NOT_SUPPORTED;
8354 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008355 }
8356
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008357 return PSA_SUCCESS;
8358exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008359 psa_pake_abort(operation);
8360 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008361}
8362
8363psa_status_t psa_pake_get_implicit_key(
8364 psa_pake_operation_t *operation,
8365 psa_key_derivation_operation_t *output)
8366{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008367 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008368 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008369 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008370 size_t shared_key_len = 0;
8371
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008372 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008373 status = PSA_ERROR_BAD_STATE;
8374 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008375 }
8376
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008377#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008378 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008379 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008380 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008381 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008382 status = PSA_ERROR_BAD_STATE;
8383 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008384 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008385 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008386#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008387 {
8388 status = PSA_ERROR_NOT_SUPPORTED;
8389 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008390 }
8391
Przemek Stekiel0c781802022-11-29 14:53:13 +01008392 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8393 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008394 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008395 &shared_key_len);
8396
8397 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008398 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008399 }
8400
8401 status = psa_key_derivation_input_bytes(output,
8402 PSA_KEY_DERIVATION_INPUT_SECRET,
8403 shared_key,
8404 shared_key_len);
8405
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008406 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008407exit:
8408 abort_status = psa_pake_abort(operation);
8409 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008410}
8411
8412psa_status_t psa_pake_abort(
8413 psa_pake_operation_t *operation)
8414{
Przemek Stekield69dca92023-01-31 19:59:20 +01008415 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008416
Przemek Stekield69dca92023-01-31 19:59:20 +01008417 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008418 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008419 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008420
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008421 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8422 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008423 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008424 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008425 }
8426 if (operation->data.inputs.user != NULL) {
8427 mbedtls_free(operation->data.inputs.user);
8428 }
8429 if (operation->data.inputs.peer != NULL) {
8430 mbedtls_free(operation->data.inputs.peer);
8431 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008432 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008433 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008434
Przemek Stekield69dca92023-01-31 19:59:20 +01008435 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008436}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008437#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008438
Gilles Peskinee59236f2018-01-27 23:32:46 +01008439#endif /* MBEDTLS_PSA_CRYPTO_C */