blob: bbd6b24ed4003cf8fec0570059919311a877d073 [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
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001853}
Gilles Peskine4747d192019-04-17 15:05:45 +02001854
1855/** Finalize the creation of a key once its key material has been set.
1856 *
1857 * This entails writing the key to persistent storage.
1858 *
1859 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001860 * See the documentation of psa_start_key_creation() for the intended use
1861 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001862 *
Ronald Cron5c522922020-11-14 16:35:34 +01001863 * If the finalization succeeds, the function unlocks the key slot (it was
1864 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1865 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001866 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001867 * \param[in,out] slot Pointer to the slot with key material.
1868 * \param[in] driver The secure element driver for the key,
1869 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001870 * \param[out] key On success, identifier of the key. Note that the
1871 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001872 *
1873 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001874 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001875 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1876 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1877 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1878 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1879 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1880 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001881 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001882 * \return If this function fails, the key slot is an invalid state.
1883 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001884 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001885static psa_status_t psa_finish_key_creation(
1886 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001887 psa_se_drv_table_entry_t *driver,
1888 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001889{
1890 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001891 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001892 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001893
1894#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001896#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001898 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001899 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001901
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001902 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1903 sizeof(data.slot_number),
1904 "Slot number size does not match psa_se_key_data_storage_t");
1905
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1907 status = psa_save_persistent_key(&slot->attr,
1908 (uint8_t *) &data,
1909 sizeof(data));
1910 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001911#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1912 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001913 /* Key material is saved in export representation in the slot, so
1914 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 status = psa_save_persistent_key(&slot->attr,
1916 slot->key.data,
1917 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001918 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001919 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001920#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1921
Gilles Peskinecbaff462019-07-12 23:46:04 +02001922#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001923 /* Finish the transaction for a key creation. This does not
1924 * happen when registering an existing key. Detect this case
1925 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001926 * creation of a persistent key in a secure element requires a transaction,
1927 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 if (driver != NULL &&
1929 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1930 status = psa_save_se_persistent_data(driver);
1931 if (status != PSA_SUCCESS) {
1932 psa_destroy_persistent_key(slot->attr.id);
1933 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001934 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001936 }
1937#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1938
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001940 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 status = psa_unlock_key_slot(slot);
1942 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001943 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001945 }
Ronald Cron50972942020-11-14 11:28:25 +01001946
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001948}
1949
1950/** Abort the creation of a key.
1951 *
1952 * You may call this function after calling psa_start_key_creation(),
1953 * or after psa_finish_key_creation() fails. In other circumstances, this
1954 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001955 * See the documentation of psa_start_key_creation() for the intended use
1956 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001957 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001958 * \param[in,out] slot Pointer to the slot with key material.
1959 * \param[in] driver The secure element driver for the key,
1960 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001961 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001962static void psa_fail_key_creation(psa_key_slot_t *slot,
1963 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001964{
Gilles Peskine011e4282019-06-26 18:34:38 +02001965 (void) driver;
1966
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001968 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001970
1971#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001972 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001973 * element, and the failure happened later (when saving metadata
1974 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001975 * element.
1976 * https://github.com/ARMmbed/mbed-crypto/issues/217
1977 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001978
Gilles Peskined7729582019-08-05 15:55:54 +02001979 /* Abort the ongoing transaction if any (there may not be one if
1980 * the creation process failed before starting one, or if the
1981 * key creation is a registration of a key in a secure element).
1982 * Earlier functions must already have done what it takes to undo any
1983 * partial creation. All that's left is to update the transaction data
1984 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001986#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1987
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001989}
1990
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001991/** Validate optional attributes during key creation.
1992 *
1993 * Some key attributes are optional during key creation. If they are
1994 * specified in the attributes structure, check that they are consistent
1995 * with the data in the slot.
1996 *
1997 * This function should be called near the end of key creation, after
1998 * the slot in memory is fully populated but before saving persistent data.
1999 */
2000static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002001 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002003{
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 if (attributes->core.type != 0) {
2005 if (attributes->core.type != slot->attr.type) {
2006 return PSA_ERROR_INVALID_ARGUMENT;
2007 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002008 }
2009
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02002011#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
2012 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2014 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002015 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002016 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002017 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002018
Ronald Cron90857082020-11-25 14:58:33 +01002019 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 slot->attr.type,
2021 slot->key.data,
2022 slot->key.bytes,
2023 &rsa);
2024 if (status != PSA_SUCCESS) {
2025 return status;
2026 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002027
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 mbedtls_mpi_init(&actual);
2029 mbedtls_mpi_init(&required);
2030 ret = mbedtls_rsa_export(rsa,
2031 NULL, NULL, NULL, NULL, &actual);
2032 mbedtls_rsa_free(rsa);
2033 mbedtls_free(rsa);
2034 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002035 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 }
2037 ret = mbedtls_mpi_read_binary(&required,
2038 attributes->domain_parameters,
2039 attributes->domain_parameters_size);
2040 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002041 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 }
2043 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002044 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 }
2046rsa_exit:
2047 mbedtls_mpi_free(&actual);
2048 mbedtls_mpi_free(&required);
2049 if (ret != 0) {
2050 return mbedtls_to_psa_error(ret);
2051 }
2052 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02002053#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
2054 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08002055 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002056 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002058 }
2059 }
2060
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 if (attributes->core.bits != 0) {
2062 if (attributes->core.bits != slot->attr.bits) {
2063 return PSA_ERROR_INVALID_ARGUMENT;
2064 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002065 }
2066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002068}
2069
Gilles Peskine449bd832023-01-11 14:50:10 +01002070psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2071 const uint8_t *data,
2072 size_t data_length,
2073 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002074{
2075 psa_status_t status;
2076 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002077 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002078 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302079 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002080
Ronald Cron81709fc2020-11-14 12:10:32 +01002081 *key = MBEDTLS_SVC_KEY_ID_INIT;
2082
Gilles Peskine0f84d622019-09-12 19:03:13 +02002083 /* Reject zero-length symmetric keys (including raw data key objects).
2084 * This also rejects any key which might be encoded as an empty string,
2085 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 if (data_length == 0) {
2087 return PSA_ERROR_INVALID_ARGUMENT;
2088 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002089
Archana449608b2021-09-08 15:36:05 +05302090 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 if (data_length > SIZE_MAX / 8) {
2092 return PSA_ERROR_NOT_SUPPORTED;
2093 }
Archana6ed4bda2021-08-04 10:47:15 +05302094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2096 &slot, &driver);
2097 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002098 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002100
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002101 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302102 * storage ( thus not in the case of importing a key in a secure element
2103 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2104 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 if (slot->key.data == NULL) {
2106 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302107 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 attributes, data, data_length, &storage_size);
2109 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302110 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 }
Archanad8a83dc2021-06-14 10:04:16 +05302112 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 status = psa_allocate_buffer_to_slot(slot, storage_size);
2114 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002115 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002117 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002118
2119 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 status = psa_driver_wrapper_import_key(attributes,
2121 data, data_length,
2122 slot->key.data,
2123 slot->key.bytes,
2124 &slot->key.bytes, &bits);
2125 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002126 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002128
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002130 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002132 status = PSA_ERROR_INVALID_ARGUMENT;
2133 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002134 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002135
Archana6ed4bda2021-08-04 10:47:15 +05302136 /* Enforce a size limit, and in particular ensure that the bit
2137 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302139 status = PSA_ERROR_NOT_SUPPORTED;
2140 goto exit;
2141 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 status = psa_validate_optional_attributes(slot, attributes);
2143 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002144 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002146
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002148exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 if (status != PSA_SUCCESS) {
2150 psa_fail_key_creation(slot, driver);
2151 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002152
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002154}
2155
Gilles Peskined7729582019-08-05 15:55:54 +02002156#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2157psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002159{
2160 psa_status_t status;
2161 psa_key_slot_t *slot = NULL;
2162 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002163 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002164
2165 /* Leaving attributes unspecified is not currently supported.
2166 * It could make sense to query the key type and size from the
2167 * secure element, but not all secure elements support this
2168 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2170 return PSA_ERROR_NOT_SUPPORTED;
2171 }
2172 if (psa_get_key_bits(attributes) == 0) {
2173 return PSA_ERROR_NOT_SUPPORTED;
2174 }
Gilles Peskined7729582019-08-05 15:55:54 +02002175
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2177 &slot, &driver);
2178 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002179 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 }
Gilles Peskined7729582019-08-05 15:55:54 +02002181
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002183
2184exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 if (status != PSA_SUCCESS) {
2186 psa_fail_key_creation(slot, driver);
2187 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002188
Gilles Peskined7729582019-08-05 15:55:54 +02002189 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 psa_close_key(key);
2191 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002192}
2193#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2194
Gilles Peskine449bd832023-01-11 14:50:10 +01002195psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2196 const psa_key_attributes_t *specified_attributes,
2197 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002198{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002199 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002200 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002201 psa_key_slot_t *source_slot = NULL;
2202 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002203 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002204 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302205 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002206
Ronald Cron81709fc2020-11-14 12:10:32 +01002207 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2208
Archana8a180362021-07-05 02:18:48 +05302209 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2211 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002212 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002214
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 status = psa_validate_optional_attributes(source_slot,
2216 specified_attributes);
2217 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002218 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002220
Archana9d17bf42021-09-10 06:22:44 +05302221 /* The target key type and number of bits have been validated by
2222 * psa_validate_optional_attributes() to be either equal to zero or
2223 * equal to the ones of the source key. So it is safe to inherit
2224 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302225 * */
Archana9d17bf42021-09-10 06:22:44 +05302226 actual_attributes.core.bits = source_slot->attr.bits;
2227 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302228
2229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 status = psa_restrict_key_policy(source_slot->attr.type,
2231 &actual_attributes.core.policy,
2232 &source_slot->attr.policy);
2233 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002234 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002236
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2238 &target_slot, &driver);
2239 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002240 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 }
2242 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2243 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302244 /*
Archana9d17bf42021-09-10 06:22:44 +05302245 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302246 * the source key would need to be exported as plaintext and re-imported
2247 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302248 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302249 * appropriate API invocations from the application, if needed.
2250 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002251 status = PSA_ERROR_NOT_SUPPORTED;
2252 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002253 }
Archana8a180362021-07-05 02:18:48 +05302254 /*
2255 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302256 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302257 * - For opaque keys this translates to an invocation of the drivers'
2258 * copy_key entry point through the dispatch layer.
2259 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2261 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2262 &storage_size);
2263 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302264 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 }
Archana374fe5b2021-09-08 15:50:28 +05302266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2268 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302269 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 }
Archana374fe5b2021-09-08 15:50:28 +05302271
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 status = psa_driver_wrapper_copy_key(&actual_attributes,
2273 source_slot->key.data,
2274 source_slot->key.bytes,
2275 target_slot->key.data,
2276 target_slot->key.bytes,
2277 &target_slot->key.bytes);
2278 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302279 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 }
2281 } else {
2282 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302283 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 source_slot->key.bytes);
2285 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302286 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 }
Archana8a180362021-07-05 02:18:48 +05302288 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002290exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 if (status != PSA_SUCCESS) {
2292 psa_fail_key_creation(target_slot, driver);
2293 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002294
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002298}
2299
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002300
2301
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002302/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002303/* Message digests */
2304/****************************************************************/
2305
Gilles Peskine449bd832023-01-11 14:50:10 +01002306psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002307{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002308 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 if (operation->id == 0) {
2310 return PSA_SUCCESS;
2311 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002312
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002314 operation->id = 0;
2315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002317}
2318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2320 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002321{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002322 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002323
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002324 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002326 status = PSA_ERROR_BAD_STATE;
2327 goto exit;
2328 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002331 status = PSA_ERROR_INVALID_ARGUMENT;
2332 goto exit;
2333 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002334
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002335 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2336 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002340
2341exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 if (status != PSA_SUCCESS) {
2343 psa_hash_abort(operation);
2344 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002345
2346 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002347}
2348
Gilles Peskine449bd832023-01-11 14:50:10 +01002349psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2350 const uint8_t *input,
2351 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002352{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002353 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002356 status = PSA_ERROR_BAD_STATE;
2357 goto exit;
2358 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002359
Steven Cooremanc8288352021-03-04 14:02:19 +01002360 /* Don't require hash implementations to behave correctly on a
2361 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 if (input_length == 0) {
2363 return PSA_SUCCESS;
2364 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002365
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002367
2368exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 if (status != PSA_SUCCESS) {
2370 psa_hash_abort(operation);
2371 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002372
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002374}
2375
Gilles Peskine449bd832023-01-11 14:50:10 +01002376psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2377 uint8_t *hash,
2378 size_t hash_size,
2379 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002380{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002381 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 if (operation->id == 0) {
2383 return PSA_ERROR_BAD_STATE;
2384 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002385
Steven Cooreman1e582352021-02-18 17:24:37 +01002386 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 operation, hash, hash_size, hash_length);
2388 psa_hash_abort(operation);
2389 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002390}
2391
Gilles Peskine449bd832023-01-11 14:50:10 +01002392psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2393 const uint8_t *hash,
2394 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002395{
Ronald Cron69a63422021-10-18 09:47:58 +02002396 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002397 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002398 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 operation,
2400 actual_hash, sizeof(actual_hash),
2401 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002402
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002404 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002406
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002408 status = PSA_ERROR_INVALID_SIGNATURE;
2409 goto exit;
2410 }
2411
Dave Rodgman78701152023-08-29 14:20:18 +01002412 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002413 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002415
2416exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2418 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002419 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002421
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002423}
2424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425psa_status_t psa_hash_compute(psa_algorithm_t alg,
2426 const uint8_t *input, size_t input_length,
2427 uint8_t *hash, size_t hash_size,
2428 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002429{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002430 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 if (!PSA_ALG_IS_HASH(alg)) {
2432 return PSA_ERROR_INVALID_ARGUMENT;
2433 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002434
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2436 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002437}
2438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439psa_status_t psa_hash_compare(psa_algorithm_t alg,
2440 const uint8_t *input, size_t input_length,
2441 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002442{
Ronald Cron69a63422021-10-18 09:47:58 +02002443 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002444 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002445
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 if (!PSA_ALG_IS_HASH(alg)) {
2447 return PSA_ERROR_INVALID_ARGUMENT;
2448 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002449
Steven Cooreman1e582352021-02-18 17:24:37 +01002450 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 alg, input, input_length,
2452 actual_hash, sizeof(actual_hash),
2453 &actual_hash_length);
2454 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002455 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 }
2457 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002458 status = PSA_ERROR_INVALID_SIGNATURE;
2459 goto exit;
2460 }
Dave Rodgman78701152023-08-29 14:20:18 +01002461 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002462 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002464
2465exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2467 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002468}
2469
Gilles Peskine449bd832023-01-11 14:50:10 +01002470psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2471 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002472{
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 if (source_operation->id == 0 ||
2474 target_operation->id != 0) {
2475 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002476 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2479 target_operation);
2480 if (status != PSA_SUCCESS) {
2481 psa_hash_abort(target_operation);
2482 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002483
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002485}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002486
2487
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002488/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002489/* MAC */
2490/****************************************************************/
2491
Gilles Peskine449bd832023-01-11 14:50:10 +01002492psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002493{
Steven Cooremane6804192021-03-19 18:28:56 +01002494 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 if (operation->id == 0) {
2496 return PSA_SUCCESS;
2497 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002498
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002500 operation->mac_size = 0;
2501 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002502 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002503
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002505}
2506
Ronald Cron2dff3b22021-06-17 16:33:22 +02002507static psa_status_t psa_mac_finalize_alg_and_key_validation(
2508 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002509 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002511{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002512 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 psa_key_type_t key_type = psa_get_key_type(attributes);
2514 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002515
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 if (!PSA_ALG_IS_MAC(alg)) {
2517 return PSA_ERROR_INVALID_ARGUMENT;
2518 }
Steven Cooremane6804192021-03-19 18:28:56 +01002519
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002520 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 status = psa_mac_key_can_do(alg, key_type);
2522 if (status != PSA_SUCCESS) {
2523 return status;
2524 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002525
Steven Cooremand1a68f12021-05-10 11:13:41 +02002526 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002528
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002530 /* A very short MAC is too short for security since it can be
2531 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2532 * so we make this our minimum, even though 32 bits is still
2533 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002535 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002536
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2538 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002539 /* It's impossible to "truncate" to a larger length than the full length
2540 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002542 }
2543
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002545 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2546 * that is disabled in the compile-time configuration. The result can
2547 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2548 * configuration into account. In this case, force a return of
2549 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2550 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2551 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2552 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2553 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002555 }
2556
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002558}
2559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2561 mbedtls_svc_key_id_t key,
2562 psa_algorithm_t alg,
2563 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002564{
2565 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2566 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002567 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002568 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002569
2570 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002572 status = PSA_ERROR_BAD_STATE;
2573 goto exit;
2574 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002575
2576 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 key,
2578 &slot,
2579 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2580 alg);
2581 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002582 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002584
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002585 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002586 .core = slot->attr
2587 };
2588
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2590 &operation->mac_size);
2591 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002592 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002594
2595 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002596 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 if (is_sign) {
2598 status = psa_driver_wrapper_mac_sign_setup(operation,
2599 &attributes,
2600 slot->key.data,
2601 slot->key.bytes,
2602 alg);
2603 } else {
2604 status = psa_driver_wrapper_mac_verify_setup(operation,
2605 &attributes,
2606 slot->key.data,
2607 slot->key.bytes,
2608 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002609 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002610
Gilles Peskinefbfac682018-07-08 20:51:54 +02002611exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 if (status != PSA_SUCCESS) {
2613 psa_mac_abort(operation);
2614 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002615
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002619}
2620
Gilles Peskine449bd832023-01-11 14:50:10 +01002621psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2622 mbedtls_svc_key_id_t key,
2623 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002624{
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002626}
2627
Gilles Peskine449bd832023-01-11 14:50:10 +01002628psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2629 mbedtls_svc_key_id_t key,
2630 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002631{
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002633}
2634
Gilles Peskine449bd832023-01-11 14:50:10 +01002635psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2636 const uint8_t *input,
2637 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002638{
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 if (operation->id == 0) {
2640 return PSA_ERROR_BAD_STATE;
2641 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002642
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002643 /* Don't require hash implementations to behave correctly on a
2644 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 if (input_length == 0) {
2646 return PSA_SUCCESS;
2647 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002648
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2650 input, input_length);
2651 if (status != PSA_SUCCESS) {
2652 psa_mac_abort(operation);
2653 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002656}
2657
Gilles Peskine449bd832023-01-11 14:50:10 +01002658psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2659 uint8_t *mac,
2660 size_t mac_size,
2661 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002662{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002663 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2664 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002667 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002668 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002669 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002670
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002672 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002673 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002674 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002675
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002676 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2677 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002679 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002680 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002681 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002684 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002685 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002686 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002687
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 status = psa_driver_wrapper_mac_sign_finish(operation,
2689 mac, operation->mac_size,
2690 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002691
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002692exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002693 /* In case of success, set the potential excess room in the output buffer
2694 * to an invalid value, to avoid potentially leaking a longer MAC.
2695 * In case of error, set the output length and content to a safe default,
2696 * such that in case the caller misses an error check, the output would be
2697 * an unachievable MAC.
2698 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002700 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002701 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002702 }
mohammad16036df908f2018-04-02 08:34:15 -07002703
Paul Elliottdc42ca82023-02-24 18:11:59 +00002704 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002705
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002707
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002709}
2710
Gilles Peskine449bd832023-01-11 14:50:10 +01002711psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2712 const uint8_t *mac,
2713 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002714{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002715 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2716 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002717
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002719 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002720 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002721 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002722
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002724 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002725 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002726 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002727
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002729 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002730 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002731 }
2732
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 status = psa_driver_wrapper_mac_verify_finish(operation,
2734 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002735
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002736exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002738
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002740}
2741
Gilles Peskine449bd832023-01-11 14:50:10 +01002742static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2743 psa_algorithm_t alg,
2744 const uint8_t *input,
2745 size_t input_length,
2746 uint8_t *mac,
2747 size_t mac_size,
2748 size_t *mac_length,
2749 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002750{
2751 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002752 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2753 psa_key_slot_t *slot;
2754 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002755 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002756
Ronald Cron51131b52021-06-17 17:17:20 +02002757 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 key,
2759 &slot,
2760 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2761 alg);
2762 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002763 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002765
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002766 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002767 .core = slot->attr
2768 };
2769
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2771 &operation_mac_size);
2772 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002773 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002775
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002777 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002778 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002779 }
2780
2781 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 &attributes,
2783 slot->key.data, slot->key.bytes,
2784 alg,
2785 input, input_length,
2786 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002787
2788exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002789 /* In case of success, set the potential excess room in the output buffer
2790 * to an invalid value, to avoid potentially leaking a longer MAC.
2791 * In case of error, set the output length and content to a safe default,
2792 * such that in case the caller misses an error check, the output would be
2793 * an unachievable MAC.
2794 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002796 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002797 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002798 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002799
2800 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002801
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002803
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002805}
2806
Gilles Peskine449bd832023-01-11 14:50:10 +01002807psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002808 psa_algorithm_t alg,
2809 const uint8_t *input,
2810 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 uint8_t *mac,
2812 size_t mac_size,
2813 size_t *mac_length)
2814{
2815 return psa_mac_compute_internal(key, alg,
2816 input, input_length,
2817 mac, mac_size, mac_length, 1);
2818}
2819
2820psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2821 psa_algorithm_t alg,
2822 const uint8_t *input,
2823 size_t input_length,
2824 const uint8_t *mac,
2825 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002826{
2827 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002828 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2829 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002830
Gilles Peskine449bd832023-01-11 14:50:10 +01002831 status = psa_mac_compute_internal(key, alg,
2832 input, input_length,
2833 actual_mac, sizeof(actual_mac),
2834 &actual_mac_length, 0);
2835 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002836 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002838
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002840 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002841 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002842 }
Dave Rodgman78701152023-08-29 14:20:18 +01002843 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002844 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002845 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002846 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002847
2848exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002850
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002852}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002853
Gilles Peskinee59236f2018-01-27 23:32:46 +01002854/****************************************************************/
2855/* Asymmetric cryptography */
2856/****************************************************************/
2857
Gilles Peskine449bd832023-01-11 14:50:10 +01002858static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2859 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002860{
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 if (input_is_message) {
2862 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2863 return PSA_ERROR_INVALID_ARGUMENT;
2864 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002865
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2867 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2868 return PSA_ERROR_INVALID_ARGUMENT;
2869 }
2870 }
2871 } else {
2872 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2873 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002874 }
2875 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002876
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002878}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002879
Gilles Peskine449bd832023-01-11 14:50:10 +01002880static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2881 int input_is_message,
2882 psa_algorithm_t alg,
2883 const uint8_t *input,
2884 size_t input_length,
2885 uint8_t *signature,
2886 size_t signature_size,
2887 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002888{
2889 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2890 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2891 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002892 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002893
2894 *signature_length = 0;
2895
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 status = psa_sign_verify_check_alg(input_is_message, alg);
2897 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002898 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002900
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002901 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002902 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002903 * buffer can in principle be empty since it doesn't actually have
2904 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 if (signature_size == 0) {
2906 return PSA_ERROR_BUFFER_TOO_SMALL;
2907 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002908
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002909 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 key, &slot,
2911 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2912 PSA_KEY_USAGE_SIGN_HASH,
2913 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002914
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002916 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002918
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002920 status = PSA_ERROR_INVALID_ARGUMENT;
2921 goto exit;
2922 }
2923
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002924 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002926 };
2927
Gilles Peskine449bd832023-01-11 14:50:10 +01002928 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002929 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002930 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002931 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002932 signature, signature_size, signature_length);
2933 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002934
2935 status = psa_driver_wrapper_sign_hash(
2936 &attributes, slot->key.data, slot->key.bytes,
2937 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002938 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002939 }
2940
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002941
2942exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002943 psa_wipe_tag_output_buffer(signature, status, signature_size,
2944 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002945
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002947
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002949}
2950
Gilles Peskine449bd832023-01-11 14:50:10 +01002951static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2952 int input_is_message,
2953 psa_algorithm_t alg,
2954 const uint8_t *input,
2955 size_t input_length,
2956 const uint8_t *signature,
2957 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002958{
2959 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2960 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2961 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002962
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 status = psa_sign_verify_check_alg(input_is_message, alg);
2964 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002965 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002967
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002968 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 key, &slot,
2970 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2971 PSA_KEY_USAGE_VERIFY_HASH,
2972 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002973
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 if (status != PSA_SUCCESS) {
2975 return status;
2976 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002977
2978 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002980 };
2981
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002983 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002984 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002985 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 signature, signature_length);
2987 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002988 status = psa_driver_wrapper_verify_hash(
2989 &attributes, slot->key.data, slot->key.bytes,
2990 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002992 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002993
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002995
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002997
2998}
2999
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003000psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003001 const psa_key_attributes_t *attributes,
3002 const uint8_t *key_buffer,
3003 size_t key_buffer_size,
3004 psa_algorithm_t alg,
3005 const uint8_t *input,
3006 size_t input_length,
3007 uint8_t *signature,
3008 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003010{
3011 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003012
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003014 size_t hash_length;
3015 uint8_t hash[PSA_HASH_MAX_SIZE];
3016
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003017 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 PSA_ALG_SIGN_GET_HASH(alg),
3019 input, input_length,
3020 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003021
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003023 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003025
3026 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 attributes, key_buffer, key_buffer_size,
3028 alg, hash, hash_length,
3029 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003030 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003031
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003033}
3034
Gilles Peskine449bd832023-01-11 14:50:10 +01003035psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3036 psa_algorithm_t alg,
3037 const uint8_t *input,
3038 size_t input_length,
3039 uint8_t *signature,
3040 size_t signature_size,
3041 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003042{
3043 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003044 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003046}
3047
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003048psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003049 const psa_key_attributes_t *attributes,
3050 const uint8_t *key_buffer,
3051 size_t key_buffer_size,
3052 psa_algorithm_t alg,
3053 const uint8_t *input,
3054 size_t input_length,
3055 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003057{
3058 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003059
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003061 size_t hash_length;
3062 uint8_t hash[PSA_HASH_MAX_SIZE];
3063
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003064 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 PSA_ALG_SIGN_GET_HASH(alg),
3066 input, input_length,
3067 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003068
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003070 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003072
3073 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 attributes, key_buffer, key_buffer_size,
3075 alg, hash, hash_length,
3076 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003077 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003078
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003080}
3081
Gilles Peskine449bd832023-01-11 14:50:10 +01003082psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3083 psa_algorithm_t alg,
3084 const uint8_t *input,
3085 size_t input_length,
3086 const uint8_t *signature,
3087 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003088{
3089 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003090 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003092}
3093
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003094psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003095 const psa_key_attributes_t *attributes,
3096 const uint8_t *key_buffer, size_t key_buffer_size,
3097 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003099{
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3101 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3102 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003103#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003104 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3105 return mbedtls_psa_rsa_sign_hash(
3106 attributes,
3107 key_buffer, key_buffer_size,
3108 alg, hash, hash_length,
3109 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003110#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3111 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 } else {
3113 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003114 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3116 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003117#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3119 return mbedtls_psa_ecdsa_sign_hash(
3120 attributes,
3121 key_buffer, key_buffer_size,
3122 alg, hash, hash_length,
3123 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003124#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3125 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 } else {
3127 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003128 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003129 }
Ronald Cron4501c982021-03-19 20:09:32 +01003130
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 (void) key_buffer;
3132 (void) key_buffer_size;
3133 (void) hash;
3134 (void) hash_length;
3135 (void) signature;
3136 (void) signature_size;
3137 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003138
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003140}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003141
Gilles Peskine449bd832023-01-11 14:50:10 +01003142psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3143 psa_algorithm_t alg,
3144 const uint8_t *hash,
3145 size_t hash_length,
3146 uint8_t *signature,
3147 size_t signature_size,
3148 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003149{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003150 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003151 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003153}
3154
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003155psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003156 const psa_key_attributes_t *attributes,
3157 const uint8_t *key_buffer, size_t key_buffer_size,
3158 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003159 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003160{
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3162 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3163 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003164#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3166 return mbedtls_psa_rsa_verify_hash(
3167 attributes,
3168 key_buffer, key_buffer_size,
3169 alg, hash, hash_length,
3170 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003171#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3172 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003173 } else {
3174 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003175 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003176 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3177 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003178#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3180 return mbedtls_psa_ecdsa_verify_hash(
3181 attributes,
3182 key_buffer, key_buffer_size,
3183 alg, hash, hash_length,
3184 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003185#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3186 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 } else {
3188 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003189 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003190 }
Ronald Cron4501c982021-03-19 20:09:32 +01003191
Gilles Peskine449bd832023-01-11 14:50:10 +01003192 (void) key_buffer;
3193 (void) key_buffer_size;
3194 (void) hash;
3195 (void) hash_length;
3196 (void) signature;
3197 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003198
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003200}
3201
Gilles Peskine449bd832023-01-11 14:50:10 +01003202psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3203 psa_algorithm_t alg,
3204 const uint8_t *hash,
3205 size_t hash_length,
3206 const uint8_t *signature,
3207 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003208{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003209 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003210 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003212}
3213
Gilles Peskine449bd832023-01-11 14:50:10 +01003214psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3215 psa_algorithm_t alg,
3216 const uint8_t *input,
3217 size_t input_length,
3218 const uint8_t *salt,
3219 size_t salt_length,
3220 uint8_t *output,
3221 size_t output_size,
3222 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003223{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003224 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003225 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003226 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003227 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003228
Darryl Green5cc689a2018-07-24 15:34:10 +01003229 (void) input;
3230 (void) input_length;
3231 (void) salt;
3232 (void) output;
3233 (void) output_size;
3234
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003235 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003236
Gilles Peskine449bd832023-01-11 14:50:10 +01003237 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3238 return PSA_ERROR_INVALID_ARGUMENT;
3239 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003240
Ronald Cron5c522922020-11-14 16:35:34 +01003241 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003242 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3243 if (status != PSA_SUCCESS) {
3244 return status;
3245 }
3246 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3247 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003248 status = PSA_ERROR_INVALID_ARGUMENT;
3249 goto exit;
3250 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003251
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003252 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003254 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003255
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003256 status = psa_driver_wrapper_asymmetric_encrypt(
3257 &attributes, slot->key.data, slot->key.bytes,
3258 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003259 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003260exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003262
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003264}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003265
Gilles Peskine449bd832023-01-11 14:50:10 +01003266psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3267 psa_algorithm_t alg,
3268 const uint8_t *input,
3269 size_t input_length,
3270 const uint8_t *salt,
3271 size_t salt_length,
3272 uint8_t *output,
3273 size_t output_size,
3274 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003275{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003276 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003277 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003278 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003279 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003280
Darryl Green5cc689a2018-07-24 15:34:10 +01003281 (void) input;
3282 (void) input_length;
3283 (void) salt;
3284 (void) output;
3285 (void) output_size;
3286
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003287 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003288
Gilles Peskine449bd832023-01-11 14:50:10 +01003289 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3290 return PSA_ERROR_INVALID_ARGUMENT;
3291 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003292
Ronald Cron5c522922020-11-14 16:35:34 +01003293 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3295 if (status != PSA_SUCCESS) {
3296 return status;
3297 }
3298 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003299 status = PSA_ERROR_INVALID_ARGUMENT;
3300 goto exit;
3301 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003302
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003303 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003304 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003305 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003306
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003307 status = psa_driver_wrapper_asymmetric_decrypt(
3308 &attributes, slot->key.data, slot->key.bytes,
3309 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003310 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003311
3312exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003313 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003314
Gilles Peskine449bd832023-01-11 14:50:10 +01003315 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003316}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003317
Paul Elliott9fe12f62022-11-30 19:16:02 +00003318/****************************************************************/
3319/* Asymmetric interruptible cryptography */
3320/****************************************************************/
3321
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003322static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3323
Paul Elliott9fe12f62022-11-30 19:16:02 +00003324void psa_interruptible_set_max_ops(uint32_t max_ops)
3325{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003326 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003327}
3328
3329uint32_t psa_interruptible_get_max_ops(void)
3330{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003331 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003332}
3333
Paul Elliott9fe12f62022-11-30 19:16:02 +00003334uint32_t psa_sign_hash_get_num_ops(
3335 const psa_sign_hash_interruptible_operation_t *operation)
3336{
Paul Elliott296ede92022-12-15 17:00:30 +00003337 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003338}
3339
3340uint32_t psa_verify_hash_get_num_ops(
3341 const psa_verify_hash_interruptible_operation_t *operation)
3342{
Paul Elliott296ede92022-12-15 17:00:30 +00003343 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003344}
3345
Paul Elliott59ad9452022-12-18 15:09:02 +00003346static psa_status_t psa_sign_hash_abort_internal(
3347 psa_sign_hash_interruptible_operation_t *operation)
3348{
3349 if (operation->id == 0) {
3350 /* The object has (apparently) been initialized but it is not (yet)
3351 * in use. It's ok to call abort on such an object, and there's
3352 * nothing to do. */
3353 return PSA_SUCCESS;
3354 }
3355
3356 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3357
3358 status = psa_driver_wrapper_sign_hash_abort(operation);
3359
3360 operation->id = 0;
3361
Paul Elliotte6145dc2023-02-07 12:51:21 +00003362 /* Do not clear either the error_occurred or num_ops elements here as they
3363 * only want to be cleared by the application calling abort, not by abort
3364 * being called at completion of an operation. */
3365
Paul Elliott59ad9452022-12-18 15:09:02 +00003366 return status;
3367}
3368
Paul Elliott9fe12f62022-11-30 19:16:02 +00003369psa_status_t psa_sign_hash_start(
3370 psa_sign_hash_interruptible_operation_t *operation,
3371 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3372 const uint8_t *hash, size_t hash_length)
3373{
3374 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3375 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3376 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003377 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003378
Paul Elliottc9774412023-02-06 15:14:07 +00003379 /* Check that start has not been previously called, or operation has not
3380 * previously errored. */
3381 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003382 return PSA_ERROR_BAD_STATE;
3383 }
3384
Paul Elliott9fe12f62022-11-30 19:16:02 +00003385 status = psa_sign_verify_check_alg(0, alg);
3386 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003387 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003388 return status;
3389 }
3390
3391 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3392 PSA_KEY_USAGE_SIGN_HASH,
3393 alg);
3394
3395 if (status != PSA_SUCCESS) {
3396 goto exit;
3397 }
3398
3399 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3400 status = PSA_ERROR_INVALID_ARGUMENT;
3401 goto exit;
3402 }
3403
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003404 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003405 .core = slot->attr
3406 };
3407
Paul Elliott296ede92022-12-15 17:00:30 +00003408 /* Ensure ops count gets reset, in case of operation re-use. */
3409 operation->num_ops = 0;
3410
Paul Elliott9fe12f62022-11-30 19:16:02 +00003411 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3412 slot->key.data,
3413 slot->key.bytes, alg,
3414 hash, hash_length);
3415exit:
3416
3417 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003418 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003419 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003420 }
3421
3422 unlock_status = psa_unlock_key_slot(slot);
3423
Paul Elliottc9774412023-02-06 15:14:07 +00003424 if (unlock_status != PSA_SUCCESS) {
3425 operation->error_occurred = 1;
3426 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003427
Paul Elliottc9774412023-02-06 15:14:07 +00003428 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003429}
3430
3431
3432psa_status_t psa_sign_hash_complete(
3433 psa_sign_hash_interruptible_operation_t *operation,
3434 uint8_t *signature, size_t signature_size,
3435 size_t *signature_length)
3436{
3437 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3438
3439 *signature_length = 0;
3440
Paul Elliottc9774412023-02-06 15:14:07 +00003441 /* Check that start has been called first, and that operation has not
3442 * previously errored. */
3443 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003444 status = PSA_ERROR_BAD_STATE;
3445 goto exit;
3446 }
3447
Paul Elliott096abc42023-02-03 18:33:23 +00003448 /* Immediately reject a zero-length signature buffer. This guarantees that
3449 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003450 if (signature_size == 0) {
3451 status = PSA_ERROR_BUFFER_TOO_SMALL;
3452 goto exit;
3453 }
3454
3455 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3456 signature_size,
3457 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003458
Paul Elliott296ede92022-12-15 17:00:30 +00003459 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003460 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003461
Paul Elliottebe225c2023-02-10 14:32:53 +00003462exit:
3463
Paul Elliott59200a22023-02-21 15:07:40 +00003464 psa_wipe_tag_output_buffer(signature, status, signature_size,
3465 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003466
Paul Elliott53bb3122023-02-10 14:22:22 +00003467 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003468 if (status != PSA_SUCCESS) {
3469 operation->error_occurred = 1;
3470 }
3471
Paul Elliott59ad9452022-12-18 15:09:02 +00003472 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003473 }
3474
3475 return status;
3476}
3477
3478psa_status_t psa_sign_hash_abort(
3479 psa_sign_hash_interruptible_operation_t *operation)
3480{
Paul Elliott59ad9452022-12-18 15:09:02 +00003481 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3482
3483 status = psa_sign_hash_abort_internal(operation);
3484
3485 /* We clear the number of ops done here, so that it is not cleared when
3486 * the operation fails or succeeds, only on manual abort. */
3487 operation->num_ops = 0;
3488
Paul Elliottc9774412023-02-06 15:14:07 +00003489 /* Likewise, failure state. */
3490 operation->error_occurred = 0;
3491
Paul Elliott59ad9452022-12-18 15:09:02 +00003492 return status;
3493}
3494
3495static psa_status_t psa_verify_hash_abort_internal(
3496 psa_verify_hash_interruptible_operation_t *operation)
3497{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003498 if (operation->id == 0) {
3499 /* The object has (apparently) been initialized but it is not (yet)
3500 * in use. It's ok to call abort on such an object, and there's
3501 * nothing to do. */
3502 return PSA_SUCCESS;
3503 }
3504
Paul Elliott59ad9452022-12-18 15:09:02 +00003505 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3506
3507 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003508
3509 operation->id = 0;
3510
Paul Elliotte6145dc2023-02-07 12:51:21 +00003511 /* Do not clear either the error_occurred or num_ops elements here as they
3512 * only want to be cleared by the application calling abort, not by abort
3513 * being called at completion of an operation. */
3514
Paul Elliott59ad9452022-12-18 15:09:02 +00003515 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003516}
3517
3518psa_status_t psa_verify_hash_start(
3519 psa_verify_hash_interruptible_operation_t *operation,
3520 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3521 const uint8_t *hash, size_t hash_length,
3522 const uint8_t *signature, size_t signature_length)
3523{
3524 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3525 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3526 psa_key_slot_t *slot;
3527
Paul Elliottc9774412023-02-06 15:14:07 +00003528 /* Check that start has not been previously called, or operation has not
3529 * previously errored. */
3530 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003531 return PSA_ERROR_BAD_STATE;
3532 }
3533
3534 status = psa_sign_verify_check_alg(0, alg);
3535 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003536 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003537 return status;
3538 }
3539
3540 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3541 PSA_KEY_USAGE_VERIFY_HASH,
3542 alg);
3543
3544 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003545 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003546 return status;
3547 }
3548
3549 psa_key_attributes_t attributes = {
3550 .core = slot->attr
3551 };
3552
Paul Elliott296ede92022-12-15 17:00:30 +00003553 /* Ensure ops count gets reset, in case of operation re-use. */
3554 operation->num_ops = 0;
3555
Paul Elliott9fe12f62022-11-30 19:16:02 +00003556 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3557 slot->key.data,
3558 slot->key.bytes,
3559 alg, hash, hash_length,
3560 signature, signature_length);
3561
3562 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003563 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003564 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003565 }
3566
3567 unlock_status = psa_unlock_key_slot(slot);
3568
Paul Elliottc9774412023-02-06 15:14:07 +00003569 if (unlock_status != PSA_SUCCESS) {
3570 operation->error_occurred = 1;
3571 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003572
Paul Elliottc9774412023-02-06 15:14:07 +00003573 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003574}
3575
3576psa_status_t psa_verify_hash_complete(
3577 psa_verify_hash_interruptible_operation_t *operation)
3578{
3579 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3580
Paul Elliottc9774412023-02-06 15:14:07 +00003581 /* Check that start has been called first, and that operation has not
3582 * previously errored. */
3583 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003584 status = PSA_ERROR_BAD_STATE;
3585 goto exit;
3586 }
3587
3588 status = psa_driver_wrapper_verify_hash_complete(operation);
3589
Paul Elliott296ede92022-12-15 17:00:30 +00003590 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003591 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003592 operation);
3593
Paul Elliottebe225c2023-02-10 14:32:53 +00003594exit:
3595
Paul Elliott9fe12f62022-11-30 19:16:02 +00003596 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003597 if (status != PSA_SUCCESS) {
3598 operation->error_occurred = 1;
3599 }
3600
Paul Elliott59ad9452022-12-18 15:09:02 +00003601 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003602 }
3603
3604 return status;
3605}
3606
3607psa_status_t psa_verify_hash_abort(
3608 psa_verify_hash_interruptible_operation_t *operation)
3609{
Paul Elliott59ad9452022-12-18 15:09:02 +00003610 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003611
Paul Elliott59ad9452022-12-18 15:09:02 +00003612 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003613
Paul Elliott59ad9452022-12-18 15:09:02 +00003614 /* We clear the number of ops done here, so that it is not cleared when
3615 * the operation fails or succeeds, only on manual abort. */
3616 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003617
Paul Elliottc9774412023-02-06 15:14:07 +00003618 /* Likewise, failure state. */
3619 operation->error_occurred = 0;
3620
Paul Elliott59ad9452022-12-18 15:09:02 +00003621 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003622}
3623
Paul Elliott588f8ed2022-12-02 18:10:26 +00003624/****************************************************************/
3625/* Asymmetric interruptible cryptography internal */
3626/* implementations */
3627/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003628
Paul Elliott588f8ed2022-12-02 18:10:26 +00003629void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3630{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003631
Paul Elliott588f8ed2022-12-02 18:10:26 +00003632#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3633 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3634 defined(MBEDTLS_ECP_RESTARTABLE)
3635
3636 /* Internal implementation uses zero to indicate infinite number max ops,
3637 * therefore avoid this value, and set to minimum possible. */
3638 if (max_ops == 0) {
3639 max_ops = 1;
3640 }
3641
Paul Elliott588f8ed2022-12-02 18:10:26 +00003642 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003643#else
3644 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003645#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3646 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3647 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3648}
3649
Paul Elliott588f8ed2022-12-02 18:10:26 +00003650uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003651 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003652{
3653#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3654 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3655 defined(MBEDTLS_ECP_RESTARTABLE)
3656
Paul Elliott93d9ca82023-02-15 18:14:21 +00003657 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003658#else
3659 (void) operation;
3660 return 0;
3661#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3662 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3663 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3664}
3665
3666uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003667 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003668{
3669 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3670 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3671 defined(MBEDTLS_ECP_RESTARTABLE)
3672
Paul Elliott93d9ca82023-02-15 18:14:21 +00003673 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003674#else
3675 (void) operation;
3676 return 0;
3677#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3678 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3679 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3680}
3681
3682psa_status_t mbedtls_psa_sign_hash_start(
3683 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3684 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3685 size_t key_buffer_size, psa_algorithm_t alg,
3686 const uint8_t *hash, size_t hash_length)
3687{
3688 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003689 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003690
Paul Elliott068fe072023-01-16 13:59:15 +00003691 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3692 return PSA_ERROR_NOT_SUPPORTED;
3693 }
3694
3695 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003696 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003697 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003698
3699#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003700 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3701 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003702
Paul Elliotta1c94092023-02-15 16:38:04 +00003703 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3704
Paul Elliott93d9ca82023-02-15 18:14:21 +00003705 /* Ensure num_ops is zero'ed in case of context re-use. */
3706 operation->num_ops = 0;
3707
Paul Elliott068fe072023-01-16 13:59:15 +00003708 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3709 attributes->core.bits,
3710 key_buffer,
3711 key_buffer_size,
3712 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003713
Paul Elliott068fe072023-01-16 13:59:15 +00003714 if (status != PSA_SUCCESS) {
3715 return status;
3716 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003717
Paul Elliott1bc59df2023-02-05 13:41:57 +00003718 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003719 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003720
Paul Elliott068fe072023-01-16 13:59:15 +00003721 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003722 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003723 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003724
Paul Elliott0290a762023-02-09 14:30:24 +00003725 /* We only need to store the same length of hash as the private key size
3726 * here, it would be truncated by the internal implementation anyway. */
3727 required_hash_length = (hash_length < operation->coordinate_bytes ?
3728 hash_length : operation->coordinate_bytes);
3729
Paul Elliottba70ad42023-02-15 18:23:53 +00003730 if (required_hash_length > sizeof(operation->hash)) {
3731 /* Shouldn't happen, but better safe than sorry. */
3732 return PSA_ERROR_CORRUPTION_DETECTED;
3733 }
3734
Paul Elliott0290a762023-02-09 14:30:24 +00003735 memcpy(operation->hash, hash, required_hash_length);
3736 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003737
3738 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003739
3740#else
Paul Elliott068fe072023-01-16 13:59:15 +00003741 (void) operation;
3742 (void) key_buffer;
3743 (void) key_buffer_size;
3744 (void) alg;
3745 (void) hash;
3746 (void) hash_length;
3747 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003748 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003749
Paul Elliott068fe072023-01-16 13:59:15 +00003750 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003751#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3752 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3753 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003754}
3755
3756psa_status_t mbedtls_psa_sign_hash_complete(
3757 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3758 uint8_t *signature, size_t signature_size,
3759 size_t *signature_length)
3760{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003761#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3762 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3763 defined(MBEDTLS_ECP_RESTARTABLE)
3764
Paul Elliotta1c94092023-02-15 16:38:04 +00003765 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003766 mbedtls_mpi r;
3767 mbedtls_mpi s;
3768
Paul Elliott46845252023-02-03 14:59:11 +00003769 mbedtls_mpi_init(&r);
3770 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003771
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003772 /* Ensure max_ops is set to the current value (or default). */
3773 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3774
Paul Elliotta1c94092023-02-15 16:38:04 +00003775 if (signature_size < 2 * operation->coordinate_bytes) {
3776 status = PSA_ERROR_BUFFER_TOO_SMALL;
3777 goto exit;
3778 }
3779
Paul Elliott588f8ed2022-12-02 18:10:26 +00003780 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003781
Paul Elliott588f8ed2022-12-02 18:10:26 +00003782#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3783 status = mbedtls_to_psa_error(
3784 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003785 &r,
3786 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003787 &operation->ctx->d,
3788 operation->hash,
3789 operation->hash_length,
3790 operation->md_alg,
3791 mbedtls_psa_get_random,
3792 MBEDTLS_PSA_RANDOM_STATE,
3793 &operation->restart_ctx));
3794#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003795 status = PSA_ERROR_NOT_SUPPORTED;
3796 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003797#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3798 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003799 status = mbedtls_to_psa_error(
3800 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003801 &r,
3802 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003803 &operation->ctx->d,
3804 operation->hash,
3805 operation->hash_length,
3806 mbedtls_psa_get_random,
3807 MBEDTLS_PSA_RANDOM_STATE,
3808 mbedtls_psa_get_random,
3809 MBEDTLS_PSA_RANDOM_STATE,
3810 &operation->restart_ctx));
3811 }
3812
Paul Elliottf8e5b562023-02-19 18:43:45 +00003813 /* Hide the fact that the restart context only holds a delta of number of
3814 * ops done during the last operation, not an absolute value. */
3815 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3816
Paul Elliott724bd252023-02-08 12:35:08 +00003817 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003818 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003819 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003820 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003821 operation->coordinate_bytes)
3822 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003823
3824 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003825 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003826 }
3827
3828 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003829 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003830 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003831 operation->coordinate_bytes,
3832 operation->coordinate_bytes)
3833 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003834
3835 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003836 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003837 }
3838
Paul Elliott1bc59df2023-02-05 13:41:57 +00003839 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003840
Paul Elliott724bd252023-02-08 12:35:08 +00003841 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003842 }
Paul Elliott724bd252023-02-08 12:35:08 +00003843
3844exit:
3845
3846 mbedtls_mpi_free(&r);
3847 mbedtls_mpi_free(&s);
3848 return status;
3849
Paul Elliott588f8ed2022-12-02 18:10:26 +00003850 #else
3851
3852 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003853 (void) signature;
3854 (void) signature_size;
3855 (void) signature_length;
3856
3857 return PSA_ERROR_NOT_SUPPORTED;
3858
3859#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3860 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3861 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3862}
3863
3864psa_status_t mbedtls_psa_sign_hash_abort(
3865 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3866{
3867
3868#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3869 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3870 defined(MBEDTLS_ECP_RESTARTABLE)
3871
3872 if (operation->ctx) {
3873 mbedtls_ecdsa_free(operation->ctx);
3874 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003875 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003876 }
3877
3878 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3879
Paul Elliott93d9ca82023-02-15 18:14:21 +00003880 operation->num_ops = 0;
3881
Paul Elliott588f8ed2022-12-02 18:10:26 +00003882 return PSA_SUCCESS;
3883
3884#else
3885
3886 (void) operation;
3887
3888 return PSA_ERROR_NOT_SUPPORTED;
3889
3890#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3891 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3892 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3893}
3894
3895psa_status_t mbedtls_psa_verify_hash_start(
3896 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3897 const psa_key_attributes_t *attributes,
3898 const uint8_t *key_buffer, size_t key_buffer_size,
3899 psa_algorithm_t alg,
3900 const uint8_t *hash, size_t hash_length,
3901 const uint8_t *signature, size_t signature_length)
3902{
3903 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003904 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003905 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003906
Paul Elliott068fe072023-01-16 13:59:15 +00003907 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3908 return PSA_ERROR_NOT_SUPPORTED;
3909 }
3910
3911 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003912 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003913 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003914
3915#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003916 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3917 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003918
Paul Elliotta1c94092023-02-15 16:38:04 +00003919 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3920 mbedtls_mpi_init(&operation->r);
3921 mbedtls_mpi_init(&operation->s);
3922
Paul Elliott93d9ca82023-02-15 18:14:21 +00003923 /* Ensure num_ops is zero'ed in case of context re-use. */
3924 operation->num_ops = 0;
3925
Paul Elliott068fe072023-01-16 13:59:15 +00003926 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3927 attributes->core.bits,
3928 key_buffer,
3929 key_buffer_size,
3930 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003931
Paul Elliott068fe072023-01-16 13:59:15 +00003932 if (status != PSA_SUCCESS) {
3933 return status;
3934 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003935
Paul Elliottc569fc22023-02-10 13:02:54 +00003936 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003937
Paul Elliott1bc59df2023-02-05 13:41:57 +00003938 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003939 return PSA_ERROR_INVALID_SIGNATURE;
3940 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003941
Paul Elliott068fe072023-01-16 13:59:15 +00003942 status = mbedtls_to_psa_error(
3943 mbedtls_mpi_read_binary(&operation->r,
3944 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003945 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003946
Paul Elliott068fe072023-01-16 13:59:15 +00003947 if (status != PSA_SUCCESS) {
3948 return status;
3949 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003950
Paul Elliott068fe072023-01-16 13:59:15 +00003951 status = mbedtls_to_psa_error(
3952 mbedtls_mpi_read_binary(&operation->s,
3953 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003954 coordinate_bytes,
3955 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003956
Paul Elliott068fe072023-01-16 13:59:15 +00003957 if (status != PSA_SUCCESS) {
3958 return status;
3959 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003960
Paul Elliott2c9843f2023-02-15 17:32:42 +00003961 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003962
Paul Elliott2c9843f2023-02-15 17:32:42 +00003963 if (status != PSA_SUCCESS) {
3964 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003965 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003966
Paul Elliott0290a762023-02-09 14:30:24 +00003967 /* We only need to store the same length of hash as the private key size
3968 * here, it would be truncated by the internal implementation anyway. */
3969 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3970 coordinate_bytes);
3971
Paul Elliottba70ad42023-02-15 18:23:53 +00003972 if (required_hash_length > sizeof(operation->hash)) {
3973 /* Shouldn't happen, but better safe than sorry. */
3974 return PSA_ERROR_CORRUPTION_DETECTED;
3975 }
3976
Paul Elliott0290a762023-02-09 14:30:24 +00003977 memcpy(operation->hash, hash, required_hash_length);
3978 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003979
3980 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003981#else
Paul Elliott068fe072023-01-16 13:59:15 +00003982 (void) operation;
3983 (void) key_buffer;
3984 (void) key_buffer_size;
3985 (void) alg;
3986 (void) hash;
3987 (void) hash_length;
3988 (void) signature;
3989 (void) signature_length;
3990 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003991 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003992 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003993
Paul Elliott068fe072023-01-16 13:59:15 +00003994 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003995#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3996 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3997 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003998}
3999
4000psa_status_t mbedtls_psa_verify_hash_complete(
4001 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4002{
4003
4004#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4005 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4006 defined(MBEDTLS_ECP_RESTARTABLE)
4007
Paul Elliottf8e5b562023-02-19 18:43:45 +00004008 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4009
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004010 /* Ensure max_ops is set to the current value (or default). */
4011 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4012
Paul Elliottf8e5b562023-02-19 18:43:45 +00004013 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004014 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4015 operation->hash,
4016 operation->hash_length,
4017 &operation->ctx->Q,
4018 &operation->r,
4019 &operation->s,
4020 &operation->restart_ctx));
4021
Paul Elliottf8e5b562023-02-19 18:43:45 +00004022 /* Hide the fact that the restart context only holds a delta of number of
4023 * ops done during the last operation, not an absolute value. */
4024 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4025
4026 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004027#else
4028 (void) operation;
4029
4030 return PSA_ERROR_NOT_SUPPORTED;
4031
4032#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4033 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4034 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4035}
4036
4037psa_status_t mbedtls_psa_verify_hash_abort(
4038 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4039{
4040
4041#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4042 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4043 defined(MBEDTLS_ECP_RESTARTABLE)
4044
4045 if (operation->ctx) {
4046 mbedtls_ecdsa_free(operation->ctx);
4047 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004048 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004049 }
4050
4051 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4052
Paul Elliott93d9ca82023-02-15 18:14:21 +00004053 operation->num_ops = 0;
4054
Paul Elliott588f8ed2022-12-02 18:10:26 +00004055 mbedtls_mpi_free(&operation->r);
4056 mbedtls_mpi_free(&operation->s);
4057
4058 return PSA_SUCCESS;
4059
4060#else
4061 (void) operation;
4062
4063 return PSA_ERROR_NOT_SUPPORTED;
4064
4065#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4066 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4067 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4068}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004069
mohammad1603503973b2018-03-12 15:59:30 +02004070/****************************************************************/
4071/* Symmetric cryptography */
4072/****************************************************************/
4073
Gilles Peskine449bd832023-01-11 14:50:10 +01004074static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4075 mbedtls_svc_key_id_t key,
4076 psa_algorithm_t alg,
4077 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004078{
4079 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4080 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004081 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004082 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4083 PSA_KEY_USAGE_ENCRYPT :
4084 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004085 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004086
4087 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004088 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004089 status = PSA_ERROR_BAD_STATE;
4090 goto exit;
4091 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004092
Gilles Peskine449bd832023-01-11 14:50:10 +01004093 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004094 status = PSA_ERROR_INVALID_ARGUMENT;
4095 goto exit;
4096 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004097
Gilles Peskine449bd832023-01-11 14:50:10 +01004098 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4099 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004100 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004101 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004102
Ronald Cron49fafa92021-03-10 08:34:23 +01004103 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004104 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004105 * so we only set it (in the driver wrapper) after resources have been
4106 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004107 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004108 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004109 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004110 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004111 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004112 }
4113 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004114
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004115 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004116 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004117 };
4118
Ronald Cronab99ac22020-10-01 16:38:28 +02004119 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004120 if (cipher_operation == MBEDTLS_ENCRYPT) {
4121 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4122 &attributes,
4123 slot->key.data,
4124 slot->key.bytes,
4125 alg);
4126 } else {
4127 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4128 &attributes,
4129 slot->key.data,
4130 slot->key.bytes,
4131 alg);
4132 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004133
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004134exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004135 if (status != PSA_SUCCESS) {
4136 psa_cipher_abort(operation);
4137 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004138
Gilles Peskine449bd832023-01-11 14:50:10 +01004139 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004140
Gilles Peskine449bd832023-01-11 14:50:10 +01004141 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004142}
4143
Gilles Peskine449bd832023-01-11 14:50:10 +01004144psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4145 mbedtls_svc_key_id_t key,
4146 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004147{
Gilles Peskine449bd832023-01-11 14:50:10 +01004148 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004149}
4150
Gilles Peskine449bd832023-01-11 14:50:10 +01004151psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4152 mbedtls_svc_key_id_t key,
4153 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004154{
Gilles Peskine449bd832023-01-11 14:50:10 +01004155 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004156}
4157
Gilles Peskine449bd832023-01-11 14:50:10 +01004158psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4159 uint8_t *iv,
4160 size_t iv_size,
4161 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004162{
Ronald Cron6d051732020-10-01 14:10:20 +02004163 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004164 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004165 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004166
Gilles Peskine449bd832023-01-11 14:50:10 +01004167 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004168 status = PSA_ERROR_BAD_STATE;
4169 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004170 }
4171
Gilles Peskine449bd832023-01-11 14:50:10 +01004172 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004173 status = PSA_ERROR_BAD_STATE;
4174 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004175 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004176
Ronald Cron2fb90522021-07-05 12:31:44 +02004177 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004178 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004179 status = PSA_ERROR_BUFFER_TOO_SMALL;
4180 goto exit;
4181 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004182
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004184 status = PSA_ERROR_GENERIC_ERROR;
4185 goto exit;
4186 }
4187
Gilles Peskine449bd832023-01-11 14:50:10 +01004188 status = psa_generate_random(local_iv, default_iv_length);
4189 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004190 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 }
Ronald Cron5618a392021-03-26 09:52:26 +01004192
Gilles Peskine449bd832023-01-11 14:50:10 +01004193 status = psa_driver_wrapper_cipher_set_iv(operation,
4194 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004195
4196exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 if (status == PSA_SUCCESS) {
4198 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004199 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004200 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004201 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004202 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004204 }
Ronald Cron6d051732020-10-01 14:10:20 +02004205
Gilles Peskine449bd832023-01-11 14:50:10 +01004206 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004207}
4208
Gilles Peskine449bd832023-01-11 14:50:10 +01004209psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4210 const uint8_t *iv,
4211 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004212{
Ronald Cron6d051732020-10-01 14:10:20 +02004213 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004214
Gilles Peskine449bd832023-01-11 14:50:10 +01004215 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004216 status = PSA_ERROR_BAD_STATE;
4217 goto exit;
4218 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004219
Gilles Peskine449bd832023-01-11 14:50:10 +01004220 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004221 status = PSA_ERROR_BAD_STATE;
4222 goto exit;
4223 }
Ronald Crona0d68172021-03-26 10:15:08 +01004224
Gilles Peskine449bd832023-01-11 14:50:10 +01004225 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004226 status = PSA_ERROR_INVALID_ARGUMENT;
4227 goto exit;
4228 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004229
Gilles Peskine449bd832023-01-11 14:50:10 +01004230 status = psa_driver_wrapper_cipher_set_iv(operation,
4231 iv,
4232 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004233
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004234exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004235 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004236 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 } else {
4238 psa_cipher_abort(operation);
4239 }
4240 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004241}
4242
Gilles Peskine449bd832023-01-11 14:50:10 +01004243psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4244 const uint8_t *input,
4245 size_t input_length,
4246 uint8_t *output,
4247 size_t output_size,
4248 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004249{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004250 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004251
Gilles Peskine449bd832023-01-11 14:50:10 +01004252 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004253 status = PSA_ERROR_BAD_STATE;
4254 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004255 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004256
Gilles Peskine449bd832023-01-11 14:50:10 +01004257 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004258 status = PSA_ERROR_BAD_STATE;
4259 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004260 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004261
Gilles Peskine449bd832023-01-11 14:50:10 +01004262 status = psa_driver_wrapper_cipher_update(operation,
4263 input,
4264 input_length,
4265 output,
4266 output_size,
4267 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004268
4269exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004270 if (status != PSA_SUCCESS) {
4271 psa_cipher_abort(operation);
4272 }
Ronald Cron6d051732020-10-01 14:10:20 +02004273
Gilles Peskine449bd832023-01-11 14:50:10 +01004274 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004275}
4276
Gilles Peskine449bd832023-01-11 14:50:10 +01004277psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4278 uint8_t *output,
4279 size_t output_size,
4280 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004281{
David Saadab4ecc272019-02-14 13:48:10 +02004282 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004283
Gilles Peskine449bd832023-01-11 14:50:10 +01004284 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004285 status = PSA_ERROR_BAD_STATE;
4286 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004287 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004288
Gilles Peskine449bd832023-01-11 14:50:10 +01004289 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004290 status = PSA_ERROR_BAD_STATE;
4291 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004292 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004293
Gilles Peskine449bd832023-01-11 14:50:10 +01004294 status = psa_driver_wrapper_cipher_finish(operation,
4295 output,
4296 output_size,
4297 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004298
4299exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 if (status == PSA_SUCCESS) {
4301 return psa_cipher_abort(operation);
4302 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004303 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004304 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004305
Gilles Peskine449bd832023-01-11 14:50:10 +01004306 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004307 }
mohammad1603503973b2018-03-12 15:59:30 +02004308}
4309
Gilles Peskine449bd832023-01-11 14:50:10 +01004310psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004311{
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004313 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004314 * in use. It's ok to call abort on such an object, and there's
4315 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004316 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004317 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004318
Gilles Peskine449bd832023-01-11 14:50:10 +01004319 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004320
Ronald Cron49fafa92021-03-10 08:34:23 +01004321 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004322 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004323 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004324
Gilles Peskine449bd832023-01-11 14:50:10 +01004325 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004326}
4327
Gilles Peskine449bd832023-01-11 14:50:10 +01004328psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4329 psa_algorithm_t alg,
4330 const uint8_t *input,
4331 size_t input_length,
4332 uint8_t *output,
4333 size_t output_size,
4334 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004335{
4336 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004337 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004338 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004339 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4340 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004341 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004342
Gilles Peskine449bd832023-01-11 14:50:10 +01004343 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004344 status = PSA_ERROR_INVALID_ARGUMENT;
4345 goto exit;
4346 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004347
Gilles Peskine449bd832023-01-11 14:50:10 +01004348 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4349 PSA_KEY_USAGE_ENCRYPT,
4350 alg);
4351 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004352 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004354
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004355 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004356 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004357 };
4358
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4360 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004361 status = PSA_ERROR_GENERIC_ERROR;
4362 goto exit;
4363 }
4364
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 if (default_iv_length > 0) {
4366 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004367 status = PSA_ERROR_BUFFER_TOO_SMALL;
4368 goto exit;
4369 }
4370
Gilles Peskine449bd832023-01-11 14:50:10 +01004371 status = psa_generate_random(local_iv, default_iv_length);
4372 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004373 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004375 }
4376
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004377 status = psa_driver_wrapper_cipher_encrypt(
4378 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004379 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004380 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004381 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004382
4383exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004384 unlock_status = psa_unlock_key_slot(slot);
4385 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004386 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004387 }
Ronald Cron23919522021-07-08 19:00:07 +02004388
Gilles Peskine449bd832023-01-11 14:50:10 +01004389 if (status == PSA_SUCCESS) {
4390 if (default_iv_length > 0) {
4391 memcpy(output, local_iv, default_iv_length);
4392 }
4393 *output_length += default_iv_length;
4394 } else {
4395 *output_length = 0;
4396 }
4397
4398 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004399}
4400
Gilles Peskine449bd832023-01-11 14:50:10 +01004401psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4402 psa_algorithm_t alg,
4403 const uint8_t *input,
4404 size_t input_length,
4405 uint8_t *output,
4406 size_t output_size,
4407 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004408{
4409 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004410 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004411 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004412 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004413
Gilles Peskine449bd832023-01-11 14:50:10 +01004414 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004415 status = PSA_ERROR_INVALID_ARGUMENT;
4416 goto exit;
4417 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004418
Gilles Peskine449bd832023-01-11 14:50:10 +01004419 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4420 PSA_KEY_USAGE_DECRYPT,
4421 alg);
4422 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004423 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004424 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004425
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004426 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004428 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004429
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4431 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004432 status = PSA_ERROR_INVALID_ARGUMENT;
4433 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004434 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004435 status = PSA_ERROR_INVALID_ARGUMENT;
4436 goto exit;
4437 }
4438
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004439 status = psa_driver_wrapper_cipher_decrypt(
4440 &attributes, slot->key.data, slot->key.bytes,
4441 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004443
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004444exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 unlock_status = psa_unlock_key_slot(slot);
4446 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004447 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004449
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004451 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004452 }
Ronald Cron23919522021-07-08 19:00:07 +02004453
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004455}
4456
4457
mohammad16035955c982018-04-26 00:53:03 +03004458/****************************************************************/
4459/* AEAD */
4460/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004461
Paul Elliottbaff51c2021-09-28 17:44:45 +01004462/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004463static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004464{
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004466}
4467
4468/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004469static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4470 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004471{
Gilles Peskine449bd832023-01-11 14:50:10 +01004472 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004473
Gilles Peskine449bd832023-01-11 14:50:10 +01004474 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004475#if defined(PSA_WANT_ALG_GCM)
4476 case PSA_ALG_GCM:
4477 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 * arbitrarily large nonces. Please note that we do not generally
4479 * recommend the usage of nonces of greater length than
4480 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4481 * size, which can then lead to collisions if you encrypt a very
4482 * large number of messages.*/
4483 if (nonce_length != 0) {
4484 return PSA_SUCCESS;
4485 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004486 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004487#endif /* PSA_WANT_ALG_GCM */
4488#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004489 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 if (nonce_length >= 7 && nonce_length <= 13) {
4491 return PSA_SUCCESS;
4492 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004493 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004494#endif /* PSA_WANT_ALG_CCM */
4495#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004496 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 if (nonce_length == 12) {
4498 return PSA_SUCCESS;
4499 } else if (nonce_length == 8) {
4500 return PSA_ERROR_NOT_SUPPORTED;
4501 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004502 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004503#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004504 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004505 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004507 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004508
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004510}
4511
Gilles Peskine449bd832023-01-11 14:50:10 +01004512static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004513{
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4515 return PSA_ERROR_INVALID_ARGUMENT;
4516 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004517
Gilles Peskine449bd832023-01-11 14:50:10 +01004518 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004519}
4520
Gilles Peskine449bd832023-01-11 14:50:10 +01004521psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4522 psa_algorithm_t alg,
4523 const uint8_t *nonce,
4524 size_t nonce_length,
4525 const uint8_t *additional_data,
4526 size_t additional_data_length,
4527 const uint8_t *plaintext,
4528 size_t plaintext_length,
4529 uint8_t *ciphertext,
4530 size_t ciphertext_size,
4531 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004532{
Ronald Cron215633c2021-03-16 17:15:37 +01004533 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4534 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004535
mohammad1603f08a5502018-06-03 15:05:47 +03004536 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004537
Gilles Peskine449bd832023-01-11 14:50:10 +01004538 status = psa_aead_check_algorithm(alg);
4539 if (status != PSA_SUCCESS) {
4540 return status;
4541 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004542
Ronald Cron9a986162021-03-26 12:40:07 +01004543 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004544 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4545 if (status != PSA_SUCCESS) {
4546 return status;
4547 }
mohammad16035955c982018-04-26 00:53:03 +03004548
Ronald Cron215633c2021-03-16 17:15:37 +01004549 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004551 };
mohammad16035955c982018-04-26 00:53:03 +03004552
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 status = psa_aead_check_nonce_length(alg, nonce_length);
4554 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004555 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004557
Ronald Cronde822812021-03-17 16:08:20 +01004558 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004559 &attributes, slot->key.data, slot->key.bytes,
4560 alg,
4561 nonce, nonce_length,
4562 additional_data, additional_data_length,
4563 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004564 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004565
Gilles Peskine449bd832023-01-11 14:50:10 +01004566 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4567 memset(ciphertext, 0, ciphertext_size);
4568 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004569
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004570exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004572
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004574}
4575
Gilles Peskine449bd832023-01-11 14:50:10 +01004576psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4577 psa_algorithm_t alg,
4578 const uint8_t *nonce,
4579 size_t nonce_length,
4580 const uint8_t *additional_data,
4581 size_t additional_data_length,
4582 const uint8_t *ciphertext,
4583 size_t ciphertext_length,
4584 uint8_t *plaintext,
4585 size_t plaintext_size,
4586 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004587{
Ronald Cron215633c2021-03-16 17:15:37 +01004588 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4589 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004590
Gilles Peskineee652a32018-06-01 19:23:52 +02004591 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004592
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 status = psa_aead_check_algorithm(alg);
4594 if (status != PSA_SUCCESS) {
4595 return status;
4596 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004597
Ronald Cron9a986162021-03-26 12:40:07 +01004598 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4600 if (status != PSA_SUCCESS) {
4601 return status;
4602 }
mohammad16035955c982018-04-26 00:53:03 +03004603
Ronald Cron215633c2021-03-16 17:15:37 +01004604 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004606 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004607
Gilles Peskine449bd832023-01-11 14:50:10 +01004608 status = psa_aead_check_nonce_length(alg, nonce_length);
4609 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004610 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004612
Ronald Cronde822812021-03-17 16:08:20 +01004613 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004614 &attributes, slot->key.data, slot->key.bytes,
4615 alg,
4616 nonce, nonce_length,
4617 additional_data, additional_data_length,
4618 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004620
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 if (status != PSA_SUCCESS && plaintext_size != 0) {
4622 memset(plaintext, 0, plaintext_size);
4623 }
mohammad160360a64d02018-06-03 17:20:42 +03004624
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004625exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004626 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004627
Gilles Peskine449bd832023-01-11 14:50:10 +01004628 return status;
mohammad16035955c982018-04-26 00:53:03 +03004629}
4630
Gilles Peskine449bd832023-01-11 14:50:10 +01004631static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4632{
4633 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004634
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004636#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004637 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004638 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4640 return PSA_ERROR_INVALID_ARGUMENT;
4641 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004642 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004643#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004644
Przemek Stekiel86679c72022-10-06 17:06:56 +02004645#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004646 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004647 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4649 return PSA_ERROR_INVALID_ARGUMENT;
4650 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004651 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004652#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004653
Przemek Stekiel86679c72022-10-06 17:06:56 +02004654#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004656 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 if (tag_len != 16) {
4658 return PSA_ERROR_INVALID_ARGUMENT;
4659 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004660 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004661#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004662
4663 default:
4664 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004665 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004666 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004668}
4669
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004670/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004671static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4672 int is_encrypt,
4673 mbedtls_svc_key_id_t key,
4674 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004675{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004676 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4677 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4678 psa_key_slot_t *slot = NULL;
4679 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004680 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004681
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 status = psa_aead_check_algorithm(alg);
4683 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004684 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004686
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004688 status = PSA_ERROR_BAD_STATE;
4689 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004690 }
4691
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 if (operation->nonce_set || operation->lengths_set ||
4693 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004694 status = PSA_ERROR_BAD_STATE;
4695 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004696 }
4697
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004699 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004700 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004701 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004703
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4705 alg);
4706 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004707 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004708 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004709
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004710 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004711 .core = slot->attr
4712 };
4713
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004715 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004717
Gilles Peskine449bd832023-01-11 14:50:10 +01004718 if (is_encrypt) {
4719 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4720 &attributes,
4721 slot->key.data,
4722 slot->key.bytes,
4723 alg);
4724 } else {
4725 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4726 &attributes,
4727 slot->key.data,
4728 slot->key.bytes,
4729 alg);
4730 }
4731 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004732 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004733 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004734
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004736
4737exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004739
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004741 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004743 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 } else {
4745 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004746 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004747
Gilles Peskine449bd832023-01-11 14:50:10 +01004748 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004749}
4750
Paul Elliott302ff6b2021-04-20 18:10:30 +01004751/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004752psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4753 mbedtls_svc_key_id_t key,
4754 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004755{
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004757}
4758
4759/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004760psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4761 mbedtls_svc_key_id_t key,
4762 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004763{
Gilles Peskine449bd832023-01-11 14:50:10 +01004764 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004765}
4766
4767/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004768psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4769 uint8_t *nonce,
4770 size_t nonce_size,
4771 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004772{
4773 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004774 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004775 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004776
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004777 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004778
Gilles Peskine449bd832023-01-11 14:50:10 +01004779 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004780 status = PSA_ERROR_BAD_STATE;
4781 goto exit;
4782 }
4783
Gilles Peskine449bd832023-01-11 14:50:10 +01004784 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004785 status = PSA_ERROR_BAD_STATE;
4786 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004787 }
4788
Paul Elliotte193ea82021-10-01 13:00:16 +01004789 /* For CCM, this size may not be correct according to the PSA
4790 * specification. The PSA Crypto 1.0.1 specification states:
4791 *
4792 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4793 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4794 *
4795 * However this restriction that L has to be the smallest integer is not
4796 * applied in practice, and it is not implementable here since the
4797 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4799 operation->alg);
4800 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004801 status = PSA_ERROR_BUFFER_TOO_SMALL;
4802 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004803 }
4804
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 status = psa_generate_random(local_nonce, required_nonce_size);
4806 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004807 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004809
Gilles Peskine449bd832023-01-11 14:50:10 +01004810 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004811
Paul Elliott39dc6b82021-05-11 19:16:09 +01004812exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004813 if (status == PSA_SUCCESS) {
4814 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004815 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 } else {
4817 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004818 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004819
Gilles Peskine449bd832023-01-11 14:50:10 +01004820 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004821}
4822
4823/* Set the nonce for a multipart authenticated encryption or decryption
4824 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004825psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4826 const uint8_t *nonce,
4827 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004828{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004829 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4830
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004832 status = PSA_ERROR_BAD_STATE;
4833 goto exit;
4834 }
4835
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004837 status = PSA_ERROR_BAD_STATE;
4838 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004839 }
4840
Gilles Peskine449bd832023-01-11 14:50:10 +01004841 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4842 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004843 status = PSA_ERROR_INVALID_ARGUMENT;
4844 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004845 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004846
Gilles Peskine449bd832023-01-11 14:50:10 +01004847 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4848 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004849
Paul Elliott39dc6b82021-05-11 19:16:09 +01004850exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004852 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004853 } else {
4854 psa_aead_abort(operation);
4855 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004856
Gilles Peskine449bd832023-01-11 14:50:10 +01004857 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004858}
4859
4860/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004861psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4862 size_t ad_length,
4863 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004864{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004865 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4866
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004868 status = PSA_ERROR_BAD_STATE;
4869 goto exit;
4870 }
4871
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 if (operation->lengths_set || operation->ad_started ||
4873 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004874 status = PSA_ERROR_BAD_STATE;
4875 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004876 }
4877
Gilles Peskine449bd832023-01-11 14:50:10 +01004878 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004879#if defined(PSA_WANT_ALG_GCM)
4880 case PSA_ALG_GCM:
4881 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 * bits. Without the guard this code will generate warnings on 32bit
4883 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004884#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 if (((uint64_t) ad_length) >> 61 != 0 ||
4886 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004887 status = PSA_ERROR_INVALID_ARGUMENT;
4888 goto exit;
4889 }
Paul Elliott325d3742021-09-27 17:56:28 +01004890#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004891 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004892#endif /* PSA_WANT_ALG_GCM */
4893#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004894 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004896 status = PSA_ERROR_INVALID_ARGUMENT;
4897 goto exit;
4898 }
4899 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004900#endif /* PSA_WANT_ALG_CCM */
4901#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004902 case PSA_ALG_CHACHA20_POLY1305:
4903 /* No length restrictions for ChaChaPoly. */
4904 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004905#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004906 default:
4907 break;
4908 }
Paul Elliott325d3742021-09-27 17:56:28 +01004909
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4911 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004912
Paul Elliott39dc6b82021-05-11 19:16:09 +01004913exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004914 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004915 operation->ad_remaining = ad_length;
4916 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004917 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004918 } else {
4919 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004920 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004921
Gilles Peskine449bd832023-01-11 14:50:10 +01004922 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004923}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004924
4925/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004926psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4927 const uint8_t *input,
4928 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004929{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004930 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4931
Gilles Peskine449bd832023-01-11 14:50:10 +01004932 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004933 status = PSA_ERROR_BAD_STATE;
4934 goto exit;
4935 }
4936
Gilles Peskine449bd832023-01-11 14:50:10 +01004937 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004938 status = PSA_ERROR_BAD_STATE;
4939 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004940 }
4941
Gilles Peskine449bd832023-01-11 14:50:10 +01004942 if (operation->lengths_set) {
4943 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004944 status = PSA_ERROR_INVALID_ARGUMENT;
4945 goto exit;
4946 }
4947
4948 operation->ad_remaining -= input_length;
4949 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004950#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004951 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004952 status = PSA_ERROR_BAD_STATE;
4953 goto exit;
4954 }
4955#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004956
Gilles Peskine449bd832023-01-11 14:50:10 +01004957 status = psa_driver_wrapper_aead_update_ad(operation, input,
4958 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004959
Paul Elliott39dc6b82021-05-11 19:16:09 +01004960exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004962 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004963 } else {
4964 psa_aead_abort(operation);
4965 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004966
Gilles Peskine449bd832023-01-11 14:50:10 +01004967 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004968}
4969
4970/* Encrypt or decrypt a message fragment in an active multipart AEAD
4971 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004972psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4973 const uint8_t *input,
4974 size_t input_length,
4975 uint8_t *output,
4976 size_t output_size,
4977 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004978{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004979 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004980
4981 *output_length = 0;
4982
Gilles Peskine449bd832023-01-11 14:50:10 +01004983 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004984 status = PSA_ERROR_BAD_STATE;
4985 goto exit;
4986 }
4987
Gilles Peskine449bd832023-01-11 14:50:10 +01004988 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004989 status = PSA_ERROR_BAD_STATE;
4990 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004991 }
4992
Gilles Peskine449bd832023-01-11 14:50:10 +01004993 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004994 /* Additional data length was supplied, but not all the additional
4995 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004997 status = PSA_ERROR_INVALID_ARGUMENT;
4998 goto exit;
4999 }
5000
5001 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005003 status = PSA_ERROR_INVALID_ARGUMENT;
5004 goto exit;
5005 }
5006
5007 operation->body_remaining -= input_length;
5008 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005009#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005010 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005011 status = PSA_ERROR_BAD_STATE;
5012 goto exit;
5013 }
5014#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005015
Gilles Peskine449bd832023-01-11 14:50:10 +01005016 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5017 output, output_size,
5018 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005019
Paul Elliott39dc6b82021-05-11 19:16:09 +01005020exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005022 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005023 } else {
5024 psa_aead_abort(operation);
5025 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005026
Gilles Peskine449bd832023-01-11 14:50:10 +01005027 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005028}
5029
Gilles Peskine449bd832023-01-11 14:50:10 +01005030static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005031{
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 if (operation->id == 0 || !operation->nonce_set) {
5033 return PSA_ERROR_BAD_STATE;
5034 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005035
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5037 operation->body_remaining != 0)) {
5038 return PSA_ERROR_INVALID_ARGUMENT;
5039 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005040
Gilles Peskine449bd832023-01-11 14:50:10 +01005041 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005042}
5043
Paul Elliott302ff6b2021-04-20 18:10:30 +01005044/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005045psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5046 uint8_t *ciphertext,
5047 size_t ciphertext_size,
5048 size_t *ciphertext_length,
5049 uint8_t *tag,
5050 size_t tag_size,
5051 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005052{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005053 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5054
Paul Elliott302ff6b2021-04-20 18:10:30 +01005055 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005056 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005057
Gilles Peskine449bd832023-01-11 14:50:10 +01005058 status = psa_aead_final_checks(operation);
5059 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005060 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005061 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005062
Gilles Peskine449bd832023-01-11 14:50:10 +01005063 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005064 status = PSA_ERROR_BAD_STATE;
5065 goto exit;
5066 }
5067
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5069 ciphertext_size,
5070 ciphertext_length,
5071 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005072
Paul Elliott39dc6b82021-05-11 19:16:09 +01005073exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005074
5075
Paul Elliottf47b0952021-05-21 18:02:33 +01005076 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005077 * the zero tag size, make sure the tag is set to something implausible.
5078 * Even if the operation succeeds, make sure we clear the rest of the
5079 * buffer to prevent potential leakage of anything previously placed in
5080 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005081 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005082
Gilles Peskine449bd832023-01-11 14:50:10 +01005083 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005084
Gilles Peskine449bd832023-01-11 14:50:10 +01005085 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005086}
5087
5088/* Finish authenticating and decrypting a message in a multipart AEAD
5089 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005090psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5091 uint8_t *plaintext,
5092 size_t plaintext_size,
5093 size_t *plaintext_length,
5094 const uint8_t *tag,
5095 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005096{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005097 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5098
Paul Elliott302ff6b2021-04-20 18:10:30 +01005099 *plaintext_length = 0;
5100
Gilles Peskine449bd832023-01-11 14:50:10 +01005101 status = psa_aead_final_checks(operation);
5102 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005103 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005104 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005105
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005107 status = PSA_ERROR_BAD_STATE;
5108 goto exit;
5109 }
5110
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5112 plaintext_size,
5113 plaintext_length,
5114 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005115
5116exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005118
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005120}
5121
5122/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005123psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005124{
5125 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5126
Gilles Peskine449bd832023-01-11 14:50:10 +01005127 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005128 /* The object has (apparently) been initialized but it is not (yet)
5129 * in use. It's ok to call abort on such an object, and there's
5130 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005132 }
5133
Gilles Peskine449bd832023-01-11 14:50:10 +01005134 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005135
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005137
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005139}
5140
Gilles Peskinee59236f2018-01-27 23:32:46 +01005141/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005142/* Generators */
5143/****************************************************************/
5144
Przemek Stekiel69c46792022-06-10 12:59:51 +02005145#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005146 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005147 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305148 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305149 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005150#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005151#endif /* At least one builtin KDF */
5152
Przemek Stekiel69c46792022-06-10 12:59:51 +02005153#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005154 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5155 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5156static psa_status_t psa_key_derivation_start_hmac(
5157 psa_mac_operation_t *operation,
5158 psa_algorithm_t hash_alg,
5159 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005161{
5162 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5163 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5165 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5166 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005167
Steven Cooreman72f736a2021-05-07 14:14:37 +02005168 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005170
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 status = psa_driver_wrapper_mac_sign_setup(operation,
5172 &attributes,
5173 hmac_key, hmac_key_length,
5174 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005175
Gilles Peskine449bd832023-01-11 14:50:10 +01005176 psa_reset_key_attributes(&attributes);
5177 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005178}
5179#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005180
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005181#define HKDF_STATE_INIT 0 /* no input yet */
5182#define HKDF_STATE_STARTED 1 /* got salt */
5183#define HKDF_STATE_KEYED 2 /* got key */
5184#define HKDF_STATE_OUTPUT 3 /* output started */
5185
Gilles Peskinecbe66502019-05-16 16:59:18 +02005186static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005188{
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5190 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5191 } else {
5192 return operation->alg;
5193 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005194}
5195
Gilles Peskine449bd832023-01-11 14:50:10 +01005196psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005197{
5198 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005199 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5200 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005201 /* The object has (apparently) been initialized but it is not
5202 * in use. It's ok to call abort on such an object, and there's
5203 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005204 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005205#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5207 mbedtls_free(operation->ctx.hkdf.info);
5208 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5209 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005210#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005211#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5212 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005213 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5214 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5215 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5216 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005217 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005219 }
5220
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005222 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005223 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005224 }
5225
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005227 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005228 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005229 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005230#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005232 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005233 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005234 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005235#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005236 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005237
5238 /* We leave the fields Ai and output_block to be erased safely by the
5239 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005240 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005241#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5242 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005243#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5245 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5246 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5247 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005248#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305249#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305250 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305251 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005252 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305253 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305254 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305255
5256 status = PSA_SUCCESS;
5257 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305258#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005259 {
5260 status = PSA_ERROR_BAD_STATE;
5261 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 mbedtls_platform_zeroize(operation, sizeof(*operation));
5263 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005264}
5265
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005266psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005268{
Gilles Peskine449bd832023-01-11 14:50:10 +01005269 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005270 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005272 }
5273
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005274 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005276}
5277
Gilles Peskine449bd832023-01-11 14:50:10 +01005278psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5279 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005280{
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 if (operation->alg == 0) {
5282 return PSA_ERROR_BAD_STATE;
5283 }
5284 if (capacity > operation->capacity) {
5285 return PSA_ERROR_INVALID_ARGUMENT;
5286 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005287 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005288 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005289}
5290
Przemek Stekiel69c46792022-06-10 12:59:51 +02005291#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005292/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005293static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5294 psa_algorithm_t kdf_alg,
5295 uint8_t *output,
5296 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005297{
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5299 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005300 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005301 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005302#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005303 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005304#else
5305 const uint8_t last_block = 0xff;
5306#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005307
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 if (hkdf->state < HKDF_STATE_KEYED ||
5309 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005310#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005312#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 )) {
5314 return PSA_ERROR_BAD_STATE;
5315 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005316 hkdf->state = HKDF_STATE_OUTPUT;
5317
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005319 /* Copy what remains of the current block */
5320 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005321 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005322 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 }
5324 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005325 output += n;
5326 output_length -= n;
5327 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005329 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005331 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005332 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005333 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005334 * object was corrupted or if this function is called directly
5335 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 if (hkdf->block_number == last_block) {
5337 return PSA_ERROR_BAD_STATE;
5338 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005339
5340 /* We need a new block */
5341 ++hkdf->block_number;
5342 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005343
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5345 hash_alg,
5346 hkdf->prk,
5347 hash_length);
5348 if (status != PSA_SUCCESS) {
5349 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005350 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005351
5352 if (hkdf->block_number != 1) {
5353 status = psa_mac_update(&hkdf->hmac,
5354 hkdf->output_block,
5355 hash_length);
5356 if (status != PSA_SUCCESS) {
5357 return status;
5358 }
5359 }
5360 status = psa_mac_update(&hkdf->hmac,
5361 hkdf->info,
5362 hkdf->info_length);
5363 if (status != PSA_SUCCESS) {
5364 return status;
5365 }
5366 status = psa_mac_update(&hkdf->hmac,
5367 &hkdf->block_number, 1);
5368 if (status != PSA_SUCCESS) {
5369 return status;
5370 }
5371 status = psa_mac_sign_finish(&hkdf->hmac,
5372 hkdf->output_block,
5373 sizeof(hkdf->output_block),
5374 &hmac_output_length);
5375 if (status != PSA_SUCCESS) {
5376 return status;
5377 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005378 }
5379
Gilles Peskine449bd832023-01-11 14:50:10 +01005380 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005381}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005382#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005383
John Durkop07cc04a2020-11-16 22:08:34 -08005384#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5385 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005386static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5387 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005389{
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5391 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005392 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5393 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005394 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005395
Janos Follath7742fee2019-06-17 12:58:10 +01005396 /* We can't be wanting more output after block 0xff, otherwise
5397 * the capacity check in psa_key_derivation_output_bytes() would have
5398 * prevented this call. It could happen only if the operation
5399 * object was corrupted or if this function is called directly
5400 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 if (tls12_prf->block_number == 0xff) {
5402 return PSA_ERROR_CORRUPTION_DETECTED;
5403 }
Janos Follath7742fee2019-06-17 12:58:10 +01005404
5405 /* We need a new block */
5406 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005407 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005408
5409 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5410 *
5411 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5412 *
5413 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5414 * HMAC_hash(secret, A(2) + seed) +
5415 * HMAC_hash(secret, A(3) + seed) + ...
5416 *
5417 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005418 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005419 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005420 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005421 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005422 * is currently extracted as `output_block` and where i is
5423 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005424 */
5425
Gilles Peskine449bd832023-01-11 14:50:10 +01005426 status = psa_key_derivation_start_hmac(&hmac,
5427 hash_alg,
5428 tls12_prf->secret,
5429 tls12_prf->secret_length);
5430 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005431 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005433
5434 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005436 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5437 * the variable seed and in this instance means it in the context of the
5438 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 status = psa_mac_update(&hmac,
5440 tls12_prf->label,
5441 tls12_prf->label_length);
5442 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005443 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 }
5445 status = psa_mac_update(&hmac,
5446 tls12_prf->seed,
5447 tls12_prf->seed_length);
5448 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005449 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 }
5451 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005452 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5454 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005455 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005457 }
5458
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 status = psa_mac_sign_finish(&hmac,
5460 tls12_prf->Ai, hash_length,
5461 &hmac_output_length);
5462 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005463 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 }
5465 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005466 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005468
5469 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 status = psa_key_derivation_start_hmac(&hmac,
5471 hash_alg,
5472 tls12_prf->secret,
5473 tls12_prf->secret_length);
5474 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005475 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 }
5477 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5478 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005479 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 }
5481 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5482 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005483 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 }
5485 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5486 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005487 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 }
5489 status = psa_mac_sign_finish(&hmac,
5490 tls12_prf->output_block, hash_length,
5491 &hmac_output_length);
5492 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005493 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005494 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005495
Janos Follath7742fee2019-06-17 12:58:10 +01005496
5497cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 cleanup_status = psa_mac_abort(&hmac);
5499 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005500 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005502
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005504}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005505
Janos Follath844eb0e2019-06-19 12:10:49 +01005506static psa_status_t psa_key_derivation_tls12_prf_read(
5507 psa_tls12_prf_key_derivation_t *tls12_prf,
5508 psa_algorithm_t alg,
5509 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005511{
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5513 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005514 psa_status_t status;
5515 uint8_t offset, length;
5516
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005518 case PSA_TLS12_PRF_STATE_LABEL_SET:
5519 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5520 break;
5521 case PSA_TLS12_PRF_STATE_OUTPUT:
5522 break;
5523 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005525 }
5526
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005528 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 if (tls12_prf->left_in_block == 0) {
5530 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5531 alg);
5532 if (status != PSA_SUCCESS) {
5533 return status;
5534 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005535
5536 continue;
5537 }
5538
Gilles Peskine449bd832023-01-11 14:50:10 +01005539 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005540 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005541 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005542 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005544
5545 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005546 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005547 output += length;
5548 output_length -= length;
5549 tls12_prf->left_in_block -= length;
5550 }
5551
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005553}
John Durkop07cc04a2020-11-16 22:08:34 -08005554#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5555 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005556
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005557#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5558static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5559 psa_tls12_ecjpake_to_pms_t *ecjpake,
5560 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005561 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005562{
5563 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005564 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005565
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 if (output_length != 32) {
5567 return PSA_ERROR_INVALID_ARGUMENT;
5568 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005569
Gilles Peskine449bd832023-01-11 14:50:10 +01005570 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5571 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5572 &output_size);
5573 if (status != PSA_SUCCESS) {
5574 return status;
5575 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005576
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 if (output_size != output_length) {
5578 return PSA_ERROR_GENERIC_ERROR;
5579 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005580
Gilles Peskine449bd832023-01-11 14:50:10 +01005581 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005582}
5583#endif
5584
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305585#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305586static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5587 psa_pbkdf2_key_derivation_t *pbkdf2,
5588 psa_algorithm_t prf_alg,
5589 uint8_t prf_output_length,
5590 psa_key_attributes_t *attributes)
5591{
5592 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305593 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305594 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305595 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305596 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305597 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305598 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305599
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305600 mac_operation.is_sign = 1;
5601 mac_operation.mac_size = prf_output_length;
5602 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305603
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305604 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5605 attributes,
5606 pbkdf2->password,
5607 pbkdf2->password_length,
5608 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305609 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305610 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305611 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305612 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5613 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305614 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305615 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305616 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305617 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305618 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305619 }
5620 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5621 &mac_output_length);
5622 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305623 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305624 }
5625
5626 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305627 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305628 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305629 }
5630
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305631 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305632
5633 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305634 /* We are passing prf_output_length as mac_size because the driver
5635 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305636 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305637 status = psa_driver_wrapper_mac_compute(attributes,
5638 pbkdf2->password,
5639 pbkdf2->password_length,
5640 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305641 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305642 &mac_output_length);
5643 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305644 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305645 }
5646
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305647 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305648 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305649
5650cleanup:
5651 /* Zeroise buffers to clear sensitive data from memory. */
5652 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5653 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305654}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305655
5656static psa_status_t psa_key_derivation_pbkdf2_read(
5657 psa_pbkdf2_key_derivation_t *pbkdf2,
5658 psa_algorithm_t kdf_alg,
5659 uint8_t *output,
5660 size_t output_length)
5661{
5662 psa_status_t status;
5663 psa_algorithm_t prf_alg;
5664 uint8_t prf_output_length;
5665 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5666 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5667 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5668
5669 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5670 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5671 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5672 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305673 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5674 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305675 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305676 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305677 } else {
5678 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305679 }
5680
5681 switch (pbkdf2->state) {
5682 case PSA_PBKDF2_STATE_PASSWORD_SET:
5683 /* Initially we need a new block so bytes_used is equal to block size*/
5684 pbkdf2->bytes_used = prf_output_length;
5685 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5686 break;
5687 case PSA_PBKDF2_STATE_OUTPUT:
5688 break;
5689 default:
5690 return PSA_ERROR_BAD_STATE;
5691 }
5692
5693 while (output_length != 0) {
5694 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5695 if (n > output_length) {
5696 n = (uint8_t) output_length;
5697 }
5698 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5699 output += n;
5700 output_length -= n;
5701 pbkdf2->bytes_used += n;
5702
5703 if (output_length == 0) {
5704 break;
5705 }
5706
5707 /* We need a new block */
5708 pbkdf2->bytes_used = 0;
5709 pbkdf2->block_number++;
5710
5711 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5712 prf_output_length,
5713 &attributes);
5714 if (status != PSA_SUCCESS) {
5715 return status;
5716 }
5717 }
5718
5719 return PSA_SUCCESS;
5720}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305721#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305722
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005723psa_status_t psa_key_derivation_output_bytes(
5724 psa_key_derivation_operation_t *operation,
5725 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005727{
5728 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005730
Gilles Peskine449bd832023-01-11 14:50:10 +01005731 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005732 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005734 }
5735
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005737 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005738 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005739 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005740 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005741 goto exit;
5742 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005744 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005745 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005746 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5747 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005748 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005749 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005750 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005751 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005752 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005753
Przemek Stekiel69c46792022-06-10 12:59:51 +02005754#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005755 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5756 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5757 output, output_length);
5758 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005759#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005760#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5761 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5763 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5764 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5765 kdf_alg, output,
5766 output_length);
5767 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005768#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5769 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005770#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005772 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5774 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005775#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305776#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305777 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305778 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5779 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305780 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305781#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005782
Gilles Peskineeab56e42018-07-12 17:12:33 +02005783 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005784 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005786 }
5787
5788exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005790 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005791 * This allows us to differentiate between exhausted operations and
5792 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5793 * operations. */
5794 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005795 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005796 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005797 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005798 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005800}
5801
David Brown7807bf72021-02-09 16:28:23 -07005802#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005803static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005804{
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 if (data_size >= 8) {
5806 mbedtls_des_key_set_parity(data);
5807 }
5808 if (data_size >= 16) {
5809 mbedtls_des_key_set_parity(data + 8);
5810 }
5811 if (data_size >= 24) {
5812 mbedtls_des_key_set_parity(data + 16);
5813 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005814}
David Brown7807bf72021-02-09 16:28:23 -07005815#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005816
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005817/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 * ECC keys on a Weierstrass elliptic curve require the generation
5819 * of a private key which is an integer
5820 * in the range [1, N - 1], where N is the boundary of the private key domain:
5821 * N is the prime p for Diffie-Hellman, or the order of the
5822 * curve’s base point for ECC.
5823 *
5824 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5825 * This function generates the private key using the following process:
5826 *
5827 * 1. Draw a byte string of length ceiling(m/8) bytes.
5828 * 2. If m is not a multiple of 8, set the most significant
5829 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5830 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5831 * 4. If k > N - 2, discard the result and return to step 1.
5832 * 5. Output k + 1 as the private key.
5833 *
5834 * This method allows compliance to NIST standards, specifically the methods titled
5835 * Key-Pair Generation by Testing Candidates in the following publications:
5836 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5837 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5838 * Diffie-Hellman keys.
5839 *
5840 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5841 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5842 *
5843 * Note: Function allocates memory for *data buffer, so given *data should be
5844 * always NULL.
5845 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005846#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5847#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005848static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005849 psa_key_slot_t *slot,
5850 size_t bits,
5851 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005852 uint8_t **data
5853 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005854{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005855 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005856 mbedtls_mpi k;
5857 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005858 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005859 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005860 size_t m;
5861 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005862
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 mbedtls_mpi_init(&k);
5864 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005865
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005866 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005868 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005869 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005870
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005872 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005873 goto cleanup;
5874 }
5875
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005876 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005878
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005880
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005881 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005882 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005883 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005884
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005885 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005886
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005887 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005888 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005889
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005890 /* Note: This function is always called with *data == NULL and it
5891 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005892 *data = mbedtls_calloc(1, m_bytes);
5893 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005894 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005895 goto cleanup;
5896 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005897
Gilles Peskine449bd832023-01-11 14:50:10 +01005898 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005899 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005900 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005901 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005902 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005903
5904 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005906 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005907 * (8 * ceiling(m/8) - m) bits of the first byte in
5908 * the string to zero.
5909 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005910 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005911 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005912 }
5913
5914 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005915 * big-endian byte string.
5916 */
5917 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005918
5919 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 * Result of comparison is returned. When it indicates error
5921 * then this function is called again.
5922 */
5923 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005924 }
5925
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005926 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005927 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5928 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005929cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 if (ret != 0) {
5931 status = mbedtls_to_psa_error(ret);
5932 }
5933 if (status != PSA_SUCCESS) {
5934 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005935 *data = NULL;
5936 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005937 mbedtls_mpi_free(&k);
5938 mbedtls_mpi_free(&diff_N_2);
5939 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005940}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005941
5942/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5943 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5944 *
5945 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5946 * draw a 32-byte string and process it as specified in
5947 * Elliptic Curves for Security [RFC7748] §5.
5948 *
5949 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5950 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5951 *
5952 * Note: Function allocates memory for *data buffer, so given *data should be
5953 * always NULL.
5954 */
5955
5956static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5957 size_t bits,
5958 psa_key_derivation_operation_t *operation,
5959 uint8_t **data
5960 )
5961{
5962 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005963 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005964
Gilles Peskine449bd832023-01-11 14:50:10 +01005965 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005966 case 255:
5967 output_length = 32;
5968 break;
5969 case 448:
5970 output_length = 56;
5971 break;
5972 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005974 break;
5975 }
5976
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005978
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 if (*data == NULL) {
5980 return PSA_ERROR_INSUFFICIENT_MEMORY;
5981 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005982
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005984
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005986 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005987 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005988
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005990 case 255:
5991 (*data)[0] &= 248;
5992 (*data)[31] &= 127;
5993 (*data)[31] |= 64;
5994 break;
5995 case 448:
5996 (*data)[0] &= 252;
5997 (*data)[55] |= 128;
5998 break;
5999 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006001 break;
6002 }
6003
6004 return status;
6005}
Valerio Setti86587ab2023-06-27 13:09:30 +02006006#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6007static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6008 psa_key_slot_t *slot, size_t bits,
6009 psa_key_derivation_operation_t *operation, uint8_t **data)
6010{
6011 (void) slot;
6012 (void) bits;
6013 (void) operation;
6014 (void) data;
6015 return PSA_ERROR_NOT_SUPPORTED;
6016}
6017
6018static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6019 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6020{
6021 (void) bits;
6022 (void) operation;
6023 (void) data;
6024 return PSA_ERROR_NOT_SUPPORTED;
6025}
6026#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6027#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006028
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006029static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006030 psa_key_slot_t *slot,
6031 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006032 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006033{
6034 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006035 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306036 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006037 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006038 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006039
Gilles Peskine449bd832023-01-11 14:50:10 +01006040 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6041 return PSA_ERROR_INVALID_ARGUMENT;
6042 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006043
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006044#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006045 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6047 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6048 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006049 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006050 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6051 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006052 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 }
6054 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006055 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6057 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006058 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006059 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006060 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006061 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006062#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006063 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 if (key_type_is_raw_bytes(slot->attr.type)) {
6065 if (bits % 8 != 0) {
6066 return PSA_ERROR_INVALID_ARGUMENT;
6067 }
6068 data = mbedtls_calloc(1, bytes);
6069 if (data == NULL) {
6070 return PSA_ERROR_INSUFFICIENT_MEMORY;
6071 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006072
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 status = psa_key_derivation_output_bytes(operation, data, bytes);
6074 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006075 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 }
David Brown12ca5032021-02-11 11:02:00 -07006077#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6079 psa_des_set_key_parity(data, bytes);
6080 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006081#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 } else {
6083 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006084 }
Ronald Crondd04d422020-11-28 15:14:42 +01006085
Ronald Cronbf33c932020-11-28 18:06:53 +01006086 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006087 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006089 };
6090
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6092 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6093 &storage_size);
6094 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306095 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 }
Archanad8a83dc2021-06-14 10:04:16 +05306097 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 status = psa_allocate_buffer_to_slot(slot, storage_size);
6099 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306100 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 }
Archanad8a83dc2021-06-14 10:04:16 +05306102
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 status = psa_driver_wrapper_import_key(&attributes,
6104 data, bytes,
6105 slot->key.data,
6106 slot->key.bytes,
6107 &slot->key.bytes, &bits);
6108 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006109 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006111
6112exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 mbedtls_free(data);
6114 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006115}
6116
Gilles Peskine449bd832023-01-11 14:50:10 +01006117psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6118 psa_key_derivation_operation_t *operation,
6119 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006120{
6121 psa_status_t status;
6122 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006123 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006124
Ronald Cron81709fc2020-11-14 12:10:32 +01006125 *key = MBEDTLS_SVC_KEY_ID_INIT;
6126
Gilles Peskine0f84d622019-09-12 19:03:13 +02006127 /* Reject any attempt to create a zero-length key so that we don't
6128 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 if (psa_get_key_bits(attributes) == 0) {
6130 return PSA_ERROR_INVALID_ARGUMENT;
6131 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006132
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 if (operation->alg == PSA_ALG_NONE) {
6134 return PSA_ERROR_BAD_STATE;
6135 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006136
Gilles Peskine449bd832023-01-11 14:50:10 +01006137 if (!operation->can_output_key) {
6138 return PSA_ERROR_NOT_PERMITTED;
6139 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006140
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6142 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006143#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006145 /* Deriving a key in a secure element is not implemented yet. */
6146 status = PSA_ERROR_NOT_SUPPORTED;
6147 }
6148#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 if (status == PSA_SUCCESS) {
6150 status = psa_generate_derived_key_internal(slot,
6151 attributes->core.bits,
6152 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006153 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 if (status == PSA_SUCCESS) {
6155 status = psa_finish_key_creation(slot, driver, key);
6156 }
6157 if (status != PSA_SUCCESS) {
6158 psa_fail_key_creation(slot, driver);
6159 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006160
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006162}
6163
Gilles Peskineeab56e42018-07-12 17:12:33 +02006164
6165
6166/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006167/* Key derivation */
6168/****************************************************************/
6169
Steven Cooreman932ffb72021-02-15 12:14:32 +01006170#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006171static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006172{
6173#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6175 return 1;
6176 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006177#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006178#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006179 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6180 return 1;
6181 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006182#endif
6183#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006184 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6185 return 1;
6186 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006187#endif
6188#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6190 return 1;
6191 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006192#endif
6193#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006194 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6195 return 1;
6196 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006197#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006198#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006199 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6200 return 1;
6201 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006202#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306203#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6204 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6205 return 1;
6206 }
6207#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306208#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6209 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6210 return 1;
6211 }
6212#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006214}
6215
Gilles Peskine449bd832023-01-11 14:50:10 +01006216static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006217{
6218 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 psa_status_t status = psa_hash_setup(&operation, alg);
6220 psa_hash_abort(&operation);
6221 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006222}
6223
Gilles Peskine969c5d62019-01-16 15:53:06 +01006224static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006225 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006227{
Janos Follath5fe19732019-06-20 15:09:30 +01006228 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6229 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006230 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006231
Gilles Peskine969c5d62019-01-16 15:53:06 +01006232 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006233 if (!is_kdf_alg_supported(kdf_alg)) {
6234 return PSA_ERROR_NOT_SUPPORTED;
6235 }
John Durkop07cc04a2020-11-16 22:08:34 -08006236
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006237 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306238 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6240 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306241 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6242 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6243 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306244 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306245 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 if (hash_size == 0) {
6247 return PSA_ERROR_NOT_SUPPORTED;
6248 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006249
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006250 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6251 * we might fail later, which is somewhat unfriendly and potentially
6252 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 psa_status_t status = psa_hash_try_support(hash_alg);
6254 if (status != PSA_SUCCESS) {
6255 return status;
6256 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006257 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006258
Gilles Peskine449bd832023-01-11 14:50:10 +01006259 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6260 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6261 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6262 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006263 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006264#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006265 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6267 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006268 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006270#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6271 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 operation->capacity = 255 * hash_size;
6273 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006274}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006275
Gilles Peskine449bd832023-01-11 14:50:10 +01006276static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006277{
6278#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 if (alg == PSA_ALG_ECDH) {
6280 return PSA_SUCCESS;
6281 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006282#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006283#if defined(PSA_WANT_ALG_FFDH)
6284 if (alg == PSA_ALG_FFDH) {
6285 return PSA_SUCCESS;
6286 }
6287#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006288 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006290}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006291
6292static int psa_key_derivation_allows_free_form_secret_input(
6293 psa_algorithm_t kdf_alg)
6294{
6295#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6296 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6297 return 0;
6298 }
6299#endif
6300 (void) kdf_alg;
6301 return 1;
6302}
John Durkop07cc04a2020-11-16 22:08:34 -08006303#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006304
Gilles Peskine449bd832023-01-11 14:50:10 +01006305psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6306 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006307{
6308 psa_status_t status;
6309
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 if (operation->alg != 0) {
6311 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006312 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006313
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6315 return PSA_ERROR_INVALID_ARGUMENT;
6316 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6317#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6318 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6319 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6320 status = psa_key_agreement_try_support(ka_alg);
6321 if (status != PSA_SUCCESS) {
6322 return status;
6323 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006324 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6325 return PSA_ERROR_INVALID_ARGUMENT;
6326 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6328#else
6329 return PSA_ERROR_NOT_SUPPORTED;
6330#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6331 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6332#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6333 status = psa_key_derivation_setup_kdf(operation, alg);
6334#else
6335 return PSA_ERROR_NOT_SUPPORTED;
6336#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6337 } else {
6338 return PSA_ERROR_INVALID_ARGUMENT;
6339 }
6340
6341 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006342 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 }
6344 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006345}
6346
Przemek Stekiel69c46792022-06-10 12:59:51 +02006347#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006348static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6349 psa_algorithm_t kdf_alg,
6350 psa_key_derivation_step_t step,
6351 const uint8_t *data,
6352 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006353{
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006355 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006357 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006358#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6360 return PSA_ERROR_INVALID_ARGUMENT;
6361 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006362#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 if (hkdf->state != HKDF_STATE_INIT) {
6364 return PSA_ERROR_BAD_STATE;
6365 } else {
6366 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6367 hash_alg,
6368 data, data_length);
6369 if (status != PSA_SUCCESS) {
6370 return status;
6371 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006372 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006374 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006375 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006376#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006378 /* We shouldn't be in different state as HKDF_EXPAND only allows
6379 * two inputs: SECRET (this case) and INFO which does not modify
6380 * the state. It could happen only if the hkdf
6381 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 if (hkdf->state != HKDF_STATE_INIT) {
6383 return PSA_ERROR_BAD_STATE;
6384 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006385
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006386 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6388 return PSA_ERROR_INVALID_ARGUMENT;
6389 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006390
Gilles Peskine449bd832023-01-11 14:50:10 +01006391 memcpy(hkdf->prk, data, data_length);
6392 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006393#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006394 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006395 /* HKDF: If no salt was provided, use an empty salt.
6396 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006398#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6400 return PSA_ERROR_BAD_STATE;
6401 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006402#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6404 hash_alg,
6405 NULL, 0);
6406 if (status != PSA_SUCCESS) {
6407 return status;
6408 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006409 hkdf->state = HKDF_STATE_STARTED;
6410 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 if (hkdf->state != HKDF_STATE_STARTED) {
6412 return PSA_ERROR_BAD_STATE;
6413 }
6414 status = psa_mac_update(&hkdf->hmac,
6415 data, data_length);
6416 if (status != PSA_SUCCESS) {
6417 return status;
6418 }
6419 status = psa_mac_sign_finish(&hkdf->hmac,
6420 hkdf->prk,
6421 sizeof(hkdf->prk),
6422 &data_length);
6423 if (status != PSA_SUCCESS) {
6424 return status;
6425 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006426 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006427
Gilles Peskine2b522db2019-04-12 15:11:49 +02006428 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006429 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006430#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006432 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006434 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006435 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006436#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006437 {
6438 /* Block 0 is empty, and the next block will be
6439 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006441 }
6442
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006444 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006445#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6447 return PSA_ERROR_INVALID_ARGUMENT;
6448 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006449#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006450#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6452 hkdf->state == HKDF_STATE_INIT) {
6453 return PSA_ERROR_BAD_STATE;
6454 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006455#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006456 if (hkdf->state == HKDF_STATE_OUTPUT) {
6457 return PSA_ERROR_BAD_STATE;
6458 }
6459 if (hkdf->info_set) {
6460 return PSA_ERROR_BAD_STATE;
6461 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006462 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 if (data_length != 0) {
6464 hkdf->info = mbedtls_calloc(1, data_length);
6465 if (hkdf->info == NULL) {
6466 return PSA_ERROR_INSUFFICIENT_MEMORY;
6467 }
6468 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006469 }
6470 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006472 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006474 }
6475}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006476#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006477
John Durkop07cc04a2020-11-16 22:08:34 -08006478#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6479 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006480static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6481 const uint8_t *data,
6482 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006483{
Gilles Peskine449bd832023-01-11 14:50:10 +01006484 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6485 return PSA_ERROR_BAD_STATE;
6486 }
Janos Follathf08e2652019-06-13 09:05:41 +01006487
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 if (data_length != 0) {
6489 prf->seed = mbedtls_calloc(1, data_length);
6490 if (prf->seed == NULL) {
6491 return PSA_ERROR_INSUFFICIENT_MEMORY;
6492 }
Janos Follathf08e2652019-06-13 09:05:41 +01006493
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006495 prf->seed_length = data_length;
6496 }
Janos Follathf08e2652019-06-13 09:05:41 +01006497
Gilles Peskine43f958b2020-12-13 14:55:14 +01006498 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006499
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006501}
6502
Gilles Peskine449bd832023-01-11 14:50:10 +01006503static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6504 const uint8_t *data,
6505 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006506{
Gilles Peskine449bd832023-01-11 14:50:10 +01006507 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6508 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6509 return PSA_ERROR_BAD_STATE;
6510 }
Janos Follath81550542019-06-13 14:26:34 +01006511
Gilles Peskine449bd832023-01-11 14:50:10 +01006512 if (data_length != 0) {
6513 prf->secret = mbedtls_calloc(1, data_length);
6514 if (prf->secret == NULL) {
6515 return PSA_ERROR_INSUFFICIENT_MEMORY;
6516 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006517
Gilles Peskine449bd832023-01-11 14:50:10 +01006518 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006519 prf->secret_length = data_length;
6520 }
Janos Follath81550542019-06-13 14:26:34 +01006521
Gilles Peskine43f958b2020-12-13 14:55:14 +01006522 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006523
Gilles Peskine449bd832023-01-11 14:50:10 +01006524 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006525}
6526
Gilles Peskine449bd832023-01-11 14:50:10 +01006527static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6528 const uint8_t *data,
6529 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006530{
Gilles Peskine449bd832023-01-11 14:50:10 +01006531 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6532 return PSA_ERROR_BAD_STATE;
6533 }
Janos Follath63028dd2019-06-13 09:15:47 +01006534
Gilles Peskine449bd832023-01-11 14:50:10 +01006535 if (data_length != 0) {
6536 prf->label = mbedtls_calloc(1, data_length);
6537 if (prf->label == NULL) {
6538 return PSA_ERROR_INSUFFICIENT_MEMORY;
6539 }
Janos Follath63028dd2019-06-13 09:15:47 +01006540
Gilles Peskine449bd832023-01-11 14:50:10 +01006541 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006542 prf->label_length = data_length;
6543 }
Janos Follath63028dd2019-06-13 09:15:47 +01006544
Gilles Peskine43f958b2020-12-13 14:55:14 +01006545 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006546
Gilles Peskine449bd832023-01-11 14:50:10 +01006547 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006548}
6549
Gilles Peskine449bd832023-01-11 14:50:10 +01006550static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6551 psa_key_derivation_step_t step,
6552 const uint8_t *data,
6553 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006554{
Gilles Peskine449bd832023-01-11 14:50:10 +01006555 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006556 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006558 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006560 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006561 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006562 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006563 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006564 }
6565}
John Durkop07cc04a2020-11-16 22:08:34 -08006566#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6567 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6568
6569#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6570static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6571 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006572 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006573 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006574{
6575 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006576 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6577 4 + data_length + prf->other_secret_length :
6578 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006579
Gilles Peskine449bd832023-01-11 14:50:10 +01006580 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6581 return PSA_ERROR_INVALID_ARGUMENT;
6582 }
John Durkop07cc04a2020-11-16 22:08:34 -08006583
Gilles Peskine449bd832023-01-11 14:50:10 +01006584 uint8_t *pms = mbedtls_calloc(1, pms_len);
6585 if (pms == NULL) {
6586 return PSA_ERROR_INSUFFICIENT_MEMORY;
6587 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006588 uint8_t *cur = pms;
6589
6590 /* pure-PSK:
6591 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006592 *
6593 * The premaster secret is formed as follows: if the PSK is N octets
6594 * long, concatenate a uint16 with the value N, N zero octets, a second
6595 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006596 *
6597 * mixed-PSK:
6598 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6599 * follows: concatenate a uint16 with the length of the other secret,
6600 * the other secret itself, uint16 with the length of PSK, and the
6601 * PSK itself.
6602 * For details please check:
6603 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6604 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6605 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006606 */
6607
Gilles Peskine449bd832023-01-11 14:50:10 +01006608 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6609 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6610 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6611 if (prf->other_secret_length != 0) {
6612 memcpy(cur, prf->other_secret, prf->other_secret_length);
6613 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006614 cur += prf->other_secret_length;
6615 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006616 } else {
6617 *cur++ = MBEDTLS_BYTE_1(data_length);
6618 *cur++ = MBEDTLS_BYTE_0(data_length);
6619 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006620 cur += data_length;
6621 }
6622
Gilles Peskine449bd832023-01-11 14:50:10 +01006623 *cur++ = MBEDTLS_BYTE_1(data_length);
6624 *cur++ = MBEDTLS_BYTE_0(data_length);
6625 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006626 cur += data_length;
6627
Gilles Peskine449bd832023-01-11 14:50:10 +01006628 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006629
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006630 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006631 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006632}
Janos Follath6660f0e2019-06-17 08:44:03 +01006633
Przemek Stekiele47201b2022-04-19 13:53:28 +02006634static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6635 psa_tls12_prf_key_derivation_t *prf,
6636 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006637 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006638{
Gilles Peskine449bd832023-01-11 14:50:10 +01006639 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6640 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006641 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006642
6643 if (data_length != 0) {
6644 prf->other_secret = mbedtls_calloc(1, data_length);
6645 if (prf->other_secret == NULL) {
6646 return PSA_ERROR_INSUFFICIENT_MEMORY;
6647 }
6648
6649 memcpy(prf->other_secret, data, data_length);
6650 prf->other_secret_length = data_length;
6651 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006652 prf->other_secret_length = 0;
6653 }
6654
6655 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6656
Gilles Peskine449bd832023-01-11 14:50:10 +01006657 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006658}
6659
Janos Follath6660f0e2019-06-17 08:44:03 +01006660static psa_status_t psa_tls12_prf_psk_to_ms_input(
6661 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006662 psa_key_derivation_step_t step,
6663 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006664 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006665{
Gilles Peskine449bd832023-01-11 14:50:10 +01006666 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006667 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006668 return psa_tls12_prf_psk_to_ms_set_key(prf,
6669 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006670 break;
6671 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006672 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6673 data,
6674 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006675 break;
6676 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006677 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006678 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006679
Przemek Stekiele47201b2022-04-19 13:53:28 +02006680 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006681}
John Durkop07cc04a2020-11-16 22:08:34 -08006682#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006683
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006684#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6685static psa_status_t psa_tls12_ecjpake_to_pms_input(
6686 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006687 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006688 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006689 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006690{
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6692 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6693 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006694 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006695
6696 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006697 if (data[0] != 0x04) {
6698 return PSA_ERROR_INVALID_ARGUMENT;
6699 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006700
6701 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006702 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006703
Gilles Peskine449bd832023-01-11 14:50:10 +01006704 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006705}
6706#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306707
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306708#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306709static psa_status_t psa_pbkdf2_set_input_cost(
6710 psa_pbkdf2_key_derivation_t *pbkdf2,
6711 psa_key_derivation_step_t step,
6712 uint64_t data)
6713{
6714 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6715 return PSA_ERROR_INVALID_ARGUMENT;
6716 }
6717
6718 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6719 return PSA_ERROR_BAD_STATE;
6720 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306721
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306722 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306723 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306724 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306725
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306726 if (data == 0) {
6727 return PSA_ERROR_INVALID_ARGUMENT;
6728 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306729
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306730 pbkdf2->input_cost = data;
6731 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6732
6733 return PSA_SUCCESS;
6734}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306735
6736static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6737 const uint8_t *data,
6738 size_t data_length)
6739{
Gilles Peskineead17662023-08-20 22:02:51 +02006740 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6741 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6742 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6743 /* Appending to existing salt. No state change. */
6744 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306745 return PSA_ERROR_BAD_STATE;
6746 }
6747
Gilles Peskineca57d782023-07-20 19:08:06 +02006748 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006749 /* Appending an empty string, nothing to do. */
6750 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306751 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306752
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306753 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6754 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306755 return PSA_ERROR_INSUFFICIENT_MEMORY;
6756 }
6757
Gilles Peskineead17662023-08-20 22:02:51 +02006758 if (pbkdf2->salt_length != 0) {
6759 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6760 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306761 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306762 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306763 mbedtls_free(pbkdf2->salt);
6764 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306765 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306766 return PSA_SUCCESS;
6767}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306768
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306769#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306770static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306771 const uint8_t *input,
6772 size_t input_len,
6773 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306774 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306775{
6776 psa_status_t status = PSA_SUCCESS;
6777 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6778 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306779 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306780 } else {
6781 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306782 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306783 }
6784 return status;
6785}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306786#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306787
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306788#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306789static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6790 size_t input_len,
6791 uint8_t *output,
6792 size_t *output_len)
6793{
6794 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306795 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306796 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306797 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306798 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6799 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6800 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306801 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306802 * mac_size as the driver function sets mac_output_length = mac_size
6803 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306804 status = psa_driver_wrapper_mac_compute(&attributes,
6805 zeros, sizeof(zeros),
6806 PSA_ALG_CMAC, input, input_len,
6807 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306808 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6809 128U,
6810 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306811 output_len);
6812 } else {
6813 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306814 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306815 }
6816 return status;
6817}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306818#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306819
6820static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306821 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306822 const uint8_t *data,
6823 size_t data_length)
6824{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306825 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306826 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6827 return PSA_ERROR_BAD_STATE;
6828 }
6829
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306830#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306831 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6832 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6833 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6834 pbkdf2->password,
6835 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306836 } else
6837#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6838#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6839 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306840 status = psa_pbkdf2_cmac_set_password(data, data_length,
6841 pbkdf2->password,
6842 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306843 } else
6844#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6845 {
6846 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306847 }
6848
6849 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6850
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306851 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306852}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306853
6854static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306855 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306856 psa_key_derivation_step_t step,
6857 const uint8_t *data,
6858 size_t data_length)
6859{
6860 switch (step) {
6861 case PSA_KEY_DERIVATION_INPUT_SALT:
6862 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6863 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306864 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306865 default:
6866 return PSA_ERROR_INVALID_ARGUMENT;
6867 }
6868}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306869#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306870
Gilles Peskineb8965192019-09-24 16:21:10 +02006871/** Check whether the given key type is acceptable for the given
6872 * input step of a key derivation.
6873 *
6874 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6875 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6876 * Both secret and non-secret inputs can alternatively have the type
6877 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6878 * that the input was passed as a buffer rather than via a key object.
6879 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006880static int psa_key_derivation_check_input_type(
6881 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006882 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006883{
Gilles Peskine449bd832023-01-11 14:50:10 +01006884 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006885 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006886 if (key_type == PSA_KEY_TYPE_DERIVE) {
6887 return PSA_SUCCESS;
6888 }
6889 if (key_type == PSA_KEY_TYPE_NONE) {
6890 return PSA_SUCCESS;
6891 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006892 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006893 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006894 if (key_type == PSA_KEY_TYPE_DERIVE) {
6895 return PSA_SUCCESS;
6896 }
6897 if (key_type == PSA_KEY_TYPE_NONE) {
6898 return PSA_SUCCESS;
6899 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006900 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006901 case PSA_KEY_DERIVATION_INPUT_LABEL:
6902 case PSA_KEY_DERIVATION_INPUT_SALT:
6903 case PSA_KEY_DERIVATION_INPUT_INFO:
6904 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006905 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6906 return PSA_SUCCESS;
6907 }
6908 if (key_type == PSA_KEY_TYPE_NONE) {
6909 return PSA_SUCCESS;
6910 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006911 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306912 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6913 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6914 return PSA_SUCCESS;
6915 }
6916 if (key_type == PSA_KEY_TYPE_DERIVE) {
6917 return PSA_SUCCESS;
6918 }
6919 if (key_type == PSA_KEY_TYPE_NONE) {
6920 return PSA_SUCCESS;
6921 }
6922 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006923 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006924 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006925}
6926
Janos Follathb80a94e2019-06-12 15:54:46 +01006927static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006928 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006929 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006930 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006931 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006932 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006933{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006934 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006935 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006936
Gilles Peskine449bd832023-01-11 14:50:10 +01006937 status = psa_key_derivation_check_input_type(step, key_type);
6938 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006939 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006940 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006941
Przemek Stekiel69c46792022-06-10 12:59:51 +02006942#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6944 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6945 step, data, data_length);
6946 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006947#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006948#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006949 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6950 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6951 step, data, data_length);
6952 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006953#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6954#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006955 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6956 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6957 step, data, data_length);
6958 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006959#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006960#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006961 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006962 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006963 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6964 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006965#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306966#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306967 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306968 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6969 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306970 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306971#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006972 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006973 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006974 (void) data;
6975 (void) data_length;
6976 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006977 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006978 }
6979
Gilles Peskine224b0d62019-09-23 18:13:17 +02006980exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006981 if (status != PSA_SUCCESS) {
6982 psa_key_derivation_abort(operation);
6983 }
6984 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006985}
6986
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306987static psa_status_t psa_key_derivation_input_integer_internal(
6988 psa_key_derivation_operation_t *operation,
6989 psa_key_derivation_step_t step,
6990 uint64_t value)
6991{
6992 psa_status_t status;
6993 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6994
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306995#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306996 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306997 status = psa_pbkdf2_set_input_cost(
6998 &operation->ctx.pbkdf2, step, value);
6999 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307000#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307001 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307002 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307003 (void) value;
7004 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307005 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307006 }
7007
7008 if (status != PSA_SUCCESS) {
7009 psa_key_derivation_abort(operation);
7010 }
7011 return status;
7012}
7013
Janos Follath51f4a0f2019-06-14 11:35:55 +01007014psa_status_t psa_key_derivation_input_bytes(
7015 psa_key_derivation_operation_t *operation,
7016 psa_key_derivation_step_t step,
7017 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007018 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007019{
Gilles Peskine449bd832023-01-11 14:50:10 +01007020 return psa_key_derivation_input_internal(operation, step,
7021 PSA_KEY_TYPE_NONE,
7022 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007023}
7024
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307025psa_status_t psa_key_derivation_input_integer(
7026 psa_key_derivation_operation_t *operation,
7027 psa_key_derivation_step_t step,
7028 uint64_t value)
7029{
7030 return psa_key_derivation_input_integer_internal(operation, step, value);
7031}
7032
Janos Follath51f4a0f2019-06-14 11:35:55 +01007033psa_status_t psa_key_derivation_input_key(
7034 psa_key_derivation_operation_t *operation,
7035 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007036 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007037{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007038 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007039 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007040 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007041
Ronald Cron5c522922020-11-14 16:35:34 +01007042 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007043 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7044 if (status != PSA_SUCCESS) {
7045 psa_key_derivation_abort(operation);
7046 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007047 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007048
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307049 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7050 * permission to output to a key object. */
7051 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7052 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007053 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007054 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007055
Gilles Peskine449bd832023-01-11 14:50:10 +01007056 status = psa_key_derivation_input_internal(operation,
7057 step, slot->attr.type,
7058 slot->key.data,
7059 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007060
Gilles Peskine449bd832023-01-11 14:50:10 +01007061 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007062
Gilles Peskine449bd832023-01-11 14:50:10 +01007063 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007064}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007065
7066
7067
7068/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007069/* Key agreement */
7070/****************************************************************/
7071
Gilles Peskine449bd832023-01-11 14:50:10 +01007072psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7073 const uint8_t *key_buffer,
7074 size_t key_buffer_size,
7075 psa_algorithm_t alg,
7076 const uint8_t *peer_key,
7077 size_t peer_key_length,
7078 uint8_t *shared_secret,
7079 size_t shared_secret_size,
7080 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007081{
Gilles Peskine449bd832023-01-11 14:50:10 +01007082 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007083#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007084 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007085 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7086 key_buffer_size, alg,
7087 peer_key, peer_key_length,
7088 shared_secret,
7089 shared_secret_size,
7090 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007091#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007092
7093#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7094 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007095 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007096 peer_key,
7097 peer_key_length,
7098 key_buffer,
7099 key_buffer_size,
7100 shared_secret,
7101 shared_secret_size,
7102 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007103#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7104
Gilles Peskine01d718c2018-09-18 12:01:02 +02007105 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007106 (void) attributes;
7107 (void) key_buffer;
7108 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007109 (void) peer_key;
7110 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007111 (void) shared_secret;
7112 (void) shared_secret_size;
7113 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007114 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007115 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007116}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007117
Aditya Deshpande17845b82022-10-13 17:21:01 +01007118/** Internal function for raw key agreement
7119 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007120 * to the driver's implementation if a driver is present.
7121 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007122 * (psa_key_agreement_raw_builtin).
7123 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007124static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7125 psa_key_slot_t *private_key,
7126 const uint8_t *peer_key,
7127 size_t peer_key_length,
7128 uint8_t *shared_secret,
7129 size_t shared_secret_size,
7130 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007131{
Gilles Peskine449bd832023-01-11 14:50:10 +01007132 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7133 return PSA_ERROR_NOT_SUPPORTED;
7134 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007135
7136 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007137 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007138 };
7139
Gilles Peskine449bd832023-01-11 14:50:10 +01007140 return psa_driver_wrapper_key_agreement(&attributes,
7141 private_key->key.data,
7142 private_key->key.bytes, alg,
7143 peer_key, peer_key_length,
7144 shared_secret,
7145 shared_secret_size,
7146 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007147}
7148
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007149/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007150 * to potentially free embedded data structures and wipe confidential data.
7151 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007152static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7153 psa_key_derivation_step_t step,
7154 psa_key_slot_t *private_key,
7155 const uint8_t *peer_key,
7156 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007157{
7158 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007159 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007160 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007161 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007162
7163 /* Step 1: run the secret agreement algorithm to generate the shared
7164 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007165 status = psa_key_agreement_raw_internal(ka_alg,
7166 private_key,
7167 peer_key, peer_key_length,
7168 shared_secret,
7169 sizeof(shared_secret),
7170 &shared_secret_length);
7171 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007172 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007173 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007174
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007175 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007176 * the shared secret. A shared secret is permitted wherever a key
7177 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007178 status = psa_key_derivation_input_internal(operation, step,
7179 PSA_KEY_TYPE_DERIVE,
7180 shared_secret,
7181 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007182exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007183 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7184 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007185}
7186
Gilles Peskine449bd832023-01-11 14:50:10 +01007187psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7188 psa_key_derivation_step_t step,
7189 mbedtls_svc_key_id_t private_key,
7190 const uint8_t *peer_key,
7191 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007192{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007193 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007194 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007195 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007196
Gilles Peskine449bd832023-01-11 14:50:10 +01007197 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7198 return PSA_ERROR_INVALID_ARGUMENT;
7199 }
Ronald Cron5c522922020-11-14 16:35:34 +01007200 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007201 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7202 if (status != PSA_SUCCESS) {
7203 return status;
7204 }
7205 status = psa_key_agreement_internal(operation, step,
7206 slot,
7207 peer_key, peer_key_length);
7208 if (status != PSA_SUCCESS) {
7209 psa_key_derivation_abort(operation);
7210 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007211 /* If a private key has been added as SECRET, we allow the derived
7212 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007213 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007214 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007215 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007216 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007217
Gilles Peskine449bd832023-01-11 14:50:10 +01007218 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007219
Gilles Peskine449bd832023-01-11 14:50:10 +01007220 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007221}
7222
Gilles Peskine449bd832023-01-11 14:50:10 +01007223psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7224 mbedtls_svc_key_id_t private_key,
7225 const uint8_t *peer_key,
7226 size_t peer_key_length,
7227 uint8_t *output,
7228 size_t output_size,
7229 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007230{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007231 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007232 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007233 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007234 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007235
Gilles Peskine449bd832023-01-11 14:50:10 +01007236 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007237 status = PSA_ERROR_INVALID_ARGUMENT;
7238 goto exit;
7239 }
Ronald Cron5c522922020-11-14 16:35:34 +01007240 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007241 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7242 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007243 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007244 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007245
Gilles Peskine3e561302022-04-14 00:17:15 +02007246 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7247 * for the output size. The PSA specification only guarantees that this
7248 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7249 * but it might be nice to allow smaller buffers if the output fits.
7250 * At the time of writing this comment, with only ECDH implemented,
7251 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7252 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7253 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007254 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007255 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7256 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007257 status = PSA_ERROR_BUFFER_TOO_SMALL;
7258 goto exit;
7259 }
7260
Gilles Peskine449bd832023-01-11 14:50:10 +01007261 status = psa_key_agreement_raw_internal(alg, slot,
7262 peer_key, peer_key_length,
7263 output, output_size,
7264 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007265
7266exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007267 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007268 /* If an error happens and is not handled properly, the output
7269 * may be used as a key to protect sensitive data. Arrange for such
7270 * a key to be random, which is likely to result in decryption or
7271 * verification errors. This is better than filling the buffer with
7272 * some constant data such as zeros, which would result in the data
7273 * being protected with a reproducible, easily knowable key.
7274 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007275 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007276 *output_length = output_size;
7277 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007278
Gilles Peskine449bd832023-01-11 14:50:10 +01007279 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007280
Gilles Peskine449bd832023-01-11 14:50:10 +01007281 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007282}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007283
7284
Gilles Peskine30524eb2020-11-13 17:02:26 +01007285
Gilles Peskine86a440b2018-11-12 18:39:40 +01007286/****************************************************************/
7287/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007288/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007289
Gilles Peskine672a7712023-04-28 21:00:28 +02007290#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7291#include "entropy_poll.h"
7292#endif
7293
Gilles Peskine30524eb2020-11-13 17:02:26 +01007294/** Initialize the PSA random generator.
7295 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007296static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007297{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007298#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007299 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007300#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7301
Gilles Peskine30524eb2020-11-13 17:02:26 +01007302 /* Set default configuration if
7303 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007304 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007305 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007306 }
7307 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007308 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007309 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007310
Gilles Peskine449bd832023-01-11 14:50:10 +01007311 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007312#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7313 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7314 /* The PSA entropy injection feature depends on using NV seed as an entropy
7315 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007316 mbedtls_entropy_add_source(&rng->entropy,
7317 mbedtls_nv_seed_poll, NULL,
7318 MBEDTLS_ENTROPY_BLOCK_SIZE,
7319 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007320#endif
7321
Gilles Peskine449bd832023-01-11 14:50:10 +01007322 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007323#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007324}
7325
7326/** Deinitialize the PSA random generator.
7327 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007328static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007329{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007330#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007331 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007332#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007333 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7334 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007335#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007336}
7337
7338/** Seed the PSA random generator.
7339 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007340static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007341{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007342#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7343 /* Do nothing: the external RNG seeds itself. */
7344 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007345 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007346#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007347 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007348 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7349 drbg_seed, sizeof(drbg_seed) - 1);
7350 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007351#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007352}
7353
Gilles Peskine449bd832023-01-11 14:50:10 +01007354psa_status_t psa_generate_random(uint8_t *output,
7355 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007356{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007357 GUARD_MODULE_INITIALIZED;
7358
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007359#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7360
7361 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007362 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7363 output, output_size,
7364 &output_length);
7365 if (status != PSA_SUCCESS) {
7366 return status;
7367 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007368 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007369 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007370 if (output_length != output_size) {
7371 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7372 }
7373 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007374
7375#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7376
Gilles Peskine449bd832023-01-11 14:50:10 +01007377 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007378 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007379 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7380 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7381 output_size);
7382 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7383 output, request_size);
7384 if (ret != 0) {
7385 return mbedtls_to_psa_error(ret);
7386 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007387 output_size -= request_size;
7388 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007389 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007390 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007391#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007392}
7393
Gilles Peskine8814fc42020-12-14 15:33:44 +01007394/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007395 *
7396 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7397 * `psa_generate_random(...)`. The state parameter is ignored since the
7398 * PSA API doesn't support passing an explicit state.
7399 *
7400 * In the non-external case, psa_generate_random() calls an
7401 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7402 * and semantics as mbedtls_psa_get_random(). As an optimization,
7403 * instead of doing this back-and-forth between the PSA API and the
7404 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7405 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007406 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007407#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7408int mbedtls_psa_get_random(void *p_rng,
7409 unsigned char *output,
7410 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007411{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007412 /* This function takes a pointer to the RNG state because that's what
7413 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7414 * its own state internally and doesn't let the caller access that state.
7415 * So we just ignore the state parameter, and in practice we'll pass
7416 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007417 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007418 psa_status_t status = psa_generate_random(output, output_size);
7419 if (status == PSA_SUCCESS) {
7420 return 0;
7421 } else {
7422 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7423 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007424}
7425#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7426
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007427#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007428psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7429 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007430{
Gilles Peskine449bd832023-01-11 14:50:10 +01007431 if (global_data.initialized) {
7432 return PSA_ERROR_NOT_PERMITTED;
7433 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007434
Gilles Peskine449bd832023-01-11 14:50:10 +01007435 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7436 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7437 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7438 return PSA_ERROR_INVALID_ARGUMENT;
7439 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007440
Gilles Peskine449bd832023-01-11 14:50:10 +01007441 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007442}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007443#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007444
Ronald Cron3772afe2021-02-08 16:10:05 +01007445/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007446 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007447 * \param type The key type
7448 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007449 *
7450 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007451 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007452 * \retval #PSA_ERROR_INVALID_ARGUMENT
7453 * The size in bits of the key is not valid.
7454 * \retval #PSA_ERROR_NOT_SUPPORTED
7455 * The type and/or the size in bits of the key or the combination of
7456 * the two is not supported.
7457 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007458static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007459 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007460{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007461 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007462
Gilles Peskine449bd832023-01-11 14:50:10 +01007463 if (key_type_is_raw_bytes(type)) {
7464 status = psa_validate_unstructured_key_bit_size(type, bits);
7465 if (status != PSA_SUCCESS) {
7466 return status;
7467 }
7468 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007469#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007470 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7471 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7472 return PSA_ERROR_NOT_SUPPORTED;
7473 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007474 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007475 return PSA_ERROR_NOT_SUPPORTED;
7476 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007477
Ronald Cron01b2aba2020-10-05 09:42:02 +02007478 /* Accept only byte-aligned keys, for the same reasons as
7479 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007480 if (bits % 8 != 0) {
7481 return PSA_ERROR_NOT_SUPPORTED;
7482 }
7483 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007484#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007485
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007486#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007487 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007488 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007489 return PSA_SUCCESS;
7490 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007491#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007492
Valerio Settia55f0422023-07-10 15:34:41 +02007493#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007494 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007495 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007496 return PSA_ERROR_NOT_SUPPORTED;
7497 }
7498 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007499#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007500 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007501 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007502 }
7503
Gilles Peskine449bd832023-01-11 14:50:10 +01007504 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007505}
7506
Ronald Cron55ed0592020-10-05 10:30:40 +02007507psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007508 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007509 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007510{
7511 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007512 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007513
Gilles Peskine449bd832023-01-11 14:50:10 +01007514 if ((attributes->domain_parameters == NULL) &&
7515 (attributes->domain_parameters_size != 0)) {
7516 return PSA_ERROR_INVALID_ARGUMENT;
7517 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007518
Gilles Peskine449bd832023-01-11 14:50:10 +01007519 if (key_type_is_raw_bytes(type)) {
7520 status = psa_generate_random(key_buffer, key_buffer_size);
7521 if (status != PSA_SUCCESS) {
7522 return status;
7523 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007524
David Brown12ca5032021-02-11 11:02:00 -07007525#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007526 if (type == PSA_KEY_TYPE_DES) {
7527 psa_des_set_key_parity(key_buffer, key_buffer_size);
7528 }
David Brown8107e312021-02-12 08:08:46 -07007529#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007530 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007531
Valerio Setti76df8c12023-07-11 14:11:28 +02007532#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007533 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7534 return mbedtls_psa_rsa_generate_key(attributes,
7535 key_buffer,
7536 key_buffer_size,
7537 key_buffer_length);
7538 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007539#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007540
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007541#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007542 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7543 return mbedtls_psa_ecp_generate_key(attributes,
7544 key_buffer,
7545 key_buffer_size,
7546 key_buffer_length);
7547 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007548#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007549
Valerio Settia55f0422023-07-10 15:34:41 +02007550#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007551 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7552 return mbedtls_psa_ffdh_generate_key(attributes,
7553 key_buffer,
7554 key_buffer_size,
7555 key_buffer_length);
7556 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007557#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007558 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007559 (void) key_buffer_length;
7560 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007561 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007562
Gilles Peskine449bd832023-01-11 14:50:10 +01007563 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007564}
Darryl Green0c6575a2018-11-07 16:05:30 +00007565
Gilles Peskine449bd832023-01-11 14:50:10 +01007566psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7567 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007568{
7569 psa_status_t status;
7570 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007571 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007572 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007573
Ronald Cron81709fc2020-11-14 12:10:32 +01007574 *key = MBEDTLS_SVC_KEY_ID_INIT;
7575
Gilles Peskine0f84d622019-09-12 19:03:13 +02007576 /* Reject any attempt to create a zero-length key so that we don't
7577 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007578 if (psa_get_key_bits(attributes) == 0) {
7579 return PSA_ERROR_INVALID_ARGUMENT;
7580 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007581
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007582 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007583 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7584 return PSA_ERROR_INVALID_ARGUMENT;
7585 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007586
Gilles Peskine449bd832023-01-11 14:50:10 +01007587 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7588 &slot, &driver);
7589 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007590 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007591 }
Gilles Peskine11792082019-08-06 18:36:36 +02007592
Ronald Cron977c2472020-10-13 08:32:21 +02007593 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307594 * storage ( thus not in the case of generating a key in a secure element
7595 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7596 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007597 if (slot->key.data == NULL) {
7598 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7599 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007600 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007601 attributes->core.type, attributes->core.bits);
7602 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007603 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007604 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007605
7606 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 attributes->core.type,
7608 attributes->core.bits);
7609 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007610 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007611 attributes, &key_buffer_size);
7612 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007613 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007614 }
Ronald Cron977c2472020-10-13 08:32:21 +02007615 }
Ronald Cron977c2472020-10-13 08:32:21 +02007616
Gilles Peskine449bd832023-01-11 14:50:10 +01007617 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7618 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007619 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 }
Ronald Cron977c2472020-10-13 08:32:21 +02007621 }
7622
Gilles Peskine449bd832023-01-11 14:50:10 +01007623 status = psa_driver_wrapper_generate_key(attributes,
7624 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007625
Gilles Peskine449bd832023-01-11 14:50:10 +01007626 if (status != PSA_SUCCESS) {
7627 psa_remove_key_data_from_memory(slot);
7628 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007629
Gilles Peskine11792082019-08-06 18:36:36 +02007630exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007631 if (status == PSA_SUCCESS) {
7632 status = psa_finish_key_creation(slot, driver, key);
7633 }
7634 if (status != PSA_SUCCESS) {
7635 psa_fail_key_creation(slot, driver);
7636 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007637
Gilles Peskine449bd832023-01-11 14:50:10 +01007638 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007639}
7640
Gilles Peskinee59236f2018-01-27 23:32:46 +01007641/****************************************************************/
7642/* Module setup */
7643/****************************************************************/
7644
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007645#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007646psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007647 void (* entropy_init)(mbedtls_entropy_context *ctx),
7648 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007649{
Gilles Peskine449bd832023-01-11 14:50:10 +01007650 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7651 return PSA_ERROR_BAD_STATE;
7652 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007653 global_data.rng.entropy_init = entropy_init;
7654 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007655 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007656}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007657#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007658
Gilles Peskine449bd832023-01-11 14:50:10 +01007659void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007660{
Gilles Peskine449bd832023-01-11 14:50:10 +01007661 psa_wipe_all_key_slots();
7662 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7663 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007664 }
7665 /* Wipe all remaining data, including configuration.
7666 * In particular, this sets all state indicator to the value
7667 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007668 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007669
7670 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007671 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007672}
7673
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007674#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7675/** Recover a transaction that was interrupted by a power failure.
7676 *
7677 * This function is called during initialization, before psa_crypto_init()
7678 * returns. If this function returns a failure status, the initialization
7679 * fails.
7680 */
7681static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007682 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007683{
Gilles Peskine449bd832023-01-11 14:50:10 +01007684 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007685 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7686 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007687 /* TODO - fall through to the failure case until this
7688 * is implemented.
7689 * https://github.com/ARMmbed/mbed-crypto/issues/218
7690 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007691 default:
7692 /* We found an unsupported transaction in the storage.
7693 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007694 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007695 }
7696}
7697#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7698
Gilles Peskine449bd832023-01-11 14:50:10 +01007699psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007700{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007701 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007702
Gilles Peskinec6b69072018-11-20 21:42:52 +01007703 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007704 if (global_data.initialized != 0) {
7705 return PSA_SUCCESS;
7706 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007707
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007708 /* Init drivers */
7709 status = psa_driver_wrapper_init();
7710 if (status != PSA_SUCCESS) {
7711 goto exit;
7712 }
7713 global_data.drivers_initialized = 1;
7714
Gilles Peskine30524eb2020-11-13 17:02:26 +01007715 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007716 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007717 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007718 status = mbedtls_psa_random_seed(&global_data.rng);
7719 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007720 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007721 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007722 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007723
Gilles Peskine449bd832023-01-11 14:50:10 +01007724 status = psa_initialize_key_slots();
7725 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007726 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007727 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007728
Gilles Peskine4b734222019-07-24 15:56:31 +02007729#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007730 status = psa_crypto_load_transaction();
7731 if (status == PSA_SUCCESS) {
7732 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7733 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007734 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007735 }
7736 status = psa_crypto_stop_transaction();
7737 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007738 /* There's no transaction to complete. It's all good. */
7739 status = PSA_SUCCESS;
7740 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007741#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007742
Gilles Peskinec6b69072018-11-20 21:42:52 +01007743 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007744 global_data.initialized = 1;
7745
7746exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007747 if (status != PSA_SUCCESS) {
7748 mbedtls_psa_crypto_free();
7749 }
7750 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007751}
7752
Yanray Wangffc3c482023-07-11 12:01:04 +08007753#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007754psa_status_t psa_crypto_driver_pake_get_password_len(
7755 const psa_crypto_driver_pake_inputs_t *inputs,
7756 size_t *password_len)
7757{
7758 if (inputs->password_len == 0) {
7759 return PSA_ERROR_BAD_STATE;
7760 }
7761
7762 *password_len = inputs->password_len;
7763
7764 return PSA_SUCCESS;
7765}
7766
7767psa_status_t psa_crypto_driver_pake_get_password(
7768 const psa_crypto_driver_pake_inputs_t *inputs,
7769 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7770{
7771 if (inputs->password_len == 0) {
7772 return PSA_ERROR_BAD_STATE;
7773 }
7774
7775 if (buffer_size < inputs->password_len) {
7776 return PSA_ERROR_BUFFER_TOO_SMALL;
7777 }
7778
7779 memcpy(buffer, inputs->password, inputs->password_len);
7780 *buffer_length = inputs->password_len;
7781
7782 return PSA_SUCCESS;
7783}
7784
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007785psa_status_t psa_crypto_driver_pake_get_user_len(
7786 const psa_crypto_driver_pake_inputs_t *inputs,
7787 size_t *user_len)
7788{
7789 if (inputs->user_len == 0) {
7790 return PSA_ERROR_BAD_STATE;
7791 }
7792
7793 *user_len = inputs->user_len;
7794
7795 return PSA_SUCCESS;
7796}
7797
7798psa_status_t psa_crypto_driver_pake_get_user(
7799 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007800 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007801{
7802 if (inputs->user_len == 0) {
7803 return PSA_ERROR_BAD_STATE;
7804 }
7805
Przemek Stekielc0e62502023-03-14 11:49:36 +01007806 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007807 return PSA_ERROR_BUFFER_TOO_SMALL;
7808 }
7809
Przemek Stekielc0e62502023-03-14 11:49:36 +01007810 memcpy(user_id, inputs->user, inputs->user_len);
7811 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007812
7813 return PSA_SUCCESS;
7814}
7815
7816psa_status_t psa_crypto_driver_pake_get_peer_len(
7817 const psa_crypto_driver_pake_inputs_t *inputs,
7818 size_t *peer_len)
7819{
7820 if (inputs->peer_len == 0) {
7821 return PSA_ERROR_BAD_STATE;
7822 }
7823
7824 *peer_len = inputs->peer_len;
7825
7826 return PSA_SUCCESS;
7827}
7828
7829psa_status_t psa_crypto_driver_pake_get_peer(
7830 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007831 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007832{
7833 if (inputs->peer_len == 0) {
7834 return PSA_ERROR_BAD_STATE;
7835 }
7836
Przemek Stekielc0e62502023-03-14 11:49:36 +01007837 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007838 return PSA_ERROR_BUFFER_TOO_SMALL;
7839 }
7840
Przemek Stekielc0e62502023-03-14 11:49:36 +01007841 memcpy(peer_id, inputs->peer, inputs->peer_len);
7842 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007843
7844 return PSA_SUCCESS;
7845}
7846
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007847psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7848 const psa_crypto_driver_pake_inputs_t *inputs,
7849 psa_pake_cipher_suite_t *cipher_suite)
7850{
7851 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7852 return PSA_ERROR_BAD_STATE;
7853 }
7854
7855 *cipher_suite = inputs->cipher_suite;
7856
7857 return PSA_SUCCESS;
7858}
7859
Neil Armstronga7d08c32022-06-01 18:21:20 +02007860psa_status_t psa_pake_setup(
7861 psa_pake_operation_t *operation,
7862 const psa_pake_cipher_suite_t *cipher_suite)
7863{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007864 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007865
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007866 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007867 status = PSA_ERROR_BAD_STATE;
7868 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007869 }
7870
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007871 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007872 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007873 status = PSA_ERROR_INVALID_ARGUMENT;
7874 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007875 }
7876
Przemek Stekiel51eac532022-12-07 11:04:51 +01007877 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7878
Przemek Stekiele12ed362022-12-21 12:54:46 +01007879 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007880 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7881 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007882 operation->data.inputs.cipher_suite = *cipher_suite;
7883
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007884#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007885 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007886 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007887 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007888
David Horstmann16f01512023-06-14 17:21:07 +01007889 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007890 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007891 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007892#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007893 {
7894 status = PSA_ERROR_NOT_SUPPORTED;
7895 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007896 }
7897
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007898 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7899
Przemek Stekiel51eac532022-12-07 11:04:51 +01007900 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007901exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007902 psa_pake_abort(operation);
7903 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007904}
7905
7906psa_status_t psa_pake_set_password_key(
7907 psa_pake_operation_t *operation,
7908 mbedtls_svc_key_id_t password)
7909{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007910 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007911 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7912 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007913 psa_key_attributes_t attributes;
7914 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007915
Przemek Stekiel51eac532022-12-07 11:04:51 +01007916 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007917 status = PSA_ERROR_BAD_STATE;
7918 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007919 }
7920
Przemek Stekiel6c764412022-11-22 14:05:12 +01007921 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7922 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007923 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007924 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007925 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007926 }
7927
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007928 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007929 .core = slot->attr
7930 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007931
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007932 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007933
Przemek Stekiel51eac532022-12-07 11:04:51 +01007934 if (type != PSA_KEY_TYPE_PASSWORD &&
7935 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7936 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007937 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007938 }
7939
Przemek Stekiel51eac532022-12-07 11:04:51 +01007940 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7941 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007942 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007943 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007944 }
7945
7946 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7947 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007948 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007949exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007950 if (status != PSA_SUCCESS) {
7951 psa_pake_abort(operation);
7952 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007953 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007954 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007955}
7956
7957psa_status_t psa_pake_set_user(
7958 psa_pake_operation_t *operation,
7959 const uint8_t *user_id,
7960 size_t user_id_len)
7961{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007962 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007963
7964 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007965 status = PSA_ERROR_BAD_STATE;
7966 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007967 }
7968
Przemek Stekiel51eac532022-12-07 11:04:51 +01007969 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007970 status = PSA_ERROR_INVALID_ARGUMENT;
7971 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007972 }
7973
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007974 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007975 status = PSA_ERROR_BAD_STATE;
7976 goto exit;
7977 }
7978
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007979 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7980 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007981 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7982 goto exit;
7983 }
7984
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007985 memcpy(operation->data.inputs.user, user_id, user_id_len);
7986 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007987
7988 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007989exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007990 psa_pake_abort(operation);
7991 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007992}
7993
7994psa_status_t psa_pake_set_peer(
7995 psa_pake_operation_t *operation,
7996 const uint8_t *peer_id,
7997 size_t peer_id_len)
7998{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007999 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008000
8001 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008002 status = PSA_ERROR_BAD_STATE;
8003 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008004 }
8005
Przemek Stekiel51eac532022-12-07 11:04:51 +01008006 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008007 status = PSA_ERROR_INVALID_ARGUMENT;
8008 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008009 }
8010
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008011 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008012 status = PSA_ERROR_BAD_STATE;
8013 goto exit;
8014 }
8015
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008016 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8017 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008018 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8019 goto exit;
8020 }
8021
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008022 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8023 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008024
8025 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008026exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008027 psa_pake_abort(operation);
8028 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008029}
8030
8031psa_status_t psa_pake_set_role(
8032 psa_pake_operation_t *operation,
8033 psa_pake_role_t role)
8034{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008035 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008036
Przemek Stekiel51eac532022-12-07 11:04:51 +01008037 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008038 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008039 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008040 }
8041
Przemek Stekiel09104b82023-03-06 14:11:51 +01008042 switch (operation->alg) {
8043#if defined(PSA_WANT_ALG_JPAKE)
8044 case PSA_ALG_JPAKE:
8045 if (role == PSA_PAKE_ROLE_NONE) {
8046 return PSA_SUCCESS;
8047 }
8048 status = PSA_ERROR_INVALID_ARGUMENT;
8049 break;
8050#endif
8051 default:
8052 (void) role;
8053 status = PSA_ERROR_NOT_SUPPORTED;
8054 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008055 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008056exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008057 psa_pake_abort(operation);
8058 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008059}
8060
David Horstmanne7f21e62023-05-12 18:17:21 +01008061/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008062#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008063static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008064 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008065{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008066 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008067 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008068 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008069
David Horstmann024e5c52023-06-14 15:48:21 +01008070 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008071 is_x1 = (stage->outputs < 1);
8072 } else {
8073 is_x1 = (stage->inputs < 1);
8074 }
8075
David Horstmann74a3d8c2023-06-14 18:28:19 +01008076 key_share_step = is_x1 ?
8077 PSA_JPAKE_X1_STEP_KEY_SHARE :
8078 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008079 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008080 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8081 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8082 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8083 } else {
8084 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008085 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008086 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008087}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008088#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008089
Przemek Stekiel2797d372022-12-22 11:19:22 +01008090static psa_status_t psa_pake_complete_inputs(
8091 psa_pake_operation_t *operation)
8092{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008093 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008094 /* Create copy of the inputs on stack as inputs share memory
8095 with the driver context which will be setup by the driver. */
8096 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008097
Przemek Stekielfde11282023-03-13 16:06:09 +01008098 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008099 return PSA_ERROR_BAD_STATE;
8100 }
8101
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008102 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008103 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8104 return PSA_ERROR_BAD_STATE;
8105 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008106 }
8107
Przemek Stekiel18620a32023-01-17 16:34:52 +01008108 /* Clear driver context */
8109 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8110
8111 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008112
8113 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008114 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008115
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008116 /* User and peer are translated to role. */
8117 mbedtls_free(inputs.user);
8118 mbedtls_free(inputs.peer);
8119
Przemek Stekiel2797d372022-12-22 11:19:22 +01008120 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008121#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008122 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008123 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008124 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008125#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008126 {
8127 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008128 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008129 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008130 return status;
8131}
8132
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008133#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008134static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008135 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008136 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008137 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008138{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008139 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8140 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8141 step != PSA_PAKE_STEP_ZK_PROOF) {
8142 return PSA_ERROR_INVALID_ARGUMENT;
8143 }
8144
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008145 psa_jpake_computation_stage_t *computation_stage =
8146 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008147
David Horstmann5da95602023-06-08 15:37:12 +01008148 if (computation_stage->round != PSA_JPAKE_FIRST &&
8149 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008150 return PSA_ERROR_BAD_STATE;
8151 }
8152
David Horstmanne7f21e62023-05-12 18:17:21 +01008153 /* Check that the step we are given is the one we were expecting */
8154 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008155 return PSA_ERROR_BAD_STATE;
8156 }
8157
David Horstmanne7f21e62023-05-12 18:17:21 +01008158 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8159 computation_stage->inputs == 0 &&
8160 computation_stage->outputs == 0) {
8161 /* Start of the round, so function decides whether we are inputting
8162 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008163 computation_stage->io_mode = io_mode;
8164 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008165 /* Middle of the round so the mode we are in must match the function
8166 * called by the user */
8167 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008168 }
8169
8170 return PSA_SUCCESS;
8171}
8172
David Horstmanne7f21e62023-05-12 18:17:21 +01008173static psa_status_t psa_jpake_epilogue(
8174 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008175 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008176{
David Horstmanne7f21e62023-05-12 18:17:21 +01008177 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008178 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008179
David Horstmanne7f21e62023-05-12 18:17:21 +01008180 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8181 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008182 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008183 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008184 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008185 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008186 }
8187 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008188 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008189 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008190 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008191 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008192 }
8193 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008194 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8195 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008196 /* End of a round, move to the next round */
8197 stage->inputs = 0;
8198 stage->outputs = 0;
8199 stage->round++;
8200 }
8201 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008202 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008203 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008204 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008205 return PSA_SUCCESS;
8206}
David Horstmanne7f21e62023-05-12 18:17:21 +01008207
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008208#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008209
Neil Armstronga7d08c32022-06-01 18:21:20 +02008210psa_status_t psa_pake_output(
8211 psa_pake_operation_t *operation,
8212 psa_pake_step_t step,
8213 uint8_t *output,
8214 size_t output_size,
8215 size_t *output_length)
8216{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008217 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008218 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008219 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008220
8221 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008222 status = psa_pake_complete_inputs(operation);
8223 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008224 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008225 }
8226 }
8227
8228 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008229 status = PSA_ERROR_BAD_STATE;
8230 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008231 }
8232
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008233 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008234 status = PSA_ERROR_INVALID_ARGUMENT;
8235 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008236 }
8237
Przemek Stekiele12ed362022-12-21 12:54:46 +01008238 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008239#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008240 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008241 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008242 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008243 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008244 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008245 driver_step = convert_jpake_computation_stage_to_driver_step(
8246 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008247 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008248#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008249 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008250 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008251 status = PSA_ERROR_NOT_SUPPORTED;
8252 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008253 }
8254
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008255 status = psa_driver_wrapper_pake_output(operation, driver_step,
8256 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008257
8258 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008259 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008260 }
8261
8262 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008263#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008264 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008265 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008266 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008267 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008268 }
8269 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008270#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008271 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008272 status = PSA_ERROR_NOT_SUPPORTED;
8273 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008274 }
8275
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008276 return PSA_SUCCESS;
8277exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008278 psa_pake_abort(operation);
8279 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008280}
8281
8282psa_status_t psa_pake_input(
8283 psa_pake_operation_t *operation,
8284 psa_pake_step_t step,
8285 const uint8_t *input,
8286 size_t input_length)
8287{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008288 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008289 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008290 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8291 operation->primitive,
8292 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008293
8294 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008295 status = psa_pake_complete_inputs(operation);
8296 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008297 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008298 }
8299 }
8300
8301 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008302 status = PSA_ERROR_BAD_STATE;
8303 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008304 }
8305
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008306 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008307 status = PSA_ERROR_INVALID_ARGUMENT;
8308 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008309 }
8310
Przemek Stekiele12ed362022-12-21 12:54:46 +01008311 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008312#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008313 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008314 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008315 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008316 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008317 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008318 driver_step = convert_jpake_computation_stage_to_driver_step(
8319 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008320 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008321#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008322 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008323 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008324 status = PSA_ERROR_NOT_SUPPORTED;
8325 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008326 }
8327
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008328 status = psa_driver_wrapper_pake_input(operation, driver_step,
8329 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008330
8331 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008332 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008333 }
8334
8335 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008336#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008337 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008338 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008339 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008340 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008341 }
8342 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008343#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008344 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008345 status = PSA_ERROR_NOT_SUPPORTED;
8346 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008347 }
8348
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008349 return PSA_SUCCESS;
8350exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008351 psa_pake_abort(operation);
8352 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008353}
8354
8355psa_status_t psa_pake_get_implicit_key(
8356 psa_pake_operation_t *operation,
8357 psa_key_derivation_operation_t *output)
8358{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008359 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008360 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008361 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008362 size_t shared_key_len = 0;
8363
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008364 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008365 status = PSA_ERROR_BAD_STATE;
8366 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008367 }
8368
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008369#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008370 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008371 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008372 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008373 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008374 status = PSA_ERROR_BAD_STATE;
8375 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008376 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008377 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008378#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008379 {
8380 status = PSA_ERROR_NOT_SUPPORTED;
8381 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008382 }
8383
Przemek Stekiel0c781802022-11-29 14:53:13 +01008384 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8385 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008386 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008387 &shared_key_len);
8388
8389 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008390 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008391 }
8392
8393 status = psa_key_derivation_input_bytes(output,
8394 PSA_KEY_DERIVATION_INPUT_SECRET,
8395 shared_key,
8396 shared_key_len);
8397
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008398 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008399exit:
8400 abort_status = psa_pake_abort(operation);
8401 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008402}
8403
8404psa_status_t psa_pake_abort(
8405 psa_pake_operation_t *operation)
8406{
Przemek Stekield69dca92023-01-31 19:59:20 +01008407 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008408
Przemek Stekield69dca92023-01-31 19:59:20 +01008409 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008410 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008411 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008412
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008413 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8414 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008415 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008416 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008417 }
8418 if (operation->data.inputs.user != NULL) {
8419 mbedtls_free(operation->data.inputs.user);
8420 }
8421 if (operation->data.inputs.peer != NULL) {
8422 mbedtls_free(operation->data.inputs.peer);
8423 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008424 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008425 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008426
Przemek Stekield69dca92023-01-31 19:59:20 +01008427 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008428}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008429#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008430
Gilles Peskinee59236f2018-01-27 23:32:46 +01008431#endif /* MBEDTLS_PSA_CRYPTO_C */