blob: 4beda811240978d6fd28a8007bf04d17d1170fee [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;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001403#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1404 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001405 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001406 default:
1407 /* Nothing else to do. */
1408 break;
1409 }
1410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 if (status != PSA_SUCCESS) {
1412 psa_reset_key_attributes(attributes);
1413 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001416
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001418}
1419
Gilles Peskinec8000c02019-08-02 20:15:51 +02001420#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1421psa_status_t psa_get_key_slot_number(
1422 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001424{
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001426 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 return PSA_SUCCESS;
1428 } else {
1429 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001430 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001431}
1432#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1435 size_t key_buffer_size,
1436 uint8_t *data,
1437 size_t data_size,
1438 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001439{
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 if (key_buffer_size > data_size) {
1441 return PSA_ERROR_BUFFER_TOO_SMALL;
1442 }
1443 memcpy(data, key_buffer, key_buffer_size);
1444 memset(data + key_buffer_size, 0,
1445 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001446 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001448}
1449
Ronald Cron7285cda2020-11-26 14:46:37 +01001450psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001451 const psa_key_attributes_t *attributes,
1452 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001454{
Ronald Crond18b5f82020-11-25 16:46:05 +01001455 psa_key_type_t type = attributes->core.type;
1456
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 if (key_type_is_raw_bytes(type) ||
1458 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001459 PSA_KEY_TYPE_IS_ECC(type) ||
1460 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 return psa_export_key_buffer_internal(
1462 key_buffer, key_buffer_size,
1463 data, data_size, data_length);
1464 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001465 /* This shouldn't happen in the reference implementation, but
1466 it is valid for a special-purpose implementation to omit
1467 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001469 }
1470}
1471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1473 uint8_t *data,
1474 size_t data_size,
1475 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001476{
1477 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1478 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1479 psa_key_slot_t *slot;
1480
Ronald Crone907e552021-01-18 13:22:38 +01001481 /* Reject a zero-length output buffer now, since this can never be a
1482 * valid key representation. This way we know that data must be a valid
1483 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 if (data_size == 0) {
1485 return PSA_ERROR_BUFFER_TOO_SMALL;
1486 }
Ronald Crone907e552021-01-18 13:22:38 +01001487
Ronald Cron9486f9d2020-11-26 13:36:23 +01001488 /* Set the key to empty now, so that even when there are errors, we always
1489 * set data_length to a value between 0 and data_size. On error, setting
1490 * the key to empty is a good choice because an empty key representation is
1491 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001492 *data_length = 0;
1493
Ronald Cron9486f9d2020-11-26 13:36:23 +01001494 /* Export requires the EXPORT flag. There is an exception for public keys,
1495 * which don't require any flag, but
1496 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1497 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1499 PSA_KEY_USAGE_EXPORT, 0);
1500 if (status != PSA_SUCCESS) {
1501 return status;
1502 }
mohammad160306e79202018-03-28 13:17:44 +03001503
Ronald Crond18b5f82020-11-25 16:46:05 +01001504 psa_key_attributes_t attributes = {
1505 .core = slot->attr
1506 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 status = psa_driver_wrapper_export_key(&attributes,
1508 slot->key.data, slot->key.bytes,
1509 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001510
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001512
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001514}
1515
Ronald Cron7285cda2020-11-26 14:46:37 +01001516psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001517 const psa_key_attributes_t *attributes,
1518 const uint8_t *key_buffer,
1519 size_t key_buffer_size,
1520 uint8_t *data,
1521 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001523{
Ronald Crond18b5f82020-11-25 16:46:05 +01001524 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001525
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001526 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001527 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1528 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001529 /* Exporting public -> public */
1530 return psa_export_key_buffer_internal(
1531 key_buffer, key_buffer_size,
1532 data, data_size, data_length);
1533 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001534#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001535 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1536 return mbedtls_psa_rsa_export_public_key(attributes,
1537 key_buffer,
1538 key_buffer_size,
1539 data,
1540 data_size,
1541 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001542#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001543 /* We don't know how to convert a private RSA key to public. */
1544 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001545#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001546 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001547 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001548#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001549 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1550 return mbedtls_psa_ecp_export_public_key(attributes,
1551 key_buffer,
1552 key_buffer_size,
1553 data,
1554 data_size,
1555 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001556#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001557 /* We don't know how to convert a private ECC key to public */
1558 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001559#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001560 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001561 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001562#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001563 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001564 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001565 key_buffer,
1566 key_buffer_size,
1567 data, data_size,
1568 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001569#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001570 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001571#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001572 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001574 (void) key_buffer;
1575 (void) key_buffer_size;
1576 (void) data;
1577 (void) data_size;
1578 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001580 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001581}
Ronald Crond18b5f82020-11-25 16:46:05 +01001582
Gilles Peskine449bd832023-01-11 14:50:10 +01001583psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1584 uint8_t *data,
1585 size_t data_size,
1586 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001587{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001588 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001589 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001590 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001591 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001592
Ronald Crone907e552021-01-18 13:22:38 +01001593 /* Reject a zero-length output buffer now, since this can never be a
1594 * valid key representation. This way we know that data must be a valid
1595 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 if (data_size == 0) {
1597 return PSA_ERROR_BUFFER_TOO_SMALL;
1598 }
Ronald Crone907e552021-01-18 13:22:38 +01001599
Darryl Greendd8fb772018-11-07 16:00:44 +00001600 /* Set the key to empty now, so that even when there are errors, we always
1601 * set data_length to a value between 0 and data_size. On error, setting
1602 * the key to empty is a good choice because an empty key representation is
1603 * unlikely to be accepted anywhere. */
1604 *data_length = 0;
1605
1606 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1608 if (status != PSA_SUCCESS) {
1609 return status;
1610 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001611
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1613 status = PSA_ERROR_INVALID_ARGUMENT;
1614 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001615 }
1616
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001617 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001618 .core = slot->attr
1619 };
Ronald Cron67227982020-11-26 15:16:05 +01001620 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001621 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001623
1624exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001628}
1629
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001630MBEDTLS_STATIC_ASSERT(
1631 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1632 "One or more key attribute flag is listed as both external-only and dual-use")
1633MBEDTLS_STATIC_ASSERT(
1634 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1635 "One or more key attribute flag is listed as both internal-only and dual-use")
1636MBEDTLS_STATIC_ASSERT(
1637 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1638 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001639
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001640/** Validate that a key policy is internally well-formed.
1641 *
1642 * This function only rejects invalid policies. It does not validate the
1643 * consistency of the policy with respect to other attributes of the key
1644 * such as the key type.
1645 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001646static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001647{
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1649 PSA_KEY_USAGE_COPY |
1650 PSA_KEY_USAGE_ENCRYPT |
1651 PSA_KEY_USAGE_DECRYPT |
1652 PSA_KEY_USAGE_SIGN_MESSAGE |
1653 PSA_KEY_USAGE_VERIFY_MESSAGE |
1654 PSA_KEY_USAGE_SIGN_HASH |
1655 PSA_KEY_USAGE_VERIFY_HASH |
1656 PSA_KEY_USAGE_VERIFY_DERIVATION |
1657 PSA_KEY_USAGE_DERIVE)) != 0) {
1658 return PSA_ERROR_INVALID_ARGUMENT;
1659 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001660
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001662}
1663
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001664/** Validate the internal consistency of key attributes.
1665 *
1666 * This function only rejects invalid attribute values. If does not
1667 * validate the consistency of the attributes with any key data that may
1668 * be involved in the creation of the key.
1669 *
1670 * Call this function early in the key creation process.
1671 *
1672 * \param[in] attributes Key attributes for the new key.
1673 * \param[out] p_drv On any return, the driver for the key, if any.
1674 * NULL for a transparent key.
1675 *
1676 */
1677static psa_status_t psa_validate_key_attributes(
1678 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001680{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001681 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1683 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 status = psa_validate_key_location(lifetime, p_drv);
1686 if (status != PSA_SUCCESS) {
1687 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001688 }
1689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 status = psa_validate_key_persistence(lifetime);
1691 if (status != PSA_SUCCESS) {
1692 return status;
1693 }
1694
1695 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1696 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1697 return PSA_ERROR_INVALID_ARGUMENT;
1698 }
1699 } else {
1700 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1701 return PSA_ERROR_INVALID_ARGUMENT;
1702 }
1703 }
1704
1705 status = psa_validate_key_policy(&attributes->core.policy);
1706 if (status != PSA_SUCCESS) {
1707 return status;
1708 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001709
1710 /* Refuse to create overly large keys.
1711 * Note that this doesn't trigger on import if the attributes don't
1712 * explicitly specify a size (so psa_get_key_bits returns 0), so
1713 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1715 return PSA_ERROR_NOT_SUPPORTED;
1716 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001717
Gilles Peskine91e8c332019-08-02 19:19:39 +02001718 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1720 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1721 return PSA_ERROR_INVALID_ARGUMENT;
1722 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001723
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001725}
1726
Gilles Peskine4747d192019-04-17 15:05:45 +02001727/** Prepare a key slot to receive key material.
1728 *
1729 * This function allocates a key slot and sets its metadata.
1730 *
1731 * If this function fails, call psa_fail_key_creation().
1732 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001733 * This function is intended to be used as follows:
1734 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001735 * it with the specified attributes, and in case of a volatile key assign it
1736 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001737 * -# Populate the slot with the key material.
1738 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1739 * In case of failure at any step, stop the sequence and call
1740 * psa_fail_key_creation().
1741 *
Ronald Cron5c522922020-11-14 16:35:34 +01001742 * On success, the key slot is locked. It is the responsibility of the caller
1743 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001744 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001745 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001746 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001747 * \param[out] p_slot On success, a pointer to the prepared slot.
1748 * \param[out] p_drv On any return, the driver for the key, if any.
1749 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001750 *
1751 * \retval #PSA_SUCCESS
1752 * The key slot is ready to receive key material.
1753 * \return If this function fails, the key slot is an invalid state.
1754 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001755 */
1756static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001757 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001758 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001759 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001761{
1762 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001763 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001764 psa_key_slot_t *slot;
1765
Gilles Peskinedf179142019-07-15 22:02:14 +02001766 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001767 *p_drv = NULL;
1768
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 status = psa_validate_key_attributes(attributes, p_drv);
1770 if (status != PSA_SUCCESS) {
1771 return status;
1772 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001773
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1775 if (status != PSA_SUCCESS) {
1776 return status;
1777 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001778 slot = *p_slot;
1779
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001780 /* We're storing the declared bit-size of the key. It's up to each
1781 * creation mechanism to verify that this information is correct.
1782 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001783 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001784 * is optional (import, copy). In case of a volatile key, assign it the
1785 * volatile key identifier associated to the slot returned to contain its
1786 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001787
1788 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001790#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1791 slot->attr.id = volatile_key_id;
1792#else
1793 slot->attr.id.key_id = volatile_key_id;
1794#endif
1795 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001796
Gilles Peskine91e8c332019-08-02 19:19:39 +02001797 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001798 * external-only flags, query `attributes`. Thanks to the check
1799 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001800 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001801 * may have set. */
1802 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001803
Gilles Peskinecbaff462019-07-12 23:46:04 +02001804#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001805 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001806 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001807 * create the key file in internal storage, create the
1808 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001809 * persistent data. This is done by starting a transaction that will
1810 * encompass these three actions.
1811 * For registering a volatile key, we just need to find an appropriate
1812 * slot number inside the SE. Since the key is designated volatile, creating
1813 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001814 /* The first thing to do is to find a slot number for the new key.
1815 * We save the slot number in persistent storage as part of the
1816 * transaction data. It will be needed to recover if the power
1817 * fails during the key creation process, to clean up on the secure
1818 * element side after restarting. Obtaining a slot number from the
1819 * secure element driver updates its persistent state, but we do not yet
1820 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001821 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001823 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1825 &slot_number);
1826 if (status != PSA_SUCCESS) {
1827 return status;
1828 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1831 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001832 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001833 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001834 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 status = psa_crypto_save_transaction();
1836 if (status != PSA_SUCCESS) {
1837 (void) psa_crypto_stop_transaction();
1838 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001839 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001840 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001841
1842 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Ryan Everettac5b32b2023-11-15 16:26:01 +00001844 if (status != PSA_SUCCESS) {
1845 return status;
1846 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001847 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001848
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001850 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001852 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001853#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1854
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001856}
Gilles Peskine4747d192019-04-17 15:05:45 +02001857
1858/** Finalize the creation of a key once its key material has been set.
1859 *
1860 * This entails writing the key to persistent storage.
1861 *
1862 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001863 * See the documentation of psa_start_key_creation() for the intended use
1864 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001865 *
Ronald Cron5c522922020-11-14 16:35:34 +01001866 * If the finalization succeeds, the function unlocks the key slot (it was
1867 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1868 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001869 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001870 * \param[in,out] slot Pointer to the slot with key material.
1871 * \param[in] driver The secure element driver for the key,
1872 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001873 * \param[out] key On success, identifier of the key. Note that the
1874 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001875 *
1876 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001877 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001878 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1879 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1880 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1881 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1882 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1883 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001884 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001885 * \return If this function fails, the key slot is an invalid state.
1886 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001887 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001888static psa_status_t psa_finish_key_creation(
1889 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001890 psa_se_drv_table_entry_t *driver,
1891 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001892{
1893 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001894 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001895 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001896
1897#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001899#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001901 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001902 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001904
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001905 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1906 sizeof(data.slot_number),
1907 "Slot number size does not match psa_se_key_data_storage_t");
1908
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1910 status = psa_save_persistent_key(&slot->attr,
1911 (uint8_t *) &data,
1912 sizeof(data));
1913 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001914#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1915 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001916 /* Key material is saved in export representation in the slot, so
1917 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 status = psa_save_persistent_key(&slot->attr,
1919 slot->key.data,
1920 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001921 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001922 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001923#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1924
Gilles Peskinecbaff462019-07-12 23:46:04 +02001925#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001926 /* Finish the transaction for a key creation. This does not
1927 * happen when registering an existing key. Detect this case
1928 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001929 * creation of a persistent key in a secure element requires a transaction,
1930 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 if (driver != NULL &&
1932 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1933 status = psa_save_se_persistent_data(driver);
1934 if (status != PSA_SUCCESS) {
1935 psa_destroy_persistent_key(slot->attr.id);
1936 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001937 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001939 }
1940#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1941
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001943 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 status = psa_unlock_key_slot(slot);
1945 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001946 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001948 }
Ronald Cron50972942020-11-14 11:28:25 +01001949
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001951}
1952
1953/** Abort the creation of a key.
1954 *
1955 * You may call this function after calling psa_start_key_creation(),
1956 * or after psa_finish_key_creation() fails. In other circumstances, this
1957 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001958 * See the documentation of psa_start_key_creation() for the intended use
1959 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001960 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001961 * \param[in,out] slot Pointer to the slot with key material.
1962 * \param[in] driver The secure element driver for the key,
1963 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001964 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001965static void psa_fail_key_creation(psa_key_slot_t *slot,
1966 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001967{
Gilles Peskine011e4282019-06-26 18:34:38 +02001968 (void) driver;
1969
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001971 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001973
1974#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001975 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001976 * element, and the failure happened later (when saving metadata
1977 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001978 * element.
1979 * https://github.com/ARMmbed/mbed-crypto/issues/217
1980 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001981
Gilles Peskined7729582019-08-05 15:55:54 +02001982 /* Abort the ongoing transaction if any (there may not be one if
1983 * the creation process failed before starting one, or if the
1984 * key creation is a registration of a key in a secure element).
1985 * Earlier functions must already have done what it takes to undo any
1986 * partial creation. All that's left is to update the transaction data
1987 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001989#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1990
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001992}
1993
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001994/** Validate optional attributes during key creation.
1995 *
1996 * Some key attributes are optional during key creation. If they are
1997 * specified in the attributes structure, check that they are consistent
1998 * with the data in the slot.
1999 *
2000 * This function should be called near the end of key creation, after
2001 * the slot in memory is fully populated but before saving persistent data.
2002 */
2003static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002004 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002006{
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 if (attributes->core.type != 0) {
2008 if (attributes->core.type != slot->attr.type) {
2009 return PSA_ERROR_INVALID_ARGUMENT;
2010 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002011 }
2012
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02002014#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
2015 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2017 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002018 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002019 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002020 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002021
Ronald Cron90857082020-11-25 14:58:33 +01002022 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 slot->attr.type,
2024 slot->key.data,
2025 slot->key.bytes,
2026 &rsa);
2027 if (status != PSA_SUCCESS) {
2028 return status;
2029 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002030
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 mbedtls_mpi_init(&actual);
2032 mbedtls_mpi_init(&required);
2033 ret = mbedtls_rsa_export(rsa,
2034 NULL, NULL, NULL, NULL, &actual);
2035 mbedtls_rsa_free(rsa);
2036 mbedtls_free(rsa);
2037 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002038 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 }
2040 ret = mbedtls_mpi_read_binary(&required,
2041 attributes->domain_parameters,
2042 attributes->domain_parameters_size);
2043 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002044 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 }
2046 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002047 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 }
2049rsa_exit:
2050 mbedtls_mpi_free(&actual);
2051 mbedtls_mpi_free(&required);
2052 if (ret != 0) {
2053 return mbedtls_to_psa_error(ret);
2054 }
2055 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02002056#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
2057 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08002058 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002059 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002061 }
2062 }
2063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 if (attributes->core.bits != 0) {
2065 if (attributes->core.bits != slot->attr.bits) {
2066 return PSA_ERROR_INVALID_ARGUMENT;
2067 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002068 }
2069
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002071}
2072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2074 const uint8_t *data,
2075 size_t data_length,
2076 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002077{
2078 psa_status_t status;
2079 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002080 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002081 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302082 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002083
Ronald Cron81709fc2020-11-14 12:10:32 +01002084 *key = MBEDTLS_SVC_KEY_ID_INIT;
2085
Gilles Peskine0f84d622019-09-12 19:03:13 +02002086 /* Reject zero-length symmetric keys (including raw data key objects).
2087 * This also rejects any key which might be encoded as an empty string,
2088 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 if (data_length == 0) {
2090 return PSA_ERROR_INVALID_ARGUMENT;
2091 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002092
Archana449608b2021-09-08 15:36:05 +05302093 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 if (data_length > SIZE_MAX / 8) {
2095 return PSA_ERROR_NOT_SUPPORTED;
2096 }
Archana6ed4bda2021-08-04 10:47:15 +05302097
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2099 &slot, &driver);
2100 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002101 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002103
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002104 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302105 * storage ( thus not in the case of importing a key in a secure element
2106 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2107 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 if (slot->key.data == NULL) {
2109 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302110 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 attributes, data, data_length, &storage_size);
2112 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302113 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 }
Archanad8a83dc2021-06-14 10:04:16 +05302115 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 status = psa_allocate_buffer_to_slot(slot, storage_size);
2117 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002118 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002120 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002121
2122 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 status = psa_driver_wrapper_import_key(attributes,
2124 data, data_length,
2125 slot->key.data,
2126 slot->key.bytes,
2127 &slot->key.bytes, &bits);
2128 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002129 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002131
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002133 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002135 status = PSA_ERROR_INVALID_ARGUMENT;
2136 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002137 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002138
Archana6ed4bda2021-08-04 10:47:15 +05302139 /* Enforce a size limit, and in particular ensure that the bit
2140 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302142 status = PSA_ERROR_NOT_SUPPORTED;
2143 goto exit;
2144 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 status = psa_validate_optional_attributes(slot, attributes);
2146 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002147 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002149
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002151exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 if (status != PSA_SUCCESS) {
2153 psa_fail_key_creation(slot, driver);
2154 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002155
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002157}
2158
Gilles Peskined7729582019-08-05 15:55:54 +02002159#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2160psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002162{
2163 psa_status_t status;
2164 psa_key_slot_t *slot = NULL;
2165 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002166 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002167
2168 /* Leaving attributes unspecified is not currently supported.
2169 * It could make sense to query the key type and size from the
2170 * secure element, but not all secure elements support this
2171 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2173 return PSA_ERROR_NOT_SUPPORTED;
2174 }
2175 if (psa_get_key_bits(attributes) == 0) {
2176 return PSA_ERROR_NOT_SUPPORTED;
2177 }
Gilles Peskined7729582019-08-05 15:55:54 +02002178
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2180 &slot, &driver);
2181 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002182 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 }
Gilles Peskined7729582019-08-05 15:55:54 +02002184
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002186
2187exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 if (status != PSA_SUCCESS) {
2189 psa_fail_key_creation(slot, driver);
2190 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002191
Gilles Peskined7729582019-08-05 15:55:54 +02002192 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 psa_close_key(key);
2194 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002195}
2196#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2197
Gilles Peskine449bd832023-01-11 14:50:10 +01002198psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2199 const psa_key_attributes_t *specified_attributes,
2200 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002201{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002202 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002203 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002204 psa_key_slot_t *source_slot = NULL;
2205 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002206 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002207 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302208 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002209
Ronald Cron81709fc2020-11-14 12:10:32 +01002210 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2211
Archana8a180362021-07-05 02:18:48 +05302212 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2214 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002215 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002217
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 status = psa_validate_optional_attributes(source_slot,
2219 specified_attributes);
2220 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002221 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002223
Archana9d17bf42021-09-10 06:22:44 +05302224 /* The target key type and number of bits have been validated by
2225 * psa_validate_optional_attributes() to be either equal to zero or
2226 * equal to the ones of the source key. So it is safe to inherit
2227 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302228 * */
Archana9d17bf42021-09-10 06:22:44 +05302229 actual_attributes.core.bits = source_slot->attr.bits;
2230 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302231
2232
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 status = psa_restrict_key_policy(source_slot->attr.type,
2234 &actual_attributes.core.policy,
2235 &source_slot->attr.policy);
2236 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002237 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002239
Gilles Peskine449bd832023-01-11 14:50:10 +01002240 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2241 &target_slot, &driver);
2242 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002243 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 }
2245 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2246 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302247 /*
Archana9d17bf42021-09-10 06:22:44 +05302248 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302249 * the source key would need to be exported as plaintext and re-imported
2250 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302251 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302252 * appropriate API invocations from the application, if needed.
2253 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002254 status = PSA_ERROR_NOT_SUPPORTED;
2255 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002256 }
Archana8a180362021-07-05 02:18:48 +05302257 /*
2258 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302259 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302260 * - For opaque keys this translates to an invocation of the drivers'
2261 * copy_key entry point through the dispatch layer.
2262 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2264 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2265 &storage_size);
2266 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302267 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 }
Archana374fe5b2021-09-08 15:50:28 +05302269
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 status = psa_allocate_buffer_to_slot(target_slot, 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_driver_wrapper_copy_key(&actual_attributes,
2276 source_slot->key.data,
2277 source_slot->key.bytes,
2278 target_slot->key.data,
2279 target_slot->key.bytes,
2280 &target_slot->key.bytes);
2281 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302282 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 }
2284 } else {
2285 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302286 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 source_slot->key.bytes);
2288 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302289 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 }
Archana8a180362021-07-05 02:18:48 +05302291 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002293exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 if (status != PSA_SUCCESS) {
2295 psa_fail_key_creation(target_slot, driver);
2296 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002299
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002301}
2302
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002303
2304
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002305/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002306/* Message digests */
2307/****************************************************************/
2308
Gilles Peskine449bd832023-01-11 14:50:10 +01002309psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002310{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002311 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 if (operation->id == 0) {
2313 return PSA_SUCCESS;
2314 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002317 operation->id = 0;
2318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002320}
2321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2323 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002324{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002325 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002326
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002327 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002329 status = PSA_ERROR_BAD_STATE;
2330 goto exit;
2331 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002334 status = PSA_ERROR_INVALID_ARGUMENT;
2335 goto exit;
2336 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002337
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002338 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2339 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002341
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002343
2344exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 if (status != PSA_SUCCESS) {
2346 psa_hash_abort(operation);
2347 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002348
2349 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002350}
2351
Gilles Peskine449bd832023-01-11 14:50:10 +01002352psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2353 const uint8_t *input,
2354 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002355{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002356 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2357
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002359 status = PSA_ERROR_BAD_STATE;
2360 goto exit;
2361 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002362
Steven Cooremanc8288352021-03-04 14:02:19 +01002363 /* Don't require hash implementations to behave correctly on a
2364 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 if (input_length == 0) {
2366 return PSA_SUCCESS;
2367 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002370
2371exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 if (status != PSA_SUCCESS) {
2373 psa_hash_abort(operation);
2374 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002375
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002377}
2378
Gilles Peskine449bd832023-01-11 14:50:10 +01002379psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2380 uint8_t *hash,
2381 size_t hash_size,
2382 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002383{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002384 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 if (operation->id == 0) {
2386 return PSA_ERROR_BAD_STATE;
2387 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002388
Steven Cooreman1e582352021-02-18 17:24:37 +01002389 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 operation, hash, hash_size, hash_length);
2391 psa_hash_abort(operation);
2392 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002393}
2394
Gilles Peskine449bd832023-01-11 14:50:10 +01002395psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2396 const uint8_t *hash,
2397 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002398{
Ronald Cron69a63422021-10-18 09:47:58 +02002399 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002400 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002401 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 operation,
2403 actual_hash, sizeof(actual_hash),
2404 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002405
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002407 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002409
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002411 status = PSA_ERROR_INVALID_SIGNATURE;
2412 goto exit;
2413 }
2414
Dave Rodgman78701152023-08-29 14:20:18 +01002415 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002416 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002418
2419exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2421 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002422 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002426}
2427
Gilles Peskine449bd832023-01-11 14:50:10 +01002428psa_status_t psa_hash_compute(psa_algorithm_t alg,
2429 const uint8_t *input, size_t input_length,
2430 uint8_t *hash, size_t hash_size,
2431 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002432{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002433 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 if (!PSA_ALG_IS_HASH(alg)) {
2435 return PSA_ERROR_INVALID_ARGUMENT;
2436 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002437
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2439 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002440}
2441
Gilles Peskine449bd832023-01-11 14:50:10 +01002442psa_status_t psa_hash_compare(psa_algorithm_t alg,
2443 const uint8_t *input, size_t input_length,
2444 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002445{
Ronald Cron69a63422021-10-18 09:47:58 +02002446 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002447 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002448
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 if (!PSA_ALG_IS_HASH(alg)) {
2450 return PSA_ERROR_INVALID_ARGUMENT;
2451 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002452
Steven Cooreman1e582352021-02-18 17:24:37 +01002453 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 alg, input, input_length,
2455 actual_hash, sizeof(actual_hash),
2456 &actual_hash_length);
2457 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002458 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 }
2460 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002461 status = PSA_ERROR_INVALID_SIGNATURE;
2462 goto exit;
2463 }
Dave Rodgman78701152023-08-29 14:20:18 +01002464 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002465 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002467
2468exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2470 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002471}
2472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2474 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002475{
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 if (source_operation->id == 0 ||
2477 target_operation->id != 0) {
2478 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002479 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002480
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2482 target_operation);
2483 if (status != PSA_SUCCESS) {
2484 psa_hash_abort(target_operation);
2485 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002486
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002488}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002489
2490
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002491/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002492/* MAC */
2493/****************************************************************/
2494
Gilles Peskine449bd832023-01-11 14:50:10 +01002495psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002496{
Steven Cooremane6804192021-03-19 18:28:56 +01002497 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 if (operation->id == 0) {
2499 return PSA_SUCCESS;
2500 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002501
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002503 operation->mac_size = 0;
2504 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002505 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002506
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002508}
2509
Ronald Cron2dff3b22021-06-17 16:33:22 +02002510static psa_status_t psa_mac_finalize_alg_and_key_validation(
2511 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002512 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002514{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002515 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 psa_key_type_t key_type = psa_get_key_type(attributes);
2517 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002518
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 if (!PSA_ALG_IS_MAC(alg)) {
2520 return PSA_ERROR_INVALID_ARGUMENT;
2521 }
Steven Cooremane6804192021-03-19 18:28:56 +01002522
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002523 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 status = psa_mac_key_can_do(alg, key_type);
2525 if (status != PSA_SUCCESS) {
2526 return status;
2527 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002528
Steven Cooremand1a68f12021-05-10 11:13:41 +02002529 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002531
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002533 /* A very short MAC is too short for security since it can be
2534 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2535 * so we make this our minimum, even though 32 bits is still
2536 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002538 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2541 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002542 /* It's impossible to "truncate" to a larger length than the full length
2543 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002545 }
2546
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002548 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2549 * that is disabled in the compile-time configuration. The result can
2550 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2551 * configuration into account. In this case, force a return of
2552 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2553 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2554 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2555 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2556 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002558 }
2559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002561}
2562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2564 mbedtls_svc_key_id_t key,
2565 psa_algorithm_t alg,
2566 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002567{
2568 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2569 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002570 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002571 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002572
2573 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002575 status = PSA_ERROR_BAD_STATE;
2576 goto exit;
2577 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002578
2579 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 key,
2581 &slot,
2582 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2583 alg);
2584 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002585 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002587
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002588 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002589 .core = slot->attr
2590 };
2591
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2593 &operation->mac_size);
2594 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002595 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002597
2598 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002599 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 if (is_sign) {
2601 status = psa_driver_wrapper_mac_sign_setup(operation,
2602 &attributes,
2603 slot->key.data,
2604 slot->key.bytes,
2605 alg);
2606 } else {
2607 status = psa_driver_wrapper_mac_verify_setup(operation,
2608 &attributes,
2609 slot->key.data,
2610 slot->key.bytes,
2611 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002612 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002613
Gilles Peskinefbfac682018-07-08 20:51:54 +02002614exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 if (status != PSA_SUCCESS) {
2616 psa_mac_abort(operation);
2617 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002618
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002620
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002622}
2623
Gilles Peskine449bd832023-01-11 14:50:10 +01002624psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2625 mbedtls_svc_key_id_t key,
2626 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002627{
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002629}
2630
Gilles Peskine449bd832023-01-11 14:50:10 +01002631psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2632 mbedtls_svc_key_id_t key,
2633 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002634{
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002636}
2637
Gilles Peskine449bd832023-01-11 14:50:10 +01002638psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2639 const uint8_t *input,
2640 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002641{
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 if (operation->id == 0) {
2643 return PSA_ERROR_BAD_STATE;
2644 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002645
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002646 /* Don't require hash implementations to behave correctly on a
2647 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 if (input_length == 0) {
2649 return PSA_SUCCESS;
2650 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002651
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2653 input, input_length);
2654 if (status != PSA_SUCCESS) {
2655 psa_mac_abort(operation);
2656 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002657
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002659}
2660
Gilles Peskine449bd832023-01-11 14:50:10 +01002661psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2662 uint8_t *mac,
2663 size_t mac_size,
2664 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002665{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002666 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2667 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002670 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002671 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002672 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 if (!operation->is_sign) {
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 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002678
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002679 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2680 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002682 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002683 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002684 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002685
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002687 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002688 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002689 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002690
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 status = psa_driver_wrapper_mac_sign_finish(operation,
2692 mac, operation->mac_size,
2693 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002694
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002695exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002696 /* In case of success, set the potential excess room in the output buffer
2697 * to an invalid value, to avoid potentially leaking a longer MAC.
2698 * In case of error, set the output length and content to a safe default,
2699 * such that in case the caller misses an error check, the output would be
2700 * an unachievable MAC.
2701 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002703 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002704 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002705 }
mohammad16036df908f2018-04-02 08:34:15 -07002706
Paul Elliottdc42ca82023-02-24 18:11:59 +00002707 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002708
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002710
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002712}
2713
Gilles Peskine449bd832023-01-11 14:50:10 +01002714psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2715 const uint8_t *mac,
2716 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002717{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002718 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2719 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002720
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002722 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002723 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002724 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002725
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 if (operation->is_sign) {
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 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002730
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002732 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002733 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002734 }
2735
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 status = psa_driver_wrapper_mac_verify_finish(operation,
2737 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002738
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002739exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002741
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002743}
2744
Gilles Peskine449bd832023-01-11 14:50:10 +01002745static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2746 psa_algorithm_t alg,
2747 const uint8_t *input,
2748 size_t input_length,
2749 uint8_t *mac,
2750 size_t mac_size,
2751 size_t *mac_length,
2752 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002753{
2754 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002755 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2756 psa_key_slot_t *slot;
2757 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002758 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002759
Ronald Cron51131b52021-06-17 17:17:20 +02002760 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 key,
2762 &slot,
2763 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2764 alg);
2765 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002766 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002768
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002769 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002770 .core = slot->attr
2771 };
2772
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2774 &operation_mac_size);
2775 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002776 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002778
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002780 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002781 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002782 }
2783
2784 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 &attributes,
2786 slot->key.data, slot->key.bytes,
2787 alg,
2788 input, input_length,
2789 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002790
2791exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002792 /* In case of success, set the potential excess room in the output buffer
2793 * to an invalid value, to avoid potentially leaking a longer MAC.
2794 * In case of error, set the output length and content to a safe default,
2795 * such that in case the caller misses an error check, the output would be
2796 * an unachievable MAC.
2797 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002799 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002800 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002801 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002802
2803 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002804
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002806
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002808}
2809
Gilles Peskine449bd832023-01-11 14:50:10 +01002810psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002811 psa_algorithm_t alg,
2812 const uint8_t *input,
2813 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 uint8_t *mac,
2815 size_t mac_size,
2816 size_t *mac_length)
2817{
2818 return psa_mac_compute_internal(key, alg,
2819 input, input_length,
2820 mac, mac_size, mac_length, 1);
2821}
2822
2823psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2824 psa_algorithm_t alg,
2825 const uint8_t *input,
2826 size_t input_length,
2827 const uint8_t *mac,
2828 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002829{
2830 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002831 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2832 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002833
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 status = psa_mac_compute_internal(key, alg,
2835 input, input_length,
2836 actual_mac, sizeof(actual_mac),
2837 &actual_mac_length, 0);
2838 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002839 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002841
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002843 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002844 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002845 }
Dave Rodgman78701152023-08-29 14:20:18 +01002846 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002847 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002848 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002849 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002850
2851exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002853
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002855}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002856
Gilles Peskinee59236f2018-01-27 23:32:46 +01002857/****************************************************************/
2858/* Asymmetric cryptography */
2859/****************************************************************/
2860
Gilles Peskine449bd832023-01-11 14:50:10 +01002861static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2862 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002863{
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 if (input_is_message) {
2865 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2866 return PSA_ERROR_INVALID_ARGUMENT;
2867 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002868
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2870 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2871 return PSA_ERROR_INVALID_ARGUMENT;
2872 }
2873 }
2874 } else {
2875 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2876 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002877 }
2878 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002879
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002881}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002882
Gilles Peskine449bd832023-01-11 14:50:10 +01002883static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2884 int input_is_message,
2885 psa_algorithm_t alg,
2886 const uint8_t *input,
2887 size_t input_length,
2888 uint8_t *signature,
2889 size_t signature_size,
2890 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002891{
2892 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2893 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2894 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002895 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002896
2897 *signature_length = 0;
2898
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 status = psa_sign_verify_check_alg(input_is_message, alg);
2900 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002901 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002903
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002904 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002905 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002906 * buffer can in principle be empty since it doesn't actually have
2907 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002908 if (signature_size == 0) {
2909 return PSA_ERROR_BUFFER_TOO_SMALL;
2910 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002911
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002912 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 key, &slot,
2914 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2915 PSA_KEY_USAGE_SIGN_HASH,
2916 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002917
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002919 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002921
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002923 status = PSA_ERROR_INVALID_ARGUMENT;
2924 goto exit;
2925 }
2926
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002927 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002928 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002929 };
2930
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002932 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002933 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002934 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 signature, signature_size, signature_length);
2936 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002937
2938 status = psa_driver_wrapper_sign_hash(
2939 &attributes, slot->key.data, slot->key.bytes,
2940 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002942 }
2943
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002944
2945exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002946 psa_wipe_tag_output_buffer(signature, status, signature_size,
2947 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002948
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002950
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002952}
2953
Gilles Peskine449bd832023-01-11 14:50:10 +01002954static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2955 int input_is_message,
2956 psa_algorithm_t alg,
2957 const uint8_t *input,
2958 size_t input_length,
2959 const uint8_t *signature,
2960 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002961{
2962 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2963 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2964 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002965
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 status = psa_sign_verify_check_alg(input_is_message, alg);
2967 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002968 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002970
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002971 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 key, &slot,
2973 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2974 PSA_KEY_USAGE_VERIFY_HASH,
2975 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002976
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 if (status != PSA_SUCCESS) {
2978 return status;
2979 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002980
2981 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002983 };
2984
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002986 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002987 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002988 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 signature, signature_length);
2990 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002991 status = psa_driver_wrapper_verify_hash(
2992 &attributes, slot->key.data, slot->key.bytes,
2993 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002995 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002996
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002998
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003000
3001}
3002
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003003psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003004 const psa_key_attributes_t *attributes,
3005 const uint8_t *key_buffer,
3006 size_t key_buffer_size,
3007 psa_algorithm_t alg,
3008 const uint8_t *input,
3009 size_t input_length,
3010 uint8_t *signature,
3011 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003013{
3014 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003015
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003017 size_t hash_length;
3018 uint8_t hash[PSA_HASH_MAX_SIZE];
3019
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003020 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 PSA_ALG_SIGN_GET_HASH(alg),
3022 input, input_length,
3023 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003026 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003028
3029 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 attributes, key_buffer, key_buffer_size,
3031 alg, hash, hash_length,
3032 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003033 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003034
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003036}
3037
Gilles Peskine449bd832023-01-11 14:50:10 +01003038psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3039 psa_algorithm_t alg,
3040 const uint8_t *input,
3041 size_t input_length,
3042 uint8_t *signature,
3043 size_t signature_size,
3044 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003045{
3046 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003047 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003049}
3050
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003051psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003052 const psa_key_attributes_t *attributes,
3053 const uint8_t *key_buffer,
3054 size_t key_buffer_size,
3055 psa_algorithm_t alg,
3056 const uint8_t *input,
3057 size_t input_length,
3058 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003060{
3061 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003062
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003064 size_t hash_length;
3065 uint8_t hash[PSA_HASH_MAX_SIZE];
3066
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003067 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 PSA_ALG_SIGN_GET_HASH(alg),
3069 input, input_length,
3070 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003071
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003073 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003075
3076 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 attributes, key_buffer, key_buffer_size,
3078 alg, hash, hash_length,
3079 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003080 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003081
Gilles Peskine449bd832023-01-11 14:50:10 +01003082 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003083}
3084
Gilles Peskine449bd832023-01-11 14:50:10 +01003085psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3086 psa_algorithm_t alg,
3087 const uint8_t *input,
3088 size_t input_length,
3089 const uint8_t *signature,
3090 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003091{
3092 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003093 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003095}
3096
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003097psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003098 const psa_key_attributes_t *attributes,
3099 const uint8_t *key_buffer, size_t key_buffer_size,
3100 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003102{
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3104 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3105 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003106#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3108 return mbedtls_psa_rsa_sign_hash(
3109 attributes,
3110 key_buffer, key_buffer_size,
3111 alg, hash, hash_length,
3112 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003113#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3114 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 } else {
3116 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003117 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3119 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003120#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003121 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3122 return mbedtls_psa_ecdsa_sign_hash(
3123 attributes,
3124 key_buffer, key_buffer_size,
3125 alg, hash, hash_length,
3126 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003127#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3128 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 } else {
3130 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003131 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003132 }
Ronald Cron4501c982021-03-19 20:09:32 +01003133
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 (void) key_buffer;
3135 (void) key_buffer_size;
3136 (void) hash;
3137 (void) hash_length;
3138 (void) signature;
3139 (void) signature_size;
3140 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003141
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003143}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003144
Gilles Peskine449bd832023-01-11 14:50:10 +01003145psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3146 psa_algorithm_t alg,
3147 const uint8_t *hash,
3148 size_t hash_length,
3149 uint8_t *signature,
3150 size_t signature_size,
3151 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003152{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003153 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003154 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003155 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003156}
3157
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003158psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003159 const psa_key_attributes_t *attributes,
3160 const uint8_t *key_buffer, size_t key_buffer_size,
3161 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003163{
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3165 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3166 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003167#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3169 return mbedtls_psa_rsa_verify_hash(
3170 attributes,
3171 key_buffer, key_buffer_size,
3172 alg, hash, hash_length,
3173 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003174#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3175 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003176 } else {
3177 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003178 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3180 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003181#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3183 return mbedtls_psa_ecdsa_verify_hash(
3184 attributes,
3185 key_buffer, key_buffer_size,
3186 alg, hash, hash_length,
3187 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003188#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3189 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 } else {
3191 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003192 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003193 }
Ronald Cron4501c982021-03-19 20:09:32 +01003194
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 (void) key_buffer;
3196 (void) key_buffer_size;
3197 (void) hash;
3198 (void) hash_length;
3199 (void) signature;
3200 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003201
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003203}
3204
Gilles Peskine449bd832023-01-11 14:50:10 +01003205psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3206 psa_algorithm_t alg,
3207 const uint8_t *hash,
3208 size_t hash_length,
3209 const uint8_t *signature,
3210 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003211{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003212 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003213 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003215}
3216
Gilles Peskine449bd832023-01-11 14:50:10 +01003217psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3218 psa_algorithm_t alg,
3219 const uint8_t *input,
3220 size_t input_length,
3221 const uint8_t *salt,
3222 size_t salt_length,
3223 uint8_t *output,
3224 size_t output_size,
3225 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003226{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003227 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003228 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003229 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003230 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003231
Darryl Green5cc689a2018-07-24 15:34:10 +01003232 (void) input;
3233 (void) input_length;
3234 (void) salt;
3235 (void) output;
3236 (void) output_size;
3237
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003238 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003239
Gilles Peskine449bd832023-01-11 14:50:10 +01003240 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3241 return PSA_ERROR_INVALID_ARGUMENT;
3242 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003243
Ronald Cron5c522922020-11-14 16:35:34 +01003244 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003245 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3246 if (status != PSA_SUCCESS) {
3247 return status;
3248 }
3249 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3250 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003251 status = PSA_ERROR_INVALID_ARGUMENT;
3252 goto exit;
3253 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003254
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003255 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003257 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003258
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003259 status = psa_driver_wrapper_asymmetric_encrypt(
3260 &attributes, slot->key.data, slot->key.bytes,
3261 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003262 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003263exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003265
Gilles Peskine449bd832023-01-11 14:50:10 +01003266 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003267}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003268
Gilles Peskine449bd832023-01-11 14:50:10 +01003269psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3270 psa_algorithm_t alg,
3271 const uint8_t *input,
3272 size_t input_length,
3273 const uint8_t *salt,
3274 size_t salt_length,
3275 uint8_t *output,
3276 size_t output_size,
3277 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003278{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003279 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003280 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003281 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003282 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003283
Darryl Green5cc689a2018-07-24 15:34:10 +01003284 (void) input;
3285 (void) input_length;
3286 (void) salt;
3287 (void) output;
3288 (void) output_size;
3289
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003290 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003291
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3293 return PSA_ERROR_INVALID_ARGUMENT;
3294 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003295
Ronald Cron5c522922020-11-14 16:35:34 +01003296 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003297 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3298 if (status != PSA_SUCCESS) {
3299 return status;
3300 }
3301 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003302 status = PSA_ERROR_INVALID_ARGUMENT;
3303 goto exit;
3304 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003305
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003306 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003307 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003308 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003309
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003310 status = psa_driver_wrapper_asymmetric_decrypt(
3311 &attributes, slot->key.data, slot->key.bytes,
3312 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003313 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003314
3315exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003316 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003317
Gilles Peskine449bd832023-01-11 14:50:10 +01003318 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003319}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003320
Paul Elliott9fe12f62022-11-30 19:16:02 +00003321/****************************************************************/
3322/* Asymmetric interruptible cryptography */
3323/****************************************************************/
3324
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003325static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3326
Paul Elliott9fe12f62022-11-30 19:16:02 +00003327void psa_interruptible_set_max_ops(uint32_t max_ops)
3328{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003329 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003330}
3331
3332uint32_t psa_interruptible_get_max_ops(void)
3333{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003334 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003335}
3336
Paul Elliott9fe12f62022-11-30 19:16:02 +00003337uint32_t psa_sign_hash_get_num_ops(
3338 const psa_sign_hash_interruptible_operation_t *operation)
3339{
Paul Elliott296ede92022-12-15 17:00:30 +00003340 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003341}
3342
3343uint32_t psa_verify_hash_get_num_ops(
3344 const psa_verify_hash_interruptible_operation_t *operation)
3345{
Paul Elliott296ede92022-12-15 17:00:30 +00003346 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003347}
3348
Paul Elliott59ad9452022-12-18 15:09:02 +00003349static psa_status_t psa_sign_hash_abort_internal(
3350 psa_sign_hash_interruptible_operation_t *operation)
3351{
3352 if (operation->id == 0) {
3353 /* The object has (apparently) been initialized but it is not (yet)
3354 * in use. It's ok to call abort on such an object, and there's
3355 * nothing to do. */
3356 return PSA_SUCCESS;
3357 }
3358
3359 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3360
3361 status = psa_driver_wrapper_sign_hash_abort(operation);
3362
3363 operation->id = 0;
3364
Paul Elliotte6145dc2023-02-07 12:51:21 +00003365 /* Do not clear either the error_occurred or num_ops elements here as they
3366 * only want to be cleared by the application calling abort, not by abort
3367 * being called at completion of an operation. */
3368
Paul Elliott59ad9452022-12-18 15:09:02 +00003369 return status;
3370}
3371
Paul Elliott9fe12f62022-11-30 19:16:02 +00003372psa_status_t psa_sign_hash_start(
3373 psa_sign_hash_interruptible_operation_t *operation,
3374 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3375 const uint8_t *hash, size_t hash_length)
3376{
3377 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3378 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3379 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003380 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003381
Paul Elliottc9774412023-02-06 15:14:07 +00003382 /* Check that start has not been previously called, or operation has not
3383 * previously errored. */
3384 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003385 return PSA_ERROR_BAD_STATE;
3386 }
3387
Paul Elliott9fe12f62022-11-30 19:16:02 +00003388 status = psa_sign_verify_check_alg(0, alg);
3389 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003390 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003391 return status;
3392 }
3393
3394 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3395 PSA_KEY_USAGE_SIGN_HASH,
3396 alg);
3397
3398 if (status != PSA_SUCCESS) {
3399 goto exit;
3400 }
3401
3402 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3403 status = PSA_ERROR_INVALID_ARGUMENT;
3404 goto exit;
3405 }
3406
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003407 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003408 .core = slot->attr
3409 };
3410
Paul Elliott296ede92022-12-15 17:00:30 +00003411 /* Ensure ops count gets reset, in case of operation re-use. */
3412 operation->num_ops = 0;
3413
Paul Elliott9fe12f62022-11-30 19:16:02 +00003414 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3415 slot->key.data,
3416 slot->key.bytes, alg,
3417 hash, hash_length);
3418exit:
3419
3420 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003421 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003422 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003423 }
3424
3425 unlock_status = psa_unlock_key_slot(slot);
3426
Paul Elliottc9774412023-02-06 15:14:07 +00003427 if (unlock_status != PSA_SUCCESS) {
3428 operation->error_occurred = 1;
3429 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003430
Paul Elliottc9774412023-02-06 15:14:07 +00003431 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003432}
3433
3434
3435psa_status_t psa_sign_hash_complete(
3436 psa_sign_hash_interruptible_operation_t *operation,
3437 uint8_t *signature, size_t signature_size,
3438 size_t *signature_length)
3439{
3440 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3441
3442 *signature_length = 0;
3443
Paul Elliottc9774412023-02-06 15:14:07 +00003444 /* Check that start has been called first, and that operation has not
3445 * previously errored. */
3446 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003447 status = PSA_ERROR_BAD_STATE;
3448 goto exit;
3449 }
3450
Paul Elliott096abc42023-02-03 18:33:23 +00003451 /* Immediately reject a zero-length signature buffer. This guarantees that
3452 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003453 if (signature_size == 0) {
3454 status = PSA_ERROR_BUFFER_TOO_SMALL;
3455 goto exit;
3456 }
3457
3458 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3459 signature_size,
3460 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003461
Paul Elliott296ede92022-12-15 17:00:30 +00003462 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003463 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003464
Paul Elliottebe225c2023-02-10 14:32:53 +00003465exit:
3466
Paul Elliott59200a22023-02-21 15:07:40 +00003467 psa_wipe_tag_output_buffer(signature, status, signature_size,
3468 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003469
Paul Elliott53bb3122023-02-10 14:22:22 +00003470 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003471 if (status != PSA_SUCCESS) {
3472 operation->error_occurred = 1;
3473 }
3474
Paul Elliott59ad9452022-12-18 15:09:02 +00003475 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003476 }
3477
3478 return status;
3479}
3480
3481psa_status_t psa_sign_hash_abort(
3482 psa_sign_hash_interruptible_operation_t *operation)
3483{
Paul Elliott59ad9452022-12-18 15:09:02 +00003484 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3485
3486 status = psa_sign_hash_abort_internal(operation);
3487
3488 /* We clear the number of ops done here, so that it is not cleared when
3489 * the operation fails or succeeds, only on manual abort. */
3490 operation->num_ops = 0;
3491
Paul Elliottc9774412023-02-06 15:14:07 +00003492 /* Likewise, failure state. */
3493 operation->error_occurred = 0;
3494
Paul Elliott59ad9452022-12-18 15:09:02 +00003495 return status;
3496}
3497
3498static psa_status_t psa_verify_hash_abort_internal(
3499 psa_verify_hash_interruptible_operation_t *operation)
3500{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003501 if (operation->id == 0) {
3502 /* The object has (apparently) been initialized but it is not (yet)
3503 * in use. It's ok to call abort on such an object, and there's
3504 * nothing to do. */
3505 return PSA_SUCCESS;
3506 }
3507
Paul Elliott59ad9452022-12-18 15:09:02 +00003508 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3509
3510 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003511
3512 operation->id = 0;
3513
Paul Elliotte6145dc2023-02-07 12:51:21 +00003514 /* Do not clear either the error_occurred or num_ops elements here as they
3515 * only want to be cleared by the application calling abort, not by abort
3516 * being called at completion of an operation. */
3517
Paul Elliott59ad9452022-12-18 15:09:02 +00003518 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003519}
3520
3521psa_status_t psa_verify_hash_start(
3522 psa_verify_hash_interruptible_operation_t *operation,
3523 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3524 const uint8_t *hash, size_t hash_length,
3525 const uint8_t *signature, size_t signature_length)
3526{
3527 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3528 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3529 psa_key_slot_t *slot;
3530
Paul Elliottc9774412023-02-06 15:14:07 +00003531 /* Check that start has not been previously called, or operation has not
3532 * previously errored. */
3533 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003534 return PSA_ERROR_BAD_STATE;
3535 }
3536
3537 status = psa_sign_verify_check_alg(0, alg);
3538 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003539 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003540 return status;
3541 }
3542
3543 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3544 PSA_KEY_USAGE_VERIFY_HASH,
3545 alg);
3546
3547 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003548 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003549 return status;
3550 }
3551
3552 psa_key_attributes_t attributes = {
3553 .core = slot->attr
3554 };
3555
Paul Elliott296ede92022-12-15 17:00:30 +00003556 /* Ensure ops count gets reset, in case of operation re-use. */
3557 operation->num_ops = 0;
3558
Paul Elliott9fe12f62022-11-30 19:16:02 +00003559 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3560 slot->key.data,
3561 slot->key.bytes,
3562 alg, hash, hash_length,
3563 signature, signature_length);
3564
3565 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003566 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003567 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003568 }
3569
3570 unlock_status = psa_unlock_key_slot(slot);
3571
Paul Elliottc9774412023-02-06 15:14:07 +00003572 if (unlock_status != PSA_SUCCESS) {
3573 operation->error_occurred = 1;
3574 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003575
Paul Elliottc9774412023-02-06 15:14:07 +00003576 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003577}
3578
3579psa_status_t psa_verify_hash_complete(
3580 psa_verify_hash_interruptible_operation_t *operation)
3581{
3582 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3583
Paul Elliottc9774412023-02-06 15:14:07 +00003584 /* Check that start has been called first, and that operation has not
3585 * previously errored. */
3586 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003587 status = PSA_ERROR_BAD_STATE;
3588 goto exit;
3589 }
3590
3591 status = psa_driver_wrapper_verify_hash_complete(operation);
3592
Paul Elliott296ede92022-12-15 17:00:30 +00003593 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003594 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003595 operation);
3596
Paul Elliottebe225c2023-02-10 14:32:53 +00003597exit:
3598
Paul Elliott9fe12f62022-11-30 19:16:02 +00003599 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003600 if (status != PSA_SUCCESS) {
3601 operation->error_occurred = 1;
3602 }
3603
Paul Elliott59ad9452022-12-18 15:09:02 +00003604 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003605 }
3606
3607 return status;
3608}
3609
3610psa_status_t psa_verify_hash_abort(
3611 psa_verify_hash_interruptible_operation_t *operation)
3612{
Paul Elliott59ad9452022-12-18 15:09:02 +00003613 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003614
Paul Elliott59ad9452022-12-18 15:09:02 +00003615 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003616
Paul Elliott59ad9452022-12-18 15:09:02 +00003617 /* We clear the number of ops done here, so that it is not cleared when
3618 * the operation fails or succeeds, only on manual abort. */
3619 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003620
Paul Elliottc9774412023-02-06 15:14:07 +00003621 /* Likewise, failure state. */
3622 operation->error_occurred = 0;
3623
Paul Elliott59ad9452022-12-18 15:09:02 +00003624 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003625}
3626
Paul Elliott588f8ed2022-12-02 18:10:26 +00003627/****************************************************************/
3628/* Asymmetric interruptible cryptography internal */
3629/* implementations */
3630/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003631
Paul Elliott588f8ed2022-12-02 18:10:26 +00003632void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3633{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003634
Paul Elliott588f8ed2022-12-02 18:10:26 +00003635#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3636 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3637 defined(MBEDTLS_ECP_RESTARTABLE)
3638
3639 /* Internal implementation uses zero to indicate infinite number max ops,
3640 * therefore avoid this value, and set to minimum possible. */
3641 if (max_ops == 0) {
3642 max_ops = 1;
3643 }
3644
Paul Elliott588f8ed2022-12-02 18:10:26 +00003645 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003646#else
3647 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003648#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3649 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3650 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3651}
3652
Paul Elliott588f8ed2022-12-02 18:10:26 +00003653uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003654 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003655{
3656#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3657 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3658 defined(MBEDTLS_ECP_RESTARTABLE)
3659
Paul Elliott93d9ca82023-02-15 18:14:21 +00003660 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003661#else
3662 (void) operation;
3663 return 0;
3664#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3665 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3666 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3667}
3668
3669uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003670 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003671{
3672 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3673 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3674 defined(MBEDTLS_ECP_RESTARTABLE)
3675
Paul Elliott93d9ca82023-02-15 18:14:21 +00003676 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003677#else
3678 (void) operation;
3679 return 0;
3680#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3681 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3682 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3683}
3684
3685psa_status_t mbedtls_psa_sign_hash_start(
3686 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3687 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3688 size_t key_buffer_size, psa_algorithm_t alg,
3689 const uint8_t *hash, size_t hash_length)
3690{
3691 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003692 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003693
Paul Elliott068fe072023-01-16 13:59:15 +00003694 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3695 return PSA_ERROR_NOT_SUPPORTED;
3696 }
3697
3698 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003699 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003700 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003701
3702#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003703 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3704 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003705
Paul Elliotta1c94092023-02-15 16:38:04 +00003706 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3707
Paul Elliott93d9ca82023-02-15 18:14:21 +00003708 /* Ensure num_ops is zero'ed in case of context re-use. */
3709 operation->num_ops = 0;
3710
Paul Elliott068fe072023-01-16 13:59:15 +00003711 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3712 attributes->core.bits,
3713 key_buffer,
3714 key_buffer_size,
3715 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003716
Paul Elliott068fe072023-01-16 13:59:15 +00003717 if (status != PSA_SUCCESS) {
3718 return status;
3719 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003720
Paul Elliott1bc59df2023-02-05 13:41:57 +00003721 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003722 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003723
Paul Elliott068fe072023-01-16 13:59:15 +00003724 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003725 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003726 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003727
Paul Elliott0290a762023-02-09 14:30:24 +00003728 /* We only need to store the same length of hash as the private key size
3729 * here, it would be truncated by the internal implementation anyway. */
3730 required_hash_length = (hash_length < operation->coordinate_bytes ?
3731 hash_length : operation->coordinate_bytes);
3732
Paul Elliottba70ad42023-02-15 18:23:53 +00003733 if (required_hash_length > sizeof(operation->hash)) {
3734 /* Shouldn't happen, but better safe than sorry. */
3735 return PSA_ERROR_CORRUPTION_DETECTED;
3736 }
3737
Paul Elliott0290a762023-02-09 14:30:24 +00003738 memcpy(operation->hash, hash, required_hash_length);
3739 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003740
3741 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003742
3743#else
Paul Elliott068fe072023-01-16 13:59:15 +00003744 (void) operation;
3745 (void) key_buffer;
3746 (void) key_buffer_size;
3747 (void) alg;
3748 (void) hash;
3749 (void) hash_length;
3750 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003751 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003752
Paul Elliott068fe072023-01-16 13:59:15 +00003753 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003754#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3755 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3756 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003757}
3758
3759psa_status_t mbedtls_psa_sign_hash_complete(
3760 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3761 uint8_t *signature, size_t signature_size,
3762 size_t *signature_length)
3763{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003764#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3765 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3766 defined(MBEDTLS_ECP_RESTARTABLE)
3767
Paul Elliotta1c94092023-02-15 16:38:04 +00003768 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003769 mbedtls_mpi r;
3770 mbedtls_mpi s;
3771
Paul Elliott46845252023-02-03 14:59:11 +00003772 mbedtls_mpi_init(&r);
3773 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003774
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003775 /* Ensure max_ops is set to the current value (or default). */
3776 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3777
Paul Elliotta1c94092023-02-15 16:38:04 +00003778 if (signature_size < 2 * operation->coordinate_bytes) {
3779 status = PSA_ERROR_BUFFER_TOO_SMALL;
3780 goto exit;
3781 }
3782
Paul Elliott588f8ed2022-12-02 18:10:26 +00003783 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003784
Paul Elliott588f8ed2022-12-02 18:10:26 +00003785#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3786 status = mbedtls_to_psa_error(
3787 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003788 &r,
3789 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003790 &operation->ctx->d,
3791 operation->hash,
3792 operation->hash_length,
3793 operation->md_alg,
3794 mbedtls_psa_get_random,
3795 MBEDTLS_PSA_RANDOM_STATE,
3796 &operation->restart_ctx));
3797#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003798 status = PSA_ERROR_NOT_SUPPORTED;
3799 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003800#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3801 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003802 status = mbedtls_to_psa_error(
3803 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003804 &r,
3805 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003806 &operation->ctx->d,
3807 operation->hash,
3808 operation->hash_length,
3809 mbedtls_psa_get_random,
3810 MBEDTLS_PSA_RANDOM_STATE,
3811 mbedtls_psa_get_random,
3812 MBEDTLS_PSA_RANDOM_STATE,
3813 &operation->restart_ctx));
3814 }
3815
Paul Elliottf8e5b562023-02-19 18:43:45 +00003816 /* Hide the fact that the restart context only holds a delta of number of
3817 * ops done during the last operation, not an absolute value. */
3818 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3819
Paul Elliott724bd252023-02-08 12:35:08 +00003820 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003821 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003822 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003823 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003824 operation->coordinate_bytes)
3825 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003826
3827 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003828 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003829 }
3830
3831 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003832 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003833 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003834 operation->coordinate_bytes,
3835 operation->coordinate_bytes)
3836 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003837
3838 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003839 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003840 }
3841
Paul Elliott1bc59df2023-02-05 13:41:57 +00003842 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003843
Paul Elliott724bd252023-02-08 12:35:08 +00003844 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003845 }
Paul Elliott724bd252023-02-08 12:35:08 +00003846
3847exit:
3848
3849 mbedtls_mpi_free(&r);
3850 mbedtls_mpi_free(&s);
3851 return status;
3852
Paul Elliott588f8ed2022-12-02 18:10:26 +00003853 #else
3854
3855 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003856 (void) signature;
3857 (void) signature_size;
3858 (void) signature_length;
3859
3860 return PSA_ERROR_NOT_SUPPORTED;
3861
3862#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3863 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3864 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3865}
3866
3867psa_status_t mbedtls_psa_sign_hash_abort(
3868 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3869{
3870
3871#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3872 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3873 defined(MBEDTLS_ECP_RESTARTABLE)
3874
3875 if (operation->ctx) {
3876 mbedtls_ecdsa_free(operation->ctx);
3877 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003878 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003879 }
3880
3881 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3882
Paul Elliott93d9ca82023-02-15 18:14:21 +00003883 operation->num_ops = 0;
3884
Paul Elliott588f8ed2022-12-02 18:10:26 +00003885 return PSA_SUCCESS;
3886
3887#else
3888
3889 (void) operation;
3890
3891 return PSA_ERROR_NOT_SUPPORTED;
3892
3893#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3894 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3895 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3896}
3897
3898psa_status_t mbedtls_psa_verify_hash_start(
3899 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3900 const psa_key_attributes_t *attributes,
3901 const uint8_t *key_buffer, size_t key_buffer_size,
3902 psa_algorithm_t alg,
3903 const uint8_t *hash, size_t hash_length,
3904 const uint8_t *signature, size_t signature_length)
3905{
3906 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003907 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003908 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003909
Paul Elliott068fe072023-01-16 13:59:15 +00003910 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3911 return PSA_ERROR_NOT_SUPPORTED;
3912 }
3913
3914 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003915 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003916 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003917
3918#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003919 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3920 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003921
Paul Elliotta1c94092023-02-15 16:38:04 +00003922 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3923 mbedtls_mpi_init(&operation->r);
3924 mbedtls_mpi_init(&operation->s);
3925
Paul Elliott93d9ca82023-02-15 18:14:21 +00003926 /* Ensure num_ops is zero'ed in case of context re-use. */
3927 operation->num_ops = 0;
3928
Paul Elliott068fe072023-01-16 13:59:15 +00003929 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3930 attributes->core.bits,
3931 key_buffer,
3932 key_buffer_size,
3933 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003934
Paul Elliott068fe072023-01-16 13:59:15 +00003935 if (status != PSA_SUCCESS) {
3936 return status;
3937 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003938
Paul Elliottc569fc22023-02-10 13:02:54 +00003939 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003940
Paul Elliott1bc59df2023-02-05 13:41:57 +00003941 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003942 return PSA_ERROR_INVALID_SIGNATURE;
3943 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003944
Paul Elliott068fe072023-01-16 13:59:15 +00003945 status = mbedtls_to_psa_error(
3946 mbedtls_mpi_read_binary(&operation->r,
3947 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003948 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003949
Paul Elliott068fe072023-01-16 13:59:15 +00003950 if (status != PSA_SUCCESS) {
3951 return status;
3952 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003953
Paul Elliott068fe072023-01-16 13:59:15 +00003954 status = mbedtls_to_psa_error(
3955 mbedtls_mpi_read_binary(&operation->s,
3956 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003957 coordinate_bytes,
3958 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003959
Paul Elliott068fe072023-01-16 13:59:15 +00003960 if (status != PSA_SUCCESS) {
3961 return status;
3962 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003963
Paul Elliott2c9843f2023-02-15 17:32:42 +00003964 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003965
Paul Elliott2c9843f2023-02-15 17:32:42 +00003966 if (status != PSA_SUCCESS) {
3967 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003968 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003969
Paul Elliott0290a762023-02-09 14:30:24 +00003970 /* We only need to store the same length of hash as the private key size
3971 * here, it would be truncated by the internal implementation anyway. */
3972 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3973 coordinate_bytes);
3974
Paul Elliottba70ad42023-02-15 18:23:53 +00003975 if (required_hash_length > sizeof(operation->hash)) {
3976 /* Shouldn't happen, but better safe than sorry. */
3977 return PSA_ERROR_CORRUPTION_DETECTED;
3978 }
3979
Paul Elliott0290a762023-02-09 14:30:24 +00003980 memcpy(operation->hash, hash, required_hash_length);
3981 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003982
3983 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003984#else
Paul Elliott068fe072023-01-16 13:59:15 +00003985 (void) operation;
3986 (void) key_buffer;
3987 (void) key_buffer_size;
3988 (void) alg;
3989 (void) hash;
3990 (void) hash_length;
3991 (void) signature;
3992 (void) signature_length;
3993 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003994 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003995 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003996
Paul Elliott068fe072023-01-16 13:59:15 +00003997 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003998#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3999 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4000 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004001}
4002
4003psa_status_t mbedtls_psa_verify_hash_complete(
4004 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4005{
4006
4007#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4008 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4009 defined(MBEDTLS_ECP_RESTARTABLE)
4010
Paul Elliottf8e5b562023-02-19 18:43:45 +00004011 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4012
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004013 /* Ensure max_ops is set to the current value (or default). */
4014 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4015
Paul Elliottf8e5b562023-02-19 18:43:45 +00004016 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004017 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4018 operation->hash,
4019 operation->hash_length,
4020 &operation->ctx->Q,
4021 &operation->r,
4022 &operation->s,
4023 &operation->restart_ctx));
4024
Paul Elliottf8e5b562023-02-19 18:43:45 +00004025 /* Hide the fact that the restart context only holds a delta of number of
4026 * ops done during the last operation, not an absolute value. */
4027 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4028
4029 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004030#else
4031 (void) operation;
4032
4033 return PSA_ERROR_NOT_SUPPORTED;
4034
4035#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4036 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4037 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4038}
4039
4040psa_status_t mbedtls_psa_verify_hash_abort(
4041 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4042{
4043
4044#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4045 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4046 defined(MBEDTLS_ECP_RESTARTABLE)
4047
4048 if (operation->ctx) {
4049 mbedtls_ecdsa_free(operation->ctx);
4050 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004051 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004052 }
4053
4054 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4055
Paul Elliott93d9ca82023-02-15 18:14:21 +00004056 operation->num_ops = 0;
4057
Paul Elliott588f8ed2022-12-02 18:10:26 +00004058 mbedtls_mpi_free(&operation->r);
4059 mbedtls_mpi_free(&operation->s);
4060
4061 return PSA_SUCCESS;
4062
4063#else
4064 (void) operation;
4065
4066 return PSA_ERROR_NOT_SUPPORTED;
4067
4068#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4069 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4070 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4071}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004072
mohammad1603503973b2018-03-12 15:59:30 +02004073/****************************************************************/
4074/* Symmetric cryptography */
4075/****************************************************************/
4076
Gilles Peskine449bd832023-01-11 14:50:10 +01004077static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4078 mbedtls_svc_key_id_t key,
4079 psa_algorithm_t alg,
4080 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004081{
4082 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4083 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004084 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4086 PSA_KEY_USAGE_ENCRYPT :
4087 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004088 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004089
4090 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004092 status = PSA_ERROR_BAD_STATE;
4093 goto exit;
4094 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004095
Gilles Peskine449bd832023-01-11 14:50:10 +01004096 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004097 status = PSA_ERROR_INVALID_ARGUMENT;
4098 goto exit;
4099 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004100
Gilles Peskine449bd832023-01-11 14:50:10 +01004101 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4102 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004103 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004105
Ronald Cron49fafa92021-03-10 08:34:23 +01004106 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004107 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004108 * so we only set it (in the driver wrapper) after resources have been
4109 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004110 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004111 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004112 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004114 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004115 }
4116 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004117
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004118 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004119 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004120 };
4121
Ronald Cronab99ac22020-10-01 16:38:28 +02004122 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 if (cipher_operation == MBEDTLS_ENCRYPT) {
4124 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4125 &attributes,
4126 slot->key.data,
4127 slot->key.bytes,
4128 alg);
4129 } else {
4130 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4131 &attributes,
4132 slot->key.data,
4133 slot->key.bytes,
4134 alg);
4135 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004136
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004137exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004138 if (status != PSA_SUCCESS) {
4139 psa_cipher_abort(operation);
4140 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004141
Gilles Peskine449bd832023-01-11 14:50:10 +01004142 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004143
Gilles Peskine449bd832023-01-11 14:50:10 +01004144 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004145}
4146
Gilles Peskine449bd832023-01-11 14:50:10 +01004147psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4148 mbedtls_svc_key_id_t key,
4149 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004150{
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004152}
4153
Gilles Peskine449bd832023-01-11 14:50:10 +01004154psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4155 mbedtls_svc_key_id_t key,
4156 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004157{
Gilles Peskine449bd832023-01-11 14:50:10 +01004158 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004159}
4160
Gilles Peskine449bd832023-01-11 14:50:10 +01004161psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4162 uint8_t *iv,
4163 size_t iv_size,
4164 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004165{
Ronald Cron6d051732020-10-01 14:10:20 +02004166 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004167 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004168 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004169
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004171 status = PSA_ERROR_BAD_STATE;
4172 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004173 }
4174
Gilles Peskine449bd832023-01-11 14:50:10 +01004175 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004176 status = PSA_ERROR_BAD_STATE;
4177 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004178 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004179
Ronald Cron2fb90522021-07-05 12:31:44 +02004180 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004181 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004182 status = PSA_ERROR_BUFFER_TOO_SMALL;
4183 goto exit;
4184 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004185
Gilles Peskine449bd832023-01-11 14:50:10 +01004186 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004187 status = PSA_ERROR_GENERIC_ERROR;
4188 goto exit;
4189 }
4190
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 status = psa_generate_random(local_iv, default_iv_length);
4192 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004193 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004194 }
Ronald Cron5618a392021-03-26 09:52:26 +01004195
Gilles Peskine449bd832023-01-11 14:50:10 +01004196 status = psa_driver_wrapper_cipher_set_iv(operation,
4197 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004198
4199exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 if (status == PSA_SUCCESS) {
4201 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004202 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004203 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004204 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004205 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004206 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004207 }
Ronald Cron6d051732020-10-01 14:10:20 +02004208
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004210}
4211
Gilles Peskine449bd832023-01-11 14:50:10 +01004212psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4213 const uint8_t *iv,
4214 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004215{
Ronald Cron6d051732020-10-01 14:10:20 +02004216 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004217
Gilles Peskine449bd832023-01-11 14:50:10 +01004218 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004219 status = PSA_ERROR_BAD_STATE;
4220 goto exit;
4221 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004222
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004224 status = PSA_ERROR_BAD_STATE;
4225 goto exit;
4226 }
Ronald Crona0d68172021-03-26 10:15:08 +01004227
Gilles Peskine449bd832023-01-11 14:50:10 +01004228 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004229 status = PSA_ERROR_INVALID_ARGUMENT;
4230 goto exit;
4231 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004232
Gilles Peskine449bd832023-01-11 14:50:10 +01004233 status = psa_driver_wrapper_cipher_set_iv(operation,
4234 iv,
4235 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004236
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004237exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004238 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004239 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004240 } else {
4241 psa_cipher_abort(operation);
4242 }
4243 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004244}
4245
Gilles Peskine449bd832023-01-11 14:50:10 +01004246psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4247 const uint8_t *input,
4248 size_t input_length,
4249 uint8_t *output,
4250 size_t output_size,
4251 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004252{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004253 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004254
Gilles Peskine449bd832023-01-11 14:50:10 +01004255 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004256 status = PSA_ERROR_BAD_STATE;
4257 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004258 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004259
Gilles Peskine449bd832023-01-11 14:50:10 +01004260 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004261 status = PSA_ERROR_BAD_STATE;
4262 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004263 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004264
Gilles Peskine449bd832023-01-11 14:50:10 +01004265 status = psa_driver_wrapper_cipher_update(operation,
4266 input,
4267 input_length,
4268 output,
4269 output_size,
4270 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004271
4272exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004273 if (status != PSA_SUCCESS) {
4274 psa_cipher_abort(operation);
4275 }
Ronald Cron6d051732020-10-01 14:10:20 +02004276
Gilles Peskine449bd832023-01-11 14:50:10 +01004277 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004278}
4279
Gilles Peskine449bd832023-01-11 14:50:10 +01004280psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4281 uint8_t *output,
4282 size_t output_size,
4283 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004284{
David Saadab4ecc272019-02-14 13:48:10 +02004285 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004286
Gilles Peskine449bd832023-01-11 14:50:10 +01004287 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004288 status = PSA_ERROR_BAD_STATE;
4289 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004290 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004291
Gilles Peskine449bd832023-01-11 14:50:10 +01004292 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004293 status = PSA_ERROR_BAD_STATE;
4294 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004295 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004296
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 status = psa_driver_wrapper_cipher_finish(operation,
4298 output,
4299 output_size,
4300 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004301
4302exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 if (status == PSA_SUCCESS) {
4304 return psa_cipher_abort(operation);
4305 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004306 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004308
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004310 }
mohammad1603503973b2018-03-12 15:59:30 +02004311}
4312
Gilles Peskine449bd832023-01-11 14:50:10 +01004313psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004314{
Gilles Peskine449bd832023-01-11 14:50:10 +01004315 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004316 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004317 * in use. It's ok to call abort on such an object, and there's
4318 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004319 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004320 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004321
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004323
Ronald Cron49fafa92021-03-10 08:34:23 +01004324 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004325 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004326 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004327
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004329}
4330
Gilles Peskine449bd832023-01-11 14:50:10 +01004331psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4332 psa_algorithm_t alg,
4333 const uint8_t *input,
4334 size_t input_length,
4335 uint8_t *output,
4336 size_t output_size,
4337 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004338{
4339 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004340 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004341 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004342 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4343 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004344 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004345
Gilles Peskine449bd832023-01-11 14:50:10 +01004346 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004347 status = PSA_ERROR_INVALID_ARGUMENT;
4348 goto exit;
4349 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004350
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4352 PSA_KEY_USAGE_ENCRYPT,
4353 alg);
4354 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004355 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004356 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004357
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004358 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004360 };
4361
Gilles Peskine449bd832023-01-11 14:50:10 +01004362 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4363 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004364 status = PSA_ERROR_GENERIC_ERROR;
4365 goto exit;
4366 }
4367
Gilles Peskine449bd832023-01-11 14:50:10 +01004368 if (default_iv_length > 0) {
4369 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004370 status = PSA_ERROR_BUFFER_TOO_SMALL;
4371 goto exit;
4372 }
4373
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 status = psa_generate_random(local_iv, default_iv_length);
4375 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004376 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004377 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004378 }
4379
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004380 status = psa_driver_wrapper_cipher_encrypt(
4381 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004382 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004383 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004384 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004385
4386exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 unlock_status = psa_unlock_key_slot(slot);
4388 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004389 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004390 }
Ronald Cron23919522021-07-08 19:00:07 +02004391
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 if (status == PSA_SUCCESS) {
4393 if (default_iv_length > 0) {
4394 memcpy(output, local_iv, default_iv_length);
4395 }
4396 *output_length += default_iv_length;
4397 } else {
4398 *output_length = 0;
4399 }
4400
4401 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004402}
4403
Gilles Peskine449bd832023-01-11 14:50:10 +01004404psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4405 psa_algorithm_t alg,
4406 const uint8_t *input,
4407 size_t input_length,
4408 uint8_t *output,
4409 size_t output_size,
4410 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004411{
4412 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004413 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004414 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004415 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004416
Gilles Peskine449bd832023-01-11 14:50:10 +01004417 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004418 status = PSA_ERROR_INVALID_ARGUMENT;
4419 goto exit;
4420 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004421
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4423 PSA_KEY_USAGE_DECRYPT,
4424 alg);
4425 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004426 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004428
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004429 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004431 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004432
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4434 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004435 status = PSA_ERROR_INVALID_ARGUMENT;
4436 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004437 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004438 status = PSA_ERROR_INVALID_ARGUMENT;
4439 goto exit;
4440 }
4441
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004442 status = psa_driver_wrapper_cipher_decrypt(
4443 &attributes, slot->key.data, slot->key.bytes,
4444 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004446
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004447exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 unlock_status = psa_unlock_key_slot(slot);
4449 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004450 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004452
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004454 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 }
Ronald Cron23919522021-07-08 19:00:07 +02004456
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004458}
4459
4460
mohammad16035955c982018-04-26 00:53:03 +03004461/****************************************************************/
4462/* AEAD */
4463/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004464
Paul Elliottbaff51c2021-09-28 17:44:45 +01004465/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004466static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004467{
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004469}
4470
4471/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004472static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4473 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004474{
Gilles Peskine449bd832023-01-11 14:50:10 +01004475 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004476
Gilles Peskine449bd832023-01-11 14:50:10 +01004477 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004478#if defined(PSA_WANT_ALG_GCM)
4479 case PSA_ALG_GCM:
4480 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004481 * arbitrarily large nonces. Please note that we do not generally
4482 * recommend the usage of nonces of greater length than
4483 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4484 * size, which can then lead to collisions if you encrypt a very
4485 * large number of messages.*/
4486 if (nonce_length != 0) {
4487 return PSA_SUCCESS;
4488 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004489 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004490#endif /* PSA_WANT_ALG_GCM */
4491#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004492 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004493 if (nonce_length >= 7 && nonce_length <= 13) {
4494 return PSA_SUCCESS;
4495 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004496 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004497#endif /* PSA_WANT_ALG_CCM */
4498#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004499 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 if (nonce_length == 12) {
4501 return PSA_SUCCESS;
4502 } else if (nonce_length == 8) {
4503 return PSA_ERROR_NOT_SUPPORTED;
4504 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004505 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004506#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004507 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004508 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004510 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004511
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004513}
4514
Gilles Peskine449bd832023-01-11 14:50:10 +01004515static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004516{
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4518 return PSA_ERROR_INVALID_ARGUMENT;
4519 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004520
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004522}
4523
Gilles Peskine449bd832023-01-11 14:50:10 +01004524psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4525 psa_algorithm_t alg,
4526 const uint8_t *nonce,
4527 size_t nonce_length,
4528 const uint8_t *additional_data,
4529 size_t additional_data_length,
4530 const uint8_t *plaintext,
4531 size_t plaintext_length,
4532 uint8_t *ciphertext,
4533 size_t ciphertext_size,
4534 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004535{
Ronald Cron215633c2021-03-16 17:15:37 +01004536 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4537 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004538
mohammad1603f08a5502018-06-03 15:05:47 +03004539 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004540
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 status = psa_aead_check_algorithm(alg);
4542 if (status != PSA_SUCCESS) {
4543 return status;
4544 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004545
Ronald Cron9a986162021-03-26 12:40:07 +01004546 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4548 if (status != PSA_SUCCESS) {
4549 return status;
4550 }
mohammad16035955c982018-04-26 00:53:03 +03004551
Ronald Cron215633c2021-03-16 17:15:37 +01004552 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004554 };
mohammad16035955c982018-04-26 00:53:03 +03004555
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 status = psa_aead_check_nonce_length(alg, nonce_length);
4557 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004558 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004559 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004560
Ronald Cronde822812021-03-17 16:08:20 +01004561 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004562 &attributes, slot->key.data, slot->key.bytes,
4563 alg,
4564 nonce, nonce_length,
4565 additional_data, additional_data_length,
4566 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004568
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4570 memset(ciphertext, 0, ciphertext_size);
4571 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004572
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004573exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004575
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004577}
4578
Gilles Peskine449bd832023-01-11 14:50:10 +01004579psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4580 psa_algorithm_t alg,
4581 const uint8_t *nonce,
4582 size_t nonce_length,
4583 const uint8_t *additional_data,
4584 size_t additional_data_length,
4585 const uint8_t *ciphertext,
4586 size_t ciphertext_length,
4587 uint8_t *plaintext,
4588 size_t plaintext_size,
4589 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004590{
Ronald Cron215633c2021-03-16 17:15:37 +01004591 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4592 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004593
Gilles Peskineee652a32018-06-01 19:23:52 +02004594 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004595
Gilles Peskine449bd832023-01-11 14:50:10 +01004596 status = psa_aead_check_algorithm(alg);
4597 if (status != PSA_SUCCESS) {
4598 return status;
4599 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004600
Ronald Cron9a986162021-03-26 12:40:07 +01004601 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004602 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4603 if (status != PSA_SUCCESS) {
4604 return status;
4605 }
mohammad16035955c982018-04-26 00:53:03 +03004606
Ronald Cron215633c2021-03-16 17:15:37 +01004607 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004608 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004609 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004610
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 status = psa_aead_check_nonce_length(alg, nonce_length);
4612 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004613 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004614 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004615
Ronald Cronde822812021-03-17 16:08:20 +01004616 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004617 &attributes, slot->key.data, slot->key.bytes,
4618 alg,
4619 nonce, nonce_length,
4620 additional_data, additional_data_length,
4621 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004623
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 if (status != PSA_SUCCESS && plaintext_size != 0) {
4625 memset(plaintext, 0, plaintext_size);
4626 }
mohammad160360a64d02018-06-03 17:20:42 +03004627
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004628exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004630
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 return status;
mohammad16035955c982018-04-26 00:53:03 +03004632}
4633
Gilles Peskine449bd832023-01-11 14:50:10 +01004634static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4635{
4636 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004637
Gilles Peskine449bd832023-01-11 14:50:10 +01004638 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004639#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004641 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4643 return PSA_ERROR_INVALID_ARGUMENT;
4644 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004645 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004646#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004647
Przemek Stekiel86679c72022-10-06 17:06:56 +02004648#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004650 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004651 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4652 return PSA_ERROR_INVALID_ARGUMENT;
4653 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004654 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004655#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004656
Przemek Stekiel86679c72022-10-06 17:06:56 +02004657#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004659 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 if (tag_len != 16) {
4661 return PSA_ERROR_INVALID_ARGUMENT;
4662 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004663 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004664#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004665
4666 default:
4667 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004669 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004670 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004671}
4672
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004673/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004674static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4675 int is_encrypt,
4676 mbedtls_svc_key_id_t key,
4677 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004678{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004679 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4680 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4681 psa_key_slot_t *slot = NULL;
4682 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004683 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004684
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 status = psa_aead_check_algorithm(alg);
4686 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004687 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004689
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004691 status = PSA_ERROR_BAD_STATE;
4692 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004693 }
4694
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 if (operation->nonce_set || operation->lengths_set ||
4696 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004697 status = PSA_ERROR_BAD_STATE;
4698 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004699 }
4700
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004702 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004703 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004704 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004706
Gilles Peskine449bd832023-01-11 14:50:10 +01004707 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4708 alg);
4709 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004710 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004712
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004713 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004714 .core = slot->attr
4715 };
4716
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004718 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004719 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004720
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 if (is_encrypt) {
4722 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4723 &attributes,
4724 slot->key.data,
4725 slot->key.bytes,
4726 alg);
4727 } else {
4728 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4729 &attributes,
4730 slot->key.data,
4731 slot->key.bytes,
4732 alg);
4733 }
4734 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004735 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004737
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004739
4740exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004742
Gilles Peskine449bd832023-01-11 14:50:10 +01004743 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004744 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004745 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004746 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004747 } else {
4748 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004749 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004750
Gilles Peskine449bd832023-01-11 14:50:10 +01004751 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004752}
4753
Paul Elliott302ff6b2021-04-20 18:10:30 +01004754/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004755psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4756 mbedtls_svc_key_id_t key,
4757 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004758{
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004760}
4761
4762/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004763psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4764 mbedtls_svc_key_id_t key,
4765 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004766{
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004768}
4769
4770/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004771psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4772 uint8_t *nonce,
4773 size_t nonce_size,
4774 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004775{
4776 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004777 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004778 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004779
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004780 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004781
Gilles Peskine449bd832023-01-11 14:50:10 +01004782 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004783 status = PSA_ERROR_BAD_STATE;
4784 goto exit;
4785 }
4786
Gilles Peskine449bd832023-01-11 14:50:10 +01004787 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004788 status = PSA_ERROR_BAD_STATE;
4789 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004790 }
4791
Paul Elliotte193ea82021-10-01 13:00:16 +01004792 /* For CCM, this size may not be correct according to the PSA
4793 * specification. The PSA Crypto 1.0.1 specification states:
4794 *
4795 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4796 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4797 *
4798 * However this restriction that L has to be the smallest integer is not
4799 * applied in practice, and it is not implementable here since the
4800 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4802 operation->alg);
4803 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004804 status = PSA_ERROR_BUFFER_TOO_SMALL;
4805 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004806 }
4807
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 status = psa_generate_random(local_nonce, required_nonce_size);
4809 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004810 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004811 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004812
Gilles Peskine449bd832023-01-11 14:50:10 +01004813 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004814
Paul Elliott39dc6b82021-05-11 19:16:09 +01004815exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 if (status == PSA_SUCCESS) {
4817 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004818 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004819 } else {
4820 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004821 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004822
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004824}
4825
4826/* Set the nonce for a multipart authenticated encryption or decryption
4827 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004828psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4829 const uint8_t *nonce,
4830 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004831{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004832 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4833
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004835 status = PSA_ERROR_BAD_STATE;
4836 goto exit;
4837 }
4838
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004840 status = PSA_ERROR_BAD_STATE;
4841 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004842 }
4843
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4845 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004846 status = PSA_ERROR_INVALID_ARGUMENT;
4847 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004848 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004849
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4851 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004852
Paul Elliott39dc6b82021-05-11 19:16:09 +01004853exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004854 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004855 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004856 } else {
4857 psa_aead_abort(operation);
4858 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004859
Gilles Peskine449bd832023-01-11 14:50:10 +01004860 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004861}
4862
4863/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004864psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4865 size_t ad_length,
4866 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004867{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004868 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4869
Gilles Peskine449bd832023-01-11 14:50:10 +01004870 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004871 status = PSA_ERROR_BAD_STATE;
4872 goto exit;
4873 }
4874
Gilles Peskine449bd832023-01-11 14:50:10 +01004875 if (operation->lengths_set || operation->ad_started ||
4876 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004877 status = PSA_ERROR_BAD_STATE;
4878 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004879 }
4880
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004882#if defined(PSA_WANT_ALG_GCM)
4883 case PSA_ALG_GCM:
4884 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 * bits. Without the guard this code will generate warnings on 32bit
4886 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004887#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004888 if (((uint64_t) ad_length) >> 61 != 0 ||
4889 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004890 status = PSA_ERROR_INVALID_ARGUMENT;
4891 goto exit;
4892 }
Paul Elliott325d3742021-09-27 17:56:28 +01004893#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004894 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004895#endif /* PSA_WANT_ALG_GCM */
4896#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004897 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004899 status = PSA_ERROR_INVALID_ARGUMENT;
4900 goto exit;
4901 }
4902 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004903#endif /* PSA_WANT_ALG_CCM */
4904#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004905 case PSA_ALG_CHACHA20_POLY1305:
4906 /* No length restrictions for ChaChaPoly. */
4907 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004908#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004909 default:
4910 break;
4911 }
Paul Elliott325d3742021-09-27 17:56:28 +01004912
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4914 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004915
Paul Elliott39dc6b82021-05-11 19:16:09 +01004916exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004917 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004918 operation->ad_remaining = ad_length;
4919 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004920 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004921 } else {
4922 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004923 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004924
Gilles Peskine449bd832023-01-11 14:50:10 +01004925 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004926}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004927
4928/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004929psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4930 const uint8_t *input,
4931 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004932{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004933 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4934
Gilles Peskine449bd832023-01-11 14:50:10 +01004935 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004936 status = PSA_ERROR_BAD_STATE;
4937 goto exit;
4938 }
4939
Gilles Peskine449bd832023-01-11 14:50:10 +01004940 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004941 status = PSA_ERROR_BAD_STATE;
4942 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004943 }
4944
Gilles Peskine449bd832023-01-11 14:50:10 +01004945 if (operation->lengths_set) {
4946 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004947 status = PSA_ERROR_INVALID_ARGUMENT;
4948 goto exit;
4949 }
4950
4951 operation->ad_remaining -= input_length;
4952 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004953#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004954 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004955 status = PSA_ERROR_BAD_STATE;
4956 goto exit;
4957 }
4958#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004959
Gilles Peskine449bd832023-01-11 14:50:10 +01004960 status = psa_driver_wrapper_aead_update_ad(operation, input,
4961 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004962
Paul Elliott39dc6b82021-05-11 19:16:09 +01004963exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004964 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004965 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004966 } else {
4967 psa_aead_abort(operation);
4968 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004969
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004971}
4972
4973/* Encrypt or decrypt a message fragment in an active multipart AEAD
4974 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004975psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4976 const uint8_t *input,
4977 size_t input_length,
4978 uint8_t *output,
4979 size_t output_size,
4980 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004981{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004982 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004983
4984 *output_length = 0;
4985
Gilles Peskine449bd832023-01-11 14:50:10 +01004986 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004987 status = PSA_ERROR_BAD_STATE;
4988 goto exit;
4989 }
4990
Gilles Peskine449bd832023-01-11 14:50:10 +01004991 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004992 status = PSA_ERROR_BAD_STATE;
4993 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004994 }
4995
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004997 /* Additional data length was supplied, but not all the additional
4998 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004999 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005000 status = PSA_ERROR_INVALID_ARGUMENT;
5001 goto exit;
5002 }
5003
5004 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005005 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005006 status = PSA_ERROR_INVALID_ARGUMENT;
5007 goto exit;
5008 }
5009
5010 operation->body_remaining -= input_length;
5011 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005012#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005013 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005014 status = PSA_ERROR_BAD_STATE;
5015 goto exit;
5016 }
5017#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005018
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5020 output, output_size,
5021 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005022
Paul Elliott39dc6b82021-05-11 19:16:09 +01005023exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005025 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005026 } else {
5027 psa_aead_abort(operation);
5028 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005029
Gilles Peskine449bd832023-01-11 14:50:10 +01005030 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005031}
5032
Gilles Peskine449bd832023-01-11 14:50:10 +01005033static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005034{
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 if (operation->id == 0 || !operation->nonce_set) {
5036 return PSA_ERROR_BAD_STATE;
5037 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005038
Gilles Peskine449bd832023-01-11 14:50:10 +01005039 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5040 operation->body_remaining != 0)) {
5041 return PSA_ERROR_INVALID_ARGUMENT;
5042 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005043
Gilles Peskine449bd832023-01-11 14:50:10 +01005044 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005045}
5046
Paul Elliott302ff6b2021-04-20 18:10:30 +01005047/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005048psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5049 uint8_t *ciphertext,
5050 size_t ciphertext_size,
5051 size_t *ciphertext_length,
5052 uint8_t *tag,
5053 size_t tag_size,
5054 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005055{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005056 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5057
Paul Elliott302ff6b2021-04-20 18:10:30 +01005058 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005059 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005060
Gilles Peskine449bd832023-01-11 14:50:10 +01005061 status = psa_aead_final_checks(operation);
5062 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005063 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005065
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005067 status = PSA_ERROR_BAD_STATE;
5068 goto exit;
5069 }
5070
Gilles Peskine449bd832023-01-11 14:50:10 +01005071 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5072 ciphertext_size,
5073 ciphertext_length,
5074 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005075
Paul Elliott39dc6b82021-05-11 19:16:09 +01005076exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005077
5078
Paul Elliottf47b0952021-05-21 18:02:33 +01005079 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005080 * the zero tag size, make sure the tag is set to something implausible.
5081 * Even if the operation succeeds, make sure we clear the rest of the
5082 * buffer to prevent potential leakage of anything previously placed in
5083 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005084 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005085
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005087
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005089}
5090
5091/* Finish authenticating and decrypting a message in a multipart AEAD
5092 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005093psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5094 uint8_t *plaintext,
5095 size_t plaintext_size,
5096 size_t *plaintext_length,
5097 const uint8_t *tag,
5098 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005099{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005100 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5101
Paul Elliott302ff6b2021-04-20 18:10:30 +01005102 *plaintext_length = 0;
5103
Gilles Peskine449bd832023-01-11 14:50:10 +01005104 status = psa_aead_final_checks(operation);
5105 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005106 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005108
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005110 status = PSA_ERROR_BAD_STATE;
5111 goto exit;
5112 }
5113
Gilles Peskine449bd832023-01-11 14:50:10 +01005114 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5115 plaintext_size,
5116 plaintext_length,
5117 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005118
5119exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005121
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005123}
5124
5125/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005126psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005127{
5128 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5129
Gilles Peskine449bd832023-01-11 14:50:10 +01005130 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005131 /* The object has (apparently) been initialized but it is not (yet)
5132 * in use. It's ok to call abort on such an object, and there's
5133 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005134 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005135 }
5136
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005138
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005140
Gilles Peskine449bd832023-01-11 14:50:10 +01005141 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005142}
5143
Gilles Peskinee59236f2018-01-27 23:32:46 +01005144/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005145/* Generators */
5146/****************************************************************/
5147
Przemek Stekiel69c46792022-06-10 12:59:51 +02005148#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005149 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005150 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305151 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305152 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005153#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005154#endif /* At least one builtin KDF */
5155
Przemek Stekiel69c46792022-06-10 12:59:51 +02005156#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005157 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5158 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5159static psa_status_t psa_key_derivation_start_hmac(
5160 psa_mac_operation_t *operation,
5161 psa_algorithm_t hash_alg,
5162 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005164{
5165 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5166 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5168 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5169 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005170
Steven Cooreman72f736a2021-05-07 14:14:37 +02005171 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005173
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 status = psa_driver_wrapper_mac_sign_setup(operation,
5175 &attributes,
5176 hmac_key, hmac_key_length,
5177 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005178
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 psa_reset_key_attributes(&attributes);
5180 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005181}
5182#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005183
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005184#define HKDF_STATE_INIT 0 /* no input yet */
5185#define HKDF_STATE_STARTED 1 /* got salt */
5186#define HKDF_STATE_KEYED 2 /* got key */
5187#define HKDF_STATE_OUTPUT 3 /* output started */
5188
Gilles Peskinecbe66502019-05-16 16:59:18 +02005189static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005190 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005191{
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5193 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5194 } else {
5195 return operation->alg;
5196 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005197}
5198
Gilles Peskine449bd832023-01-11 14:50:10 +01005199psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005200{
5201 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5203 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005204 /* The object has (apparently) been initialized but it is not
5205 * in use. It's ok to call abort on such an object, and there's
5206 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005207 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005208#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005209 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5210 mbedtls_free(operation->ctx.hkdf.info);
5211 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5212 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005213#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005214#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5215 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5217 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5218 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5219 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005220 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005222 }
5223
Gilles Peskine449bd832023-01-11 14:50:10 +01005224 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005225 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005227 }
5228
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005230 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005232 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005233#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005235 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005237 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005238#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005239 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005240
5241 /* We leave the fields Ai and output_block to be erased safely by the
5242 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005244#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5245 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005246#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005247 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5248 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5249 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5250 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005251#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305252#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305253 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305254 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005255 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305256 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305257 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305258
5259 status = PSA_SUCCESS;
5260 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305261#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005262 {
5263 status = PSA_ERROR_BAD_STATE;
5264 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 mbedtls_platform_zeroize(operation, sizeof(*operation));
5266 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005267}
5268
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005269psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005271{
Gilles Peskine449bd832023-01-11 14:50:10 +01005272 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005273 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005275 }
5276
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005277 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005279}
5280
Gilles Peskine449bd832023-01-11 14:50:10 +01005281psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5282 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005283{
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 if (operation->alg == 0) {
5285 return PSA_ERROR_BAD_STATE;
5286 }
5287 if (capacity > operation->capacity) {
5288 return PSA_ERROR_INVALID_ARGUMENT;
5289 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005290 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005292}
5293
Przemek Stekiel69c46792022-06-10 12:59:51 +02005294#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005295/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005296static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5297 psa_algorithm_t kdf_alg,
5298 uint8_t *output,
5299 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005300{
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5302 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005303 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005304 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005305#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005307#else
5308 const uint8_t last_block = 0xff;
5309#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005310
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 if (hkdf->state < HKDF_STATE_KEYED ||
5312 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005313#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005314 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005315#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 )) {
5317 return PSA_ERROR_BAD_STATE;
5318 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005319 hkdf->state = HKDF_STATE_OUTPUT;
5320
Gilles Peskine449bd832023-01-11 14:50:10 +01005321 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005322 /* Copy what remains of the current block */
5323 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005325 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 }
5327 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005328 output += n;
5329 output_length -= n;
5330 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005332 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005333 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005334 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005335 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005336 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005337 * object was corrupted or if this function is called directly
5338 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 if (hkdf->block_number == last_block) {
5340 return PSA_ERROR_BAD_STATE;
5341 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005342
5343 /* We need a new block */
5344 ++hkdf->block_number;
5345 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005346
Gilles Peskine449bd832023-01-11 14:50:10 +01005347 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5348 hash_alg,
5349 hkdf->prk,
5350 hash_length);
5351 if (status != PSA_SUCCESS) {
5352 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005353 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005354
5355 if (hkdf->block_number != 1) {
5356 status = psa_mac_update(&hkdf->hmac,
5357 hkdf->output_block,
5358 hash_length);
5359 if (status != PSA_SUCCESS) {
5360 return status;
5361 }
5362 }
5363 status = psa_mac_update(&hkdf->hmac,
5364 hkdf->info,
5365 hkdf->info_length);
5366 if (status != PSA_SUCCESS) {
5367 return status;
5368 }
5369 status = psa_mac_update(&hkdf->hmac,
5370 &hkdf->block_number, 1);
5371 if (status != PSA_SUCCESS) {
5372 return status;
5373 }
5374 status = psa_mac_sign_finish(&hkdf->hmac,
5375 hkdf->output_block,
5376 sizeof(hkdf->output_block),
5377 &hmac_output_length);
5378 if (status != PSA_SUCCESS) {
5379 return status;
5380 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005381 }
5382
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005384}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005385#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005386
John Durkop07cc04a2020-11-16 22:08:34 -08005387#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5388 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005389static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5390 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005392{
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5394 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005395 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5396 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005397 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005398
Janos Follath7742fee2019-06-17 12:58:10 +01005399 /* We can't be wanting more output after block 0xff, otherwise
5400 * the capacity check in psa_key_derivation_output_bytes() would have
5401 * prevented this call. It could happen only if the operation
5402 * object was corrupted or if this function is called directly
5403 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 if (tls12_prf->block_number == 0xff) {
5405 return PSA_ERROR_CORRUPTION_DETECTED;
5406 }
Janos Follath7742fee2019-06-17 12:58:10 +01005407
5408 /* We need a new block */
5409 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005410 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005411
5412 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5413 *
5414 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5415 *
5416 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5417 * HMAC_hash(secret, A(2) + seed) +
5418 * HMAC_hash(secret, A(3) + seed) + ...
5419 *
5420 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005421 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005422 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005423 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005424 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005425 * is currently extracted as `output_block` and where i is
5426 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005427 */
5428
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 status = psa_key_derivation_start_hmac(&hmac,
5430 hash_alg,
5431 tls12_prf->secret,
5432 tls12_prf->secret_length);
5433 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005434 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005436
5437 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005438 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005439 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5440 * the variable seed and in this instance means it in the context of the
5441 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 status = psa_mac_update(&hmac,
5443 tls12_prf->label,
5444 tls12_prf->label_length);
5445 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005446 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 }
5448 status = psa_mac_update(&hmac,
5449 tls12_prf->seed,
5450 tls12_prf->seed_length);
5451 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005452 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 }
5454 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005455 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5457 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005458 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005460 }
5461
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 status = psa_mac_sign_finish(&hmac,
5463 tls12_prf->Ai, hash_length,
5464 &hmac_output_length);
5465 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005466 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 }
5468 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005469 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005471
5472 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 status = psa_key_derivation_start_hmac(&hmac,
5474 hash_alg,
5475 tls12_prf->secret,
5476 tls12_prf->secret_length);
5477 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005478 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 }
5480 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5481 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005482 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 }
5484 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5485 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005486 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 }
5488 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5489 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005490 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 }
5492 status = psa_mac_sign_finish(&hmac,
5493 tls12_prf->output_block, hash_length,
5494 &hmac_output_length);
5495 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005496 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005498
Janos Follath7742fee2019-06-17 12:58:10 +01005499
5500cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 cleanup_status = psa_mac_abort(&hmac);
5502 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005503 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005505
Gilles Peskine449bd832023-01-11 14:50:10 +01005506 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005507}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005508
Janos Follath844eb0e2019-06-19 12:10:49 +01005509static psa_status_t psa_key_derivation_tls12_prf_read(
5510 psa_tls12_prf_key_derivation_t *tls12_prf,
5511 psa_algorithm_t alg,
5512 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005514{
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5516 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005517 psa_status_t status;
5518 uint8_t offset, length;
5519
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005521 case PSA_TLS12_PRF_STATE_LABEL_SET:
5522 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5523 break;
5524 case PSA_TLS12_PRF_STATE_OUTPUT:
5525 break;
5526 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005528 }
5529
Gilles Peskine449bd832023-01-11 14:50:10 +01005530 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005531 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005532 if (tls12_prf->left_in_block == 0) {
5533 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5534 alg);
5535 if (status != PSA_SUCCESS) {
5536 return status;
5537 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005538
5539 continue;
5540 }
5541
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005543 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005545 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005546 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005547
5548 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005550 output += length;
5551 output_length -= length;
5552 tls12_prf->left_in_block -= length;
5553 }
5554
Gilles Peskine449bd832023-01-11 14:50:10 +01005555 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005556}
John Durkop07cc04a2020-11-16 22:08:34 -08005557#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5558 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005559
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005560#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5561static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5562 psa_tls12_ecjpake_to_pms_t *ecjpake,
5563 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005565{
5566 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005567 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005568
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 if (output_length != 32) {
5570 return PSA_ERROR_INVALID_ARGUMENT;
5571 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005572
Gilles Peskine449bd832023-01-11 14:50:10 +01005573 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5574 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5575 &output_size);
5576 if (status != PSA_SUCCESS) {
5577 return status;
5578 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005579
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 if (output_size != output_length) {
5581 return PSA_ERROR_GENERIC_ERROR;
5582 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005583
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005585}
5586#endif
5587
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305588#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305589static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5590 psa_pbkdf2_key_derivation_t *pbkdf2,
5591 psa_algorithm_t prf_alg,
5592 uint8_t prf_output_length,
5593 psa_key_attributes_t *attributes)
5594{
5595 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305596 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305597 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305598 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305599 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305600 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305601 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305602
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305603 mac_operation.is_sign = 1;
5604 mac_operation.mac_size = prf_output_length;
5605 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305606
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305607 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5608 attributes,
5609 pbkdf2->password,
5610 pbkdf2->password_length,
5611 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305612 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305613 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305614 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305615 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5616 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305617 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305618 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305619 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305620 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305621 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305622 }
5623 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5624 &mac_output_length);
5625 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305626 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305627 }
5628
5629 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305630 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305631 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305632 }
5633
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305634 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305635
5636 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305637 /* We are passing prf_output_length as mac_size because the driver
5638 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305639 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305640 status = psa_driver_wrapper_mac_compute(attributes,
5641 pbkdf2->password,
5642 pbkdf2->password_length,
5643 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305644 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305645 &mac_output_length);
5646 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305647 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305648 }
5649
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305650 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305651 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305652
5653cleanup:
5654 /* Zeroise buffers to clear sensitive data from memory. */
5655 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5656 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305657}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305658
5659static psa_status_t psa_key_derivation_pbkdf2_read(
5660 psa_pbkdf2_key_derivation_t *pbkdf2,
5661 psa_algorithm_t kdf_alg,
5662 uint8_t *output,
5663 size_t output_length)
5664{
5665 psa_status_t status;
5666 psa_algorithm_t prf_alg;
5667 uint8_t prf_output_length;
5668 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5669 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5670 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5671
5672 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5673 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5674 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5675 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305676 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5677 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305678 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305679 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305680 } else {
5681 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305682 }
5683
5684 switch (pbkdf2->state) {
5685 case PSA_PBKDF2_STATE_PASSWORD_SET:
5686 /* Initially we need a new block so bytes_used is equal to block size*/
5687 pbkdf2->bytes_used = prf_output_length;
5688 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5689 break;
5690 case PSA_PBKDF2_STATE_OUTPUT:
5691 break;
5692 default:
5693 return PSA_ERROR_BAD_STATE;
5694 }
5695
5696 while (output_length != 0) {
5697 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5698 if (n > output_length) {
5699 n = (uint8_t) output_length;
5700 }
5701 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5702 output += n;
5703 output_length -= n;
5704 pbkdf2->bytes_used += n;
5705
5706 if (output_length == 0) {
5707 break;
5708 }
5709
5710 /* We need a new block */
5711 pbkdf2->bytes_used = 0;
5712 pbkdf2->block_number++;
5713
5714 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5715 prf_output_length,
5716 &attributes);
5717 if (status != PSA_SUCCESS) {
5718 return status;
5719 }
5720 }
5721
5722 return PSA_SUCCESS;
5723}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305724#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305725
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005726psa_status_t psa_key_derivation_output_bytes(
5727 psa_key_derivation_operation_t *operation,
5728 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005730{
5731 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005733
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005735 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005737 }
5738
Gilles Peskine449bd832023-01-11 14:50:10 +01005739 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005740 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005741 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005742 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005743 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005744 goto exit;
5745 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005747 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005748 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005749 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5750 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005751 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005752 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005754 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005755 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005756
Przemek Stekiel69c46792022-06-10 12:59:51 +02005757#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5759 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5760 output, output_length);
5761 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005762#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005763#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5764 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5766 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5767 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5768 kdf_alg, output,
5769 output_length);
5770 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005771#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5772 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005773#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005775 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005776 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5777 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005778#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305779#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305780 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305781 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5782 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305783 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305784#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005785
Gilles Peskineeab56e42018-07-12 17:12:33 +02005786 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005787 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005789 }
5790
5791exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005793 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005794 * This allows us to differentiate between exhausted operations and
5795 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5796 * operations. */
5797 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005799 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005801 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005803}
5804
David Brown7807bf72021-02-09 16:28:23 -07005805#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005806static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005807{
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 if (data_size >= 8) {
5809 mbedtls_des_key_set_parity(data);
5810 }
5811 if (data_size >= 16) {
5812 mbedtls_des_key_set_parity(data + 8);
5813 }
5814 if (data_size >= 24) {
5815 mbedtls_des_key_set_parity(data + 16);
5816 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005817}
David Brown7807bf72021-02-09 16:28:23 -07005818#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005819
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005820/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 * ECC keys on a Weierstrass elliptic curve require the generation
5822 * of a private key which is an integer
5823 * in the range [1, N - 1], where N is the boundary of the private key domain:
5824 * N is the prime p for Diffie-Hellman, or the order of the
5825 * curve’s base point for ECC.
5826 *
5827 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5828 * This function generates the private key using the following process:
5829 *
5830 * 1. Draw a byte string of length ceiling(m/8) bytes.
5831 * 2. If m is not a multiple of 8, set the most significant
5832 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5833 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5834 * 4. If k > N - 2, discard the result and return to step 1.
5835 * 5. Output k + 1 as the private key.
5836 *
5837 * This method allows compliance to NIST standards, specifically the methods titled
5838 * Key-Pair Generation by Testing Candidates in the following publications:
5839 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5840 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5841 * Diffie-Hellman keys.
5842 *
5843 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5844 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5845 *
5846 * Note: Function allocates memory for *data buffer, so given *data should be
5847 * always NULL.
5848 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005849#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5850#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005851static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005852 psa_key_slot_t *slot,
5853 size_t bits,
5854 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005855 uint8_t **data
5856 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005857{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005858 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005859 mbedtls_mpi k;
5860 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005861 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005862 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005863 size_t m;
5864 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005865
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 mbedtls_mpi_init(&k);
5867 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005868
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005869 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005870 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005871 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005873
Gilles Peskine449bd832023-01-11 14:50:10 +01005874 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005875 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005876 goto cleanup;
5877 }
5878
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005879 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005881
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005883
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005884 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005885 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005886 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005887
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005888 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005889
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005890 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005891 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005892
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005893 /* Note: This function is always called with *data == NULL and it
5894 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 *data = mbedtls_calloc(1, m_bytes);
5896 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005897 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005898 goto cleanup;
5899 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005900
Gilles Peskine449bd832023-01-11 14:50:10 +01005901 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005902 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005903 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005904 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005906
5907 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005908 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005909 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005910 * (8 * ceiling(m/8) - m) bits of the first byte in
5911 * the string to zero.
5912 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005914 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005915 }
5916
5917 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005918 * big-endian byte string.
5919 */
5920 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005921
5922 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005923 * Result of comparison is returned. When it indicates error
5924 * then this function is called again.
5925 */
5926 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005927 }
5928
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005929 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5931 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005932cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 if (ret != 0) {
5934 status = mbedtls_to_psa_error(ret);
5935 }
5936 if (status != PSA_SUCCESS) {
5937 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005938 *data = NULL;
5939 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 mbedtls_mpi_free(&k);
5941 mbedtls_mpi_free(&diff_N_2);
5942 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005943}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005944
5945/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5946 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5947 *
5948 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5949 * draw a 32-byte string and process it as specified in
5950 * Elliptic Curves for Security [RFC7748] §5.
5951 *
5952 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5953 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5954 *
5955 * Note: Function allocates memory for *data buffer, so given *data should be
5956 * always NULL.
5957 */
5958
5959static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5960 size_t bits,
5961 psa_key_derivation_operation_t *operation,
5962 uint8_t **data
5963 )
5964{
5965 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005966 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005967
Gilles Peskine449bd832023-01-11 14:50:10 +01005968 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005969 case 255:
5970 output_length = 32;
5971 break;
5972 case 448:
5973 output_length = 56;
5974 break;
5975 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005976 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005977 break;
5978 }
5979
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005981
Gilles Peskine449bd832023-01-11 14:50:10 +01005982 if (*data == NULL) {
5983 return PSA_ERROR_INSUFFICIENT_MEMORY;
5984 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005985
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005987
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005989 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005991
Gilles Peskine449bd832023-01-11 14:50:10 +01005992 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005993 case 255:
5994 (*data)[0] &= 248;
5995 (*data)[31] &= 127;
5996 (*data)[31] |= 64;
5997 break;
5998 case 448:
5999 (*data)[0] &= 252;
6000 (*data)[55] |= 128;
6001 break;
6002 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006004 break;
6005 }
6006
6007 return status;
6008}
Valerio Setti86587ab2023-06-27 13:09:30 +02006009#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6010static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6011 psa_key_slot_t *slot, size_t bits,
6012 psa_key_derivation_operation_t *operation, uint8_t **data)
6013{
6014 (void) slot;
6015 (void) bits;
6016 (void) operation;
6017 (void) data;
6018 return PSA_ERROR_NOT_SUPPORTED;
6019}
6020
6021static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6022 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6023{
6024 (void) bits;
6025 (void) operation;
6026 (void) data;
6027 return PSA_ERROR_NOT_SUPPORTED;
6028}
6029#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6030#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006031
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006032static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006033 psa_key_slot_t *slot,
6034 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006035 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006036{
6037 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006038 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306039 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006040 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006041 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006042
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6044 return PSA_ERROR_INVALID_ARGUMENT;
6045 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006046
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006047#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006048 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6050 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6051 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006052 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6054 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006055 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 }
6057 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006058 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006059 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6060 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006061 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006063 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006064 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006065#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006066 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 if (key_type_is_raw_bytes(slot->attr.type)) {
6068 if (bits % 8 != 0) {
6069 return PSA_ERROR_INVALID_ARGUMENT;
6070 }
6071 data = mbedtls_calloc(1, bytes);
6072 if (data == NULL) {
6073 return PSA_ERROR_INSUFFICIENT_MEMORY;
6074 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006075
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 status = psa_key_derivation_output_bytes(operation, data, bytes);
6077 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006078 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 }
David Brown12ca5032021-02-11 11:02:00 -07006080#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006081 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6082 psa_des_set_key_parity(data, bytes);
6083 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006084#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 } else {
6086 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006087 }
Ronald Crondd04d422020-11-28 15:14:42 +01006088
Ronald Cronbf33c932020-11-28 18:06:53 +01006089 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006090 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006092 };
6093
Gilles Peskine449bd832023-01-11 14:50:10 +01006094 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6095 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6096 &storage_size);
6097 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306098 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 }
Archanad8a83dc2021-06-14 10:04:16 +05306100 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 status = psa_allocate_buffer_to_slot(slot, 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_driver_wrapper_import_key(&attributes,
6107 data, bytes,
6108 slot->key.data,
6109 slot->key.bytes,
6110 &slot->key.bytes, &bits);
6111 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006112 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006114
6115exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006116 mbedtls_free(data);
6117 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006118}
6119
Gilles Peskine449bd832023-01-11 14:50:10 +01006120psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6121 psa_key_derivation_operation_t *operation,
6122 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006123{
6124 psa_status_t status;
6125 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006126 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006127
Ronald Cron81709fc2020-11-14 12:10:32 +01006128 *key = MBEDTLS_SVC_KEY_ID_INIT;
6129
Gilles Peskine0f84d622019-09-12 19:03:13 +02006130 /* Reject any attempt to create a zero-length key so that we don't
6131 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 if (psa_get_key_bits(attributes) == 0) {
6133 return PSA_ERROR_INVALID_ARGUMENT;
6134 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006135
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 if (operation->alg == PSA_ALG_NONE) {
6137 return PSA_ERROR_BAD_STATE;
6138 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006139
Gilles Peskine449bd832023-01-11 14:50:10 +01006140 if (!operation->can_output_key) {
6141 return PSA_ERROR_NOT_PERMITTED;
6142 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006143
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6145 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006146#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006148 /* Deriving a key in a secure element is not implemented yet. */
6149 status = PSA_ERROR_NOT_SUPPORTED;
6150 }
6151#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006152 if (status == PSA_SUCCESS) {
6153 status = psa_generate_derived_key_internal(slot,
6154 attributes->core.bits,
6155 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006156 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 if (status == PSA_SUCCESS) {
6158 status = psa_finish_key_creation(slot, driver, key);
6159 }
6160 if (status != PSA_SUCCESS) {
6161 psa_fail_key_creation(slot, driver);
6162 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006163
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006165}
6166
Gilles Peskineeab56e42018-07-12 17:12:33 +02006167
6168
6169/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006170/* Key derivation */
6171/****************************************************************/
6172
Steven Cooreman932ffb72021-02-15 12:14:32 +01006173#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006174static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006175{
6176#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006177 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6178 return 1;
6179 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006180#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006181#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6183 return 1;
6184 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006185#endif
6186#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006187 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6188 return 1;
6189 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006190#endif
6191#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006192 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6193 return 1;
6194 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006195#endif
6196#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6198 return 1;
6199 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006200#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006201#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6203 return 1;
6204 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006205#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306206#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6207 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6208 return 1;
6209 }
6210#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306211#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6212 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6213 return 1;
6214 }
6215#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006217}
6218
Gilles Peskine449bd832023-01-11 14:50:10 +01006219static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006220{
6221 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006222 psa_status_t status = psa_hash_setup(&operation, alg);
6223 psa_hash_abort(&operation);
6224 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006225}
6226
Gilles Peskine969c5d62019-01-16 15:53:06 +01006227static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006228 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006230{
Janos Follath5fe19732019-06-20 15:09:30 +01006231 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6232 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006233 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006234
Gilles Peskine969c5d62019-01-16 15:53:06 +01006235 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006236 if (!is_kdf_alg_supported(kdf_alg)) {
6237 return PSA_ERROR_NOT_SUPPORTED;
6238 }
John Durkop07cc04a2020-11-16 22:08:34 -08006239
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006240 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306241 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006242 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6243 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306244 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6245 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6246 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306247 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306248 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 if (hash_size == 0) {
6250 return PSA_ERROR_NOT_SUPPORTED;
6251 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006252
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006253 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6254 * we might fail later, which is somewhat unfriendly and potentially
6255 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 psa_status_t status = psa_hash_try_support(hash_alg);
6257 if (status != PSA_SUCCESS) {
6258 return status;
6259 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006260 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006261
Gilles Peskine449bd832023-01-11 14:50:10 +01006262 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6263 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6264 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6265 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006266 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006267#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006268 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6270 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006271 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006273#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6274 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 operation->capacity = 255 * hash_size;
6276 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006277}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006278
Gilles Peskine449bd832023-01-11 14:50:10 +01006279static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006280{
6281#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 if (alg == PSA_ALG_ECDH) {
6283 return PSA_SUCCESS;
6284 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006285#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006286#if defined(PSA_WANT_ALG_FFDH)
6287 if (alg == PSA_ALG_FFDH) {
6288 return PSA_SUCCESS;
6289 }
6290#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006291 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006293}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006294
6295static int psa_key_derivation_allows_free_form_secret_input(
6296 psa_algorithm_t kdf_alg)
6297{
6298#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6299 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6300 return 0;
6301 }
6302#endif
6303 (void) kdf_alg;
6304 return 1;
6305}
John Durkop07cc04a2020-11-16 22:08:34 -08006306#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006307
Gilles Peskine449bd832023-01-11 14:50:10 +01006308psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6309 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006310{
6311 psa_status_t status;
6312
Gilles Peskine449bd832023-01-11 14:50:10 +01006313 if (operation->alg != 0) {
6314 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006315 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006316
Gilles Peskine449bd832023-01-11 14:50:10 +01006317 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6318 return PSA_ERROR_INVALID_ARGUMENT;
6319 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6320#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6321 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6322 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6323 status = psa_key_agreement_try_support(ka_alg);
6324 if (status != PSA_SUCCESS) {
6325 return status;
6326 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006327 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6328 return PSA_ERROR_INVALID_ARGUMENT;
6329 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006330 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6331#else
6332 return PSA_ERROR_NOT_SUPPORTED;
6333#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6334 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6335#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6336 status = psa_key_derivation_setup_kdf(operation, alg);
6337#else
6338 return PSA_ERROR_NOT_SUPPORTED;
6339#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6340 } else {
6341 return PSA_ERROR_INVALID_ARGUMENT;
6342 }
6343
6344 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006345 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 }
6347 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006348}
6349
Przemek Stekiel69c46792022-06-10 12:59:51 +02006350#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006351static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6352 psa_algorithm_t kdf_alg,
6353 psa_key_derivation_step_t step,
6354 const uint8_t *data,
6355 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006356{
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006358 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006360 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006361#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6363 return PSA_ERROR_INVALID_ARGUMENT;
6364 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006365#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006366 if (hkdf->state != HKDF_STATE_INIT) {
6367 return PSA_ERROR_BAD_STATE;
6368 } else {
6369 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6370 hash_alg,
6371 data, data_length);
6372 if (status != PSA_SUCCESS) {
6373 return status;
6374 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006375 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006377 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006378 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006379#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006381 /* We shouldn't be in different state as HKDF_EXPAND only allows
6382 * two inputs: SECRET (this case) and INFO which does not modify
6383 * the state. It could happen only if the hkdf
6384 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 if (hkdf->state != HKDF_STATE_INIT) {
6386 return PSA_ERROR_BAD_STATE;
6387 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006388
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006389 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006390 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6391 return PSA_ERROR_INVALID_ARGUMENT;
6392 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006393
Gilles Peskine449bd832023-01-11 14:50:10 +01006394 memcpy(hkdf->prk, data, data_length);
6395 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006396#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006397 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006398 /* HKDF: If no salt was provided, use an empty salt.
6399 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006400 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006401#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6403 return PSA_ERROR_BAD_STATE;
6404 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006405#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6407 hash_alg,
6408 NULL, 0);
6409 if (status != PSA_SUCCESS) {
6410 return status;
6411 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006412 hkdf->state = HKDF_STATE_STARTED;
6413 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 if (hkdf->state != HKDF_STATE_STARTED) {
6415 return PSA_ERROR_BAD_STATE;
6416 }
6417 status = psa_mac_update(&hkdf->hmac,
6418 data, data_length);
6419 if (status != PSA_SUCCESS) {
6420 return status;
6421 }
6422 status = psa_mac_sign_finish(&hkdf->hmac,
6423 hkdf->prk,
6424 sizeof(hkdf->prk),
6425 &data_length);
6426 if (status != PSA_SUCCESS) {
6427 return status;
6428 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006429 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006430
Gilles Peskine2b522db2019-04-12 15:11:49 +02006431 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006432 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006433#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006435 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006437 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006439#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006440 {
6441 /* Block 0 is empty, and the next block will be
6442 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006444 }
6445
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006447 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006448#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6450 return PSA_ERROR_INVALID_ARGUMENT;
6451 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006452#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006453#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006454 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6455 hkdf->state == HKDF_STATE_INIT) {
6456 return PSA_ERROR_BAD_STATE;
6457 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006458#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 if (hkdf->state == HKDF_STATE_OUTPUT) {
6460 return PSA_ERROR_BAD_STATE;
6461 }
6462 if (hkdf->info_set) {
6463 return PSA_ERROR_BAD_STATE;
6464 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006465 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 if (data_length != 0) {
6467 hkdf->info = mbedtls_calloc(1, data_length);
6468 if (hkdf->info == NULL) {
6469 return PSA_ERROR_INSUFFICIENT_MEMORY;
6470 }
6471 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006472 }
6473 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006474 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006475 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006476 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006477 }
6478}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006479#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006480
John Durkop07cc04a2020-11-16 22:08:34 -08006481#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6482 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006483static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6484 const uint8_t *data,
6485 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006486{
Gilles Peskine449bd832023-01-11 14:50:10 +01006487 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6488 return PSA_ERROR_BAD_STATE;
6489 }
Janos Follathf08e2652019-06-13 09:05:41 +01006490
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 if (data_length != 0) {
6492 prf->seed = mbedtls_calloc(1, data_length);
6493 if (prf->seed == NULL) {
6494 return PSA_ERROR_INSUFFICIENT_MEMORY;
6495 }
Janos Follathf08e2652019-06-13 09:05:41 +01006496
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006498 prf->seed_length = data_length;
6499 }
Janos Follathf08e2652019-06-13 09:05:41 +01006500
Gilles Peskine43f958b2020-12-13 14:55:14 +01006501 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006502
Gilles Peskine449bd832023-01-11 14:50:10 +01006503 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006504}
6505
Gilles Peskine449bd832023-01-11 14:50:10 +01006506static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6507 const uint8_t *data,
6508 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006509{
Gilles Peskine449bd832023-01-11 14:50:10 +01006510 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6511 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6512 return PSA_ERROR_BAD_STATE;
6513 }
Janos Follath81550542019-06-13 14:26:34 +01006514
Gilles Peskine449bd832023-01-11 14:50:10 +01006515 if (data_length != 0) {
6516 prf->secret = mbedtls_calloc(1, data_length);
6517 if (prf->secret == NULL) {
6518 return PSA_ERROR_INSUFFICIENT_MEMORY;
6519 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006520
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006522 prf->secret_length = data_length;
6523 }
Janos Follath81550542019-06-13 14:26:34 +01006524
Gilles Peskine43f958b2020-12-13 14:55:14 +01006525 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006526
Gilles Peskine449bd832023-01-11 14:50:10 +01006527 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006528}
6529
Gilles Peskine449bd832023-01-11 14:50:10 +01006530static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6531 const uint8_t *data,
6532 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006533{
Gilles Peskine449bd832023-01-11 14:50:10 +01006534 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6535 return PSA_ERROR_BAD_STATE;
6536 }
Janos Follath63028dd2019-06-13 09:15:47 +01006537
Gilles Peskine449bd832023-01-11 14:50:10 +01006538 if (data_length != 0) {
6539 prf->label = mbedtls_calloc(1, data_length);
6540 if (prf->label == NULL) {
6541 return PSA_ERROR_INSUFFICIENT_MEMORY;
6542 }
Janos Follath63028dd2019-06-13 09:15:47 +01006543
Gilles Peskine449bd832023-01-11 14:50:10 +01006544 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006545 prf->label_length = data_length;
6546 }
Janos Follath63028dd2019-06-13 09:15:47 +01006547
Gilles Peskine43f958b2020-12-13 14:55:14 +01006548 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006549
Gilles Peskine449bd832023-01-11 14:50:10 +01006550 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006551}
6552
Gilles Peskine449bd832023-01-11 14:50:10 +01006553static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6554 psa_key_derivation_step_t step,
6555 const uint8_t *data,
6556 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006557{
Gilles Peskine449bd832023-01-11 14:50:10 +01006558 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006559 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006560 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006561 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006562 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006563 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006564 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006565 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006566 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006567 }
6568}
John Durkop07cc04a2020-11-16 22:08:34 -08006569#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6570 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6571
6572#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6573static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6574 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006575 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006576 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006577{
6578 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006579 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6580 4 + data_length + prf->other_secret_length :
6581 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006582
Gilles Peskine449bd832023-01-11 14:50:10 +01006583 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6584 return PSA_ERROR_INVALID_ARGUMENT;
6585 }
John Durkop07cc04a2020-11-16 22:08:34 -08006586
Gilles Peskine449bd832023-01-11 14:50:10 +01006587 uint8_t *pms = mbedtls_calloc(1, pms_len);
6588 if (pms == NULL) {
6589 return PSA_ERROR_INSUFFICIENT_MEMORY;
6590 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006591 uint8_t *cur = pms;
6592
6593 /* pure-PSK:
6594 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006595 *
6596 * The premaster secret is formed as follows: if the PSK is N octets
6597 * long, concatenate a uint16 with the value N, N zero octets, a second
6598 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006599 *
6600 * mixed-PSK:
6601 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6602 * follows: concatenate a uint16 with the length of the other secret,
6603 * the other secret itself, uint16 with the length of PSK, and the
6604 * PSK itself.
6605 * For details please check:
6606 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6607 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6608 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006609 */
6610
Gilles Peskine449bd832023-01-11 14:50:10 +01006611 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6612 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6613 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6614 if (prf->other_secret_length != 0) {
6615 memcpy(cur, prf->other_secret, prf->other_secret_length);
6616 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006617 cur += prf->other_secret_length;
6618 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006619 } else {
6620 *cur++ = MBEDTLS_BYTE_1(data_length);
6621 *cur++ = MBEDTLS_BYTE_0(data_length);
6622 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006623 cur += data_length;
6624 }
6625
Gilles Peskine449bd832023-01-11 14:50:10 +01006626 *cur++ = MBEDTLS_BYTE_1(data_length);
6627 *cur++ = MBEDTLS_BYTE_0(data_length);
6628 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006629 cur += data_length;
6630
Gilles Peskine449bd832023-01-11 14:50:10 +01006631 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006632
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006633 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006634 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006635}
Janos Follath6660f0e2019-06-17 08:44:03 +01006636
Przemek Stekiele47201b2022-04-19 13:53:28 +02006637static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6638 psa_tls12_prf_key_derivation_t *prf,
6639 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006640 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006641{
Gilles Peskine449bd832023-01-11 14:50:10 +01006642 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6643 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006644 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006645
6646 if (data_length != 0) {
6647 prf->other_secret = mbedtls_calloc(1, data_length);
6648 if (prf->other_secret == NULL) {
6649 return PSA_ERROR_INSUFFICIENT_MEMORY;
6650 }
6651
6652 memcpy(prf->other_secret, data, data_length);
6653 prf->other_secret_length = data_length;
6654 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006655 prf->other_secret_length = 0;
6656 }
6657
6658 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6659
Gilles Peskine449bd832023-01-11 14:50:10 +01006660 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006661}
6662
Janos Follath6660f0e2019-06-17 08:44:03 +01006663static psa_status_t psa_tls12_prf_psk_to_ms_input(
6664 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006665 psa_key_derivation_step_t step,
6666 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006667 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006668{
Gilles Peskine449bd832023-01-11 14:50:10 +01006669 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006670 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006671 return psa_tls12_prf_psk_to_ms_set_key(prf,
6672 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006673 break;
6674 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006675 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6676 data,
6677 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006678 break;
6679 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006680 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006681 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006682
Przemek Stekiele47201b2022-04-19 13:53:28 +02006683 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006684}
John Durkop07cc04a2020-11-16 22:08:34 -08006685#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006686
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006687#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6688static psa_status_t psa_tls12_ecjpake_to_pms_input(
6689 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006690 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006691 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006692 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006693{
Gilles Peskine449bd832023-01-11 14:50:10 +01006694 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6695 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6696 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006697 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006698
6699 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006700 if (data[0] != 0x04) {
6701 return PSA_ERROR_INVALID_ARGUMENT;
6702 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006703
6704 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006705 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006706
Gilles Peskine449bd832023-01-11 14:50:10 +01006707 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006708}
6709#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306710
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306711#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306712static psa_status_t psa_pbkdf2_set_input_cost(
6713 psa_pbkdf2_key_derivation_t *pbkdf2,
6714 psa_key_derivation_step_t step,
6715 uint64_t data)
6716{
6717 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6718 return PSA_ERROR_INVALID_ARGUMENT;
6719 }
6720
6721 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6722 return PSA_ERROR_BAD_STATE;
6723 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306724
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306725 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306726 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306727 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306728
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306729 if (data == 0) {
6730 return PSA_ERROR_INVALID_ARGUMENT;
6731 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306732
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306733 pbkdf2->input_cost = data;
6734 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6735
6736 return PSA_SUCCESS;
6737}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306738
6739static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6740 const uint8_t *data,
6741 size_t data_length)
6742{
Gilles Peskineead17662023-08-20 22:02:51 +02006743 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6744 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6745 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6746 /* Appending to existing salt. No state change. */
6747 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306748 return PSA_ERROR_BAD_STATE;
6749 }
6750
Gilles Peskineca57d782023-07-20 19:08:06 +02006751 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006752 /* Appending an empty string, nothing to do. */
6753 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306754 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306755
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306756 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6757 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306758 return PSA_ERROR_INSUFFICIENT_MEMORY;
6759 }
6760
Gilles Peskineead17662023-08-20 22:02:51 +02006761 if (pbkdf2->salt_length != 0) {
6762 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6763 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306764 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306765 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306766 mbedtls_free(pbkdf2->salt);
6767 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306768 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306769 return PSA_SUCCESS;
6770}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306771
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306772#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306773static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306774 const uint8_t *input,
6775 size_t input_len,
6776 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306777 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306778{
6779 psa_status_t status = PSA_SUCCESS;
6780 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6781 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306782 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306783 } else {
6784 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306785 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306786 }
6787 return status;
6788}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306789#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306790
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306791#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306792static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6793 size_t input_len,
6794 uint8_t *output,
6795 size_t *output_len)
6796{
6797 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306798 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306799 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306800 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306801 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6802 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6803 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306804 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306805 * mac_size as the driver function sets mac_output_length = mac_size
6806 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306807 status = psa_driver_wrapper_mac_compute(&attributes,
6808 zeros, sizeof(zeros),
6809 PSA_ALG_CMAC, input, input_len,
6810 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306811 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6812 128U,
6813 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306814 output_len);
6815 } else {
6816 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306817 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306818 }
6819 return status;
6820}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306821#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306822
6823static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306824 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306825 const uint8_t *data,
6826 size_t data_length)
6827{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306828 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306829 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6830 return PSA_ERROR_BAD_STATE;
6831 }
6832
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306833#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306834 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6835 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6836 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6837 pbkdf2->password,
6838 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306839 } else
6840#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6841#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6842 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306843 status = psa_pbkdf2_cmac_set_password(data, data_length,
6844 pbkdf2->password,
6845 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306846 } else
6847#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6848 {
6849 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306850 }
6851
6852 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6853
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306854 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306855}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306856
6857static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306858 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306859 psa_key_derivation_step_t step,
6860 const uint8_t *data,
6861 size_t data_length)
6862{
6863 switch (step) {
6864 case PSA_KEY_DERIVATION_INPUT_SALT:
6865 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6866 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306867 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306868 default:
6869 return PSA_ERROR_INVALID_ARGUMENT;
6870 }
6871}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306872#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306873
Gilles Peskineb8965192019-09-24 16:21:10 +02006874/** Check whether the given key type is acceptable for the given
6875 * input step of a key derivation.
6876 *
6877 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6878 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6879 * Both secret and non-secret inputs can alternatively have the type
6880 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6881 * that the input was passed as a buffer rather than via a key object.
6882 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006883static int psa_key_derivation_check_input_type(
6884 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006885 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006886{
Gilles Peskine449bd832023-01-11 14:50:10 +01006887 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006888 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006889 if (key_type == PSA_KEY_TYPE_DERIVE) {
6890 return PSA_SUCCESS;
6891 }
6892 if (key_type == PSA_KEY_TYPE_NONE) {
6893 return PSA_SUCCESS;
6894 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006895 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006896 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006897 if (key_type == PSA_KEY_TYPE_DERIVE) {
6898 return PSA_SUCCESS;
6899 }
6900 if (key_type == PSA_KEY_TYPE_NONE) {
6901 return PSA_SUCCESS;
6902 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006903 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006904 case PSA_KEY_DERIVATION_INPUT_LABEL:
6905 case PSA_KEY_DERIVATION_INPUT_SALT:
6906 case PSA_KEY_DERIVATION_INPUT_INFO:
6907 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006908 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6909 return PSA_SUCCESS;
6910 }
6911 if (key_type == PSA_KEY_TYPE_NONE) {
6912 return PSA_SUCCESS;
6913 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006914 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306915 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6916 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6917 return PSA_SUCCESS;
6918 }
6919 if (key_type == PSA_KEY_TYPE_DERIVE) {
6920 return PSA_SUCCESS;
6921 }
6922 if (key_type == PSA_KEY_TYPE_NONE) {
6923 return PSA_SUCCESS;
6924 }
6925 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006926 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006927 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006928}
6929
Janos Follathb80a94e2019-06-12 15:54:46 +01006930static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006931 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006932 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006933 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006934 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006935 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006936{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006937 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006938 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006939
Gilles Peskine449bd832023-01-11 14:50:10 +01006940 status = psa_key_derivation_check_input_type(step, key_type);
6941 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006942 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006944
Przemek Stekiel69c46792022-06-10 12:59:51 +02006945#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006946 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6947 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6948 step, data, data_length);
6949 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006950#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006951#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6953 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6954 step, data, data_length);
6955 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006956#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6957#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006958 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6959 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6960 step, data, data_length);
6961 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006962#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006963#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006964 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006965 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006966 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6967 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006968#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306969#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306970 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306971 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6972 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306973 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306974#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006975 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006976 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006977 (void) data;
6978 (void) data_length;
6979 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006980 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006981 }
6982
Gilles Peskine224b0d62019-09-23 18:13:17 +02006983exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006984 if (status != PSA_SUCCESS) {
6985 psa_key_derivation_abort(operation);
6986 }
6987 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006988}
6989
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306990static psa_status_t psa_key_derivation_input_integer_internal(
6991 psa_key_derivation_operation_t *operation,
6992 psa_key_derivation_step_t step,
6993 uint64_t value)
6994{
6995 psa_status_t status;
6996 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6997
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306998#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306999 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307000 status = psa_pbkdf2_set_input_cost(
7001 &operation->ctx.pbkdf2, step, value);
7002 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307003#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307004 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307005 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307006 (void) value;
7007 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307008 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307009 }
7010
7011 if (status != PSA_SUCCESS) {
7012 psa_key_derivation_abort(operation);
7013 }
7014 return status;
7015}
7016
Janos Follath51f4a0f2019-06-14 11:35:55 +01007017psa_status_t psa_key_derivation_input_bytes(
7018 psa_key_derivation_operation_t *operation,
7019 psa_key_derivation_step_t step,
7020 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007021 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007022{
Gilles Peskine449bd832023-01-11 14:50:10 +01007023 return psa_key_derivation_input_internal(operation, step,
7024 PSA_KEY_TYPE_NONE,
7025 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007026}
7027
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307028psa_status_t psa_key_derivation_input_integer(
7029 psa_key_derivation_operation_t *operation,
7030 psa_key_derivation_step_t step,
7031 uint64_t value)
7032{
7033 return psa_key_derivation_input_integer_internal(operation, step, value);
7034}
7035
Janos Follath51f4a0f2019-06-14 11:35:55 +01007036psa_status_t psa_key_derivation_input_key(
7037 psa_key_derivation_operation_t *operation,
7038 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007039 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007040{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007041 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007042 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007043 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007044
Ronald Cron5c522922020-11-14 16:35:34 +01007045 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007046 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7047 if (status != PSA_SUCCESS) {
7048 psa_key_derivation_abort(operation);
7049 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007050 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007051
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307052 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7053 * permission to output to a key object. */
7054 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7055 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007056 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007057 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007058
Gilles Peskine449bd832023-01-11 14:50:10 +01007059 status = psa_key_derivation_input_internal(operation,
7060 step, slot->attr.type,
7061 slot->key.data,
7062 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007063
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007065
Gilles Peskine449bd832023-01-11 14:50:10 +01007066 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007067}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007068
7069
7070
7071/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007072/* Key agreement */
7073/****************************************************************/
7074
Gilles Peskine449bd832023-01-11 14:50:10 +01007075psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7076 const uint8_t *key_buffer,
7077 size_t key_buffer_size,
7078 psa_algorithm_t alg,
7079 const uint8_t *peer_key,
7080 size_t peer_key_length,
7081 uint8_t *shared_secret,
7082 size_t shared_secret_size,
7083 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007084{
Gilles Peskine449bd832023-01-11 14:50:10 +01007085 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007086#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007087 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007088 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7089 key_buffer_size, alg,
7090 peer_key, peer_key_length,
7091 shared_secret,
7092 shared_secret_size,
7093 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007094#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007095
7096#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7097 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007098 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007099 peer_key,
7100 peer_key_length,
7101 key_buffer,
7102 key_buffer_size,
7103 shared_secret,
7104 shared_secret_size,
7105 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007106#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7107
Gilles Peskine01d718c2018-09-18 12:01:02 +02007108 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007109 (void) attributes;
7110 (void) key_buffer;
7111 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007112 (void) peer_key;
7113 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007114 (void) shared_secret;
7115 (void) shared_secret_size;
7116 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007117 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007118 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007119}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007120
Aditya Deshpande17845b82022-10-13 17:21:01 +01007121/** Internal function for raw key agreement
7122 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007123 * to the driver's implementation if a driver is present.
7124 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007125 * (psa_key_agreement_raw_builtin).
7126 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007127static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7128 psa_key_slot_t *private_key,
7129 const uint8_t *peer_key,
7130 size_t peer_key_length,
7131 uint8_t *shared_secret,
7132 size_t shared_secret_size,
7133 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007134{
Gilles Peskine449bd832023-01-11 14:50:10 +01007135 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7136 return PSA_ERROR_NOT_SUPPORTED;
7137 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007138
7139 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007140 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007141 };
7142
Gilles Peskine449bd832023-01-11 14:50:10 +01007143 return psa_driver_wrapper_key_agreement(&attributes,
7144 private_key->key.data,
7145 private_key->key.bytes, alg,
7146 peer_key, peer_key_length,
7147 shared_secret,
7148 shared_secret_size,
7149 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007150}
7151
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007152/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007153 * to potentially free embedded data structures and wipe confidential data.
7154 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007155static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7156 psa_key_derivation_step_t step,
7157 psa_key_slot_t *private_key,
7158 const uint8_t *peer_key,
7159 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007160{
7161 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007162 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007163 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007164 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007165
7166 /* Step 1: run the secret agreement algorithm to generate the shared
7167 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007168 status = psa_key_agreement_raw_internal(ka_alg,
7169 private_key,
7170 peer_key, peer_key_length,
7171 shared_secret,
7172 sizeof(shared_secret),
7173 &shared_secret_length);
7174 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007175 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007176 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007177
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007178 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007179 * the shared secret. A shared secret is permitted wherever a key
7180 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007181 status = psa_key_derivation_input_internal(operation, step,
7182 PSA_KEY_TYPE_DERIVE,
7183 shared_secret,
7184 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007185exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007186 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7187 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007188}
7189
Gilles Peskine449bd832023-01-11 14:50:10 +01007190psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7191 psa_key_derivation_step_t step,
7192 mbedtls_svc_key_id_t private_key,
7193 const uint8_t *peer_key,
7194 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007195{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007196 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007197 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007198 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007199
Gilles Peskine449bd832023-01-11 14:50:10 +01007200 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7201 return PSA_ERROR_INVALID_ARGUMENT;
7202 }
Ronald Cron5c522922020-11-14 16:35:34 +01007203 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007204 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7205 if (status != PSA_SUCCESS) {
7206 return status;
7207 }
7208 status = psa_key_agreement_internal(operation, step,
7209 slot,
7210 peer_key, peer_key_length);
7211 if (status != PSA_SUCCESS) {
7212 psa_key_derivation_abort(operation);
7213 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007214 /* If a private key has been added as SECRET, we allow the derived
7215 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007216 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007217 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007218 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007219 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007220
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007222
Gilles Peskine449bd832023-01-11 14:50:10 +01007223 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007224}
7225
Gilles Peskine449bd832023-01-11 14:50:10 +01007226psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7227 mbedtls_svc_key_id_t private_key,
7228 const uint8_t *peer_key,
7229 size_t peer_key_length,
7230 uint8_t *output,
7231 size_t output_size,
7232 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007233{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007234 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007235 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007236 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007237 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007238
Gilles Peskine449bd832023-01-11 14:50:10 +01007239 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007240 status = PSA_ERROR_INVALID_ARGUMENT;
7241 goto exit;
7242 }
Ronald Cron5c522922020-11-14 16:35:34 +01007243 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007244 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7245 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007246 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007247 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007248
Gilles Peskine3e561302022-04-14 00:17:15 +02007249 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7250 * for the output size. The PSA specification only guarantees that this
7251 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7252 * but it might be nice to allow smaller buffers if the output fits.
7253 * At the time of writing this comment, with only ECDH implemented,
7254 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7255 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7256 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007257 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007258 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7259 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007260 status = PSA_ERROR_BUFFER_TOO_SMALL;
7261 goto exit;
7262 }
7263
Gilles Peskine449bd832023-01-11 14:50:10 +01007264 status = psa_key_agreement_raw_internal(alg, slot,
7265 peer_key, peer_key_length,
7266 output, output_size,
7267 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007268
7269exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007270 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007271 /* If an error happens and is not handled properly, the output
7272 * may be used as a key to protect sensitive data. Arrange for such
7273 * a key to be random, which is likely to result in decryption or
7274 * verification errors. This is better than filling the buffer with
7275 * some constant data such as zeros, which would result in the data
7276 * being protected with a reproducible, easily knowable key.
7277 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007278 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007279 *output_length = output_size;
7280 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007281
Gilles Peskine449bd832023-01-11 14:50:10 +01007282 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007283
Gilles Peskine449bd832023-01-11 14:50:10 +01007284 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007285}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007286
7287
Gilles Peskine30524eb2020-11-13 17:02:26 +01007288
Gilles Peskine86a440b2018-11-12 18:39:40 +01007289/****************************************************************/
7290/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007291/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007292
Gilles Peskine672a7712023-04-28 21:00:28 +02007293#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7294#include "entropy_poll.h"
7295#endif
7296
Gilles Peskine30524eb2020-11-13 17:02:26 +01007297/** Initialize the PSA random generator.
7298 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007299static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007300{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007301#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007302 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007303#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7304
Gilles Peskine30524eb2020-11-13 17:02:26 +01007305 /* Set default configuration if
7306 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007307 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007308 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007309 }
7310 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007311 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007312 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007313
Gilles Peskine449bd832023-01-11 14:50:10 +01007314 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007315#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7316 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7317 /* The PSA entropy injection feature depends on using NV seed as an entropy
7318 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007319 mbedtls_entropy_add_source(&rng->entropy,
7320 mbedtls_nv_seed_poll, NULL,
7321 MBEDTLS_ENTROPY_BLOCK_SIZE,
7322 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007323#endif
7324
Gilles Peskine449bd832023-01-11 14:50:10 +01007325 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007326#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007327}
7328
7329/** Deinitialize the PSA random generator.
7330 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007331static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007332{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007333#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007334 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007335#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007336 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7337 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007338#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007339}
7340
7341/** Seed the PSA random generator.
7342 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007343static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007344{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007345#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7346 /* Do nothing: the external RNG seeds itself. */
7347 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007348 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007349#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007350 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007351 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7352 drbg_seed, sizeof(drbg_seed) - 1);
7353 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007354#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007355}
7356
Gilles Peskine449bd832023-01-11 14:50:10 +01007357psa_status_t psa_generate_random(uint8_t *output,
7358 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007359{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007360 GUARD_MODULE_INITIALIZED;
7361
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007362#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7363
7364 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7366 output, output_size,
7367 &output_length);
7368 if (status != PSA_SUCCESS) {
7369 return status;
7370 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007371 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007372 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007373 if (output_length != output_size) {
7374 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7375 }
7376 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007377
7378#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7379
Gilles Peskine449bd832023-01-11 14:50:10 +01007380 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007381 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007382 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7383 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7384 output_size);
7385 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7386 output, request_size);
7387 if (ret != 0) {
7388 return mbedtls_to_psa_error(ret);
7389 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007390 output_size -= request_size;
7391 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007392 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007393 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007394#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007395}
7396
Gilles Peskine8814fc42020-12-14 15:33:44 +01007397/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007398 *
7399 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7400 * `psa_generate_random(...)`. The state parameter is ignored since the
7401 * PSA API doesn't support passing an explicit state.
7402 *
7403 * In the non-external case, psa_generate_random() calls an
7404 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7405 * and semantics as mbedtls_psa_get_random(). As an optimization,
7406 * instead of doing this back-and-forth between the PSA API and the
7407 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7408 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007409 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007410#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7411int mbedtls_psa_get_random(void *p_rng,
7412 unsigned char *output,
7413 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007414{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007415 /* This function takes a pointer to the RNG state because that's what
7416 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7417 * its own state internally and doesn't let the caller access that state.
7418 * So we just ignore the state parameter, and in practice we'll pass
7419 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007420 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007421 psa_status_t status = psa_generate_random(output, output_size);
7422 if (status == PSA_SUCCESS) {
7423 return 0;
7424 } else {
7425 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7426 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007427}
7428#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7429
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007430#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007431psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7432 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007433{
Gilles Peskine449bd832023-01-11 14:50:10 +01007434 if (global_data.initialized) {
7435 return PSA_ERROR_NOT_PERMITTED;
7436 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007437
Gilles Peskine449bd832023-01-11 14:50:10 +01007438 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7439 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7440 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7441 return PSA_ERROR_INVALID_ARGUMENT;
7442 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007443
Gilles Peskine449bd832023-01-11 14:50:10 +01007444 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007445}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007446#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007447
Ronald Cron3772afe2021-02-08 16:10:05 +01007448/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007449 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007450 * \param type The key type
7451 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007452 *
7453 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007454 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007455 * \retval #PSA_ERROR_INVALID_ARGUMENT
7456 * The size in bits of the key is not valid.
7457 * \retval #PSA_ERROR_NOT_SUPPORTED
7458 * The type and/or the size in bits of the key or the combination of
7459 * the two is not supported.
7460 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007461static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007462 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007463{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007464 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007465
Gilles Peskine449bd832023-01-11 14:50:10 +01007466 if (key_type_is_raw_bytes(type)) {
7467 status = psa_validate_unstructured_key_bit_size(type, bits);
7468 if (status != PSA_SUCCESS) {
7469 return status;
7470 }
7471 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007472#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007473 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7474 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7475 return PSA_ERROR_NOT_SUPPORTED;
7476 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007477 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007478 return PSA_ERROR_NOT_SUPPORTED;
7479 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007480
Ronald Cron01b2aba2020-10-05 09:42:02 +02007481 /* Accept only byte-aligned keys, for the same reasons as
7482 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007483 if (bits % 8 != 0) {
7484 return PSA_ERROR_NOT_SUPPORTED;
7485 }
7486 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007487#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007488
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007489#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007490 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007491 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007492 return PSA_SUCCESS;
7493 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007494#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007495
Valerio Settia55f0422023-07-10 15:34:41 +02007496#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007497 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007498 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007499 return PSA_ERROR_NOT_SUPPORTED;
7500 }
7501 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007502#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007503 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007504 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007505 }
7506
Gilles Peskine449bd832023-01-11 14:50:10 +01007507 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007508}
7509
Ronald Cron55ed0592020-10-05 10:30:40 +02007510psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007511 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007512 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007513{
7514 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007515 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007516
Gilles Peskine449bd832023-01-11 14:50:10 +01007517 if ((attributes->domain_parameters == NULL) &&
7518 (attributes->domain_parameters_size != 0)) {
7519 return PSA_ERROR_INVALID_ARGUMENT;
7520 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007521
Gilles Peskine449bd832023-01-11 14:50:10 +01007522 if (key_type_is_raw_bytes(type)) {
7523 status = psa_generate_random(key_buffer, key_buffer_size);
7524 if (status != PSA_SUCCESS) {
7525 return status;
7526 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007527
David Brown12ca5032021-02-11 11:02:00 -07007528#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007529 if (type == PSA_KEY_TYPE_DES) {
7530 psa_des_set_key_parity(key_buffer, key_buffer_size);
7531 }
David Brown8107e312021-02-12 08:08:46 -07007532#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007533 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007534
Valerio Setti76df8c12023-07-11 14:11:28 +02007535#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007536 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7537 return mbedtls_psa_rsa_generate_key(attributes,
7538 key_buffer,
7539 key_buffer_size,
7540 key_buffer_length);
7541 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007542#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007543
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007544#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007545 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7546 return mbedtls_psa_ecp_generate_key(attributes,
7547 key_buffer,
7548 key_buffer_size,
7549 key_buffer_length);
7550 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007551#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007552
Valerio Settia55f0422023-07-10 15:34:41 +02007553#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007554 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7555 return mbedtls_psa_ffdh_generate_key(attributes,
7556 key_buffer,
7557 key_buffer_size,
7558 key_buffer_length);
7559 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007560#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007561 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007562 (void) key_buffer_length;
7563 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007564 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007565
Gilles Peskine449bd832023-01-11 14:50:10 +01007566 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007567}
Darryl Green0c6575a2018-11-07 16:05:30 +00007568
Gilles Peskine449bd832023-01-11 14:50:10 +01007569psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7570 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007571{
7572 psa_status_t status;
7573 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007574 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007575 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007576
Ronald Cron81709fc2020-11-14 12:10:32 +01007577 *key = MBEDTLS_SVC_KEY_ID_INIT;
7578
Gilles Peskine0f84d622019-09-12 19:03:13 +02007579 /* Reject any attempt to create a zero-length key so that we don't
7580 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007581 if (psa_get_key_bits(attributes) == 0) {
7582 return PSA_ERROR_INVALID_ARGUMENT;
7583 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007584
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007585 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007586 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7587 return PSA_ERROR_INVALID_ARGUMENT;
7588 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007589
Gilles Peskine449bd832023-01-11 14:50:10 +01007590 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7591 &slot, &driver);
7592 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007593 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007594 }
Gilles Peskine11792082019-08-06 18:36:36 +02007595
Ronald Cron977c2472020-10-13 08:32:21 +02007596 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307597 * storage ( thus not in the case of generating a key in a secure element
7598 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7599 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007600 if (slot->key.data == NULL) {
7601 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7602 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007603 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007604 attributes->core.type, attributes->core.bits);
7605 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007606 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007608
7609 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 attributes->core.type,
7611 attributes->core.bits);
7612 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007613 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007614 attributes, &key_buffer_size);
7615 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007616 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007617 }
Ronald Cron977c2472020-10-13 08:32:21 +02007618 }
Ronald Cron977c2472020-10-13 08:32:21 +02007619
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7621 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007622 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007623 }
Ronald Cron977c2472020-10-13 08:32:21 +02007624 }
7625
Gilles Peskine449bd832023-01-11 14:50:10 +01007626 status = psa_driver_wrapper_generate_key(attributes,
7627 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007628
Gilles Peskine449bd832023-01-11 14:50:10 +01007629 if (status != PSA_SUCCESS) {
7630 psa_remove_key_data_from_memory(slot);
7631 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007632
Gilles Peskine11792082019-08-06 18:36:36 +02007633exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007634 if (status == PSA_SUCCESS) {
7635 status = psa_finish_key_creation(slot, driver, key);
7636 }
7637 if (status != PSA_SUCCESS) {
7638 psa_fail_key_creation(slot, driver);
7639 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007640
Gilles Peskine449bd832023-01-11 14:50:10 +01007641 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007642}
7643
Gilles Peskinee59236f2018-01-27 23:32:46 +01007644/****************************************************************/
7645/* Module setup */
7646/****************************************************************/
7647
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007648#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007649psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007650 void (* entropy_init)(mbedtls_entropy_context *ctx),
7651 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007652{
Gilles Peskine449bd832023-01-11 14:50:10 +01007653 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7654 return PSA_ERROR_BAD_STATE;
7655 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007656 global_data.rng.entropy_init = entropy_init;
7657 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007658 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007659}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007660#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007661
Gilles Peskine449bd832023-01-11 14:50:10 +01007662void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007663{
Gilles Peskine449bd832023-01-11 14:50:10 +01007664 psa_wipe_all_key_slots();
7665 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7666 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007667 }
7668 /* Wipe all remaining data, including configuration.
7669 * In particular, this sets all state indicator to the value
7670 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007671 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007672
7673 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007674 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007675}
7676
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007677#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7678/** Recover a transaction that was interrupted by a power failure.
7679 *
7680 * This function is called during initialization, before psa_crypto_init()
7681 * returns. If this function returns a failure status, the initialization
7682 * fails.
7683 */
7684static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007685 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007686{
Gilles Peskine449bd832023-01-11 14:50:10 +01007687 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007688 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7689 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007690 /* TODO - fall through to the failure case until this
7691 * is implemented.
7692 * https://github.com/ARMmbed/mbed-crypto/issues/218
7693 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007694 default:
7695 /* We found an unsupported transaction in the storage.
7696 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007697 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007698 }
7699}
7700#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7701
Gilles Peskine449bd832023-01-11 14:50:10 +01007702psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007703{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007704 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007705
Gilles Peskinec6b69072018-11-20 21:42:52 +01007706 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007707 if (global_data.initialized != 0) {
7708 return PSA_SUCCESS;
7709 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007710
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007711 /* Init drivers */
7712 status = psa_driver_wrapper_init();
7713 if (status != PSA_SUCCESS) {
7714 goto exit;
7715 }
7716 global_data.drivers_initialized = 1;
7717
Gilles Peskine30524eb2020-11-13 17:02:26 +01007718 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007719 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007720 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007721 status = mbedtls_psa_random_seed(&global_data.rng);
7722 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007723 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007724 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007725 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007726
Gilles Peskine449bd832023-01-11 14:50:10 +01007727 status = psa_initialize_key_slots();
7728 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007729 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007730 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007731
Gilles Peskine4b734222019-07-24 15:56:31 +02007732#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007733 status = psa_crypto_load_transaction();
7734 if (status == PSA_SUCCESS) {
7735 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7736 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007737 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007738 }
7739 status = psa_crypto_stop_transaction();
7740 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007741 /* There's no transaction to complete. It's all good. */
7742 status = PSA_SUCCESS;
7743 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007744#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007745
Gilles Peskinec6b69072018-11-20 21:42:52 +01007746 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007747 global_data.initialized = 1;
7748
7749exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007750 if (status != PSA_SUCCESS) {
7751 mbedtls_psa_crypto_free();
7752 }
7753 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007754}
7755
Yanray Wangffc3c482023-07-11 12:01:04 +08007756#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007757psa_status_t psa_crypto_driver_pake_get_password_len(
7758 const psa_crypto_driver_pake_inputs_t *inputs,
7759 size_t *password_len)
7760{
7761 if (inputs->password_len == 0) {
7762 return PSA_ERROR_BAD_STATE;
7763 }
7764
7765 *password_len = inputs->password_len;
7766
7767 return PSA_SUCCESS;
7768}
7769
7770psa_status_t psa_crypto_driver_pake_get_password(
7771 const psa_crypto_driver_pake_inputs_t *inputs,
7772 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7773{
7774 if (inputs->password_len == 0) {
7775 return PSA_ERROR_BAD_STATE;
7776 }
7777
7778 if (buffer_size < inputs->password_len) {
7779 return PSA_ERROR_BUFFER_TOO_SMALL;
7780 }
7781
7782 memcpy(buffer, inputs->password, inputs->password_len);
7783 *buffer_length = inputs->password_len;
7784
7785 return PSA_SUCCESS;
7786}
7787
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007788psa_status_t psa_crypto_driver_pake_get_user_len(
7789 const psa_crypto_driver_pake_inputs_t *inputs,
7790 size_t *user_len)
7791{
7792 if (inputs->user_len == 0) {
7793 return PSA_ERROR_BAD_STATE;
7794 }
7795
7796 *user_len = inputs->user_len;
7797
7798 return PSA_SUCCESS;
7799}
7800
7801psa_status_t psa_crypto_driver_pake_get_user(
7802 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007803 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007804{
7805 if (inputs->user_len == 0) {
7806 return PSA_ERROR_BAD_STATE;
7807 }
7808
Przemek Stekielc0e62502023-03-14 11:49:36 +01007809 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007810 return PSA_ERROR_BUFFER_TOO_SMALL;
7811 }
7812
Przemek Stekielc0e62502023-03-14 11:49:36 +01007813 memcpy(user_id, inputs->user, inputs->user_len);
7814 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007815
7816 return PSA_SUCCESS;
7817}
7818
7819psa_status_t psa_crypto_driver_pake_get_peer_len(
7820 const psa_crypto_driver_pake_inputs_t *inputs,
7821 size_t *peer_len)
7822{
7823 if (inputs->peer_len == 0) {
7824 return PSA_ERROR_BAD_STATE;
7825 }
7826
7827 *peer_len = inputs->peer_len;
7828
7829 return PSA_SUCCESS;
7830}
7831
7832psa_status_t psa_crypto_driver_pake_get_peer(
7833 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007834 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007835{
7836 if (inputs->peer_len == 0) {
7837 return PSA_ERROR_BAD_STATE;
7838 }
7839
Przemek Stekielc0e62502023-03-14 11:49:36 +01007840 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007841 return PSA_ERROR_BUFFER_TOO_SMALL;
7842 }
7843
Przemek Stekielc0e62502023-03-14 11:49:36 +01007844 memcpy(peer_id, inputs->peer, inputs->peer_len);
7845 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007846
7847 return PSA_SUCCESS;
7848}
7849
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007850psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7851 const psa_crypto_driver_pake_inputs_t *inputs,
7852 psa_pake_cipher_suite_t *cipher_suite)
7853{
7854 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7855 return PSA_ERROR_BAD_STATE;
7856 }
7857
7858 *cipher_suite = inputs->cipher_suite;
7859
7860 return PSA_SUCCESS;
7861}
7862
Neil Armstronga7d08c32022-06-01 18:21:20 +02007863psa_status_t psa_pake_setup(
7864 psa_pake_operation_t *operation,
7865 const psa_pake_cipher_suite_t *cipher_suite)
7866{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007867 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007868
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007869 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007870 status = PSA_ERROR_BAD_STATE;
7871 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007872 }
7873
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007874 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007875 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007876 status = PSA_ERROR_INVALID_ARGUMENT;
7877 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007878 }
7879
Przemek Stekiel51eac532022-12-07 11:04:51 +01007880 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7881
Przemek Stekiele12ed362022-12-21 12:54:46 +01007882 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007883 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7884 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007885 operation->data.inputs.cipher_suite = *cipher_suite;
7886
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007887#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007888 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007889 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007890 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007891
David Horstmann16f01512023-06-14 17:21:07 +01007892 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007893 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007894 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007895#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007896 {
7897 status = PSA_ERROR_NOT_SUPPORTED;
7898 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007899 }
7900
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007901 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7902
Przemek Stekiel51eac532022-12-07 11:04:51 +01007903 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007904exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007905 psa_pake_abort(operation);
7906 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007907}
7908
7909psa_status_t psa_pake_set_password_key(
7910 psa_pake_operation_t *operation,
7911 mbedtls_svc_key_id_t password)
7912{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007913 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007914 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7915 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007916 psa_key_attributes_t attributes;
7917 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007918
Przemek Stekiel51eac532022-12-07 11:04:51 +01007919 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007920 status = PSA_ERROR_BAD_STATE;
7921 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007922 }
7923
Przemek Stekiel6c764412022-11-22 14:05:12 +01007924 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7925 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007926 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007927 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007928 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007929 }
7930
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007931 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007932 .core = slot->attr
7933 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007934
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007935 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007936
Przemek Stekiel51eac532022-12-07 11:04:51 +01007937 if (type != PSA_KEY_TYPE_PASSWORD &&
7938 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7939 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007940 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007941 }
7942
Przemek Stekiel51eac532022-12-07 11:04:51 +01007943 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7944 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007945 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007946 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007947 }
7948
7949 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7950 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007951 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007952exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007953 if (status != PSA_SUCCESS) {
7954 psa_pake_abort(operation);
7955 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007956 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007957 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007958}
7959
7960psa_status_t psa_pake_set_user(
7961 psa_pake_operation_t *operation,
7962 const uint8_t *user_id,
7963 size_t user_id_len)
7964{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007965 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007966
7967 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007968 status = PSA_ERROR_BAD_STATE;
7969 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007970 }
7971
Przemek Stekiel51eac532022-12-07 11:04:51 +01007972 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007973 status = PSA_ERROR_INVALID_ARGUMENT;
7974 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007975 }
7976
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007977 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007978 status = PSA_ERROR_BAD_STATE;
7979 goto exit;
7980 }
7981
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007982 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7983 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007984 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7985 goto exit;
7986 }
7987
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007988 memcpy(operation->data.inputs.user, user_id, user_id_len);
7989 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007990
7991 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007992exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007993 psa_pake_abort(operation);
7994 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007995}
7996
7997psa_status_t psa_pake_set_peer(
7998 psa_pake_operation_t *operation,
7999 const uint8_t *peer_id,
8000 size_t peer_id_len)
8001{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008002 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008003
8004 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008005 status = PSA_ERROR_BAD_STATE;
8006 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008007 }
8008
Przemek Stekiel51eac532022-12-07 11:04:51 +01008009 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008010 status = PSA_ERROR_INVALID_ARGUMENT;
8011 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008012 }
8013
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008014 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008015 status = PSA_ERROR_BAD_STATE;
8016 goto exit;
8017 }
8018
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008019 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8020 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008021 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8022 goto exit;
8023 }
8024
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008025 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8026 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008027
8028 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008029exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008030 psa_pake_abort(operation);
8031 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008032}
8033
8034psa_status_t psa_pake_set_role(
8035 psa_pake_operation_t *operation,
8036 psa_pake_role_t role)
8037{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008038 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008039
Przemek Stekiel51eac532022-12-07 11:04:51 +01008040 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008041 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008042 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008043 }
8044
Przemek Stekiel09104b82023-03-06 14:11:51 +01008045 switch (operation->alg) {
8046#if defined(PSA_WANT_ALG_JPAKE)
8047 case PSA_ALG_JPAKE:
8048 if (role == PSA_PAKE_ROLE_NONE) {
8049 return PSA_SUCCESS;
8050 }
8051 status = PSA_ERROR_INVALID_ARGUMENT;
8052 break;
8053#endif
8054 default:
8055 (void) role;
8056 status = PSA_ERROR_NOT_SUPPORTED;
8057 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008058 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008059exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008060 psa_pake_abort(operation);
8061 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008062}
8063
David Horstmanne7f21e62023-05-12 18:17:21 +01008064/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008065#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008066static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008067 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008068{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008069 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008070 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008071 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008072
David Horstmann024e5c52023-06-14 15:48:21 +01008073 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008074 is_x1 = (stage->outputs < 1);
8075 } else {
8076 is_x1 = (stage->inputs < 1);
8077 }
8078
David Horstmann74a3d8c2023-06-14 18:28:19 +01008079 key_share_step = is_x1 ?
8080 PSA_JPAKE_X1_STEP_KEY_SHARE :
8081 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008082 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008083 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8084 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8085 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8086 } else {
8087 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008088 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008089 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008090}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008091#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008092
Przemek Stekiel2797d372022-12-22 11:19:22 +01008093static psa_status_t psa_pake_complete_inputs(
8094 psa_pake_operation_t *operation)
8095{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008096 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008097 /* Create copy of the inputs on stack as inputs share memory
8098 with the driver context which will be setup by the driver. */
8099 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008100
Przemek Stekielfde11282023-03-13 16:06:09 +01008101 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008102 return PSA_ERROR_BAD_STATE;
8103 }
8104
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008105 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008106 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8107 return PSA_ERROR_BAD_STATE;
8108 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008109 }
8110
Przemek Stekiel18620a32023-01-17 16:34:52 +01008111 /* Clear driver context */
8112 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8113
8114 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008115
8116 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008117 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008118
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008119 /* User and peer are translated to role. */
8120 mbedtls_free(inputs.user);
8121 mbedtls_free(inputs.peer);
8122
Przemek Stekiel2797d372022-12-22 11:19:22 +01008123 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008124#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008125 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008126 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008127 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008128#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008129 {
8130 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008131 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008132 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008133 return status;
8134}
8135
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008136#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008137static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008138 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008139 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008140 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008141{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008142 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8143 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8144 step != PSA_PAKE_STEP_ZK_PROOF) {
8145 return PSA_ERROR_INVALID_ARGUMENT;
8146 }
8147
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008148 psa_jpake_computation_stage_t *computation_stage =
8149 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008150
David Horstmann5da95602023-06-08 15:37:12 +01008151 if (computation_stage->round != PSA_JPAKE_FIRST &&
8152 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008153 return PSA_ERROR_BAD_STATE;
8154 }
8155
David Horstmanne7f21e62023-05-12 18:17:21 +01008156 /* Check that the step we are given is the one we were expecting */
8157 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008158 return PSA_ERROR_BAD_STATE;
8159 }
8160
David Horstmanne7f21e62023-05-12 18:17:21 +01008161 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8162 computation_stage->inputs == 0 &&
8163 computation_stage->outputs == 0) {
8164 /* Start of the round, so function decides whether we are inputting
8165 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008166 computation_stage->io_mode = io_mode;
8167 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008168 /* Middle of the round so the mode we are in must match the function
8169 * called by the user */
8170 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008171 }
8172
8173 return PSA_SUCCESS;
8174}
8175
David Horstmanne7f21e62023-05-12 18:17:21 +01008176static psa_status_t psa_jpake_epilogue(
8177 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008178 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008179{
David Horstmanne7f21e62023-05-12 18:17:21 +01008180 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008181 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008182
David Horstmanne7f21e62023-05-12 18:17:21 +01008183 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8184 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008185 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008186 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008187 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008188 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008189 }
8190 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008191 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008192 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008193 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008194 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008195 }
8196 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008197 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8198 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008199 /* End of a round, move to the next round */
8200 stage->inputs = 0;
8201 stage->outputs = 0;
8202 stage->round++;
8203 }
8204 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008205 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008206 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008207 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008208 return PSA_SUCCESS;
8209}
David Horstmanne7f21e62023-05-12 18:17:21 +01008210
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008211#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008212
Neil Armstronga7d08c32022-06-01 18:21:20 +02008213psa_status_t psa_pake_output(
8214 psa_pake_operation_t *operation,
8215 psa_pake_step_t step,
8216 uint8_t *output,
8217 size_t output_size,
8218 size_t *output_length)
8219{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008220 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008221 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008222 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008223
8224 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008225 status = psa_pake_complete_inputs(operation);
8226 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008227 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008228 }
8229 }
8230
8231 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008232 status = PSA_ERROR_BAD_STATE;
8233 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008234 }
8235
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008236 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008237 status = PSA_ERROR_INVALID_ARGUMENT;
8238 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008239 }
8240
Przemek Stekiele12ed362022-12-21 12:54:46 +01008241 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008242#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008243 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008244 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008245 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008246 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008247 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008248 driver_step = convert_jpake_computation_stage_to_driver_step(
8249 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008250 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008251#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008252 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008253 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008254 status = PSA_ERROR_NOT_SUPPORTED;
8255 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008256 }
8257
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008258 status = psa_driver_wrapper_pake_output(operation, driver_step,
8259 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008260
8261 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008262 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008263 }
8264
8265 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008266#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008267 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008268 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008269 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008270 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008271 }
8272 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008273#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008274 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008275 status = PSA_ERROR_NOT_SUPPORTED;
8276 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008277 }
8278
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008279 return PSA_SUCCESS;
8280exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008281 psa_pake_abort(operation);
8282 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008283}
8284
8285psa_status_t psa_pake_input(
8286 psa_pake_operation_t *operation,
8287 psa_pake_step_t step,
8288 const uint8_t *input,
8289 size_t input_length)
8290{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008291 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008292 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008293 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8294 operation->primitive,
8295 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008296
8297 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008298 status = psa_pake_complete_inputs(operation);
8299 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008300 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008301 }
8302 }
8303
8304 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008305 status = PSA_ERROR_BAD_STATE;
8306 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008307 }
8308
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008309 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008310 status = PSA_ERROR_INVALID_ARGUMENT;
8311 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008312 }
8313
Przemek Stekiele12ed362022-12-21 12:54:46 +01008314 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008315#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008316 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008317 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008318 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008319 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008320 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008321 driver_step = convert_jpake_computation_stage_to_driver_step(
8322 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008323 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008324#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008325 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008326 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008327 status = PSA_ERROR_NOT_SUPPORTED;
8328 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008329 }
8330
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008331 status = psa_driver_wrapper_pake_input(operation, driver_step,
8332 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008333
8334 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008335 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008336 }
8337
8338 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008339#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008340 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008341 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008342 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008343 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008344 }
8345 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008346#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008347 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008348 status = PSA_ERROR_NOT_SUPPORTED;
8349 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008350 }
8351
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008352 return PSA_SUCCESS;
8353exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008354 psa_pake_abort(operation);
8355 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008356}
8357
8358psa_status_t psa_pake_get_implicit_key(
8359 psa_pake_operation_t *operation,
8360 psa_key_derivation_operation_t *output)
8361{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008362 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008363 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008364 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008365 size_t shared_key_len = 0;
8366
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008367 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008368 status = PSA_ERROR_BAD_STATE;
8369 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008370 }
8371
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008372#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008373 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008374 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008375 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008376 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008377 status = PSA_ERROR_BAD_STATE;
8378 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008379 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008380 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008381#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008382 {
8383 status = PSA_ERROR_NOT_SUPPORTED;
8384 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008385 }
8386
Przemek Stekiel0c781802022-11-29 14:53:13 +01008387 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8388 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008389 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008390 &shared_key_len);
8391
8392 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008393 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008394 }
8395
8396 status = psa_key_derivation_input_bytes(output,
8397 PSA_KEY_DERIVATION_INPUT_SECRET,
8398 shared_key,
8399 shared_key_len);
8400
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008401 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008402exit:
8403 abort_status = psa_pake_abort(operation);
8404 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008405}
8406
8407psa_status_t psa_pake_abort(
8408 psa_pake_operation_t *operation)
8409{
Przemek Stekield69dca92023-01-31 19:59:20 +01008410 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008411
Przemek Stekield69dca92023-01-31 19:59:20 +01008412 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008413 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008414 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008415
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008416 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8417 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008418 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008419 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008420 }
8421 if (operation->data.inputs.user != NULL) {
8422 mbedtls_free(operation->data.inputs.user);
8423 }
8424 if (operation->data.inputs.peer != NULL) {
8425 mbedtls_free(operation->data.inputs.peer);
8426 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008427 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008428 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008429
Przemek Stekield69dca92023-01-31 19:59:20 +01008430 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008431}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008432#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008433
Gilles Peskinee59236f2018-01-27 23:32:46 +01008434#endif /* MBEDTLS_PSA_CRYPTO_C */