blob: 114994019ebca5ca8431eb2f9ecf4ba3364709d3 [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));
Darryl Green0c6575a2018-11-07 16:05:30 +00001844 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001845
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001847 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001849 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001850#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1851
Ryan Everett975d4112023-11-16 13:37:51 +00001852 slot->status = PSA_SLOT_OCCUPIED;
1853
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001855}
Gilles Peskine4747d192019-04-17 15:05:45 +02001856
1857/** Finalize the creation of a key once its key material has been set.
1858 *
1859 * This entails writing the key to persistent storage.
1860 *
1861 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001862 * See the documentation of psa_start_key_creation() for the intended use
1863 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001864 *
Ronald Cron5c522922020-11-14 16:35:34 +01001865 * If the finalization succeeds, the function unlocks the key slot (it was
1866 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1867 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001868 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001869 * \param[in,out] slot Pointer to the slot with key material.
1870 * \param[in] driver The secure element driver for the key,
1871 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001872 * \param[out] key On success, identifier of the key. Note that the
1873 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001874 *
1875 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001876 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001877 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1878 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1879 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1880 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1881 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1882 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001883 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001884 * \return If this function fails, the key slot is an invalid state.
1885 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001886 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001887static psa_status_t psa_finish_key_creation(
1888 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001889 psa_se_drv_table_entry_t *driver,
1890 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001891{
1892 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001893 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001894 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001895
1896#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001898#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001900 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001901 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001903
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001904 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1905 sizeof(data.slot_number),
1906 "Slot number size does not match psa_se_key_data_storage_t");
1907
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1909 status = psa_save_persistent_key(&slot->attr,
1910 (uint8_t *) &data,
1911 sizeof(data));
1912 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001913#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1914 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001915 /* Key material is saved in export representation in the slot, so
1916 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 status = psa_save_persistent_key(&slot->attr,
1918 slot->key.data,
1919 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001920 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001921 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001922#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1923
Gilles Peskinecbaff462019-07-12 23:46:04 +02001924#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001925 /* Finish the transaction for a key creation. This does not
1926 * happen when registering an existing key. Detect this case
1927 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001928 * creation of a persistent key in a secure element requires a transaction,
1929 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 if (driver != NULL &&
1931 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1932 status = psa_save_se_persistent_data(driver);
1933 if (status != PSA_SUCCESS) {
1934 psa_destroy_persistent_key(slot->attr.id);
1935 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001936 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001938 }
1939#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1940
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001942 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 status = psa_unlock_key_slot(slot);
1944 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001945 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001947 }
Ronald Cron50972942020-11-14 11:28:25 +01001948
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001950}
1951
1952/** Abort the creation of a key.
1953 *
1954 * You may call this function after calling psa_start_key_creation(),
1955 * or after psa_finish_key_creation() fails. In other circumstances, this
1956 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001957 * See the documentation of psa_start_key_creation() for the intended use
1958 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001959 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001960 * \param[in,out] slot Pointer to the slot with key material.
1961 * \param[in] driver The secure element driver for the key,
1962 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001963 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001964static void psa_fail_key_creation(psa_key_slot_t *slot,
1965 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001966{
Gilles Peskine011e4282019-06-26 18:34:38 +02001967 (void) driver;
1968
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001970 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001972
1973#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001974 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001975 * element, and the failure happened later (when saving metadata
1976 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001977 * element.
1978 * https://github.com/ARMmbed/mbed-crypto/issues/217
1979 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001980
Gilles Peskined7729582019-08-05 15:55:54 +02001981 /* Abort the ongoing transaction if any (there may not be one if
1982 * the creation process failed before starting one, or if the
1983 * key creation is a registration of a key in a secure element).
1984 * Earlier functions must already have done what it takes to undo any
1985 * partial creation. All that's left is to update the transaction data
1986 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001988#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1989
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001991}
1992
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001993/** Validate optional attributes during key creation.
1994 *
1995 * Some key attributes are optional during key creation. If they are
1996 * specified in the attributes structure, check that they are consistent
1997 * with the data in the slot.
1998 *
1999 * This function should be called near the end of key creation, after
2000 * the slot in memory is fully populated but before saving persistent data.
2001 */
2002static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002003 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002005{
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 if (attributes->core.type != 0) {
2007 if (attributes->core.type != slot->attr.type) {
2008 return PSA_ERROR_INVALID_ARGUMENT;
2009 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002010 }
2011
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02002013#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
2014 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2016 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002017 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002018 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002019 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002020
Ronald Cron90857082020-11-25 14:58:33 +01002021 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 slot->attr.type,
2023 slot->key.data,
2024 slot->key.bytes,
2025 &rsa);
2026 if (status != PSA_SUCCESS) {
2027 return status;
2028 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002029
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 mbedtls_mpi_init(&actual);
2031 mbedtls_mpi_init(&required);
2032 ret = mbedtls_rsa_export(rsa,
2033 NULL, NULL, NULL, NULL, &actual);
2034 mbedtls_rsa_free(rsa);
2035 mbedtls_free(rsa);
2036 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002037 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 }
2039 ret = mbedtls_mpi_read_binary(&required,
2040 attributes->domain_parameters,
2041 attributes->domain_parameters_size);
2042 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002043 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 }
2045 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002046 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 }
2048rsa_exit:
2049 mbedtls_mpi_free(&actual);
2050 mbedtls_mpi_free(&required);
2051 if (ret != 0) {
2052 return mbedtls_to_psa_error(ret);
2053 }
2054 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02002055#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
2056 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08002057 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002058 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002060 }
2061 }
2062
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 if (attributes->core.bits != 0) {
2064 if (attributes->core.bits != slot->attr.bits) {
2065 return PSA_ERROR_INVALID_ARGUMENT;
2066 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002067 }
2068
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002070}
2071
Gilles Peskine449bd832023-01-11 14:50:10 +01002072psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2073 const uint8_t *data,
2074 size_t data_length,
2075 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002076{
2077 psa_status_t status;
2078 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002079 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002080 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302081 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002082
Ronald Cron81709fc2020-11-14 12:10:32 +01002083 *key = MBEDTLS_SVC_KEY_ID_INIT;
2084
Gilles Peskine0f84d622019-09-12 19:03:13 +02002085 /* Reject zero-length symmetric keys (including raw data key objects).
2086 * This also rejects any key which might be encoded as an empty string,
2087 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 if (data_length == 0) {
2089 return PSA_ERROR_INVALID_ARGUMENT;
2090 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002091
Archana449608b2021-09-08 15:36:05 +05302092 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 if (data_length > SIZE_MAX / 8) {
2094 return PSA_ERROR_NOT_SUPPORTED;
2095 }
Archana6ed4bda2021-08-04 10:47:15 +05302096
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2098 &slot, &driver);
2099 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002100 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002102
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002103 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302104 * storage ( thus not in the case of importing a key in a secure element
2105 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2106 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 if (slot->key.data == NULL) {
2108 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302109 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 attributes, data, data_length, &storage_size);
2111 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302112 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 }
Archanad8a83dc2021-06-14 10:04:16 +05302114 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 status = psa_allocate_buffer_to_slot(slot, storage_size);
2116 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002117 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002119 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002120
2121 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 status = psa_driver_wrapper_import_key(attributes,
2123 data, data_length,
2124 slot->key.data,
2125 slot->key.bytes,
2126 &slot->key.bytes, &bits);
2127 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002128 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002130
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002132 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002134 status = PSA_ERROR_INVALID_ARGUMENT;
2135 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002136 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002137
Archana6ed4bda2021-08-04 10:47:15 +05302138 /* Enforce a size limit, and in particular ensure that the bit
2139 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302141 status = PSA_ERROR_NOT_SUPPORTED;
2142 goto exit;
2143 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 status = psa_validate_optional_attributes(slot, attributes);
2145 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002146 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002150exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 if (status != PSA_SUCCESS) {
2152 psa_fail_key_creation(slot, driver);
2153 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002156}
2157
Gilles Peskined7729582019-08-05 15:55:54 +02002158#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2159psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002161{
2162 psa_status_t status;
2163 psa_key_slot_t *slot = NULL;
2164 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002165 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002166
2167 /* Leaving attributes unspecified is not currently supported.
2168 * It could make sense to query the key type and size from the
2169 * secure element, but not all secure elements support this
2170 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2172 return PSA_ERROR_NOT_SUPPORTED;
2173 }
2174 if (psa_get_key_bits(attributes) == 0) {
2175 return PSA_ERROR_NOT_SUPPORTED;
2176 }
Gilles Peskined7729582019-08-05 15:55:54 +02002177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2179 &slot, &driver);
2180 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002181 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 }
Gilles Peskined7729582019-08-05 15:55:54 +02002183
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002185
2186exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 if (status != PSA_SUCCESS) {
2188 psa_fail_key_creation(slot, driver);
2189 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002190
Gilles Peskined7729582019-08-05 15:55:54 +02002191 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 psa_close_key(key);
2193 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002194}
2195#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2196
Gilles Peskine449bd832023-01-11 14:50:10 +01002197psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2198 const psa_key_attributes_t *specified_attributes,
2199 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002200{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002201 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002202 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002203 psa_key_slot_t *source_slot = NULL;
2204 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002205 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002206 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302207 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002208
Ronald Cron81709fc2020-11-14 12:10:32 +01002209 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2210
Archana8a180362021-07-05 02:18:48 +05302211 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2213 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002214 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 status = psa_validate_optional_attributes(source_slot,
2218 specified_attributes);
2219 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002220 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002222
Archana9d17bf42021-09-10 06:22:44 +05302223 /* The target key type and number of bits have been validated by
2224 * psa_validate_optional_attributes() to be either equal to zero or
2225 * equal to the ones of the source key. So it is safe to inherit
2226 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302227 * */
Archana9d17bf42021-09-10 06:22:44 +05302228 actual_attributes.core.bits = source_slot->attr.bits;
2229 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302230
2231
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 status = psa_restrict_key_policy(source_slot->attr.type,
2233 &actual_attributes.core.policy,
2234 &source_slot->attr.policy);
2235 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002236 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002238
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2240 &target_slot, &driver);
2241 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002242 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 }
2244 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2245 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302246 /*
Archana9d17bf42021-09-10 06:22:44 +05302247 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302248 * the source key would need to be exported as plaintext and re-imported
2249 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302250 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302251 * appropriate API invocations from the application, if needed.
2252 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002253 status = PSA_ERROR_NOT_SUPPORTED;
2254 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002255 }
Archana8a180362021-07-05 02:18:48 +05302256 /*
2257 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302258 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302259 * - For opaque keys this translates to an invocation of the drivers'
2260 * copy_key entry point through the dispatch layer.
2261 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2263 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2264 &storage_size);
2265 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302266 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 }
Archana374fe5b2021-09-08 15:50:28 +05302268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2270 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302271 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 }
Archana374fe5b2021-09-08 15:50:28 +05302273
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 status = psa_driver_wrapper_copy_key(&actual_attributes,
2275 source_slot->key.data,
2276 source_slot->key.bytes,
2277 target_slot->key.data,
2278 target_slot->key.bytes,
2279 &target_slot->key.bytes);
2280 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302281 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 }
2283 } else {
2284 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302285 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 source_slot->key.bytes);
2287 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302288 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 }
Archana8a180362021-07-05 02:18:48 +05302290 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002292exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 if (status != PSA_SUCCESS) {
2294 psa_fail_key_creation(target_slot, driver);
2295 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002300}
2301
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002302
2303
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002304/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002305/* Message digests */
2306/****************************************************************/
2307
Gilles Peskine449bd832023-01-11 14:50:10 +01002308psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002309{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002310 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 if (operation->id == 0) {
2312 return PSA_SUCCESS;
2313 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002314
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002316 operation->id = 0;
2317
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002319}
2320
Gilles Peskine449bd832023-01-11 14:50:10 +01002321psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2322 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002323{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002324 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002325
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002326 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002328 status = PSA_ERROR_BAD_STATE;
2329 goto exit;
2330 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002333 status = PSA_ERROR_INVALID_ARGUMENT;
2334 goto exit;
2335 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002336
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002337 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2338 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002342
2343exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 if (status != PSA_SUCCESS) {
2345 psa_hash_abort(operation);
2346 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002347
2348 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002349}
2350
Gilles Peskine449bd832023-01-11 14:50:10 +01002351psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2352 const uint8_t *input,
2353 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002354{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002355 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002358 status = PSA_ERROR_BAD_STATE;
2359 goto exit;
2360 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002361
Steven Cooremanc8288352021-03-04 14:02:19 +01002362 /* Don't require hash implementations to behave correctly on a
2363 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 if (input_length == 0) {
2365 return PSA_SUCCESS;
2366 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002369
2370exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 if (status != PSA_SUCCESS) {
2372 psa_hash_abort(operation);
2373 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002374
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002376}
2377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2379 uint8_t *hash,
2380 size_t hash_size,
2381 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002382{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002383 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 if (operation->id == 0) {
2385 return PSA_ERROR_BAD_STATE;
2386 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002387
Steven Cooreman1e582352021-02-18 17:24:37 +01002388 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 operation, hash, hash_size, hash_length);
2390 psa_hash_abort(operation);
2391 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002392}
2393
Gilles Peskine449bd832023-01-11 14:50:10 +01002394psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2395 const uint8_t *hash,
2396 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002397{
Ronald Cron69a63422021-10-18 09:47:58 +02002398 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002399 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002400 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 operation,
2402 actual_hash, sizeof(actual_hash),
2403 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002404
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002406 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002408
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002410 status = PSA_ERROR_INVALID_SIGNATURE;
2411 goto exit;
2412 }
2413
Dave Rodgman78701152023-08-29 14:20:18 +01002414 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002415 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002417
2418exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2420 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002421 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002425}
2426
Gilles Peskine449bd832023-01-11 14:50:10 +01002427psa_status_t psa_hash_compute(psa_algorithm_t alg,
2428 const uint8_t *input, size_t input_length,
2429 uint8_t *hash, size_t hash_size,
2430 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002431{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002432 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 if (!PSA_ALG_IS_HASH(alg)) {
2434 return PSA_ERROR_INVALID_ARGUMENT;
2435 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2438 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002439}
2440
Gilles Peskine449bd832023-01-11 14:50:10 +01002441psa_status_t psa_hash_compare(psa_algorithm_t alg,
2442 const uint8_t *input, size_t input_length,
2443 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002444{
Ronald Cron69a63422021-10-18 09:47:58 +02002445 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002446 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002447
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 if (!PSA_ALG_IS_HASH(alg)) {
2449 return PSA_ERROR_INVALID_ARGUMENT;
2450 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002451
Steven Cooreman1e582352021-02-18 17:24:37 +01002452 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 alg, input, input_length,
2454 actual_hash, sizeof(actual_hash),
2455 &actual_hash_length);
2456 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002457 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 }
2459 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002460 status = PSA_ERROR_INVALID_SIGNATURE;
2461 goto exit;
2462 }
Dave Rodgman78701152023-08-29 14:20:18 +01002463 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002464 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002466
2467exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2469 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002470}
2471
Gilles Peskine449bd832023-01-11 14:50:10 +01002472psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2473 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002474{
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 if (source_operation->id == 0 ||
2476 target_operation->id != 0) {
2477 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002478 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002479
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2481 target_operation);
2482 if (status != PSA_SUCCESS) {
2483 psa_hash_abort(target_operation);
2484 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002487}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002488
2489
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002490/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002491/* MAC */
2492/****************************************************************/
2493
Gilles Peskine449bd832023-01-11 14:50:10 +01002494psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002495{
Steven Cooremane6804192021-03-19 18:28:56 +01002496 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 if (operation->id == 0) {
2498 return PSA_SUCCESS;
2499 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002500
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002502 operation->mac_size = 0;
2503 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002504 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002507}
2508
Ronald Cron2dff3b22021-06-17 16:33:22 +02002509static psa_status_t psa_mac_finalize_alg_and_key_validation(
2510 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002511 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002513{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002514 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 psa_key_type_t key_type = psa_get_key_type(attributes);
2516 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 if (!PSA_ALG_IS_MAC(alg)) {
2519 return PSA_ERROR_INVALID_ARGUMENT;
2520 }
Steven Cooremane6804192021-03-19 18:28:56 +01002521
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002522 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 status = psa_mac_key_can_do(alg, key_type);
2524 if (status != PSA_SUCCESS) {
2525 return status;
2526 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002527
Steven Cooremand1a68f12021-05-10 11:13:41 +02002528 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002530
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002532 /* A very short MAC is too short for security since it can be
2533 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2534 * so we make this our minimum, even though 32 bits is still
2535 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002537 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002538
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2540 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002541 /* It's impossible to "truncate" to a larger length than the full length
2542 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002544 }
2545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002547 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2548 * that is disabled in the compile-time configuration. The result can
2549 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2550 * configuration into account. In this case, force a return of
2551 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2552 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2553 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2554 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2555 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002557 }
2558
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002560}
2561
Gilles Peskine449bd832023-01-11 14:50:10 +01002562static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2563 mbedtls_svc_key_id_t key,
2564 psa_algorithm_t alg,
2565 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002566{
2567 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2568 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002569 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002570 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002571
2572 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002574 status = PSA_ERROR_BAD_STATE;
2575 goto exit;
2576 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002577
2578 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 key,
2580 &slot,
2581 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2582 alg);
2583 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002584 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002586
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002587 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002588 .core = slot->attr
2589 };
2590
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2592 &operation->mac_size);
2593 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002594 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002596
2597 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002598 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 if (is_sign) {
2600 status = psa_driver_wrapper_mac_sign_setup(operation,
2601 &attributes,
2602 slot->key.data,
2603 slot->key.bytes,
2604 alg);
2605 } else {
2606 status = psa_driver_wrapper_mac_verify_setup(operation,
2607 &attributes,
2608 slot->key.data,
2609 slot->key.bytes,
2610 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002611 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002612
Gilles Peskinefbfac682018-07-08 20:51:54 +02002613exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 if (status != PSA_SUCCESS) {
2615 psa_mac_abort(operation);
2616 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002619
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002621}
2622
Gilles Peskine449bd832023-01-11 14:50:10 +01002623psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2624 mbedtls_svc_key_id_t key,
2625 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002626{
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002628}
2629
Gilles Peskine449bd832023-01-11 14:50:10 +01002630psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2631 mbedtls_svc_key_id_t key,
2632 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002633{
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002635}
2636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2638 const uint8_t *input,
2639 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002640{
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 if (operation->id == 0) {
2642 return PSA_ERROR_BAD_STATE;
2643 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002644
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002645 /* Don't require hash implementations to behave correctly on a
2646 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 if (input_length == 0) {
2648 return PSA_SUCCESS;
2649 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002650
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2652 input, input_length);
2653 if (status != PSA_SUCCESS) {
2654 psa_mac_abort(operation);
2655 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002656
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002658}
2659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2661 uint8_t *mac,
2662 size_t mac_size,
2663 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002664{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002665 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2666 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002667
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002669 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002670 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002671 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002672
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002674 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002675 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002676 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002677
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002678 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2679 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002681 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002682 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002683 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002684
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002686 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002687 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002688 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002689
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 status = psa_driver_wrapper_mac_sign_finish(operation,
2691 mac, operation->mac_size,
2692 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002693
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002694exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002695 /* In case of success, set the potential excess room in the output buffer
2696 * to an invalid value, to avoid potentially leaking a longer MAC.
2697 * In case of error, set the output length and content to a safe default,
2698 * such that in case the caller misses an error check, the output would be
2699 * an unachievable MAC.
2700 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002702 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002703 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002704 }
mohammad16036df908f2018-04-02 08:34:15 -07002705
Paul Elliottdc42ca82023-02-24 18:11:59 +00002706 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002707
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002709
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002711}
2712
Gilles Peskine449bd832023-01-11 14:50:10 +01002713psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2714 const uint8_t *mac,
2715 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002716{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002717 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2718 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002721 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002722 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002723 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002724
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002726 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002727 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002728 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002729
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002731 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002732 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002733 }
2734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 status = psa_driver_wrapper_mac_verify_finish(operation,
2736 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002737
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002738exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002740
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002742}
2743
Gilles Peskine449bd832023-01-11 14:50:10 +01002744static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2745 psa_algorithm_t alg,
2746 const uint8_t *input,
2747 size_t input_length,
2748 uint8_t *mac,
2749 size_t mac_size,
2750 size_t *mac_length,
2751 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002752{
2753 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002754 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2755 psa_key_slot_t *slot;
2756 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002757 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002758
Ronald Cron51131b52021-06-17 17:17:20 +02002759 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002760 key,
2761 &slot,
2762 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2763 alg);
2764 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002765 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002767
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002768 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002769 .core = slot->attr
2770 };
2771
Gilles Peskine449bd832023-01-11 14:50:10 +01002772 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2773 &operation_mac_size);
2774 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002775 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002777
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002779 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002780 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002781 }
2782
2783 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 &attributes,
2785 slot->key.data, slot->key.bytes,
2786 alg,
2787 input, input_length,
2788 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002789
2790exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002791 /* In case of success, set the potential excess room in the output buffer
2792 * to an invalid value, to avoid potentially leaking a longer MAC.
2793 * In case of error, set the output length and content to a safe default,
2794 * such that in case the caller misses an error check, the output would be
2795 * an unachievable MAC.
2796 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002797 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002798 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002799 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002800 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002801
2802 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002803
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002805
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002807}
2808
Gilles Peskine449bd832023-01-11 14:50:10 +01002809psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002810 psa_algorithm_t alg,
2811 const uint8_t *input,
2812 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 uint8_t *mac,
2814 size_t mac_size,
2815 size_t *mac_length)
2816{
2817 return psa_mac_compute_internal(key, alg,
2818 input, input_length,
2819 mac, mac_size, mac_length, 1);
2820}
2821
2822psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2823 psa_algorithm_t alg,
2824 const uint8_t *input,
2825 size_t input_length,
2826 const uint8_t *mac,
2827 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002828{
2829 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002830 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2831 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002832
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 status = psa_mac_compute_internal(key, alg,
2834 input, input_length,
2835 actual_mac, sizeof(actual_mac),
2836 &actual_mac_length, 0);
2837 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002838 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002840
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002842 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002843 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002844 }
Dave Rodgman78701152023-08-29 14:20:18 +01002845 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002846 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002847 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002848 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002849
2850exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002852
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002854}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002855
Gilles Peskinee59236f2018-01-27 23:32:46 +01002856/****************************************************************/
2857/* Asymmetric cryptography */
2858/****************************************************************/
2859
Gilles Peskine449bd832023-01-11 14:50:10 +01002860static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2861 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002862{
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 if (input_is_message) {
2864 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2865 return PSA_ERROR_INVALID_ARGUMENT;
2866 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002867
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2869 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2870 return PSA_ERROR_INVALID_ARGUMENT;
2871 }
2872 }
2873 } else {
2874 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2875 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002876 }
2877 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002878
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002880}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002881
Gilles Peskine449bd832023-01-11 14:50:10 +01002882static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2883 int input_is_message,
2884 psa_algorithm_t alg,
2885 const uint8_t *input,
2886 size_t input_length,
2887 uint8_t *signature,
2888 size_t signature_size,
2889 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002890{
2891 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2892 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2893 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002894 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002895
2896 *signature_length = 0;
2897
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 status = psa_sign_verify_check_alg(input_is_message, alg);
2899 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002900 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002902
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002903 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002904 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002905 * buffer can in principle be empty since it doesn't actually have
2906 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 if (signature_size == 0) {
2908 return PSA_ERROR_BUFFER_TOO_SMALL;
2909 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002910
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002911 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 key, &slot,
2913 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2914 PSA_KEY_USAGE_SIGN_HASH,
2915 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002916
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002918 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002920
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002922 status = PSA_ERROR_INVALID_ARGUMENT;
2923 goto exit;
2924 }
2925
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002926 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002928 };
2929
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002931 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002932 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002933 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 signature, signature_size, signature_length);
2935 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002936
2937 status = psa_driver_wrapper_sign_hash(
2938 &attributes, slot->key.data, slot->key.bytes,
2939 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002941 }
2942
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002943
2944exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002945 psa_wipe_tag_output_buffer(signature, status, signature_size,
2946 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002947
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002949
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002951}
2952
Gilles Peskine449bd832023-01-11 14:50:10 +01002953static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2954 int input_is_message,
2955 psa_algorithm_t alg,
2956 const uint8_t *input,
2957 size_t input_length,
2958 const uint8_t *signature,
2959 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002960{
2961 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2962 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2963 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002964
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 status = psa_sign_verify_check_alg(input_is_message, alg);
2966 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002967 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002969
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002970 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 key, &slot,
2972 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2973 PSA_KEY_USAGE_VERIFY_HASH,
2974 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002975
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 if (status != PSA_SUCCESS) {
2977 return status;
2978 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002979
2980 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002982 };
2983
Gilles Peskine449bd832023-01-11 14:50:10 +01002984 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002985 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002986 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002987 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 signature, signature_length);
2989 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002990 status = psa_driver_wrapper_verify_hash(
2991 &attributes, slot->key.data, slot->key.bytes,
2992 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002994 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002995
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002997
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002999
3000}
3001
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003002psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003003 const psa_key_attributes_t *attributes,
3004 const uint8_t *key_buffer,
3005 size_t key_buffer_size,
3006 psa_algorithm_t alg,
3007 const uint8_t *input,
3008 size_t input_length,
3009 uint8_t *signature,
3010 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003012{
3013 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003014
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003016 size_t hash_length;
3017 uint8_t hash[PSA_HASH_MAX_SIZE];
3018
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003019 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 PSA_ALG_SIGN_GET_HASH(alg),
3021 input, input_length,
3022 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003023
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003025 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003027
3028 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 attributes, key_buffer, key_buffer_size,
3030 alg, hash, hash_length,
3031 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003032 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003033
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003035}
3036
Gilles Peskine449bd832023-01-11 14:50:10 +01003037psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3038 psa_algorithm_t alg,
3039 const uint8_t *input,
3040 size_t input_length,
3041 uint8_t *signature,
3042 size_t signature_size,
3043 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003044{
3045 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003046 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003048}
3049
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003050psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003051 const psa_key_attributes_t *attributes,
3052 const uint8_t *key_buffer,
3053 size_t key_buffer_size,
3054 psa_algorithm_t alg,
3055 const uint8_t *input,
3056 size_t input_length,
3057 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003059{
3060 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003061
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003063 size_t hash_length;
3064 uint8_t hash[PSA_HASH_MAX_SIZE];
3065
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003066 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 PSA_ALG_SIGN_GET_HASH(alg),
3068 input, input_length,
3069 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003070
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003072 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003074
3075 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 attributes, key_buffer, key_buffer_size,
3077 alg, hash, hash_length,
3078 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003079 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003080
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003082}
3083
Gilles Peskine449bd832023-01-11 14:50:10 +01003084psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3085 psa_algorithm_t alg,
3086 const uint8_t *input,
3087 size_t input_length,
3088 const uint8_t *signature,
3089 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003090{
3091 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003092 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003094}
3095
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003096psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003097 const psa_key_attributes_t *attributes,
3098 const uint8_t *key_buffer, size_t key_buffer_size,
3099 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003101{
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3103 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3104 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003105#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3107 return mbedtls_psa_rsa_sign_hash(
3108 attributes,
3109 key_buffer, key_buffer_size,
3110 alg, hash, hash_length,
3111 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003112#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3113 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 } else {
3115 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003116 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3118 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003119#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3121 return mbedtls_psa_ecdsa_sign_hash(
3122 attributes,
3123 key_buffer, key_buffer_size,
3124 alg, hash, hash_length,
3125 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003126#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3127 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 } else {
3129 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003130 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003131 }
Ronald Cron4501c982021-03-19 20:09:32 +01003132
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 (void) key_buffer;
3134 (void) key_buffer_size;
3135 (void) hash;
3136 (void) hash_length;
3137 (void) signature;
3138 (void) signature_size;
3139 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003140
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003142}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003143
Gilles Peskine449bd832023-01-11 14:50:10 +01003144psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3145 psa_algorithm_t alg,
3146 const uint8_t *hash,
3147 size_t hash_length,
3148 uint8_t *signature,
3149 size_t signature_size,
3150 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003151{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003152 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003153 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003155}
3156
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003157psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003158 const psa_key_attributes_t *attributes,
3159 const uint8_t *key_buffer, size_t key_buffer_size,
3160 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003162{
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3164 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3165 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003166#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3168 return mbedtls_psa_rsa_verify_hash(
3169 attributes,
3170 key_buffer, key_buffer_size,
3171 alg, hash, hash_length,
3172 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003173#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3174 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 } else {
3176 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003177 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3179 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003180#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3182 return mbedtls_psa_ecdsa_verify_hash(
3183 attributes,
3184 key_buffer, key_buffer_size,
3185 alg, hash, hash_length,
3186 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003187#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3188 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 } else {
3190 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003191 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003192 }
Ronald Cron4501c982021-03-19 20:09:32 +01003193
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 (void) key_buffer;
3195 (void) key_buffer_size;
3196 (void) hash;
3197 (void) hash_length;
3198 (void) signature;
3199 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003200
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003202}
3203
Gilles Peskine449bd832023-01-11 14:50:10 +01003204psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3205 psa_algorithm_t alg,
3206 const uint8_t *hash,
3207 size_t hash_length,
3208 const uint8_t *signature,
3209 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003210{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003211 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003212 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003213 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003214}
3215
Gilles Peskine449bd832023-01-11 14:50:10 +01003216psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3217 psa_algorithm_t alg,
3218 const uint8_t *input,
3219 size_t input_length,
3220 const uint8_t *salt,
3221 size_t salt_length,
3222 uint8_t *output,
3223 size_t output_size,
3224 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003225{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003226 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003227 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003228 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003229 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003230
Darryl Green5cc689a2018-07-24 15:34:10 +01003231 (void) input;
3232 (void) input_length;
3233 (void) salt;
3234 (void) output;
3235 (void) output_size;
3236
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003237 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003238
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3240 return PSA_ERROR_INVALID_ARGUMENT;
3241 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003242
Ronald Cron5c522922020-11-14 16:35:34 +01003243 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003244 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3245 if (status != PSA_SUCCESS) {
3246 return status;
3247 }
3248 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3249 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003250 status = PSA_ERROR_INVALID_ARGUMENT;
3251 goto exit;
3252 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003253
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003254 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003255 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003256 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003257
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003258 status = psa_driver_wrapper_asymmetric_encrypt(
3259 &attributes, slot->key.data, slot->key.bytes,
3260 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003262exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003264
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003266}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003267
Gilles Peskine449bd832023-01-11 14:50:10 +01003268psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3269 psa_algorithm_t alg,
3270 const uint8_t *input,
3271 size_t input_length,
3272 const uint8_t *salt,
3273 size_t salt_length,
3274 uint8_t *output,
3275 size_t output_size,
3276 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003277{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003278 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003279 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003280 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003281 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003282
Darryl Green5cc689a2018-07-24 15:34:10 +01003283 (void) input;
3284 (void) input_length;
3285 (void) salt;
3286 (void) output;
3287 (void) output_size;
3288
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003289 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003290
Gilles Peskine449bd832023-01-11 14:50:10 +01003291 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3292 return PSA_ERROR_INVALID_ARGUMENT;
3293 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003294
Ronald Cron5c522922020-11-14 16:35:34 +01003295 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3297 if (status != PSA_SUCCESS) {
3298 return status;
3299 }
3300 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003301 status = PSA_ERROR_INVALID_ARGUMENT;
3302 goto exit;
3303 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003304
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003305 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003307 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003308
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003309 status = psa_driver_wrapper_asymmetric_decrypt(
3310 &attributes, slot->key.data, slot->key.bytes,
3311 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003312 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003313
3314exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003315 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003316
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003318}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003319
Paul Elliott9fe12f62022-11-30 19:16:02 +00003320/****************************************************************/
3321/* Asymmetric interruptible cryptography */
3322/****************************************************************/
3323
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003324static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3325
Paul Elliott9fe12f62022-11-30 19:16:02 +00003326void psa_interruptible_set_max_ops(uint32_t max_ops)
3327{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003328 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003329}
3330
3331uint32_t psa_interruptible_get_max_ops(void)
3332{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003333 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003334}
3335
Paul Elliott9fe12f62022-11-30 19:16:02 +00003336uint32_t psa_sign_hash_get_num_ops(
3337 const psa_sign_hash_interruptible_operation_t *operation)
3338{
Paul Elliott296ede92022-12-15 17:00:30 +00003339 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003340}
3341
3342uint32_t psa_verify_hash_get_num_ops(
3343 const psa_verify_hash_interruptible_operation_t *operation)
3344{
Paul Elliott296ede92022-12-15 17:00:30 +00003345 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003346}
3347
Paul Elliott59ad9452022-12-18 15:09:02 +00003348static psa_status_t psa_sign_hash_abort_internal(
3349 psa_sign_hash_interruptible_operation_t *operation)
3350{
3351 if (operation->id == 0) {
3352 /* The object has (apparently) been initialized but it is not (yet)
3353 * in use. It's ok to call abort on such an object, and there's
3354 * nothing to do. */
3355 return PSA_SUCCESS;
3356 }
3357
3358 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3359
3360 status = psa_driver_wrapper_sign_hash_abort(operation);
3361
3362 operation->id = 0;
3363
Paul Elliotte6145dc2023-02-07 12:51:21 +00003364 /* Do not clear either the error_occurred or num_ops elements here as they
3365 * only want to be cleared by the application calling abort, not by abort
3366 * being called at completion of an operation. */
3367
Paul Elliott59ad9452022-12-18 15:09:02 +00003368 return status;
3369}
3370
Paul Elliott9fe12f62022-11-30 19:16:02 +00003371psa_status_t psa_sign_hash_start(
3372 psa_sign_hash_interruptible_operation_t *operation,
3373 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3374 const uint8_t *hash, size_t hash_length)
3375{
3376 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3377 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3378 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003379 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003380
Paul Elliottc9774412023-02-06 15:14:07 +00003381 /* Check that start has not been previously called, or operation has not
3382 * previously errored. */
3383 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003384 return PSA_ERROR_BAD_STATE;
3385 }
3386
Paul Elliott9fe12f62022-11-30 19:16:02 +00003387 status = psa_sign_verify_check_alg(0, alg);
3388 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003389 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003390 return status;
3391 }
3392
3393 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3394 PSA_KEY_USAGE_SIGN_HASH,
3395 alg);
3396
3397 if (status != PSA_SUCCESS) {
3398 goto exit;
3399 }
3400
3401 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3402 status = PSA_ERROR_INVALID_ARGUMENT;
3403 goto exit;
3404 }
3405
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003406 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003407 .core = slot->attr
3408 };
3409
Paul Elliott296ede92022-12-15 17:00:30 +00003410 /* Ensure ops count gets reset, in case of operation re-use. */
3411 operation->num_ops = 0;
3412
Paul Elliott9fe12f62022-11-30 19:16:02 +00003413 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3414 slot->key.data,
3415 slot->key.bytes, alg,
3416 hash, hash_length);
3417exit:
3418
3419 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003420 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003421 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003422 }
3423
3424 unlock_status = psa_unlock_key_slot(slot);
3425
Paul Elliottc9774412023-02-06 15:14:07 +00003426 if (unlock_status != PSA_SUCCESS) {
3427 operation->error_occurred = 1;
3428 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003429
Paul Elliottc9774412023-02-06 15:14:07 +00003430 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003431}
3432
3433
3434psa_status_t psa_sign_hash_complete(
3435 psa_sign_hash_interruptible_operation_t *operation,
3436 uint8_t *signature, size_t signature_size,
3437 size_t *signature_length)
3438{
3439 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3440
3441 *signature_length = 0;
3442
Paul Elliottc9774412023-02-06 15:14:07 +00003443 /* Check that start has been called first, and that operation has not
3444 * previously errored. */
3445 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003446 status = PSA_ERROR_BAD_STATE;
3447 goto exit;
3448 }
3449
Paul Elliott096abc42023-02-03 18:33:23 +00003450 /* Immediately reject a zero-length signature buffer. This guarantees that
3451 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003452 if (signature_size == 0) {
3453 status = PSA_ERROR_BUFFER_TOO_SMALL;
3454 goto exit;
3455 }
3456
3457 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3458 signature_size,
3459 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003460
Paul Elliott296ede92022-12-15 17:00:30 +00003461 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003462 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003463
Paul Elliottebe225c2023-02-10 14:32:53 +00003464exit:
3465
Paul Elliott59200a22023-02-21 15:07:40 +00003466 psa_wipe_tag_output_buffer(signature, status, signature_size,
3467 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003468
Paul Elliott53bb3122023-02-10 14:22:22 +00003469 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003470 if (status != PSA_SUCCESS) {
3471 operation->error_occurred = 1;
3472 }
3473
Paul Elliott59ad9452022-12-18 15:09:02 +00003474 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003475 }
3476
3477 return status;
3478}
3479
3480psa_status_t psa_sign_hash_abort(
3481 psa_sign_hash_interruptible_operation_t *operation)
3482{
Paul Elliott59ad9452022-12-18 15:09:02 +00003483 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3484
3485 status = psa_sign_hash_abort_internal(operation);
3486
3487 /* We clear the number of ops done here, so that it is not cleared when
3488 * the operation fails or succeeds, only on manual abort. */
3489 operation->num_ops = 0;
3490
Paul Elliottc9774412023-02-06 15:14:07 +00003491 /* Likewise, failure state. */
3492 operation->error_occurred = 0;
3493
Paul Elliott59ad9452022-12-18 15:09:02 +00003494 return status;
3495}
3496
3497static psa_status_t psa_verify_hash_abort_internal(
3498 psa_verify_hash_interruptible_operation_t *operation)
3499{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003500 if (operation->id == 0) {
3501 /* The object has (apparently) been initialized but it is not (yet)
3502 * in use. It's ok to call abort on such an object, and there's
3503 * nothing to do. */
3504 return PSA_SUCCESS;
3505 }
3506
Paul Elliott59ad9452022-12-18 15:09:02 +00003507 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3508
3509 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003510
3511 operation->id = 0;
3512
Paul Elliotte6145dc2023-02-07 12:51:21 +00003513 /* Do not clear either the error_occurred or num_ops elements here as they
3514 * only want to be cleared by the application calling abort, not by abort
3515 * being called at completion of an operation. */
3516
Paul Elliott59ad9452022-12-18 15:09:02 +00003517 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003518}
3519
3520psa_status_t psa_verify_hash_start(
3521 psa_verify_hash_interruptible_operation_t *operation,
3522 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3523 const uint8_t *hash, size_t hash_length,
3524 const uint8_t *signature, size_t signature_length)
3525{
3526 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3527 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3528 psa_key_slot_t *slot;
3529
Paul Elliottc9774412023-02-06 15:14:07 +00003530 /* Check that start has not been previously called, or operation has not
3531 * previously errored. */
3532 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003533 return PSA_ERROR_BAD_STATE;
3534 }
3535
3536 status = psa_sign_verify_check_alg(0, alg);
3537 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003538 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003539 return status;
3540 }
3541
3542 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3543 PSA_KEY_USAGE_VERIFY_HASH,
3544 alg);
3545
3546 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003547 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003548 return status;
3549 }
3550
3551 psa_key_attributes_t attributes = {
3552 .core = slot->attr
3553 };
3554
Paul Elliott296ede92022-12-15 17:00:30 +00003555 /* Ensure ops count gets reset, in case of operation re-use. */
3556 operation->num_ops = 0;
3557
Paul Elliott9fe12f62022-11-30 19:16:02 +00003558 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3559 slot->key.data,
3560 slot->key.bytes,
3561 alg, hash, hash_length,
3562 signature, signature_length);
3563
3564 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003565 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003566 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003567 }
3568
3569 unlock_status = psa_unlock_key_slot(slot);
3570
Paul Elliottc9774412023-02-06 15:14:07 +00003571 if (unlock_status != PSA_SUCCESS) {
3572 operation->error_occurred = 1;
3573 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003574
Paul Elliottc9774412023-02-06 15:14:07 +00003575 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003576}
3577
3578psa_status_t psa_verify_hash_complete(
3579 psa_verify_hash_interruptible_operation_t *operation)
3580{
3581 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3582
Paul Elliottc9774412023-02-06 15:14:07 +00003583 /* Check that start has been called first, and that operation has not
3584 * previously errored. */
3585 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003586 status = PSA_ERROR_BAD_STATE;
3587 goto exit;
3588 }
3589
3590 status = psa_driver_wrapper_verify_hash_complete(operation);
3591
Paul Elliott296ede92022-12-15 17:00:30 +00003592 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003593 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003594 operation);
3595
Paul Elliottebe225c2023-02-10 14:32:53 +00003596exit:
3597
Paul Elliott9fe12f62022-11-30 19:16:02 +00003598 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003599 if (status != PSA_SUCCESS) {
3600 operation->error_occurred = 1;
3601 }
3602
Paul Elliott59ad9452022-12-18 15:09:02 +00003603 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003604 }
3605
3606 return status;
3607}
3608
3609psa_status_t psa_verify_hash_abort(
3610 psa_verify_hash_interruptible_operation_t *operation)
3611{
Paul Elliott59ad9452022-12-18 15:09:02 +00003612 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003613
Paul Elliott59ad9452022-12-18 15:09:02 +00003614 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003615
Paul Elliott59ad9452022-12-18 15:09:02 +00003616 /* We clear the number of ops done here, so that it is not cleared when
3617 * the operation fails or succeeds, only on manual abort. */
3618 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003619
Paul Elliottc9774412023-02-06 15:14:07 +00003620 /* Likewise, failure state. */
3621 operation->error_occurred = 0;
3622
Paul Elliott59ad9452022-12-18 15:09:02 +00003623 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003624}
3625
Paul Elliott588f8ed2022-12-02 18:10:26 +00003626/****************************************************************/
3627/* Asymmetric interruptible cryptography internal */
3628/* implementations */
3629/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003630
Paul Elliott588f8ed2022-12-02 18:10:26 +00003631void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3632{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003633
Paul Elliott588f8ed2022-12-02 18:10:26 +00003634#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3635 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3636 defined(MBEDTLS_ECP_RESTARTABLE)
3637
3638 /* Internal implementation uses zero to indicate infinite number max ops,
3639 * therefore avoid this value, and set to minimum possible. */
3640 if (max_ops == 0) {
3641 max_ops = 1;
3642 }
3643
Paul Elliott588f8ed2022-12-02 18:10:26 +00003644 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003645#else
3646 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003647#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3648 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3649 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3650}
3651
Paul Elliott588f8ed2022-12-02 18:10:26 +00003652uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003653 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003654{
3655#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3656 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3657 defined(MBEDTLS_ECP_RESTARTABLE)
3658
Paul Elliott93d9ca82023-02-15 18:14:21 +00003659 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003660#else
3661 (void) operation;
3662 return 0;
3663#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3664 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3665 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3666}
3667
3668uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003669 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003670{
3671 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3672 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3673 defined(MBEDTLS_ECP_RESTARTABLE)
3674
Paul Elliott93d9ca82023-02-15 18:14:21 +00003675 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003676#else
3677 (void) operation;
3678 return 0;
3679#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3680 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3681 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3682}
3683
3684psa_status_t mbedtls_psa_sign_hash_start(
3685 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3686 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3687 size_t key_buffer_size, psa_algorithm_t alg,
3688 const uint8_t *hash, size_t hash_length)
3689{
3690 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003691 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003692
Paul Elliott068fe072023-01-16 13:59:15 +00003693 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3694 return PSA_ERROR_NOT_SUPPORTED;
3695 }
3696
3697 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003698 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003699 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003700
3701#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003702 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3703 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003704
Paul Elliotta1c94092023-02-15 16:38:04 +00003705 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3706
Paul Elliott93d9ca82023-02-15 18:14:21 +00003707 /* Ensure num_ops is zero'ed in case of context re-use. */
3708 operation->num_ops = 0;
3709
Paul Elliott068fe072023-01-16 13:59:15 +00003710 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3711 attributes->core.bits,
3712 key_buffer,
3713 key_buffer_size,
3714 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003715
Paul Elliott068fe072023-01-16 13:59:15 +00003716 if (status != PSA_SUCCESS) {
3717 return status;
3718 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003719
Paul Elliott1bc59df2023-02-05 13:41:57 +00003720 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003721 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003722
Paul Elliott068fe072023-01-16 13:59:15 +00003723 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003724 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003725 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003726
Paul Elliott0290a762023-02-09 14:30:24 +00003727 /* We only need to store the same length of hash as the private key size
3728 * here, it would be truncated by the internal implementation anyway. */
3729 required_hash_length = (hash_length < operation->coordinate_bytes ?
3730 hash_length : operation->coordinate_bytes);
3731
Paul Elliottba70ad42023-02-15 18:23:53 +00003732 if (required_hash_length > sizeof(operation->hash)) {
3733 /* Shouldn't happen, but better safe than sorry. */
3734 return PSA_ERROR_CORRUPTION_DETECTED;
3735 }
3736
Paul Elliott0290a762023-02-09 14:30:24 +00003737 memcpy(operation->hash, hash, required_hash_length);
3738 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003739
3740 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003741
3742#else
Paul Elliott068fe072023-01-16 13:59:15 +00003743 (void) operation;
3744 (void) key_buffer;
3745 (void) key_buffer_size;
3746 (void) alg;
3747 (void) hash;
3748 (void) hash_length;
3749 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003750 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003751
Paul Elliott068fe072023-01-16 13:59:15 +00003752 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003753#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3754 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3755 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003756}
3757
3758psa_status_t mbedtls_psa_sign_hash_complete(
3759 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3760 uint8_t *signature, size_t signature_size,
3761 size_t *signature_length)
3762{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003763#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3764 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3765 defined(MBEDTLS_ECP_RESTARTABLE)
3766
Paul Elliotta1c94092023-02-15 16:38:04 +00003767 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003768 mbedtls_mpi r;
3769 mbedtls_mpi s;
3770
Paul Elliott46845252023-02-03 14:59:11 +00003771 mbedtls_mpi_init(&r);
3772 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003773
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003774 /* Ensure max_ops is set to the current value (or default). */
3775 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3776
Paul Elliotta1c94092023-02-15 16:38:04 +00003777 if (signature_size < 2 * operation->coordinate_bytes) {
3778 status = PSA_ERROR_BUFFER_TOO_SMALL;
3779 goto exit;
3780 }
3781
Paul Elliott588f8ed2022-12-02 18:10:26 +00003782 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003783
Paul Elliott588f8ed2022-12-02 18:10:26 +00003784#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3785 status = mbedtls_to_psa_error(
3786 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003787 &r,
3788 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003789 &operation->ctx->d,
3790 operation->hash,
3791 operation->hash_length,
3792 operation->md_alg,
3793 mbedtls_psa_get_random,
3794 MBEDTLS_PSA_RANDOM_STATE,
3795 &operation->restart_ctx));
3796#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003797 status = PSA_ERROR_NOT_SUPPORTED;
3798 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003799#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3800 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003801 status = mbedtls_to_psa_error(
3802 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003803 &r,
3804 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003805 &operation->ctx->d,
3806 operation->hash,
3807 operation->hash_length,
3808 mbedtls_psa_get_random,
3809 MBEDTLS_PSA_RANDOM_STATE,
3810 mbedtls_psa_get_random,
3811 MBEDTLS_PSA_RANDOM_STATE,
3812 &operation->restart_ctx));
3813 }
3814
Paul Elliottf8e5b562023-02-19 18:43:45 +00003815 /* Hide the fact that the restart context only holds a delta of number of
3816 * ops done during the last operation, not an absolute value. */
3817 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3818
Paul Elliott724bd252023-02-08 12:35:08 +00003819 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003820 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003821 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003822 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003823 operation->coordinate_bytes)
3824 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003825
3826 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003827 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003828 }
3829
3830 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003831 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003832 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003833 operation->coordinate_bytes,
3834 operation->coordinate_bytes)
3835 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003836
3837 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003838 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003839 }
3840
Paul Elliott1bc59df2023-02-05 13:41:57 +00003841 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003842
Paul Elliott724bd252023-02-08 12:35:08 +00003843 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003844 }
Paul Elliott724bd252023-02-08 12:35:08 +00003845
3846exit:
3847
3848 mbedtls_mpi_free(&r);
3849 mbedtls_mpi_free(&s);
3850 return status;
3851
Paul Elliott588f8ed2022-12-02 18:10:26 +00003852 #else
3853
3854 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003855 (void) signature;
3856 (void) signature_size;
3857 (void) signature_length;
3858
3859 return PSA_ERROR_NOT_SUPPORTED;
3860
3861#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3862 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3863 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3864}
3865
3866psa_status_t mbedtls_psa_sign_hash_abort(
3867 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3868{
3869
3870#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3871 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3872 defined(MBEDTLS_ECP_RESTARTABLE)
3873
3874 if (operation->ctx) {
3875 mbedtls_ecdsa_free(operation->ctx);
3876 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003877 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003878 }
3879
3880 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3881
Paul Elliott93d9ca82023-02-15 18:14:21 +00003882 operation->num_ops = 0;
3883
Paul Elliott588f8ed2022-12-02 18:10:26 +00003884 return PSA_SUCCESS;
3885
3886#else
3887
3888 (void) operation;
3889
3890 return PSA_ERROR_NOT_SUPPORTED;
3891
3892#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3893 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3894 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3895}
3896
3897psa_status_t mbedtls_psa_verify_hash_start(
3898 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3899 const psa_key_attributes_t *attributes,
3900 const uint8_t *key_buffer, size_t key_buffer_size,
3901 psa_algorithm_t alg,
3902 const uint8_t *hash, size_t hash_length,
3903 const uint8_t *signature, size_t signature_length)
3904{
3905 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003906 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003907 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003908
Paul Elliott068fe072023-01-16 13:59:15 +00003909 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3910 return PSA_ERROR_NOT_SUPPORTED;
3911 }
3912
3913 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003914 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003915 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003916
3917#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003918 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3919 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003920
Paul Elliotta1c94092023-02-15 16:38:04 +00003921 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3922 mbedtls_mpi_init(&operation->r);
3923 mbedtls_mpi_init(&operation->s);
3924
Paul Elliott93d9ca82023-02-15 18:14:21 +00003925 /* Ensure num_ops is zero'ed in case of context re-use. */
3926 operation->num_ops = 0;
3927
Paul Elliott068fe072023-01-16 13:59:15 +00003928 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3929 attributes->core.bits,
3930 key_buffer,
3931 key_buffer_size,
3932 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003933
Paul Elliott068fe072023-01-16 13:59:15 +00003934 if (status != PSA_SUCCESS) {
3935 return status;
3936 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003937
Paul Elliottc569fc22023-02-10 13:02:54 +00003938 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003939
Paul Elliott1bc59df2023-02-05 13:41:57 +00003940 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003941 return PSA_ERROR_INVALID_SIGNATURE;
3942 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003943
Paul Elliott068fe072023-01-16 13:59:15 +00003944 status = mbedtls_to_psa_error(
3945 mbedtls_mpi_read_binary(&operation->r,
3946 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003947 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003948
Paul Elliott068fe072023-01-16 13:59:15 +00003949 if (status != PSA_SUCCESS) {
3950 return status;
3951 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003952
Paul Elliott068fe072023-01-16 13:59:15 +00003953 status = mbedtls_to_psa_error(
3954 mbedtls_mpi_read_binary(&operation->s,
3955 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003956 coordinate_bytes,
3957 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003958
Paul Elliott068fe072023-01-16 13:59:15 +00003959 if (status != PSA_SUCCESS) {
3960 return status;
3961 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003962
Paul Elliott2c9843f2023-02-15 17:32:42 +00003963 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003964
Paul Elliott2c9843f2023-02-15 17:32:42 +00003965 if (status != PSA_SUCCESS) {
3966 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003967 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003968
Paul Elliott0290a762023-02-09 14:30:24 +00003969 /* We only need to store the same length of hash as the private key size
3970 * here, it would be truncated by the internal implementation anyway. */
3971 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3972 coordinate_bytes);
3973
Paul Elliottba70ad42023-02-15 18:23:53 +00003974 if (required_hash_length > sizeof(operation->hash)) {
3975 /* Shouldn't happen, but better safe than sorry. */
3976 return PSA_ERROR_CORRUPTION_DETECTED;
3977 }
3978
Paul Elliott0290a762023-02-09 14:30:24 +00003979 memcpy(operation->hash, hash, required_hash_length);
3980 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003981
3982 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003983#else
Paul Elliott068fe072023-01-16 13:59:15 +00003984 (void) operation;
3985 (void) key_buffer;
3986 (void) key_buffer_size;
3987 (void) alg;
3988 (void) hash;
3989 (void) hash_length;
3990 (void) signature;
3991 (void) signature_length;
3992 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003993 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003994 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003995
Paul Elliott068fe072023-01-16 13:59:15 +00003996 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003997#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3998 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3999 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004000}
4001
4002psa_status_t mbedtls_psa_verify_hash_complete(
4003 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4004{
4005
4006#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4007 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4008 defined(MBEDTLS_ECP_RESTARTABLE)
4009
Paul Elliottf8e5b562023-02-19 18:43:45 +00004010 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4011
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004012 /* Ensure max_ops is set to the current value (or default). */
4013 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4014
Paul Elliottf8e5b562023-02-19 18:43:45 +00004015 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004016 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4017 operation->hash,
4018 operation->hash_length,
4019 &operation->ctx->Q,
4020 &operation->r,
4021 &operation->s,
4022 &operation->restart_ctx));
4023
Paul Elliottf8e5b562023-02-19 18:43:45 +00004024 /* Hide the fact that the restart context only holds a delta of number of
4025 * ops done during the last operation, not an absolute value. */
4026 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4027
4028 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004029#else
4030 (void) operation;
4031
4032 return PSA_ERROR_NOT_SUPPORTED;
4033
4034#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4035 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4036 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4037}
4038
4039psa_status_t mbedtls_psa_verify_hash_abort(
4040 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4041{
4042
4043#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4044 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4045 defined(MBEDTLS_ECP_RESTARTABLE)
4046
4047 if (operation->ctx) {
4048 mbedtls_ecdsa_free(operation->ctx);
4049 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004050 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004051 }
4052
4053 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4054
Paul Elliott93d9ca82023-02-15 18:14:21 +00004055 operation->num_ops = 0;
4056
Paul Elliott588f8ed2022-12-02 18:10:26 +00004057 mbedtls_mpi_free(&operation->r);
4058 mbedtls_mpi_free(&operation->s);
4059
4060 return PSA_SUCCESS;
4061
4062#else
4063 (void) operation;
4064
4065 return PSA_ERROR_NOT_SUPPORTED;
4066
4067#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4068 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4069 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4070}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004071
mohammad1603503973b2018-03-12 15:59:30 +02004072/****************************************************************/
4073/* Symmetric cryptography */
4074/****************************************************************/
4075
Gilles Peskine449bd832023-01-11 14:50:10 +01004076static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4077 mbedtls_svc_key_id_t key,
4078 psa_algorithm_t alg,
4079 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004080{
4081 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4082 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004083 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004084 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4085 PSA_KEY_USAGE_ENCRYPT :
4086 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004087 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004088
4089 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004090 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004091 status = PSA_ERROR_BAD_STATE;
4092 goto exit;
4093 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004094
Gilles Peskine449bd832023-01-11 14:50:10 +01004095 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004096 status = PSA_ERROR_INVALID_ARGUMENT;
4097 goto exit;
4098 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004099
Gilles Peskine449bd832023-01-11 14:50:10 +01004100 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4101 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004102 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004103 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004104
Ronald Cron49fafa92021-03-10 08:34:23 +01004105 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004106 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004107 * so we only set it (in the driver wrapper) after resources have been
4108 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004109 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004110 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004111 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004112 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004113 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 }
4115 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004116
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004117 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004119 };
4120
Ronald Cronab99ac22020-10-01 16:38:28 +02004121 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 if (cipher_operation == MBEDTLS_ENCRYPT) {
4123 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4124 &attributes,
4125 slot->key.data,
4126 slot->key.bytes,
4127 alg);
4128 } else {
4129 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4130 &attributes,
4131 slot->key.data,
4132 slot->key.bytes,
4133 alg);
4134 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004135
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004136exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004137 if (status != PSA_SUCCESS) {
4138 psa_cipher_abort(operation);
4139 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004140
Gilles Peskine449bd832023-01-11 14:50:10 +01004141 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004142
Gilles Peskine449bd832023-01-11 14:50:10 +01004143 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004144}
4145
Gilles Peskine449bd832023-01-11 14:50:10 +01004146psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4147 mbedtls_svc_key_id_t key,
4148 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004149{
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004151}
4152
Gilles Peskine449bd832023-01-11 14:50:10 +01004153psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4154 mbedtls_svc_key_id_t key,
4155 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004156{
Gilles Peskine449bd832023-01-11 14:50:10 +01004157 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004158}
4159
Gilles Peskine449bd832023-01-11 14:50:10 +01004160psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4161 uint8_t *iv,
4162 size_t iv_size,
4163 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004164{
Ronald Cron6d051732020-10-01 14:10:20 +02004165 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004166 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004167 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004168
Gilles Peskine449bd832023-01-11 14:50:10 +01004169 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004170 status = PSA_ERROR_BAD_STATE;
4171 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004172 }
4173
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004175 status = PSA_ERROR_BAD_STATE;
4176 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004177 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004178
Ronald Cron2fb90522021-07-05 12:31:44 +02004179 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004180 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004181 status = PSA_ERROR_BUFFER_TOO_SMALL;
4182 goto exit;
4183 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004184
Gilles Peskine449bd832023-01-11 14:50:10 +01004185 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004186 status = PSA_ERROR_GENERIC_ERROR;
4187 goto exit;
4188 }
4189
Gilles Peskine449bd832023-01-11 14:50:10 +01004190 status = psa_generate_random(local_iv, default_iv_length);
4191 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004192 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004193 }
Ronald Cron5618a392021-03-26 09:52:26 +01004194
Gilles Peskine449bd832023-01-11 14:50:10 +01004195 status = psa_driver_wrapper_cipher_set_iv(operation,
4196 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004197
4198exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 if (status == PSA_SUCCESS) {
4200 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004201 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004202 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004204 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004205 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004206 }
Ronald Cron6d051732020-10-01 14:10:20 +02004207
Gilles Peskine449bd832023-01-11 14:50:10 +01004208 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004209}
4210
Gilles Peskine449bd832023-01-11 14:50:10 +01004211psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4212 const uint8_t *iv,
4213 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004214{
Ronald Cron6d051732020-10-01 14:10:20 +02004215 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004216
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004218 status = PSA_ERROR_BAD_STATE;
4219 goto exit;
4220 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004221
Gilles Peskine449bd832023-01-11 14:50:10 +01004222 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004223 status = PSA_ERROR_BAD_STATE;
4224 goto exit;
4225 }
Ronald Crona0d68172021-03-26 10:15:08 +01004226
Gilles Peskine449bd832023-01-11 14:50:10 +01004227 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004228 status = PSA_ERROR_INVALID_ARGUMENT;
4229 goto exit;
4230 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004231
Gilles Peskine449bd832023-01-11 14:50:10 +01004232 status = psa_driver_wrapper_cipher_set_iv(operation,
4233 iv,
4234 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004235
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004236exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004238 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004239 } else {
4240 psa_cipher_abort(operation);
4241 }
4242 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004243}
4244
Gilles Peskine449bd832023-01-11 14:50:10 +01004245psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4246 const uint8_t *input,
4247 size_t input_length,
4248 uint8_t *output,
4249 size_t output_size,
4250 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004251{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004252 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004253
Gilles Peskine449bd832023-01-11 14:50:10 +01004254 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004255 status = PSA_ERROR_BAD_STATE;
4256 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004257 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004258
Gilles Peskine449bd832023-01-11 14:50:10 +01004259 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004260 status = PSA_ERROR_BAD_STATE;
4261 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004262 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004263
Gilles Peskine449bd832023-01-11 14:50:10 +01004264 status = psa_driver_wrapper_cipher_update(operation,
4265 input,
4266 input_length,
4267 output,
4268 output_size,
4269 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004270
4271exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004272 if (status != PSA_SUCCESS) {
4273 psa_cipher_abort(operation);
4274 }
Ronald Cron6d051732020-10-01 14:10:20 +02004275
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004277}
4278
Gilles Peskine449bd832023-01-11 14:50:10 +01004279psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4280 uint8_t *output,
4281 size_t output_size,
4282 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004283{
David Saadab4ecc272019-02-14 13:48:10 +02004284 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004285
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004287 status = PSA_ERROR_BAD_STATE;
4288 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004289 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004290
Gilles Peskine449bd832023-01-11 14:50:10 +01004291 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004292 status = PSA_ERROR_BAD_STATE;
4293 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004294 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004295
Gilles Peskine449bd832023-01-11 14:50:10 +01004296 status = psa_driver_wrapper_cipher_finish(operation,
4297 output,
4298 output_size,
4299 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004300
4301exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004302 if (status == PSA_SUCCESS) {
4303 return psa_cipher_abort(operation);
4304 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004305 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004306 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004307
Gilles Peskine449bd832023-01-11 14:50:10 +01004308 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004309 }
mohammad1603503973b2018-03-12 15:59:30 +02004310}
4311
Gilles Peskine449bd832023-01-11 14:50:10 +01004312psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004313{
Gilles Peskine449bd832023-01-11 14:50:10 +01004314 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004315 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004316 * in use. It's ok to call abort on such an object, and there's
4317 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004319 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004320
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004322
Ronald Cron49fafa92021-03-10 08:34:23 +01004323 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004324 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004325 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004326
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004328}
4329
Gilles Peskine449bd832023-01-11 14:50:10 +01004330psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4331 psa_algorithm_t alg,
4332 const uint8_t *input,
4333 size_t input_length,
4334 uint8_t *output,
4335 size_t output_size,
4336 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004337{
4338 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004339 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004340 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004341 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4342 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004343 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004344
Gilles Peskine449bd832023-01-11 14:50:10 +01004345 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004346 status = PSA_ERROR_INVALID_ARGUMENT;
4347 goto exit;
4348 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004349
Gilles Peskine449bd832023-01-11 14:50:10 +01004350 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4351 PSA_KEY_USAGE_ENCRYPT,
4352 alg);
4353 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004354 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004355 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004356
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004357 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004359 };
4360
Gilles Peskine449bd832023-01-11 14:50:10 +01004361 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4362 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004363 status = PSA_ERROR_GENERIC_ERROR;
4364 goto exit;
4365 }
4366
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 if (default_iv_length > 0) {
4368 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004369 status = PSA_ERROR_BUFFER_TOO_SMALL;
4370 goto exit;
4371 }
4372
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 status = psa_generate_random(local_iv, default_iv_length);
4374 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004375 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004377 }
4378
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004379 status = psa_driver_wrapper_cipher_encrypt(
4380 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004381 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004382 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004383 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004384
4385exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 unlock_status = psa_unlock_key_slot(slot);
4387 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004388 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004389 }
Ronald Cron23919522021-07-08 19:00:07 +02004390
Gilles Peskine449bd832023-01-11 14:50:10 +01004391 if (status == PSA_SUCCESS) {
4392 if (default_iv_length > 0) {
4393 memcpy(output, local_iv, default_iv_length);
4394 }
4395 *output_length += default_iv_length;
4396 } else {
4397 *output_length = 0;
4398 }
4399
4400 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004401}
4402
Gilles Peskine449bd832023-01-11 14:50:10 +01004403psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4404 psa_algorithm_t alg,
4405 const uint8_t *input,
4406 size_t input_length,
4407 uint8_t *output,
4408 size_t output_size,
4409 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004410{
4411 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004412 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004413 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004414 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004415
Gilles Peskine449bd832023-01-11 14:50:10 +01004416 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004417 status = PSA_ERROR_INVALID_ARGUMENT;
4418 goto exit;
4419 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004420
Gilles Peskine449bd832023-01-11 14:50:10 +01004421 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4422 PSA_KEY_USAGE_DECRYPT,
4423 alg);
4424 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004425 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004426 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004427
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004428 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004430 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004431
Gilles Peskine449bd832023-01-11 14:50:10 +01004432 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4433 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004434 status = PSA_ERROR_INVALID_ARGUMENT;
4435 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004436 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004437 status = PSA_ERROR_INVALID_ARGUMENT;
4438 goto exit;
4439 }
4440
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004441 status = psa_driver_wrapper_cipher_decrypt(
4442 &attributes, slot->key.data, slot->key.bytes,
4443 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004445
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004446exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 unlock_status = psa_unlock_key_slot(slot);
4448 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004449 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004451
Gilles Peskine449bd832023-01-11 14:50:10 +01004452 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004453 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 }
Ronald Cron23919522021-07-08 19:00:07 +02004455
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004457}
4458
4459
mohammad16035955c982018-04-26 00:53:03 +03004460/****************************************************************/
4461/* AEAD */
4462/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004463
Paul Elliottbaff51c2021-09-28 17:44:45 +01004464/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004465static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004466{
Gilles Peskine449bd832023-01-11 14:50:10 +01004467 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004468}
4469
4470/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004471static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4472 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004473{
Gilles Peskine449bd832023-01-11 14:50:10 +01004474 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004475
Gilles Peskine449bd832023-01-11 14:50:10 +01004476 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004477#if defined(PSA_WANT_ALG_GCM)
4478 case PSA_ALG_GCM:
4479 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004480 * arbitrarily large nonces. Please note that we do not generally
4481 * recommend the usage of nonces of greater length than
4482 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4483 * size, which can then lead to collisions if you encrypt a very
4484 * large number of messages.*/
4485 if (nonce_length != 0) {
4486 return PSA_SUCCESS;
4487 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004488 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004489#endif /* PSA_WANT_ALG_GCM */
4490#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004491 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004492 if (nonce_length >= 7 && nonce_length <= 13) {
4493 return PSA_SUCCESS;
4494 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004495 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004496#endif /* PSA_WANT_ALG_CCM */
4497#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004498 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 if (nonce_length == 12) {
4500 return PSA_SUCCESS;
4501 } else if (nonce_length == 8) {
4502 return PSA_ERROR_NOT_SUPPORTED;
4503 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004504 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004505#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004506 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004507 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004508 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004509 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004510
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004512}
4513
Gilles Peskine449bd832023-01-11 14:50:10 +01004514static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004515{
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4517 return PSA_ERROR_INVALID_ARGUMENT;
4518 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004519
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004521}
4522
Gilles Peskine449bd832023-01-11 14:50:10 +01004523psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4524 psa_algorithm_t alg,
4525 const uint8_t *nonce,
4526 size_t nonce_length,
4527 const uint8_t *additional_data,
4528 size_t additional_data_length,
4529 const uint8_t *plaintext,
4530 size_t plaintext_length,
4531 uint8_t *ciphertext,
4532 size_t ciphertext_size,
4533 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004534{
Ronald Cron215633c2021-03-16 17:15:37 +01004535 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4536 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004537
mohammad1603f08a5502018-06-03 15:05:47 +03004538 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004539
Gilles Peskine449bd832023-01-11 14:50:10 +01004540 status = psa_aead_check_algorithm(alg);
4541 if (status != PSA_SUCCESS) {
4542 return status;
4543 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004544
Ronald Cron9a986162021-03-26 12:40:07 +01004545 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4547 if (status != PSA_SUCCESS) {
4548 return status;
4549 }
mohammad16035955c982018-04-26 00:53:03 +03004550
Ronald Cron215633c2021-03-16 17:15:37 +01004551 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004553 };
mohammad16035955c982018-04-26 00:53:03 +03004554
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 status = psa_aead_check_nonce_length(alg, nonce_length);
4556 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004557 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004559
Ronald Cronde822812021-03-17 16:08:20 +01004560 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004561 &attributes, slot->key.data, slot->key.bytes,
4562 alg,
4563 nonce, nonce_length,
4564 additional_data, additional_data_length,
4565 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004566 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004567
Gilles Peskine449bd832023-01-11 14:50:10 +01004568 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4569 memset(ciphertext, 0, ciphertext_size);
4570 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004571
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004572exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004574
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004576}
4577
Gilles Peskine449bd832023-01-11 14:50:10 +01004578psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4579 psa_algorithm_t alg,
4580 const uint8_t *nonce,
4581 size_t nonce_length,
4582 const uint8_t *additional_data,
4583 size_t additional_data_length,
4584 const uint8_t *ciphertext,
4585 size_t ciphertext_length,
4586 uint8_t *plaintext,
4587 size_t plaintext_size,
4588 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004589{
Ronald Cron215633c2021-03-16 17:15:37 +01004590 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4591 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004592
Gilles Peskineee652a32018-06-01 19:23:52 +02004593 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004594
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 status = psa_aead_check_algorithm(alg);
4596 if (status != PSA_SUCCESS) {
4597 return status;
4598 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004599
Ronald Cron9a986162021-03-26 12:40:07 +01004600 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4602 if (status != PSA_SUCCESS) {
4603 return status;
4604 }
mohammad16035955c982018-04-26 00:53:03 +03004605
Ronald Cron215633c2021-03-16 17:15:37 +01004606 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004608 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004609
Gilles Peskine449bd832023-01-11 14:50:10 +01004610 status = psa_aead_check_nonce_length(alg, nonce_length);
4611 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004612 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004613 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004614
Ronald Cronde822812021-03-17 16:08:20 +01004615 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004616 &attributes, slot->key.data, slot->key.bytes,
4617 alg,
4618 nonce, nonce_length,
4619 additional_data, additional_data_length,
4620 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004622
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 if (status != PSA_SUCCESS && plaintext_size != 0) {
4624 memset(plaintext, 0, plaintext_size);
4625 }
mohammad160360a64d02018-06-03 17:20:42 +03004626
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004627exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004628 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004629
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 return status;
mohammad16035955c982018-04-26 00:53:03 +03004631}
4632
Gilles Peskine449bd832023-01-11 14:50:10 +01004633static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4634{
4635 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004636
Gilles Peskine449bd832023-01-11 14:50:10 +01004637 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004638#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004640 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004641 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4642 return PSA_ERROR_INVALID_ARGUMENT;
4643 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004644 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004645#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004646
Przemek Stekiel86679c72022-10-06 17:06:56 +02004647#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004649 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004650 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4651 return PSA_ERROR_INVALID_ARGUMENT;
4652 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004653 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004654#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004655
Przemek Stekiel86679c72022-10-06 17:06:56 +02004656#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004658 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004659 if (tag_len != 16) {
4660 return PSA_ERROR_INVALID_ARGUMENT;
4661 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004662 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004663#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004664
4665 default:
4666 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004668 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004670}
4671
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004672/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004673static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4674 int is_encrypt,
4675 mbedtls_svc_key_id_t key,
4676 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004677{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004678 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4679 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4680 psa_key_slot_t *slot = NULL;
4681 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004682 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004683
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 status = psa_aead_check_algorithm(alg);
4685 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004686 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004688
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004690 status = PSA_ERROR_BAD_STATE;
4691 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004692 }
4693
Gilles Peskine449bd832023-01-11 14:50:10 +01004694 if (operation->nonce_set || operation->lengths_set ||
4695 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004696 status = PSA_ERROR_BAD_STATE;
4697 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004698 }
4699
Gilles Peskine449bd832023-01-11 14:50:10 +01004700 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004701 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004703 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004705
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4707 alg);
4708 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004709 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004710 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004711
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004712 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004713 .core = slot->attr
4714 };
4715
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004717 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004718 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004719
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 if (is_encrypt) {
4721 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4722 &attributes,
4723 slot->key.data,
4724 slot->key.bytes,
4725 alg);
4726 } else {
4727 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4728 &attributes,
4729 slot->key.data,
4730 slot->key.bytes,
4731 alg);
4732 }
4733 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004734 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004736
Gilles Peskine449bd832023-01-11 14:50:10 +01004737 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004738
4739exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004741
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004743 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004745 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004746 } else {
4747 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004748 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004749
Gilles Peskine449bd832023-01-11 14:50:10 +01004750 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004751}
4752
Paul Elliott302ff6b2021-04-20 18:10:30 +01004753/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004754psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4755 mbedtls_svc_key_id_t key,
4756 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004757{
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004759}
4760
4761/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004762psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4763 mbedtls_svc_key_id_t key,
4764 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004765{
Gilles Peskine449bd832023-01-11 14:50:10 +01004766 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004767}
4768
4769/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004770psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4771 uint8_t *nonce,
4772 size_t nonce_size,
4773 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004774{
4775 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004776 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004777 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004778
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004779 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004780
Gilles Peskine449bd832023-01-11 14:50:10 +01004781 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004782 status = PSA_ERROR_BAD_STATE;
4783 goto exit;
4784 }
4785
Gilles Peskine449bd832023-01-11 14:50:10 +01004786 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004787 status = PSA_ERROR_BAD_STATE;
4788 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004789 }
4790
Paul Elliotte193ea82021-10-01 13:00:16 +01004791 /* For CCM, this size may not be correct according to the PSA
4792 * specification. The PSA Crypto 1.0.1 specification states:
4793 *
4794 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4795 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4796 *
4797 * However this restriction that L has to be the smallest integer is not
4798 * applied in practice, and it is not implementable here since the
4799 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004800 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4801 operation->alg);
4802 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004803 status = PSA_ERROR_BUFFER_TOO_SMALL;
4804 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004805 }
4806
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 status = psa_generate_random(local_nonce, required_nonce_size);
4808 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004809 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004810 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004811
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004813
Paul Elliott39dc6b82021-05-11 19:16:09 +01004814exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004815 if (status == PSA_SUCCESS) {
4816 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004817 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 } else {
4819 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004820 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004821
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004823}
4824
4825/* Set the nonce for a multipart authenticated encryption or decryption
4826 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004827psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4828 const uint8_t *nonce,
4829 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004830{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004831 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4832
Gilles Peskine449bd832023-01-11 14:50:10 +01004833 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004834 status = PSA_ERROR_BAD_STATE;
4835 goto exit;
4836 }
4837
Gilles Peskine449bd832023-01-11 14:50:10 +01004838 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004839 status = PSA_ERROR_BAD_STATE;
4840 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004841 }
4842
Gilles Peskine449bd832023-01-11 14:50:10 +01004843 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4844 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004845 status = PSA_ERROR_INVALID_ARGUMENT;
4846 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004847 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004848
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4850 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004851
Paul Elliott39dc6b82021-05-11 19:16:09 +01004852exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004853 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004854 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004855 } else {
4856 psa_aead_abort(operation);
4857 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004858
Gilles Peskine449bd832023-01-11 14:50:10 +01004859 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004860}
4861
4862/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004863psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4864 size_t ad_length,
4865 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004866{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004867 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4868
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004870 status = PSA_ERROR_BAD_STATE;
4871 goto exit;
4872 }
4873
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 if (operation->lengths_set || operation->ad_started ||
4875 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004876 status = PSA_ERROR_BAD_STATE;
4877 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004878 }
4879
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004881#if defined(PSA_WANT_ALG_GCM)
4882 case PSA_ALG_GCM:
4883 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 * bits. Without the guard this code will generate warnings on 32bit
4885 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004886#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 if (((uint64_t) ad_length) >> 61 != 0 ||
4888 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004889 status = PSA_ERROR_INVALID_ARGUMENT;
4890 goto exit;
4891 }
Paul Elliott325d3742021-09-27 17:56:28 +01004892#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004893 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004894#endif /* PSA_WANT_ALG_GCM */
4895#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004896 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004897 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004898 status = PSA_ERROR_INVALID_ARGUMENT;
4899 goto exit;
4900 }
4901 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004902#endif /* PSA_WANT_ALG_CCM */
4903#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004904 case PSA_ALG_CHACHA20_POLY1305:
4905 /* No length restrictions for ChaChaPoly. */
4906 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004907#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004908 default:
4909 break;
4910 }
Paul Elliott325d3742021-09-27 17:56:28 +01004911
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4913 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004914
Paul Elliott39dc6b82021-05-11 19:16:09 +01004915exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004916 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004917 operation->ad_remaining = ad_length;
4918 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004919 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004920 } else {
4921 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004922 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004923
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004925}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004926
4927/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004928psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4929 const uint8_t *input,
4930 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004931{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004932 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4933
Gilles Peskine449bd832023-01-11 14:50:10 +01004934 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004935 status = PSA_ERROR_BAD_STATE;
4936 goto exit;
4937 }
4938
Gilles Peskine449bd832023-01-11 14:50:10 +01004939 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004940 status = PSA_ERROR_BAD_STATE;
4941 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004942 }
4943
Gilles Peskine449bd832023-01-11 14:50:10 +01004944 if (operation->lengths_set) {
4945 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004946 status = PSA_ERROR_INVALID_ARGUMENT;
4947 goto exit;
4948 }
4949
4950 operation->ad_remaining -= input_length;
4951 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004952#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004954 status = PSA_ERROR_BAD_STATE;
4955 goto exit;
4956 }
4957#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004958
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 status = psa_driver_wrapper_aead_update_ad(operation, input,
4960 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004961
Paul Elliott39dc6b82021-05-11 19:16:09 +01004962exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004963 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004964 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 } else {
4966 psa_aead_abort(operation);
4967 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004968
Gilles Peskine449bd832023-01-11 14:50:10 +01004969 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004970}
4971
4972/* Encrypt or decrypt a message fragment in an active multipart AEAD
4973 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004974psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4975 const uint8_t *input,
4976 size_t input_length,
4977 uint8_t *output,
4978 size_t output_size,
4979 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004980{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004981 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004982
4983 *output_length = 0;
4984
Gilles Peskine449bd832023-01-11 14:50:10 +01004985 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004986 status = PSA_ERROR_BAD_STATE;
4987 goto exit;
4988 }
4989
Gilles Peskine449bd832023-01-11 14:50:10 +01004990 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004991 status = PSA_ERROR_BAD_STATE;
4992 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004993 }
4994
Gilles Peskine449bd832023-01-11 14:50:10 +01004995 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004996 /* Additional data length was supplied, but not all the additional
4997 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004998 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004999 status = PSA_ERROR_INVALID_ARGUMENT;
5000 goto exit;
5001 }
5002
5003 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005005 status = PSA_ERROR_INVALID_ARGUMENT;
5006 goto exit;
5007 }
5008
5009 operation->body_remaining -= input_length;
5010 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005011#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005012 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005013 status = PSA_ERROR_BAD_STATE;
5014 goto exit;
5015 }
5016#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005017
Gilles Peskine449bd832023-01-11 14:50:10 +01005018 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5019 output, output_size,
5020 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005021
Paul Elliott39dc6b82021-05-11 19:16:09 +01005022exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005023 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005024 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005025 } else {
5026 psa_aead_abort(operation);
5027 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005028
Gilles Peskine449bd832023-01-11 14:50:10 +01005029 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005030}
5031
Gilles Peskine449bd832023-01-11 14:50:10 +01005032static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005033{
Gilles Peskine449bd832023-01-11 14:50:10 +01005034 if (operation->id == 0 || !operation->nonce_set) {
5035 return PSA_ERROR_BAD_STATE;
5036 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005037
Gilles Peskine449bd832023-01-11 14:50:10 +01005038 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5039 operation->body_remaining != 0)) {
5040 return PSA_ERROR_INVALID_ARGUMENT;
5041 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005042
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005044}
5045
Paul Elliott302ff6b2021-04-20 18:10:30 +01005046/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005047psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5048 uint8_t *ciphertext,
5049 size_t ciphertext_size,
5050 size_t *ciphertext_length,
5051 uint8_t *tag,
5052 size_t tag_size,
5053 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005054{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005055 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5056
Paul Elliott302ff6b2021-04-20 18:10:30 +01005057 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005058 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005059
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 status = psa_aead_final_checks(operation);
5061 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005062 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005063 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005064
Gilles Peskine449bd832023-01-11 14:50:10 +01005065 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005066 status = PSA_ERROR_BAD_STATE;
5067 goto exit;
5068 }
5069
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5071 ciphertext_size,
5072 ciphertext_length,
5073 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005074
Paul Elliott39dc6b82021-05-11 19:16:09 +01005075exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005076
5077
Paul Elliottf47b0952021-05-21 18:02:33 +01005078 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005079 * the zero tag size, make sure the tag is set to something implausible.
5080 * Even if the operation succeeds, make sure we clear the rest of the
5081 * buffer to prevent potential leakage of anything previously placed in
5082 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005083 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005084
Gilles Peskine449bd832023-01-11 14:50:10 +01005085 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005086
Gilles Peskine449bd832023-01-11 14:50:10 +01005087 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005088}
5089
5090/* Finish authenticating and decrypting a message in a multipart AEAD
5091 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005092psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5093 uint8_t *plaintext,
5094 size_t plaintext_size,
5095 size_t *plaintext_length,
5096 const uint8_t *tag,
5097 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005098{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005099 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5100
Paul Elliott302ff6b2021-04-20 18:10:30 +01005101 *plaintext_length = 0;
5102
Gilles Peskine449bd832023-01-11 14:50:10 +01005103 status = psa_aead_final_checks(operation);
5104 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005105 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005107
Gilles Peskine449bd832023-01-11 14:50:10 +01005108 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005109 status = PSA_ERROR_BAD_STATE;
5110 goto exit;
5111 }
5112
Gilles Peskine449bd832023-01-11 14:50:10 +01005113 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5114 plaintext_size,
5115 plaintext_length,
5116 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005117
5118exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005120
Gilles Peskine449bd832023-01-11 14:50:10 +01005121 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005122}
5123
5124/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005125psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005126{
5127 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5128
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005130 /* The object has (apparently) been initialized but it is not (yet)
5131 * in use. It's ok to call abort on such an object, and there's
5132 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005133 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005134 }
5135
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005137
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005139
Gilles Peskine449bd832023-01-11 14:50:10 +01005140 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005141}
5142
Gilles Peskinee59236f2018-01-27 23:32:46 +01005143/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005144/* Generators */
5145/****************************************************************/
5146
Przemek Stekiel69c46792022-06-10 12:59:51 +02005147#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005148 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005149 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305150 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305151 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005152#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005153#endif /* At least one builtin KDF */
5154
Przemek Stekiel69c46792022-06-10 12:59:51 +02005155#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005156 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5157 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5158static psa_status_t psa_key_derivation_start_hmac(
5159 psa_mac_operation_t *operation,
5160 psa_algorithm_t hash_alg,
5161 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005163{
5164 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5165 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5167 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5168 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005169
Steven Cooreman72f736a2021-05-07 14:14:37 +02005170 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005172
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 status = psa_driver_wrapper_mac_sign_setup(operation,
5174 &attributes,
5175 hmac_key, hmac_key_length,
5176 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005177
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 psa_reset_key_attributes(&attributes);
5179 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005180}
5181#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005182
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005183#define HKDF_STATE_INIT 0 /* no input yet */
5184#define HKDF_STATE_STARTED 1 /* got salt */
5185#define HKDF_STATE_KEYED 2 /* got key */
5186#define HKDF_STATE_OUTPUT 3 /* output started */
5187
Gilles Peskinecbe66502019-05-16 16:59:18 +02005188static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005190{
Gilles Peskine449bd832023-01-11 14:50:10 +01005191 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5192 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5193 } else {
5194 return operation->alg;
5195 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005196}
5197
Gilles Peskine449bd832023-01-11 14:50:10 +01005198psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005199{
5200 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005201 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5202 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005203 /* The object has (apparently) been initialized but it is not
5204 * in use. It's ok to call abort on such an object, and there's
5205 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005207#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005208 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5209 mbedtls_free(operation->ctx.hkdf.info);
5210 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5211 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005212#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005213#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5214 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005215 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5216 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5217 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5218 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005219 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005220 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005221 }
5222
Gilles Peskine449bd832023-01-11 14:50:10 +01005223 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005224 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005225 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005226 }
5227
Gilles Peskine449bd832023-01-11 14:50:10 +01005228 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005229 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005230 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005231 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005232#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005233 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005234 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005236 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005237#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005238 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005239
5240 /* We leave the fields Ai and output_block to be erased safely by the
5241 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005243#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5244 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005245#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5247 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5248 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5249 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005250#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305251#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305252 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305253 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005254 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305255 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305256 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305257
5258 status = PSA_SUCCESS;
5259 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305260#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005261 {
5262 status = PSA_ERROR_BAD_STATE;
5263 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 mbedtls_platform_zeroize(operation, sizeof(*operation));
5265 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005266}
5267
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005268psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005269 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005270{
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005272 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005274 }
5275
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005276 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005278}
5279
Gilles Peskine449bd832023-01-11 14:50:10 +01005280psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5281 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005282{
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 if (operation->alg == 0) {
5284 return PSA_ERROR_BAD_STATE;
5285 }
5286 if (capacity > operation->capacity) {
5287 return PSA_ERROR_INVALID_ARGUMENT;
5288 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005289 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005290 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005291}
5292
Przemek Stekiel69c46792022-06-10 12:59:51 +02005293#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005294/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005295static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5296 psa_algorithm_t kdf_alg,
5297 uint8_t *output,
5298 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005299{
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5301 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005302 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005303 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005304#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005305 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005306#else
5307 const uint8_t last_block = 0xff;
5308#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005309
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 if (hkdf->state < HKDF_STATE_KEYED ||
5311 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005312#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005314#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 )) {
5316 return PSA_ERROR_BAD_STATE;
5317 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005318 hkdf->state = HKDF_STATE_OUTPUT;
5319
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005321 /* Copy what remains of the current block */
5322 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005324 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 }
5326 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005327 output += n;
5328 output_length -= n;
5329 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005331 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005332 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005333 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005334 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005335 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005336 * object was corrupted or if this function is called directly
5337 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005338 if (hkdf->block_number == last_block) {
5339 return PSA_ERROR_BAD_STATE;
5340 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005341
5342 /* We need a new block */
5343 ++hkdf->block_number;
5344 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005345
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5347 hash_alg,
5348 hkdf->prk,
5349 hash_length);
5350 if (status != PSA_SUCCESS) {
5351 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005352 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005353
5354 if (hkdf->block_number != 1) {
5355 status = psa_mac_update(&hkdf->hmac,
5356 hkdf->output_block,
5357 hash_length);
5358 if (status != PSA_SUCCESS) {
5359 return status;
5360 }
5361 }
5362 status = psa_mac_update(&hkdf->hmac,
5363 hkdf->info,
5364 hkdf->info_length);
5365 if (status != PSA_SUCCESS) {
5366 return status;
5367 }
5368 status = psa_mac_update(&hkdf->hmac,
5369 &hkdf->block_number, 1);
5370 if (status != PSA_SUCCESS) {
5371 return status;
5372 }
5373 status = psa_mac_sign_finish(&hkdf->hmac,
5374 hkdf->output_block,
5375 sizeof(hkdf->output_block),
5376 &hmac_output_length);
5377 if (status != PSA_SUCCESS) {
5378 return status;
5379 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005380 }
5381
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005383}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005384#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005385
John Durkop07cc04a2020-11-16 22:08:34 -08005386#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5387 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005388static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5389 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005391{
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5393 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005394 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5395 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005396 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005397
Janos Follath7742fee2019-06-17 12:58:10 +01005398 /* We can't be wanting more output after block 0xff, otherwise
5399 * the capacity check in psa_key_derivation_output_bytes() would have
5400 * prevented this call. It could happen only if the operation
5401 * object was corrupted or if this function is called directly
5402 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 if (tls12_prf->block_number == 0xff) {
5404 return PSA_ERROR_CORRUPTION_DETECTED;
5405 }
Janos Follath7742fee2019-06-17 12:58:10 +01005406
5407 /* We need a new block */
5408 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005409 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005410
5411 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5412 *
5413 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5414 *
5415 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5416 * HMAC_hash(secret, A(2) + seed) +
5417 * HMAC_hash(secret, A(3) + seed) + ...
5418 *
5419 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005420 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005421 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005422 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005423 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005424 * is currently extracted as `output_block` and where i is
5425 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005426 */
5427
Gilles Peskine449bd832023-01-11 14:50:10 +01005428 status = psa_key_derivation_start_hmac(&hmac,
5429 hash_alg,
5430 tls12_prf->secret,
5431 tls12_prf->secret_length);
5432 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005433 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005435
5436 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005438 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5439 * the variable seed and in this instance means it in the context of the
5440 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 status = psa_mac_update(&hmac,
5442 tls12_prf->label,
5443 tls12_prf->label_length);
5444 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005445 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 }
5447 status = psa_mac_update(&hmac,
5448 tls12_prf->seed,
5449 tls12_prf->seed_length);
5450 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005451 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 }
5453 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005454 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5456 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005457 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005459 }
5460
Gilles Peskine449bd832023-01-11 14:50:10 +01005461 status = psa_mac_sign_finish(&hmac,
5462 tls12_prf->Ai, hash_length,
5463 &hmac_output_length);
5464 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005465 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 }
5467 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005468 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005470
5471 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 status = psa_key_derivation_start_hmac(&hmac,
5473 hash_alg,
5474 tls12_prf->secret,
5475 tls12_prf->secret_length);
5476 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005477 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 }
5479 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5480 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005481 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 }
5483 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5484 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005485 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 }
5487 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5488 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005489 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005490 }
5491 status = psa_mac_sign_finish(&hmac,
5492 tls12_prf->output_block, hash_length,
5493 &hmac_output_length);
5494 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005495 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005497
Janos Follath7742fee2019-06-17 12:58:10 +01005498
5499cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005500 cleanup_status = psa_mac_abort(&hmac);
5501 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005502 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005504
Gilles Peskine449bd832023-01-11 14:50:10 +01005505 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005506}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005507
Janos Follath844eb0e2019-06-19 12:10:49 +01005508static psa_status_t psa_key_derivation_tls12_prf_read(
5509 psa_tls12_prf_key_derivation_t *tls12_prf,
5510 psa_algorithm_t alg,
5511 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005513{
Gilles Peskine449bd832023-01-11 14:50:10 +01005514 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5515 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005516 psa_status_t status;
5517 uint8_t offset, length;
5518
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005520 case PSA_TLS12_PRF_STATE_LABEL_SET:
5521 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5522 break;
5523 case PSA_TLS12_PRF_STATE_OUTPUT:
5524 break;
5525 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005527 }
5528
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005530 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 if (tls12_prf->left_in_block == 0) {
5532 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5533 alg);
5534 if (status != PSA_SUCCESS) {
5535 return status;
5536 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005537
5538 continue;
5539 }
5540
Gilles Peskine449bd832023-01-11 14:50:10 +01005541 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005542 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005544 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005546
5547 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005549 output += length;
5550 output_length -= length;
5551 tls12_prf->left_in_block -= length;
5552 }
5553
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005555}
John Durkop07cc04a2020-11-16 22:08:34 -08005556#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5557 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005558
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005559#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5560static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5561 psa_tls12_ecjpake_to_pms_t *ecjpake,
5562 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005564{
5565 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005566 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005567
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 if (output_length != 32) {
5569 return PSA_ERROR_INVALID_ARGUMENT;
5570 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005571
Gilles Peskine449bd832023-01-11 14:50:10 +01005572 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5573 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5574 &output_size);
5575 if (status != PSA_SUCCESS) {
5576 return status;
5577 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005578
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 if (output_size != output_length) {
5580 return PSA_ERROR_GENERIC_ERROR;
5581 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005582
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005584}
5585#endif
5586
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305587#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305588static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5589 psa_pbkdf2_key_derivation_t *pbkdf2,
5590 psa_algorithm_t prf_alg,
5591 uint8_t prf_output_length,
5592 psa_key_attributes_t *attributes)
5593{
5594 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305595 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305596 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305597 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305598 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305599 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305600 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305601
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305602 mac_operation.is_sign = 1;
5603 mac_operation.mac_size = prf_output_length;
5604 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305605
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305606 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5607 attributes,
5608 pbkdf2->password,
5609 pbkdf2->password_length,
5610 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305611 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305612 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305613 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305614 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5615 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305616 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305617 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305618 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305619 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305620 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305621 }
5622 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5623 &mac_output_length);
5624 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305625 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305626 }
5627
5628 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305629 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305630 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305631 }
5632
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305633 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305634
5635 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305636 /* We are passing prf_output_length as mac_size because the driver
5637 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305638 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305639 status = psa_driver_wrapper_mac_compute(attributes,
5640 pbkdf2->password,
5641 pbkdf2->password_length,
5642 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305643 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305644 &mac_output_length);
5645 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305646 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305647 }
5648
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305649 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305650 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305651
5652cleanup:
5653 /* Zeroise buffers to clear sensitive data from memory. */
5654 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5655 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305656}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305657
5658static psa_status_t psa_key_derivation_pbkdf2_read(
5659 psa_pbkdf2_key_derivation_t *pbkdf2,
5660 psa_algorithm_t kdf_alg,
5661 uint8_t *output,
5662 size_t output_length)
5663{
5664 psa_status_t status;
5665 psa_algorithm_t prf_alg;
5666 uint8_t prf_output_length;
5667 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5668 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5669 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5670
5671 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5672 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5673 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5674 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305675 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5676 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305677 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305678 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305679 } else {
5680 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305681 }
5682
5683 switch (pbkdf2->state) {
5684 case PSA_PBKDF2_STATE_PASSWORD_SET:
5685 /* Initially we need a new block so bytes_used is equal to block size*/
5686 pbkdf2->bytes_used = prf_output_length;
5687 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5688 break;
5689 case PSA_PBKDF2_STATE_OUTPUT:
5690 break;
5691 default:
5692 return PSA_ERROR_BAD_STATE;
5693 }
5694
5695 while (output_length != 0) {
5696 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5697 if (n > output_length) {
5698 n = (uint8_t) output_length;
5699 }
5700 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5701 output += n;
5702 output_length -= n;
5703 pbkdf2->bytes_used += n;
5704
5705 if (output_length == 0) {
5706 break;
5707 }
5708
5709 /* We need a new block */
5710 pbkdf2->bytes_used = 0;
5711 pbkdf2->block_number++;
5712
5713 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5714 prf_output_length,
5715 &attributes);
5716 if (status != PSA_SUCCESS) {
5717 return status;
5718 }
5719 }
5720
5721 return PSA_SUCCESS;
5722}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305723#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305724
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005725psa_status_t psa_key_derivation_output_bytes(
5726 psa_key_derivation_operation_t *operation,
5727 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005729{
5730 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005731 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005732
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005734 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005736 }
5737
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005739 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005740 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005741 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005742 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005743 goto exit;
5744 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005746 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005747 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005748 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5749 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005750 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005751 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005753 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005754 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005755
Przemek Stekiel69c46792022-06-10 12:59:51 +02005756#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5758 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5759 output, output_length);
5760 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005761#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005762#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5763 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5765 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5766 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5767 kdf_alg, output,
5768 output_length);
5769 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005770#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5771 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005772#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005774 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5776 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005777#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305778#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305779 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305780 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5781 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305782 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305783#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005784
Gilles Peskineeab56e42018-07-12 17:12:33 +02005785 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005786 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005788 }
5789
5790exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005792 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005793 * This allows us to differentiate between exhausted operations and
5794 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5795 * operations. */
5796 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005797 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005798 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005800 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005802}
5803
David Brown7807bf72021-02-09 16:28:23 -07005804#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005805static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005806{
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 if (data_size >= 8) {
5808 mbedtls_des_key_set_parity(data);
5809 }
5810 if (data_size >= 16) {
5811 mbedtls_des_key_set_parity(data + 8);
5812 }
5813 if (data_size >= 24) {
5814 mbedtls_des_key_set_parity(data + 16);
5815 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005816}
David Brown7807bf72021-02-09 16:28:23 -07005817#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005818
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005819/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 * ECC keys on a Weierstrass elliptic curve require the generation
5821 * of a private key which is an integer
5822 * in the range [1, N - 1], where N is the boundary of the private key domain:
5823 * N is the prime p for Diffie-Hellman, or the order of the
5824 * curve’s base point for ECC.
5825 *
5826 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5827 * This function generates the private key using the following process:
5828 *
5829 * 1. Draw a byte string of length ceiling(m/8) bytes.
5830 * 2. If m is not a multiple of 8, set the most significant
5831 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5832 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5833 * 4. If k > N - 2, discard the result and return to step 1.
5834 * 5. Output k + 1 as the private key.
5835 *
5836 * This method allows compliance to NIST standards, specifically the methods titled
5837 * Key-Pair Generation by Testing Candidates in the following publications:
5838 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5839 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5840 * Diffie-Hellman keys.
5841 *
5842 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5843 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5844 *
5845 * Note: Function allocates memory for *data buffer, so given *data should be
5846 * always NULL.
5847 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005848#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5849#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005850static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005851 psa_key_slot_t *slot,
5852 size_t bits,
5853 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005854 uint8_t **data
5855 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005856{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005857 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005858 mbedtls_mpi k;
5859 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005860 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005861 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005862 size_t m;
5863 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005864
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 mbedtls_mpi_init(&k);
5866 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005867
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005868 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005869 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005870 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005872
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005874 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005875 goto cleanup;
5876 }
5877
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005878 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005880
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005882
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005883 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005884 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005885 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005886
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005887 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005888
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005889 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005890 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005891
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005892 /* Note: This function is always called with *data == NULL and it
5893 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 *data = mbedtls_calloc(1, m_bytes);
5895 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005896 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005897 goto cleanup;
5898 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005899
Gilles Peskine449bd832023-01-11 14:50:10 +01005900 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005901 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005902 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005903 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005904 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005905
5906 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005907 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005908 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005909 * (8 * ceiling(m/8) - m) bits of the first byte in
5910 * the string to zero.
5911 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005912 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005913 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005914 }
5915
5916 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005917 * big-endian byte string.
5918 */
5919 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005920
5921 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 * Result of comparison is returned. When it indicates error
5923 * then this function is called again.
5924 */
5925 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005926 }
5927
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005928 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005929 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5930 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005931cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005932 if (ret != 0) {
5933 status = mbedtls_to_psa_error(ret);
5934 }
5935 if (status != PSA_SUCCESS) {
5936 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005937 *data = NULL;
5938 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 mbedtls_mpi_free(&k);
5940 mbedtls_mpi_free(&diff_N_2);
5941 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005942}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005943
5944/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5945 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5946 *
5947 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5948 * draw a 32-byte string and process it as specified in
5949 * Elliptic Curves for Security [RFC7748] §5.
5950 *
5951 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5952 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5953 *
5954 * Note: Function allocates memory for *data buffer, so given *data should be
5955 * always NULL.
5956 */
5957
5958static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5959 size_t bits,
5960 psa_key_derivation_operation_t *operation,
5961 uint8_t **data
5962 )
5963{
5964 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005965 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005966
Gilles Peskine449bd832023-01-11 14:50:10 +01005967 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005968 case 255:
5969 output_length = 32;
5970 break;
5971 case 448:
5972 output_length = 56;
5973 break;
5974 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005975 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005976 break;
5977 }
5978
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005980
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 if (*data == NULL) {
5982 return PSA_ERROR_INSUFFICIENT_MEMORY;
5983 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005984
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005986
Gilles Peskine449bd832023-01-11 14:50:10 +01005987 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005988 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005990
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005992 case 255:
5993 (*data)[0] &= 248;
5994 (*data)[31] &= 127;
5995 (*data)[31] |= 64;
5996 break;
5997 case 448:
5998 (*data)[0] &= 252;
5999 (*data)[55] |= 128;
6000 break;
6001 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006002 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006003 break;
6004 }
6005
6006 return status;
6007}
Valerio Setti86587ab2023-06-27 13:09:30 +02006008#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6009static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6010 psa_key_slot_t *slot, size_t bits,
6011 psa_key_derivation_operation_t *operation, uint8_t **data)
6012{
6013 (void) slot;
6014 (void) bits;
6015 (void) operation;
6016 (void) data;
6017 return PSA_ERROR_NOT_SUPPORTED;
6018}
6019
6020static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6021 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6022{
6023 (void) bits;
6024 (void) operation;
6025 (void) data;
6026 return PSA_ERROR_NOT_SUPPORTED;
6027}
6028#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6029#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006030
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006031static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006032 psa_key_slot_t *slot,
6033 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006034 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006035{
6036 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006037 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306038 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006039 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006040 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006041
Gilles Peskine449bd832023-01-11 14:50:10 +01006042 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6043 return PSA_ERROR_INVALID_ARGUMENT;
6044 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006045
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006046#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006047 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6049 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6050 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006051 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6053 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006054 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 }
6056 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006057 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6059 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006060 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006061 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006062 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006063 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006064#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006065 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 if (key_type_is_raw_bytes(slot->attr.type)) {
6067 if (bits % 8 != 0) {
6068 return PSA_ERROR_INVALID_ARGUMENT;
6069 }
6070 data = mbedtls_calloc(1, bytes);
6071 if (data == NULL) {
6072 return PSA_ERROR_INSUFFICIENT_MEMORY;
6073 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006074
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 status = psa_key_derivation_output_bytes(operation, data, bytes);
6076 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006077 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 }
David Brown12ca5032021-02-11 11:02:00 -07006079#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6081 psa_des_set_key_parity(data, bytes);
6082 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006083#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006084 } else {
6085 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006086 }
Ronald Crondd04d422020-11-28 15:14:42 +01006087
Ronald Cronbf33c932020-11-28 18:06:53 +01006088 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006089 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006090 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006091 };
6092
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6094 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6095 &storage_size);
6096 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306097 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 }
Archanad8a83dc2021-06-14 10:04:16 +05306099 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006100 status = psa_allocate_buffer_to_slot(slot, storage_size);
6101 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306102 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 }
Archanad8a83dc2021-06-14 10:04:16 +05306104
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 status = psa_driver_wrapper_import_key(&attributes,
6106 data, bytes,
6107 slot->key.data,
6108 slot->key.bytes,
6109 &slot->key.bytes, &bits);
6110 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006111 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006113
6114exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 mbedtls_free(data);
6116 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006117}
6118
Gilles Peskine449bd832023-01-11 14:50:10 +01006119psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6120 psa_key_derivation_operation_t *operation,
6121 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006122{
6123 psa_status_t status;
6124 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006125 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006126
Ronald Cron81709fc2020-11-14 12:10:32 +01006127 *key = MBEDTLS_SVC_KEY_ID_INIT;
6128
Gilles Peskine0f84d622019-09-12 19:03:13 +02006129 /* Reject any attempt to create a zero-length key so that we don't
6130 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006131 if (psa_get_key_bits(attributes) == 0) {
6132 return PSA_ERROR_INVALID_ARGUMENT;
6133 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006134
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 if (operation->alg == PSA_ALG_NONE) {
6136 return PSA_ERROR_BAD_STATE;
6137 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006138
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 if (!operation->can_output_key) {
6140 return PSA_ERROR_NOT_PERMITTED;
6141 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006142
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6144 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006145#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006146 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006147 /* Deriving a key in a secure element is not implemented yet. */
6148 status = PSA_ERROR_NOT_SUPPORTED;
6149 }
6150#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 if (status == PSA_SUCCESS) {
6152 status = psa_generate_derived_key_internal(slot,
6153 attributes->core.bits,
6154 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006155 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 if (status == PSA_SUCCESS) {
6157 status = psa_finish_key_creation(slot, driver, key);
6158 }
6159 if (status != PSA_SUCCESS) {
6160 psa_fail_key_creation(slot, driver);
6161 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006162
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006164}
6165
Gilles Peskineeab56e42018-07-12 17:12:33 +02006166
6167
6168/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006169/* Key derivation */
6170/****************************************************************/
6171
Steven Cooreman932ffb72021-02-15 12:14:32 +01006172#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006173static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006174{
6175#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6177 return 1;
6178 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006179#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006180#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6182 return 1;
6183 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006184#endif
6185#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6187 return 1;
6188 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006189#endif
6190#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6192 return 1;
6193 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006194#endif
6195#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006196 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6197 return 1;
6198 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006199#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006200#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6202 return 1;
6203 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006204#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306205#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6206 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6207 return 1;
6208 }
6209#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306210#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6211 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6212 return 1;
6213 }
6214#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006216}
6217
Gilles Peskine449bd832023-01-11 14:50:10 +01006218static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006219{
6220 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 psa_status_t status = psa_hash_setup(&operation, alg);
6222 psa_hash_abort(&operation);
6223 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006224}
6225
Gilles Peskine969c5d62019-01-16 15:53:06 +01006226static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006227 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006229{
Janos Follath5fe19732019-06-20 15:09:30 +01006230 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6231 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006233
Gilles Peskine969c5d62019-01-16 15:53:06 +01006234 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 if (!is_kdf_alg_supported(kdf_alg)) {
6236 return PSA_ERROR_NOT_SUPPORTED;
6237 }
John Durkop07cc04a2020-11-16 22:08:34 -08006238
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006239 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306240 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6242 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306243 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6244 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6245 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306246 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306247 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 if (hash_size == 0) {
6249 return PSA_ERROR_NOT_SUPPORTED;
6250 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006251
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006252 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6253 * we might fail later, which is somewhat unfriendly and potentially
6254 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 psa_status_t status = psa_hash_try_support(hash_alg);
6256 if (status != PSA_SUCCESS) {
6257 return status;
6258 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006259 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006260
Gilles Peskine449bd832023-01-11 14:50:10 +01006261 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6262 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6263 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6264 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006265 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006266#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006267 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006268 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6269 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006270 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006271 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006272#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6273 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 operation->capacity = 255 * hash_size;
6275 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006276}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006277
Gilles Peskine449bd832023-01-11 14:50:10 +01006278static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006279{
6280#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006281 if (alg == PSA_ALG_ECDH) {
6282 return PSA_SUCCESS;
6283 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006284#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006285#if defined(PSA_WANT_ALG_FFDH)
6286 if (alg == PSA_ALG_FFDH) {
6287 return PSA_SUCCESS;
6288 }
6289#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006290 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006292}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006293
6294static int psa_key_derivation_allows_free_form_secret_input(
6295 psa_algorithm_t kdf_alg)
6296{
6297#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6298 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6299 return 0;
6300 }
6301#endif
6302 (void) kdf_alg;
6303 return 1;
6304}
John Durkop07cc04a2020-11-16 22:08:34 -08006305#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006306
Gilles Peskine449bd832023-01-11 14:50:10 +01006307psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6308 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006309{
6310 psa_status_t status;
6311
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 if (operation->alg != 0) {
6313 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006314 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006315
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6317 return PSA_ERROR_INVALID_ARGUMENT;
6318 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6319#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6320 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6321 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6322 status = psa_key_agreement_try_support(ka_alg);
6323 if (status != PSA_SUCCESS) {
6324 return status;
6325 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006326 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6327 return PSA_ERROR_INVALID_ARGUMENT;
6328 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6330#else
6331 return PSA_ERROR_NOT_SUPPORTED;
6332#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6333 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6334#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6335 status = psa_key_derivation_setup_kdf(operation, alg);
6336#else
6337 return PSA_ERROR_NOT_SUPPORTED;
6338#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6339 } else {
6340 return PSA_ERROR_INVALID_ARGUMENT;
6341 }
6342
6343 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006344 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 }
6346 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006347}
6348
Przemek Stekiel69c46792022-06-10 12:59:51 +02006349#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006350static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6351 psa_algorithm_t kdf_alg,
6352 psa_key_derivation_step_t step,
6353 const uint8_t *data,
6354 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006355{
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006357 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006358 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006359 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006360#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6362 return PSA_ERROR_INVALID_ARGUMENT;
6363 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006364#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 if (hkdf->state != HKDF_STATE_INIT) {
6366 return PSA_ERROR_BAD_STATE;
6367 } else {
6368 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6369 hash_alg,
6370 data, data_length);
6371 if (status != PSA_SUCCESS) {
6372 return status;
6373 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006374 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006376 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006377 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006378#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006380 /* We shouldn't be in different state as HKDF_EXPAND only allows
6381 * two inputs: SECRET (this case) and INFO which does not modify
6382 * the state. It could happen only if the hkdf
6383 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 if (hkdf->state != HKDF_STATE_INIT) {
6385 return PSA_ERROR_BAD_STATE;
6386 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006387
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006388 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6390 return PSA_ERROR_INVALID_ARGUMENT;
6391 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006392
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 memcpy(hkdf->prk, data, data_length);
6394 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006395#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006396 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006397 /* HKDF: If no salt was provided, use an empty salt.
6398 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006400#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6402 return PSA_ERROR_BAD_STATE;
6403 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006404#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6406 hash_alg,
6407 NULL, 0);
6408 if (status != PSA_SUCCESS) {
6409 return status;
6410 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006411 hkdf->state = HKDF_STATE_STARTED;
6412 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 if (hkdf->state != HKDF_STATE_STARTED) {
6414 return PSA_ERROR_BAD_STATE;
6415 }
6416 status = psa_mac_update(&hkdf->hmac,
6417 data, data_length);
6418 if (status != PSA_SUCCESS) {
6419 return status;
6420 }
6421 status = psa_mac_sign_finish(&hkdf->hmac,
6422 hkdf->prk,
6423 sizeof(hkdf->prk),
6424 &data_length);
6425 if (status != PSA_SUCCESS) {
6426 return status;
6427 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006428 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006429
Gilles Peskine2b522db2019-04-12 15:11:49 +02006430 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006431 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006432#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006434 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006435 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006436 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006438#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006439 {
6440 /* Block 0 is empty, and the next block will be
6441 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006442 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006443 }
6444
Gilles Peskine449bd832023-01-11 14:50:10 +01006445 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006446 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006447#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6449 return PSA_ERROR_INVALID_ARGUMENT;
6450 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006451#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006452#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006453 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6454 hkdf->state == HKDF_STATE_INIT) {
6455 return PSA_ERROR_BAD_STATE;
6456 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006457#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006458 if (hkdf->state == HKDF_STATE_OUTPUT) {
6459 return PSA_ERROR_BAD_STATE;
6460 }
6461 if (hkdf->info_set) {
6462 return PSA_ERROR_BAD_STATE;
6463 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006464 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 if (data_length != 0) {
6466 hkdf->info = mbedtls_calloc(1, data_length);
6467 if (hkdf->info == NULL) {
6468 return PSA_ERROR_INSUFFICIENT_MEMORY;
6469 }
6470 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006471 }
6472 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006474 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006476 }
6477}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006478#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006479
John Durkop07cc04a2020-11-16 22:08:34 -08006480#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6481 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006482static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6483 const uint8_t *data,
6484 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006485{
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6487 return PSA_ERROR_BAD_STATE;
6488 }
Janos Follathf08e2652019-06-13 09:05:41 +01006489
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 if (data_length != 0) {
6491 prf->seed = mbedtls_calloc(1, data_length);
6492 if (prf->seed == NULL) {
6493 return PSA_ERROR_INSUFFICIENT_MEMORY;
6494 }
Janos Follathf08e2652019-06-13 09:05:41 +01006495
Gilles Peskine449bd832023-01-11 14:50:10 +01006496 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006497 prf->seed_length = data_length;
6498 }
Janos Follathf08e2652019-06-13 09:05:41 +01006499
Gilles Peskine43f958b2020-12-13 14:55:14 +01006500 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006501
Gilles Peskine449bd832023-01-11 14:50:10 +01006502 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006503}
6504
Gilles Peskine449bd832023-01-11 14:50:10 +01006505static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6506 const uint8_t *data,
6507 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006508{
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6510 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6511 return PSA_ERROR_BAD_STATE;
6512 }
Janos Follath81550542019-06-13 14:26:34 +01006513
Gilles Peskine449bd832023-01-11 14:50:10 +01006514 if (data_length != 0) {
6515 prf->secret = mbedtls_calloc(1, data_length);
6516 if (prf->secret == NULL) {
6517 return PSA_ERROR_INSUFFICIENT_MEMORY;
6518 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006519
Gilles Peskine449bd832023-01-11 14:50:10 +01006520 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006521 prf->secret_length = data_length;
6522 }
Janos Follath81550542019-06-13 14:26:34 +01006523
Gilles Peskine43f958b2020-12-13 14:55:14 +01006524 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006525
Gilles Peskine449bd832023-01-11 14:50:10 +01006526 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006527}
6528
Gilles Peskine449bd832023-01-11 14:50:10 +01006529static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6530 const uint8_t *data,
6531 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006532{
Gilles Peskine449bd832023-01-11 14:50:10 +01006533 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6534 return PSA_ERROR_BAD_STATE;
6535 }
Janos Follath63028dd2019-06-13 09:15:47 +01006536
Gilles Peskine449bd832023-01-11 14:50:10 +01006537 if (data_length != 0) {
6538 prf->label = mbedtls_calloc(1, data_length);
6539 if (prf->label == NULL) {
6540 return PSA_ERROR_INSUFFICIENT_MEMORY;
6541 }
Janos Follath63028dd2019-06-13 09:15:47 +01006542
Gilles Peskine449bd832023-01-11 14:50:10 +01006543 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006544 prf->label_length = data_length;
6545 }
Janos Follath63028dd2019-06-13 09:15:47 +01006546
Gilles Peskine43f958b2020-12-13 14:55:14 +01006547 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006548
Gilles Peskine449bd832023-01-11 14:50:10 +01006549 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006550}
6551
Gilles Peskine449bd832023-01-11 14:50:10 +01006552static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6553 psa_key_derivation_step_t step,
6554 const uint8_t *data,
6555 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006556{
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006558 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006560 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006561 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006562 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006563 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006564 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006565 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006566 }
6567}
John Durkop07cc04a2020-11-16 22:08:34 -08006568#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6569 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6570
6571#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6572static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6573 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006574 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006575 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006576{
6577 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006578 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6579 4 + data_length + prf->other_secret_length :
6580 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006581
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6583 return PSA_ERROR_INVALID_ARGUMENT;
6584 }
John Durkop07cc04a2020-11-16 22:08:34 -08006585
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 uint8_t *pms = mbedtls_calloc(1, pms_len);
6587 if (pms == NULL) {
6588 return PSA_ERROR_INSUFFICIENT_MEMORY;
6589 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006590 uint8_t *cur = pms;
6591
6592 /* pure-PSK:
6593 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006594 *
6595 * The premaster secret is formed as follows: if the PSK is N octets
6596 * long, concatenate a uint16 with the value N, N zero octets, a second
6597 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006598 *
6599 * mixed-PSK:
6600 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6601 * follows: concatenate a uint16 with the length of the other secret,
6602 * the other secret itself, uint16 with the length of PSK, and the
6603 * PSK itself.
6604 * For details please check:
6605 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6606 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6607 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006608 */
6609
Gilles Peskine449bd832023-01-11 14:50:10 +01006610 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6611 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6612 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6613 if (prf->other_secret_length != 0) {
6614 memcpy(cur, prf->other_secret, prf->other_secret_length);
6615 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006616 cur += prf->other_secret_length;
6617 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006618 } else {
6619 *cur++ = MBEDTLS_BYTE_1(data_length);
6620 *cur++ = MBEDTLS_BYTE_0(data_length);
6621 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006622 cur += data_length;
6623 }
6624
Gilles Peskine449bd832023-01-11 14:50:10 +01006625 *cur++ = MBEDTLS_BYTE_1(data_length);
6626 *cur++ = MBEDTLS_BYTE_0(data_length);
6627 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006628 cur += data_length;
6629
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006630 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006631
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006632 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006633 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006634}
Janos Follath6660f0e2019-06-17 08:44:03 +01006635
Przemek Stekiele47201b2022-04-19 13:53:28 +02006636static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6637 psa_tls12_prf_key_derivation_t *prf,
6638 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006639 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006640{
Gilles Peskine449bd832023-01-11 14:50:10 +01006641 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6642 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006643 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006644
6645 if (data_length != 0) {
6646 prf->other_secret = mbedtls_calloc(1, data_length);
6647 if (prf->other_secret == NULL) {
6648 return PSA_ERROR_INSUFFICIENT_MEMORY;
6649 }
6650
6651 memcpy(prf->other_secret, data, data_length);
6652 prf->other_secret_length = data_length;
6653 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006654 prf->other_secret_length = 0;
6655 }
6656
6657 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6658
Gilles Peskine449bd832023-01-11 14:50:10 +01006659 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006660}
6661
Janos Follath6660f0e2019-06-17 08:44:03 +01006662static psa_status_t psa_tls12_prf_psk_to_ms_input(
6663 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006664 psa_key_derivation_step_t step,
6665 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006666 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006667{
Gilles Peskine449bd832023-01-11 14:50:10 +01006668 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006669 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006670 return psa_tls12_prf_psk_to_ms_set_key(prf,
6671 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006672 break;
6673 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006674 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6675 data,
6676 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006677 break;
6678 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006679 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006680 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006681
Przemek Stekiele47201b2022-04-19 13:53:28 +02006682 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006683}
John Durkop07cc04a2020-11-16 22:08:34 -08006684#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006685
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006686#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6687static psa_status_t psa_tls12_ecjpake_to_pms_input(
6688 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006689 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006690 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006692{
Gilles Peskine449bd832023-01-11 14:50:10 +01006693 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6694 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6695 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006696 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006697
6698 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006699 if (data[0] != 0x04) {
6700 return PSA_ERROR_INVALID_ARGUMENT;
6701 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006702
6703 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006704 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006705
Gilles Peskine449bd832023-01-11 14:50:10 +01006706 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006707}
6708#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306709
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306710#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306711static psa_status_t psa_pbkdf2_set_input_cost(
6712 psa_pbkdf2_key_derivation_t *pbkdf2,
6713 psa_key_derivation_step_t step,
6714 uint64_t data)
6715{
6716 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6717 return PSA_ERROR_INVALID_ARGUMENT;
6718 }
6719
6720 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6721 return PSA_ERROR_BAD_STATE;
6722 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306723
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306724 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306725 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306726 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306727
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306728 if (data == 0) {
6729 return PSA_ERROR_INVALID_ARGUMENT;
6730 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306731
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306732 pbkdf2->input_cost = data;
6733 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6734
6735 return PSA_SUCCESS;
6736}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306737
6738static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6739 const uint8_t *data,
6740 size_t data_length)
6741{
Gilles Peskineead17662023-08-20 22:02:51 +02006742 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6743 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6744 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6745 /* Appending to existing salt. No state change. */
6746 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306747 return PSA_ERROR_BAD_STATE;
6748 }
6749
Gilles Peskineca57d782023-07-20 19:08:06 +02006750 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006751 /* Appending an empty string, nothing to do. */
6752 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306753 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306754
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306755 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6756 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306757 return PSA_ERROR_INSUFFICIENT_MEMORY;
6758 }
6759
Gilles Peskineead17662023-08-20 22:02:51 +02006760 if (pbkdf2->salt_length != 0) {
6761 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6762 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306763 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306764 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306765 mbedtls_free(pbkdf2->salt);
6766 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306767 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306768 return PSA_SUCCESS;
6769}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306770
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306771#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306772static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306773 const uint8_t *input,
6774 size_t input_len,
6775 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306776 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306777{
6778 psa_status_t status = PSA_SUCCESS;
6779 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6780 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306781 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306782 } else {
6783 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306784 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306785 }
6786 return status;
6787}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306788#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306789
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306790#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306791static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6792 size_t input_len,
6793 uint8_t *output,
6794 size_t *output_len)
6795{
6796 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306797 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306798 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306799 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306800 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6801 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6802 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306803 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306804 * mac_size as the driver function sets mac_output_length = mac_size
6805 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306806 status = psa_driver_wrapper_mac_compute(&attributes,
6807 zeros, sizeof(zeros),
6808 PSA_ALG_CMAC, input, input_len,
6809 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306810 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6811 128U,
6812 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306813 output_len);
6814 } else {
6815 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306816 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306817 }
6818 return status;
6819}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306820#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306821
6822static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306823 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306824 const uint8_t *data,
6825 size_t data_length)
6826{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306827 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306828 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6829 return PSA_ERROR_BAD_STATE;
6830 }
6831
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306832#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306833 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6834 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6835 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6836 pbkdf2->password,
6837 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306838 } else
6839#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6840#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6841 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306842 status = psa_pbkdf2_cmac_set_password(data, data_length,
6843 pbkdf2->password,
6844 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306845 } else
6846#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6847 {
6848 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306849 }
6850
6851 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6852
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306853 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306854}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306855
6856static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306857 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306858 psa_key_derivation_step_t step,
6859 const uint8_t *data,
6860 size_t data_length)
6861{
6862 switch (step) {
6863 case PSA_KEY_DERIVATION_INPUT_SALT:
6864 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6865 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306866 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306867 default:
6868 return PSA_ERROR_INVALID_ARGUMENT;
6869 }
6870}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306871#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306872
Gilles Peskineb8965192019-09-24 16:21:10 +02006873/** Check whether the given key type is acceptable for the given
6874 * input step of a key derivation.
6875 *
6876 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6877 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6878 * Both secret and non-secret inputs can alternatively have the type
6879 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6880 * that the input was passed as a buffer rather than via a key object.
6881 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006882static int psa_key_derivation_check_input_type(
6883 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006884 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006885{
Gilles Peskine449bd832023-01-11 14:50:10 +01006886 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006887 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006888 if (key_type == PSA_KEY_TYPE_DERIVE) {
6889 return PSA_SUCCESS;
6890 }
6891 if (key_type == PSA_KEY_TYPE_NONE) {
6892 return PSA_SUCCESS;
6893 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006894 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006895 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006896 if (key_type == PSA_KEY_TYPE_DERIVE) {
6897 return PSA_SUCCESS;
6898 }
6899 if (key_type == PSA_KEY_TYPE_NONE) {
6900 return PSA_SUCCESS;
6901 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006902 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006903 case PSA_KEY_DERIVATION_INPUT_LABEL:
6904 case PSA_KEY_DERIVATION_INPUT_SALT:
6905 case PSA_KEY_DERIVATION_INPUT_INFO:
6906 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006907 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6908 return PSA_SUCCESS;
6909 }
6910 if (key_type == PSA_KEY_TYPE_NONE) {
6911 return PSA_SUCCESS;
6912 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006913 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306914 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6915 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6916 return PSA_SUCCESS;
6917 }
6918 if (key_type == PSA_KEY_TYPE_DERIVE) {
6919 return PSA_SUCCESS;
6920 }
6921 if (key_type == PSA_KEY_TYPE_NONE) {
6922 return PSA_SUCCESS;
6923 }
6924 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006925 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006926 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006927}
6928
Janos Follathb80a94e2019-06-12 15:54:46 +01006929static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006930 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006931 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006932 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006933 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006934 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006935{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006936 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006937 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006938
Gilles Peskine449bd832023-01-11 14:50:10 +01006939 status = psa_key_derivation_check_input_type(step, key_type);
6940 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006941 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006942 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006943
Przemek Stekiel69c46792022-06-10 12:59:51 +02006944#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6946 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6947 step, data, data_length);
6948 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006949#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006950#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006951 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6952 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6953 step, data, data_length);
6954 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006955#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6956#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006957 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6958 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6959 step, data, data_length);
6960 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006961#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006962#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006963 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006964 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006965 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6966 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006967#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306968#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306969 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306970 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6971 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306972 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306973#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006974 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006975 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006976 (void) data;
6977 (void) data_length;
6978 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006979 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006980 }
6981
Gilles Peskine224b0d62019-09-23 18:13:17 +02006982exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006983 if (status != PSA_SUCCESS) {
6984 psa_key_derivation_abort(operation);
6985 }
6986 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006987}
6988
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306989static psa_status_t psa_key_derivation_input_integer_internal(
6990 psa_key_derivation_operation_t *operation,
6991 psa_key_derivation_step_t step,
6992 uint64_t value)
6993{
6994 psa_status_t status;
6995 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6996
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306997#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306998 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306999 status = psa_pbkdf2_set_input_cost(
7000 &operation->ctx.pbkdf2, step, value);
7001 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307002#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307003 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307004 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307005 (void) value;
7006 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307007 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307008 }
7009
7010 if (status != PSA_SUCCESS) {
7011 psa_key_derivation_abort(operation);
7012 }
7013 return status;
7014}
7015
Janos Follath51f4a0f2019-06-14 11:35:55 +01007016psa_status_t psa_key_derivation_input_bytes(
7017 psa_key_derivation_operation_t *operation,
7018 psa_key_derivation_step_t step,
7019 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007020 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007021{
Gilles Peskine449bd832023-01-11 14:50:10 +01007022 return psa_key_derivation_input_internal(operation, step,
7023 PSA_KEY_TYPE_NONE,
7024 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007025}
7026
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307027psa_status_t psa_key_derivation_input_integer(
7028 psa_key_derivation_operation_t *operation,
7029 psa_key_derivation_step_t step,
7030 uint64_t value)
7031{
7032 return psa_key_derivation_input_integer_internal(operation, step, value);
7033}
7034
Janos Follath51f4a0f2019-06-14 11:35:55 +01007035psa_status_t psa_key_derivation_input_key(
7036 psa_key_derivation_operation_t *operation,
7037 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007038 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007039{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007040 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007041 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007042 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007043
Ronald Cron5c522922020-11-14 16:35:34 +01007044 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007045 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7046 if (status != PSA_SUCCESS) {
7047 psa_key_derivation_abort(operation);
7048 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007049 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007050
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307051 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7052 * permission to output to a key object. */
7053 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7054 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007055 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007056 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007057
Gilles Peskine449bd832023-01-11 14:50:10 +01007058 status = psa_key_derivation_input_internal(operation,
7059 step, slot->attr.type,
7060 slot->key.data,
7061 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007062
Gilles Peskine449bd832023-01-11 14:50:10 +01007063 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007064
Gilles Peskine449bd832023-01-11 14:50:10 +01007065 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007066}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007067
7068
7069
7070/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007071/* Key agreement */
7072/****************************************************************/
7073
Gilles Peskine449bd832023-01-11 14:50:10 +01007074psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7075 const uint8_t *key_buffer,
7076 size_t key_buffer_size,
7077 psa_algorithm_t alg,
7078 const uint8_t *peer_key,
7079 size_t peer_key_length,
7080 uint8_t *shared_secret,
7081 size_t shared_secret_size,
7082 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007083{
Gilles Peskine449bd832023-01-11 14:50:10 +01007084 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007085#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007086 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007087 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7088 key_buffer_size, alg,
7089 peer_key, peer_key_length,
7090 shared_secret,
7091 shared_secret_size,
7092 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007093#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007094
7095#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7096 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007097 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007098 peer_key,
7099 peer_key_length,
7100 key_buffer,
7101 key_buffer_size,
7102 shared_secret,
7103 shared_secret_size,
7104 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007105#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7106
Gilles Peskine01d718c2018-09-18 12:01:02 +02007107 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007108 (void) attributes;
7109 (void) key_buffer;
7110 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007111 (void) peer_key;
7112 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007113 (void) shared_secret;
7114 (void) shared_secret_size;
7115 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007116 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007117 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007118}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007119
Aditya Deshpande17845b82022-10-13 17:21:01 +01007120/** Internal function for raw key agreement
7121 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007122 * to the driver's implementation if a driver is present.
7123 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007124 * (psa_key_agreement_raw_builtin).
7125 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007126static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7127 psa_key_slot_t *private_key,
7128 const uint8_t *peer_key,
7129 size_t peer_key_length,
7130 uint8_t *shared_secret,
7131 size_t shared_secret_size,
7132 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007133{
Gilles Peskine449bd832023-01-11 14:50:10 +01007134 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7135 return PSA_ERROR_NOT_SUPPORTED;
7136 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007137
7138 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007139 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007140 };
7141
Gilles Peskine449bd832023-01-11 14:50:10 +01007142 return psa_driver_wrapper_key_agreement(&attributes,
7143 private_key->key.data,
7144 private_key->key.bytes, alg,
7145 peer_key, peer_key_length,
7146 shared_secret,
7147 shared_secret_size,
7148 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007149}
7150
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007151/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007152 * to potentially free embedded data structures and wipe confidential data.
7153 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007154static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7155 psa_key_derivation_step_t step,
7156 psa_key_slot_t *private_key,
7157 const uint8_t *peer_key,
7158 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007159{
7160 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007161 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007162 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007163 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007164
7165 /* Step 1: run the secret agreement algorithm to generate the shared
7166 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007167 status = psa_key_agreement_raw_internal(ka_alg,
7168 private_key,
7169 peer_key, peer_key_length,
7170 shared_secret,
7171 sizeof(shared_secret),
7172 &shared_secret_length);
7173 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007174 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007175 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007176
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007177 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007178 * the shared secret. A shared secret is permitted wherever a key
7179 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007180 status = psa_key_derivation_input_internal(operation, step,
7181 PSA_KEY_TYPE_DERIVE,
7182 shared_secret,
7183 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007184exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007185 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7186 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007187}
7188
Gilles Peskine449bd832023-01-11 14:50:10 +01007189psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7190 psa_key_derivation_step_t step,
7191 mbedtls_svc_key_id_t private_key,
7192 const uint8_t *peer_key,
7193 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007194{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007195 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007196 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007197 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007198
Gilles Peskine449bd832023-01-11 14:50:10 +01007199 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7200 return PSA_ERROR_INVALID_ARGUMENT;
7201 }
Ronald Cron5c522922020-11-14 16:35:34 +01007202 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007203 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7204 if (status != PSA_SUCCESS) {
7205 return status;
7206 }
7207 status = psa_key_agreement_internal(operation, step,
7208 slot,
7209 peer_key, peer_key_length);
7210 if (status != PSA_SUCCESS) {
7211 psa_key_derivation_abort(operation);
7212 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007213 /* If a private key has been added as SECRET, we allow the derived
7214 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007215 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007216 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007217 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007218 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007219
Gilles Peskine449bd832023-01-11 14:50:10 +01007220 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007221
Gilles Peskine449bd832023-01-11 14:50:10 +01007222 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007223}
7224
Gilles Peskine449bd832023-01-11 14:50:10 +01007225psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7226 mbedtls_svc_key_id_t private_key,
7227 const uint8_t *peer_key,
7228 size_t peer_key_length,
7229 uint8_t *output,
7230 size_t output_size,
7231 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007232{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007233 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007234 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007235 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007236 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007237
Gilles Peskine449bd832023-01-11 14:50:10 +01007238 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007239 status = PSA_ERROR_INVALID_ARGUMENT;
7240 goto exit;
7241 }
Ronald Cron5c522922020-11-14 16:35:34 +01007242 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007243 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7244 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007245 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007246 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007247
Gilles Peskine3e561302022-04-14 00:17:15 +02007248 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7249 * for the output size. The PSA specification only guarantees that this
7250 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7251 * but it might be nice to allow smaller buffers if the output fits.
7252 * At the time of writing this comment, with only ECDH implemented,
7253 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7254 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7255 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007256 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007257 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7258 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007259 status = PSA_ERROR_BUFFER_TOO_SMALL;
7260 goto exit;
7261 }
7262
Gilles Peskine449bd832023-01-11 14:50:10 +01007263 status = psa_key_agreement_raw_internal(alg, slot,
7264 peer_key, peer_key_length,
7265 output, output_size,
7266 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007267
7268exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007269 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007270 /* If an error happens and is not handled properly, the output
7271 * may be used as a key to protect sensitive data. Arrange for such
7272 * a key to be random, which is likely to result in decryption or
7273 * verification errors. This is better than filling the buffer with
7274 * some constant data such as zeros, which would result in the data
7275 * being protected with a reproducible, easily knowable key.
7276 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007277 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007278 *output_length = output_size;
7279 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007280
Gilles Peskine449bd832023-01-11 14:50:10 +01007281 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007282
Gilles Peskine449bd832023-01-11 14:50:10 +01007283 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007284}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007285
7286
Gilles Peskine30524eb2020-11-13 17:02:26 +01007287
Gilles Peskine86a440b2018-11-12 18:39:40 +01007288/****************************************************************/
7289/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007290/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007291
Gilles Peskine672a7712023-04-28 21:00:28 +02007292#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7293#include "entropy_poll.h"
7294#endif
7295
Gilles Peskine30524eb2020-11-13 17:02:26 +01007296/** Initialize the PSA random generator.
7297 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007298static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007299{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007300#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007301 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007302#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7303
Gilles Peskine30524eb2020-11-13 17:02:26 +01007304 /* Set default configuration if
7305 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007306 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007307 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007308 }
7309 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007310 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007311 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007312
Gilles Peskine449bd832023-01-11 14:50:10 +01007313 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007314#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7315 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7316 /* The PSA entropy injection feature depends on using NV seed as an entropy
7317 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007318 mbedtls_entropy_add_source(&rng->entropy,
7319 mbedtls_nv_seed_poll, NULL,
7320 MBEDTLS_ENTROPY_BLOCK_SIZE,
7321 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007322#endif
7323
Gilles Peskine449bd832023-01-11 14:50:10 +01007324 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007325#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007326}
7327
7328/** Deinitialize the PSA random generator.
7329 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007330static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007331{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007332#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007333 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007334#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007335 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7336 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007337#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007338}
7339
7340/** Seed the PSA random generator.
7341 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007342static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007343{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007344#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7345 /* Do nothing: the external RNG seeds itself. */
7346 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007347 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007348#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007349 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007350 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7351 drbg_seed, sizeof(drbg_seed) - 1);
7352 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007353#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007354}
7355
Gilles Peskine449bd832023-01-11 14:50:10 +01007356psa_status_t psa_generate_random(uint8_t *output,
7357 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007358{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007359 GUARD_MODULE_INITIALIZED;
7360
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007361#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7362
7363 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007364 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7365 output, output_size,
7366 &output_length);
7367 if (status != PSA_SUCCESS) {
7368 return status;
7369 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007370 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007371 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007372 if (output_length != output_size) {
7373 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7374 }
7375 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007376
7377#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7378
Gilles Peskine449bd832023-01-11 14:50:10 +01007379 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007380 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007381 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7382 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7383 output_size);
7384 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7385 output, request_size);
7386 if (ret != 0) {
7387 return mbedtls_to_psa_error(ret);
7388 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007389 output_size -= request_size;
7390 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007391 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007392 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007393#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007394}
7395
Gilles Peskine8814fc42020-12-14 15:33:44 +01007396/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007397 *
7398 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7399 * `psa_generate_random(...)`. The state parameter is ignored since the
7400 * PSA API doesn't support passing an explicit state.
7401 *
7402 * In the non-external case, psa_generate_random() calls an
7403 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7404 * and semantics as mbedtls_psa_get_random(). As an optimization,
7405 * instead of doing this back-and-forth between the PSA API and the
7406 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7407 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007408 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007409#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7410int mbedtls_psa_get_random(void *p_rng,
7411 unsigned char *output,
7412 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007413{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007414 /* This function takes a pointer to the RNG state because that's what
7415 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7416 * its own state internally and doesn't let the caller access that state.
7417 * So we just ignore the state parameter, and in practice we'll pass
7418 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007419 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007420 psa_status_t status = psa_generate_random(output, output_size);
7421 if (status == PSA_SUCCESS) {
7422 return 0;
7423 } else {
7424 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7425 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007426}
7427#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7428
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007429#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007430psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7431 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007432{
Gilles Peskine449bd832023-01-11 14:50:10 +01007433 if (global_data.initialized) {
7434 return PSA_ERROR_NOT_PERMITTED;
7435 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007436
Gilles Peskine449bd832023-01-11 14:50:10 +01007437 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7438 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7439 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7440 return PSA_ERROR_INVALID_ARGUMENT;
7441 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007442
Gilles Peskine449bd832023-01-11 14:50:10 +01007443 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007444}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007445#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007446
Ronald Cron3772afe2021-02-08 16:10:05 +01007447/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007448 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007449 * \param type The key type
7450 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007451 *
7452 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007453 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007454 * \retval #PSA_ERROR_INVALID_ARGUMENT
7455 * The size in bits of the key is not valid.
7456 * \retval #PSA_ERROR_NOT_SUPPORTED
7457 * The type and/or the size in bits of the key or the combination of
7458 * the two is not supported.
7459 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007460static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007461 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007462{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007463 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007464
Gilles Peskine449bd832023-01-11 14:50:10 +01007465 if (key_type_is_raw_bytes(type)) {
7466 status = psa_validate_unstructured_key_bit_size(type, bits);
7467 if (status != PSA_SUCCESS) {
7468 return status;
7469 }
7470 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007471#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007472 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7473 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7474 return PSA_ERROR_NOT_SUPPORTED;
7475 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007476 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007477 return PSA_ERROR_NOT_SUPPORTED;
7478 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007479
Ronald Cron01b2aba2020-10-05 09:42:02 +02007480 /* Accept only byte-aligned keys, for the same reasons as
7481 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007482 if (bits % 8 != 0) {
7483 return PSA_ERROR_NOT_SUPPORTED;
7484 }
7485 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007486#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007487
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007488#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007489 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007490 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007491 return PSA_SUCCESS;
7492 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007493#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007494
Valerio Settia55f0422023-07-10 15:34:41 +02007495#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007496 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007497 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007498 return PSA_ERROR_NOT_SUPPORTED;
7499 }
7500 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007501#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007502 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007503 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007504 }
7505
Gilles Peskine449bd832023-01-11 14:50:10 +01007506 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007507}
7508
Ronald Cron55ed0592020-10-05 10:30:40 +02007509psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007510 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007511 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007512{
7513 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007514 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007515
Gilles Peskine449bd832023-01-11 14:50:10 +01007516 if ((attributes->domain_parameters == NULL) &&
7517 (attributes->domain_parameters_size != 0)) {
7518 return PSA_ERROR_INVALID_ARGUMENT;
7519 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007520
Gilles Peskine449bd832023-01-11 14:50:10 +01007521 if (key_type_is_raw_bytes(type)) {
7522 status = psa_generate_random(key_buffer, key_buffer_size);
7523 if (status != PSA_SUCCESS) {
7524 return status;
7525 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007526
David Brown12ca5032021-02-11 11:02:00 -07007527#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007528 if (type == PSA_KEY_TYPE_DES) {
7529 psa_des_set_key_parity(key_buffer, key_buffer_size);
7530 }
David Brown8107e312021-02-12 08:08:46 -07007531#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007532 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007533
Valerio Setti76df8c12023-07-11 14:11:28 +02007534#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007535 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7536 return mbedtls_psa_rsa_generate_key(attributes,
7537 key_buffer,
7538 key_buffer_size,
7539 key_buffer_length);
7540 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007541#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007542
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007543#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007544 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7545 return mbedtls_psa_ecp_generate_key(attributes,
7546 key_buffer,
7547 key_buffer_size,
7548 key_buffer_length);
7549 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007550#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007551
Valerio Settia55f0422023-07-10 15:34:41 +02007552#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007553 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7554 return mbedtls_psa_ffdh_generate_key(attributes,
7555 key_buffer,
7556 key_buffer_size,
7557 key_buffer_length);
7558 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007559#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007560 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007561 (void) key_buffer_length;
7562 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007563 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007564
Gilles Peskine449bd832023-01-11 14:50:10 +01007565 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007566}
Darryl Green0c6575a2018-11-07 16:05:30 +00007567
Gilles Peskine449bd832023-01-11 14:50:10 +01007568psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7569 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007570{
7571 psa_status_t status;
7572 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007573 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007574 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007575
Ronald Cron81709fc2020-11-14 12:10:32 +01007576 *key = MBEDTLS_SVC_KEY_ID_INIT;
7577
Gilles Peskine0f84d622019-09-12 19:03:13 +02007578 /* Reject any attempt to create a zero-length key so that we don't
7579 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007580 if (psa_get_key_bits(attributes) == 0) {
7581 return PSA_ERROR_INVALID_ARGUMENT;
7582 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007583
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007584 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007585 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7586 return PSA_ERROR_INVALID_ARGUMENT;
7587 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007588
Gilles Peskine449bd832023-01-11 14:50:10 +01007589 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7590 &slot, &driver);
7591 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007592 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007593 }
Gilles Peskine11792082019-08-06 18:36:36 +02007594
Ronald Cron977c2472020-10-13 08:32:21 +02007595 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307596 * storage ( thus not in the case of generating a key in a secure element
7597 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7598 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007599 if (slot->key.data == NULL) {
7600 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7601 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007602 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 attributes->core.type, attributes->core.bits);
7604 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007605 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007606 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007607
7608 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007609 attributes->core.type,
7610 attributes->core.bits);
7611 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007612 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007613 attributes, &key_buffer_size);
7614 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007615 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007616 }
Ronald Cron977c2472020-10-13 08:32:21 +02007617 }
Ronald Cron977c2472020-10-13 08:32:21 +02007618
Gilles Peskine449bd832023-01-11 14:50:10 +01007619 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7620 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007621 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007622 }
Ronald Cron977c2472020-10-13 08:32:21 +02007623 }
7624
Gilles Peskine449bd832023-01-11 14:50:10 +01007625 status = psa_driver_wrapper_generate_key(attributes,
7626 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007627
Gilles Peskine449bd832023-01-11 14:50:10 +01007628 if (status != PSA_SUCCESS) {
7629 psa_remove_key_data_from_memory(slot);
7630 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007631
Gilles Peskine11792082019-08-06 18:36:36 +02007632exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007633 if (status == PSA_SUCCESS) {
7634 status = psa_finish_key_creation(slot, driver, key);
7635 }
7636 if (status != PSA_SUCCESS) {
7637 psa_fail_key_creation(slot, driver);
7638 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007639
Gilles Peskine449bd832023-01-11 14:50:10 +01007640 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007641}
7642
Gilles Peskinee59236f2018-01-27 23:32:46 +01007643/****************************************************************/
7644/* Module setup */
7645/****************************************************************/
7646
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007647#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007648psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007649 void (* entropy_init)(mbedtls_entropy_context *ctx),
7650 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007651{
Gilles Peskine449bd832023-01-11 14:50:10 +01007652 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7653 return PSA_ERROR_BAD_STATE;
7654 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007655 global_data.rng.entropy_init = entropy_init;
7656 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007657 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007658}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007659#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007660
Gilles Peskine449bd832023-01-11 14:50:10 +01007661void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007662{
Gilles Peskine449bd832023-01-11 14:50:10 +01007663 psa_wipe_all_key_slots();
7664 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7665 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007666 }
7667 /* Wipe all remaining data, including configuration.
7668 * In particular, this sets all state indicator to the value
7669 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007670 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007671
7672 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007673 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007674}
7675
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007676#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7677/** Recover a transaction that was interrupted by a power failure.
7678 *
7679 * This function is called during initialization, before psa_crypto_init()
7680 * returns. If this function returns a failure status, the initialization
7681 * fails.
7682 */
7683static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007684 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007685{
Gilles Peskine449bd832023-01-11 14:50:10 +01007686 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007687 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7688 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007689 /* TODO - fall through to the failure case until this
7690 * is implemented.
7691 * https://github.com/ARMmbed/mbed-crypto/issues/218
7692 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007693 default:
7694 /* We found an unsupported transaction in the storage.
7695 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007696 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007697 }
7698}
7699#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7700
Gilles Peskine449bd832023-01-11 14:50:10 +01007701psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007702{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007703 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007704
Gilles Peskinec6b69072018-11-20 21:42:52 +01007705 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007706 if (global_data.initialized != 0) {
7707 return PSA_SUCCESS;
7708 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007709
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007710 /* Init drivers */
7711 status = psa_driver_wrapper_init();
7712 if (status != PSA_SUCCESS) {
7713 goto exit;
7714 }
7715 global_data.drivers_initialized = 1;
7716
Gilles Peskine30524eb2020-11-13 17:02:26 +01007717 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007718 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007719 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007720 status = mbedtls_psa_random_seed(&global_data.rng);
7721 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007722 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007723 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007724 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007725
Gilles Peskine449bd832023-01-11 14:50:10 +01007726 status = psa_initialize_key_slots();
7727 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007728 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007729 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007730
Gilles Peskine4b734222019-07-24 15:56:31 +02007731#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007732 status = psa_crypto_load_transaction();
7733 if (status == PSA_SUCCESS) {
7734 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7735 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007736 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007737 }
7738 status = psa_crypto_stop_transaction();
7739 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007740 /* There's no transaction to complete. It's all good. */
7741 status = PSA_SUCCESS;
7742 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007743#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007744
Gilles Peskinec6b69072018-11-20 21:42:52 +01007745 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007746 global_data.initialized = 1;
7747
7748exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007749 if (status != PSA_SUCCESS) {
7750 mbedtls_psa_crypto_free();
7751 }
7752 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007753}
7754
Yanray Wangffc3c482023-07-11 12:01:04 +08007755#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007756psa_status_t psa_crypto_driver_pake_get_password_len(
7757 const psa_crypto_driver_pake_inputs_t *inputs,
7758 size_t *password_len)
7759{
7760 if (inputs->password_len == 0) {
7761 return PSA_ERROR_BAD_STATE;
7762 }
7763
7764 *password_len = inputs->password_len;
7765
7766 return PSA_SUCCESS;
7767}
7768
7769psa_status_t psa_crypto_driver_pake_get_password(
7770 const psa_crypto_driver_pake_inputs_t *inputs,
7771 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7772{
7773 if (inputs->password_len == 0) {
7774 return PSA_ERROR_BAD_STATE;
7775 }
7776
7777 if (buffer_size < inputs->password_len) {
7778 return PSA_ERROR_BUFFER_TOO_SMALL;
7779 }
7780
7781 memcpy(buffer, inputs->password, inputs->password_len);
7782 *buffer_length = inputs->password_len;
7783
7784 return PSA_SUCCESS;
7785}
7786
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007787psa_status_t psa_crypto_driver_pake_get_user_len(
7788 const psa_crypto_driver_pake_inputs_t *inputs,
7789 size_t *user_len)
7790{
7791 if (inputs->user_len == 0) {
7792 return PSA_ERROR_BAD_STATE;
7793 }
7794
7795 *user_len = inputs->user_len;
7796
7797 return PSA_SUCCESS;
7798}
7799
7800psa_status_t psa_crypto_driver_pake_get_user(
7801 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007802 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007803{
7804 if (inputs->user_len == 0) {
7805 return PSA_ERROR_BAD_STATE;
7806 }
7807
Przemek Stekielc0e62502023-03-14 11:49:36 +01007808 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007809 return PSA_ERROR_BUFFER_TOO_SMALL;
7810 }
7811
Przemek Stekielc0e62502023-03-14 11:49:36 +01007812 memcpy(user_id, inputs->user, inputs->user_len);
7813 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007814
7815 return PSA_SUCCESS;
7816}
7817
7818psa_status_t psa_crypto_driver_pake_get_peer_len(
7819 const psa_crypto_driver_pake_inputs_t *inputs,
7820 size_t *peer_len)
7821{
7822 if (inputs->peer_len == 0) {
7823 return PSA_ERROR_BAD_STATE;
7824 }
7825
7826 *peer_len = inputs->peer_len;
7827
7828 return PSA_SUCCESS;
7829}
7830
7831psa_status_t psa_crypto_driver_pake_get_peer(
7832 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007833 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007834{
7835 if (inputs->peer_len == 0) {
7836 return PSA_ERROR_BAD_STATE;
7837 }
7838
Przemek Stekielc0e62502023-03-14 11:49:36 +01007839 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007840 return PSA_ERROR_BUFFER_TOO_SMALL;
7841 }
7842
Przemek Stekielc0e62502023-03-14 11:49:36 +01007843 memcpy(peer_id, inputs->peer, inputs->peer_len);
7844 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007845
7846 return PSA_SUCCESS;
7847}
7848
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007849psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7850 const psa_crypto_driver_pake_inputs_t *inputs,
7851 psa_pake_cipher_suite_t *cipher_suite)
7852{
7853 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7854 return PSA_ERROR_BAD_STATE;
7855 }
7856
7857 *cipher_suite = inputs->cipher_suite;
7858
7859 return PSA_SUCCESS;
7860}
7861
Neil Armstronga7d08c32022-06-01 18:21:20 +02007862psa_status_t psa_pake_setup(
7863 psa_pake_operation_t *operation,
7864 const psa_pake_cipher_suite_t *cipher_suite)
7865{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007866 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007867
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007868 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007869 status = PSA_ERROR_BAD_STATE;
7870 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007871 }
7872
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007873 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007874 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007875 status = PSA_ERROR_INVALID_ARGUMENT;
7876 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007877 }
7878
Przemek Stekiel51eac532022-12-07 11:04:51 +01007879 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7880
Przemek Stekiele12ed362022-12-21 12:54:46 +01007881 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007882 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7883 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007884 operation->data.inputs.cipher_suite = *cipher_suite;
7885
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007886#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007887 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007888 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007889 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007890
David Horstmann16f01512023-06-14 17:21:07 +01007891 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007892 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007893 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007894#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007895 {
7896 status = PSA_ERROR_NOT_SUPPORTED;
7897 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007898 }
7899
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007900 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7901
Przemek Stekiel51eac532022-12-07 11:04:51 +01007902 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007903exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007904 psa_pake_abort(operation);
7905 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007906}
7907
7908psa_status_t psa_pake_set_password_key(
7909 psa_pake_operation_t *operation,
7910 mbedtls_svc_key_id_t password)
7911{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007912 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007913 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7914 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007915 psa_key_attributes_t attributes;
7916 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007917
Przemek Stekiel51eac532022-12-07 11:04:51 +01007918 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007919 status = PSA_ERROR_BAD_STATE;
7920 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007921 }
7922
Przemek Stekiel6c764412022-11-22 14:05:12 +01007923 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7924 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007925 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007926 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007927 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007928 }
7929
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007930 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007931 .core = slot->attr
7932 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007933
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007934 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007935
Przemek Stekiel51eac532022-12-07 11:04:51 +01007936 if (type != PSA_KEY_TYPE_PASSWORD &&
7937 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7938 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007939 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007940 }
7941
Przemek Stekiel51eac532022-12-07 11:04:51 +01007942 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7943 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007944 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007945 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007946 }
7947
7948 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7949 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007950 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007951exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007952 if (status != PSA_SUCCESS) {
7953 psa_pake_abort(operation);
7954 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007955 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007956 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007957}
7958
7959psa_status_t psa_pake_set_user(
7960 psa_pake_operation_t *operation,
7961 const uint8_t *user_id,
7962 size_t user_id_len)
7963{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007964 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007965
7966 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007967 status = PSA_ERROR_BAD_STATE;
7968 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007969 }
7970
Przemek Stekiel51eac532022-12-07 11:04:51 +01007971 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007972 status = PSA_ERROR_INVALID_ARGUMENT;
7973 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007974 }
7975
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007976 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007977 status = PSA_ERROR_BAD_STATE;
7978 goto exit;
7979 }
7980
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007981 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7982 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007983 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7984 goto exit;
7985 }
7986
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007987 memcpy(operation->data.inputs.user, user_id, user_id_len);
7988 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007989
7990 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007991exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007992 psa_pake_abort(operation);
7993 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007994}
7995
7996psa_status_t psa_pake_set_peer(
7997 psa_pake_operation_t *operation,
7998 const uint8_t *peer_id,
7999 size_t peer_id_len)
8000{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008001 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008002
8003 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008004 status = PSA_ERROR_BAD_STATE;
8005 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008006 }
8007
Przemek Stekiel51eac532022-12-07 11:04:51 +01008008 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008009 status = PSA_ERROR_INVALID_ARGUMENT;
8010 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008011 }
8012
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008013 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008014 status = PSA_ERROR_BAD_STATE;
8015 goto exit;
8016 }
8017
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008018 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8019 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008020 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8021 goto exit;
8022 }
8023
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008024 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8025 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008026
8027 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008028exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008029 psa_pake_abort(operation);
8030 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008031}
8032
8033psa_status_t psa_pake_set_role(
8034 psa_pake_operation_t *operation,
8035 psa_pake_role_t role)
8036{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008037 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008038
Przemek Stekiel51eac532022-12-07 11:04:51 +01008039 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008040 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008041 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008042 }
8043
Przemek Stekiel09104b82023-03-06 14:11:51 +01008044 switch (operation->alg) {
8045#if defined(PSA_WANT_ALG_JPAKE)
8046 case PSA_ALG_JPAKE:
8047 if (role == PSA_PAKE_ROLE_NONE) {
8048 return PSA_SUCCESS;
8049 }
8050 status = PSA_ERROR_INVALID_ARGUMENT;
8051 break;
8052#endif
8053 default:
8054 (void) role;
8055 status = PSA_ERROR_NOT_SUPPORTED;
8056 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008057 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008058exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008059 psa_pake_abort(operation);
8060 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008061}
8062
David Horstmanne7f21e62023-05-12 18:17:21 +01008063/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008064#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008065static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008066 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008067{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008068 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008069 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008070 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008071
David Horstmann024e5c52023-06-14 15:48:21 +01008072 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008073 is_x1 = (stage->outputs < 1);
8074 } else {
8075 is_x1 = (stage->inputs < 1);
8076 }
8077
David Horstmann74a3d8c2023-06-14 18:28:19 +01008078 key_share_step = is_x1 ?
8079 PSA_JPAKE_X1_STEP_KEY_SHARE :
8080 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008081 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008082 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8083 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8084 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8085 } else {
8086 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008087 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008088 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008089}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008090#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008091
Przemek Stekiel2797d372022-12-22 11:19:22 +01008092static psa_status_t psa_pake_complete_inputs(
8093 psa_pake_operation_t *operation)
8094{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008095 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008096 /* Create copy of the inputs on stack as inputs share memory
8097 with the driver context which will be setup by the driver. */
8098 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008099
Przemek Stekielfde11282023-03-13 16:06:09 +01008100 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008101 return PSA_ERROR_BAD_STATE;
8102 }
8103
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008104 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008105 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8106 return PSA_ERROR_BAD_STATE;
8107 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008108 }
8109
Przemek Stekiel18620a32023-01-17 16:34:52 +01008110 /* Clear driver context */
8111 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8112
8113 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008114
8115 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008116 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008117
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008118 /* User and peer are translated to role. */
8119 mbedtls_free(inputs.user);
8120 mbedtls_free(inputs.peer);
8121
Przemek Stekiel2797d372022-12-22 11:19:22 +01008122 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008123#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008124 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008125 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008126 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008127#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008128 {
8129 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008130 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008131 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008132 return status;
8133}
8134
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008135#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008136static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008137 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008138 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008139 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008140{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008141 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8142 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8143 step != PSA_PAKE_STEP_ZK_PROOF) {
8144 return PSA_ERROR_INVALID_ARGUMENT;
8145 }
8146
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008147 psa_jpake_computation_stage_t *computation_stage =
8148 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008149
David Horstmann5da95602023-06-08 15:37:12 +01008150 if (computation_stage->round != PSA_JPAKE_FIRST &&
8151 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008152 return PSA_ERROR_BAD_STATE;
8153 }
8154
David Horstmanne7f21e62023-05-12 18:17:21 +01008155 /* Check that the step we are given is the one we were expecting */
8156 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008157 return PSA_ERROR_BAD_STATE;
8158 }
8159
David Horstmanne7f21e62023-05-12 18:17:21 +01008160 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8161 computation_stage->inputs == 0 &&
8162 computation_stage->outputs == 0) {
8163 /* Start of the round, so function decides whether we are inputting
8164 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008165 computation_stage->io_mode = io_mode;
8166 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008167 /* Middle of the round so the mode we are in must match the function
8168 * called by the user */
8169 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008170 }
8171
8172 return PSA_SUCCESS;
8173}
8174
David Horstmanne7f21e62023-05-12 18:17:21 +01008175static psa_status_t psa_jpake_epilogue(
8176 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008177 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008178{
David Horstmanne7f21e62023-05-12 18:17:21 +01008179 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008180 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008181
David Horstmanne7f21e62023-05-12 18:17:21 +01008182 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8183 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008184 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008185 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008186 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008187 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008188 }
8189 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008190 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008191 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008192 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008193 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008194 }
8195 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008196 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8197 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008198 /* End of a round, move to the next round */
8199 stage->inputs = 0;
8200 stage->outputs = 0;
8201 stage->round++;
8202 }
8203 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008204 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008205 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008206 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008207 return PSA_SUCCESS;
8208}
David Horstmanne7f21e62023-05-12 18:17:21 +01008209
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008210#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008211
Neil Armstronga7d08c32022-06-01 18:21:20 +02008212psa_status_t psa_pake_output(
8213 psa_pake_operation_t *operation,
8214 psa_pake_step_t step,
8215 uint8_t *output,
8216 size_t output_size,
8217 size_t *output_length)
8218{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008219 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008220 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008221 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008222
8223 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008224 status = psa_pake_complete_inputs(operation);
8225 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008226 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008227 }
8228 }
8229
8230 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008231 status = PSA_ERROR_BAD_STATE;
8232 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008233 }
8234
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008235 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008236 status = PSA_ERROR_INVALID_ARGUMENT;
8237 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008238 }
8239
Przemek Stekiele12ed362022-12-21 12:54:46 +01008240 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008241#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008242 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008243 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008244 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008245 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008246 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008247 driver_step = convert_jpake_computation_stage_to_driver_step(
8248 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008249 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008250#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008251 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008252 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008253 status = PSA_ERROR_NOT_SUPPORTED;
8254 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008255 }
8256
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008257 status = psa_driver_wrapper_pake_output(operation, driver_step,
8258 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008259
8260 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008261 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008262 }
8263
8264 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008265#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008266 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008267 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008268 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008269 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008270 }
8271 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008272#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008273 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008274 status = PSA_ERROR_NOT_SUPPORTED;
8275 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008276 }
8277
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008278 return PSA_SUCCESS;
8279exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008280 psa_pake_abort(operation);
8281 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008282}
8283
8284psa_status_t psa_pake_input(
8285 psa_pake_operation_t *operation,
8286 psa_pake_step_t step,
8287 const uint8_t *input,
8288 size_t input_length)
8289{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008290 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008291 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008292 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8293 operation->primitive,
8294 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008295
8296 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008297 status = psa_pake_complete_inputs(operation);
8298 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008299 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008300 }
8301 }
8302
8303 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008304 status = PSA_ERROR_BAD_STATE;
8305 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008306 }
8307
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008308 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008309 status = PSA_ERROR_INVALID_ARGUMENT;
8310 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008311 }
8312
Przemek Stekiele12ed362022-12-21 12:54:46 +01008313 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008314#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008315 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008316 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008317 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008318 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008319 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008320 driver_step = convert_jpake_computation_stage_to_driver_step(
8321 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008322 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008323#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008324 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008325 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008326 status = PSA_ERROR_NOT_SUPPORTED;
8327 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008328 }
8329
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008330 status = psa_driver_wrapper_pake_input(operation, driver_step,
8331 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008332
8333 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008334 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008335 }
8336
8337 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008338#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008339 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008340 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008341 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008342 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008343 }
8344 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008345#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008346 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008347 status = PSA_ERROR_NOT_SUPPORTED;
8348 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008349 }
8350
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008351 return PSA_SUCCESS;
8352exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008353 psa_pake_abort(operation);
8354 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008355}
8356
8357psa_status_t psa_pake_get_implicit_key(
8358 psa_pake_operation_t *operation,
8359 psa_key_derivation_operation_t *output)
8360{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008361 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008362 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008363 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008364 size_t shared_key_len = 0;
8365
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008366 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008367 status = PSA_ERROR_BAD_STATE;
8368 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008369 }
8370
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008371#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008372 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008373 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008374 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008375 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008376 status = PSA_ERROR_BAD_STATE;
8377 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008378 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008379 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008380#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008381 {
8382 status = PSA_ERROR_NOT_SUPPORTED;
8383 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008384 }
8385
Przemek Stekiel0c781802022-11-29 14:53:13 +01008386 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8387 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008388 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008389 &shared_key_len);
8390
8391 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008392 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008393 }
8394
8395 status = psa_key_derivation_input_bytes(output,
8396 PSA_KEY_DERIVATION_INPUT_SECRET,
8397 shared_key,
8398 shared_key_len);
8399
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008400 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008401exit:
8402 abort_status = psa_pake_abort(operation);
8403 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008404}
8405
8406psa_status_t psa_pake_abort(
8407 psa_pake_operation_t *operation)
8408{
Przemek Stekield69dca92023-01-31 19:59:20 +01008409 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008410
Przemek Stekield69dca92023-01-31 19:59:20 +01008411 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008412 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008413 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008414
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008415 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8416 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008417 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008418 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008419 }
8420 if (operation->data.inputs.user != NULL) {
8421 mbedtls_free(operation->data.inputs.user);
8422 }
8423 if (operation->data.inputs.peer != NULL) {
8424 mbedtls_free(operation->data.inputs.peer);
8425 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008426 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008427 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008428
Przemek Stekield69dca92023-01-31 19:59:20 +01008429 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008430}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008431#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008432
Gilles Peskinee59236f2018-01-27 23:32:46 +01008433#endif /* MBEDTLS_PSA_CRYPTO_C */