blob: 0660ee411bfeba9737c966e3212cea68a3ad2070 [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;
Ryan Everett5567e3a2023-11-08 13:28:20 +00001789 slot->status = PSA_SLOT_OCCUPIED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001791#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1792 slot->attr.id = volatile_key_id;
1793#else
1794 slot->attr.id.key_id = volatile_key_id;
1795#endif
1796 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001797
Gilles Peskine91e8c332019-08-02 19:19:39 +02001798 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001799 * external-only flags, query `attributes`. Thanks to the check
1800 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001801 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001802 * may have set. */
1803 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001804
Gilles Peskinecbaff462019-07-12 23:46:04 +02001805#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001806 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001807 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001808 * create the key file in internal storage, create the
1809 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001810 * persistent data. This is done by starting a transaction that will
1811 * encompass these three actions.
1812 * For registering a volatile key, we just need to find an appropriate
1813 * slot number inside the SE. Since the key is designated volatile, creating
1814 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001815 /* The first thing to do is to find a slot number for the new key.
1816 * We save the slot number in persistent storage as part of the
1817 * transaction data. It will be needed to recover if the power
1818 * fails during the key creation process, to clean up on the secure
1819 * element side after restarting. Obtaining a slot number from the
1820 * secure element driver updates its persistent state, but we do not yet
1821 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001822 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001824 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1826 &slot_number);
1827 if (status != PSA_SUCCESS) {
1828 return status;
1829 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001830
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1832 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001833 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001834 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001835 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 status = psa_crypto_save_transaction();
1837 if (status != PSA_SUCCESS) {
1838 (void) psa_crypto_stop_transaction();
1839 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001840 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001841 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001842
1843 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001845 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001848 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001850 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001851#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1852
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001854}
Gilles Peskine4747d192019-04-17 15:05:45 +02001855
1856/** Finalize the creation of a key once its key material has been set.
1857 *
1858 * This entails writing the key to persistent storage.
1859 *
1860 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001861 * See the documentation of psa_start_key_creation() for the intended use
1862 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001863 *
Ronald Cron5c522922020-11-14 16:35:34 +01001864 * If the finalization succeeds, the function unlocks the key slot (it was
1865 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1866 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001867 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001868 * \param[in,out] slot Pointer to the slot with key material.
1869 * \param[in] driver The secure element driver for the key,
1870 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001871 * \param[out] key On success, identifier of the key. Note that the
1872 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001873 *
1874 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001875 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001876 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1877 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1878 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1879 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1880 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1881 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001882 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001883 * \return If this function fails, the key slot is an invalid state.
1884 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001885 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001886static psa_status_t psa_finish_key_creation(
1887 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001888 psa_se_drv_table_entry_t *driver,
1889 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001890{
1891 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001892 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001893 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001894
1895#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001896 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001897#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001899 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001900 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001902
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001903 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1904 sizeof(data.slot_number),
1905 "Slot number size does not match psa_se_key_data_storage_t");
1906
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1908 status = psa_save_persistent_key(&slot->attr,
1909 (uint8_t *) &data,
1910 sizeof(data));
1911 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001912#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1913 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001914 /* Key material is saved in export representation in the slot, so
1915 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 status = psa_save_persistent_key(&slot->attr,
1917 slot->key.data,
1918 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001919 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001920 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001921#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1922
Gilles Peskinecbaff462019-07-12 23:46:04 +02001923#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001924 /* Finish the transaction for a key creation. This does not
1925 * happen when registering an existing key. Detect this case
1926 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001927 * creation of a persistent key in a secure element requires a transaction,
1928 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 if (driver != NULL &&
1930 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1931 status = psa_save_se_persistent_data(driver);
1932 if (status != PSA_SUCCESS) {
1933 psa_destroy_persistent_key(slot->attr.id);
1934 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001935 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001937 }
1938#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001941 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 status = psa_unlock_key_slot(slot);
1943 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001944 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001946 }
Ronald Cron50972942020-11-14 11:28:25 +01001947
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001949}
1950
1951/** Abort the creation of a key.
1952 *
1953 * You may call this function after calling psa_start_key_creation(),
1954 * or after psa_finish_key_creation() fails. In other circumstances, this
1955 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001956 * See the documentation of psa_start_key_creation() for the intended use
1957 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001958 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001959 * \param[in,out] slot Pointer to the slot with key material.
1960 * \param[in] driver The secure element driver for the key,
1961 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001962 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001963static void psa_fail_key_creation(psa_key_slot_t *slot,
1964 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001965{
Gilles Peskine011e4282019-06-26 18:34:38 +02001966 (void) driver;
1967
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001969 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001971
1972#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001973 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001974 * element, and the failure happened later (when saving metadata
1975 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001976 * element.
1977 * https://github.com/ARMmbed/mbed-crypto/issues/217
1978 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001979
Gilles Peskined7729582019-08-05 15:55:54 +02001980 /* Abort the ongoing transaction if any (there may not be one if
1981 * the creation process failed before starting one, or if the
1982 * key creation is a registration of a key in a secure element).
1983 * Earlier functions must already have done what it takes to undo any
1984 * partial creation. All that's left is to update the transaction data
1985 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001987#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001990}
1991
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001992/** Validate optional attributes during key creation.
1993 *
1994 * Some key attributes are optional during key creation. If they are
1995 * specified in the attributes structure, check that they are consistent
1996 * with the data in the slot.
1997 *
1998 * This function should be called near the end of key creation, after
1999 * the slot in memory is fully populated but before saving persistent data.
2000 */
2001static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002002 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002004{
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 if (attributes->core.type != 0) {
2006 if (attributes->core.type != slot->attr.type) {
2007 return PSA_ERROR_INVALID_ARGUMENT;
2008 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002009 }
2010
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02002012#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
2013 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2015 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002016 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002017 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002018 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002019
Ronald Cron90857082020-11-25 14:58:33 +01002020 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 slot->attr.type,
2022 slot->key.data,
2023 slot->key.bytes,
2024 &rsa);
2025 if (status != PSA_SUCCESS) {
2026 return status;
2027 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002028
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 mbedtls_mpi_init(&actual);
2030 mbedtls_mpi_init(&required);
2031 ret = mbedtls_rsa_export(rsa,
2032 NULL, NULL, NULL, NULL, &actual);
2033 mbedtls_rsa_free(rsa);
2034 mbedtls_free(rsa);
2035 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002036 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 }
2038 ret = mbedtls_mpi_read_binary(&required,
2039 attributes->domain_parameters,
2040 attributes->domain_parameters_size);
2041 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002042 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 }
2044 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002045 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 }
2047rsa_exit:
2048 mbedtls_mpi_free(&actual);
2049 mbedtls_mpi_free(&required);
2050 if (ret != 0) {
2051 return mbedtls_to_psa_error(ret);
2052 }
2053 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02002054#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
2055 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08002056 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002057 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002059 }
2060 }
2061
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 if (attributes->core.bits != 0) {
2063 if (attributes->core.bits != slot->attr.bits) {
2064 return PSA_ERROR_INVALID_ARGUMENT;
2065 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002066 }
2067
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002069}
2070
Gilles Peskine449bd832023-01-11 14:50:10 +01002071psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2072 const uint8_t *data,
2073 size_t data_length,
2074 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002075{
2076 psa_status_t status;
2077 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002078 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002079 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302080 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002081
Ronald Cron81709fc2020-11-14 12:10:32 +01002082 *key = MBEDTLS_SVC_KEY_ID_INIT;
2083
Gilles Peskine0f84d622019-09-12 19:03:13 +02002084 /* Reject zero-length symmetric keys (including raw data key objects).
2085 * This also rejects any key which might be encoded as an empty string,
2086 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 if (data_length == 0) {
2088 return PSA_ERROR_INVALID_ARGUMENT;
2089 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002090
Archana449608b2021-09-08 15:36:05 +05302091 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 if (data_length > SIZE_MAX / 8) {
2093 return PSA_ERROR_NOT_SUPPORTED;
2094 }
Archana6ed4bda2021-08-04 10:47:15 +05302095
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2097 &slot, &driver);
2098 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002099 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002101
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002102 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302103 * storage ( thus not in the case of importing a key in a secure element
2104 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2105 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 if (slot->key.data == NULL) {
2107 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302108 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 attributes, data, data_length, &storage_size);
2110 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302111 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 }
Archanad8a83dc2021-06-14 10:04:16 +05302113 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 status = psa_allocate_buffer_to_slot(slot, storage_size);
2115 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002116 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002118 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002119
2120 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 status = psa_driver_wrapper_import_key(attributes,
2122 data, data_length,
2123 slot->key.data,
2124 slot->key.bytes,
2125 &slot->key.bytes, &bits);
2126 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002127 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002129
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002131 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002133 status = PSA_ERROR_INVALID_ARGUMENT;
2134 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002135 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002136
Archana6ed4bda2021-08-04 10:47:15 +05302137 /* Enforce a size limit, and in particular ensure that the bit
2138 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302140 status = PSA_ERROR_NOT_SUPPORTED;
2141 goto exit;
2142 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 status = psa_validate_optional_attributes(slot, attributes);
2144 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002145 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002147
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002149exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 if (status != PSA_SUCCESS) {
2151 psa_fail_key_creation(slot, driver);
2152 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002155}
2156
Gilles Peskined7729582019-08-05 15:55:54 +02002157#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2158psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002160{
2161 psa_status_t status;
2162 psa_key_slot_t *slot = NULL;
2163 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002164 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002165
2166 /* Leaving attributes unspecified is not currently supported.
2167 * It could make sense to query the key type and size from the
2168 * secure element, but not all secure elements support this
2169 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2171 return PSA_ERROR_NOT_SUPPORTED;
2172 }
2173 if (psa_get_key_bits(attributes) == 0) {
2174 return PSA_ERROR_NOT_SUPPORTED;
2175 }
Gilles Peskined7729582019-08-05 15:55:54 +02002176
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2178 &slot, &driver);
2179 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002180 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 }
Gilles Peskined7729582019-08-05 15:55:54 +02002182
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002184
2185exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 if (status != PSA_SUCCESS) {
2187 psa_fail_key_creation(slot, driver);
2188 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002189
Gilles Peskined7729582019-08-05 15:55:54 +02002190 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 psa_close_key(key);
2192 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002193}
2194#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2197 const psa_key_attributes_t *specified_attributes,
2198 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002199{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002200 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002201 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002202 psa_key_slot_t *source_slot = NULL;
2203 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002204 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002205 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302206 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002207
Ronald Cron81709fc2020-11-14 12:10:32 +01002208 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2209
Archana8a180362021-07-05 02:18:48 +05302210 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2212 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002213 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 status = psa_validate_optional_attributes(source_slot,
2217 specified_attributes);
2218 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002219 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002221
Archana9d17bf42021-09-10 06:22:44 +05302222 /* The target key type and number of bits have been validated by
2223 * psa_validate_optional_attributes() to be either equal to zero or
2224 * equal to the ones of the source key. So it is safe to inherit
2225 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302226 * */
Archana9d17bf42021-09-10 06:22:44 +05302227 actual_attributes.core.bits = source_slot->attr.bits;
2228 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302229
2230
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 status = psa_restrict_key_policy(source_slot->attr.type,
2232 &actual_attributes.core.policy,
2233 &source_slot->attr.policy);
2234 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002235 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002237
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2239 &target_slot, &driver);
2240 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002241 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002242 }
2243 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2244 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302245 /*
Archana9d17bf42021-09-10 06:22:44 +05302246 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302247 * the source key would need to be exported as plaintext and re-imported
2248 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302249 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302250 * appropriate API invocations from the application, if needed.
2251 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002252 status = PSA_ERROR_NOT_SUPPORTED;
2253 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002254 }
Archana8a180362021-07-05 02:18:48 +05302255 /*
2256 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302257 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302258 * - For opaque keys this translates to an invocation of the drivers'
2259 * copy_key entry point through the dispatch layer.
2260 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2262 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2263 &storage_size);
2264 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302265 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 }
Archana374fe5b2021-09-08 15:50:28 +05302267
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2269 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302270 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 }
Archana374fe5b2021-09-08 15:50:28 +05302272
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 status = psa_driver_wrapper_copy_key(&actual_attributes,
2274 source_slot->key.data,
2275 source_slot->key.bytes,
2276 target_slot->key.data,
2277 target_slot->key.bytes,
2278 &target_slot->key.bytes);
2279 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302280 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 }
2282 } else {
2283 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302284 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 source_slot->key.bytes);
2286 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302287 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 }
Archana8a180362021-07-05 02:18:48 +05302289 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002291exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 if (status != PSA_SUCCESS) {
2293 psa_fail_key_creation(target_slot, driver);
2294 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002295
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002299}
2300
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002301
2302
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002303/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002304/* Message digests */
2305/****************************************************************/
2306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002308{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002309 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 if (operation->id == 0) {
2311 return PSA_SUCCESS;
2312 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002315 operation->id = 0;
2316
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002318}
2319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2321 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002322{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002323 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002324
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002325 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002327 status = PSA_ERROR_BAD_STATE;
2328 goto exit;
2329 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002330
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002332 status = PSA_ERROR_INVALID_ARGUMENT;
2333 goto exit;
2334 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002335
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002336 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2337 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002339
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002341
2342exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 if (status != PSA_SUCCESS) {
2344 psa_hash_abort(operation);
2345 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002346
2347 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002348}
2349
Gilles Peskine449bd832023-01-11 14:50:10 +01002350psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2351 const uint8_t *input,
2352 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002353{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002354 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2355
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002357 status = PSA_ERROR_BAD_STATE;
2358 goto exit;
2359 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002360
Steven Cooremanc8288352021-03-04 14:02:19 +01002361 /* Don't require hash implementations to behave correctly on a
2362 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 if (input_length == 0) {
2364 return PSA_SUCCESS;
2365 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002366
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002368
2369exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 if (status != PSA_SUCCESS) {
2371 psa_hash_abort(operation);
2372 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002373
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002375}
2376
Gilles Peskine449bd832023-01-11 14:50:10 +01002377psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2378 uint8_t *hash,
2379 size_t hash_size,
2380 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002381{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002382 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 if (operation->id == 0) {
2384 return PSA_ERROR_BAD_STATE;
2385 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002386
Steven Cooreman1e582352021-02-18 17:24:37 +01002387 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 operation, hash, hash_size, hash_length);
2389 psa_hash_abort(operation);
2390 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002391}
2392
Gilles Peskine449bd832023-01-11 14:50:10 +01002393psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2394 const uint8_t *hash,
2395 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002396{
Ronald Cron69a63422021-10-18 09:47:58 +02002397 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002398 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002399 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 operation,
2401 actual_hash, sizeof(actual_hash),
2402 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002403
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002405 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002407
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002409 status = PSA_ERROR_INVALID_SIGNATURE;
2410 goto exit;
2411 }
2412
Dave Rodgman78701152023-08-29 14:20:18 +01002413 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002414 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002416
2417exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2419 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002420 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002422
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002424}
2425
Gilles Peskine449bd832023-01-11 14:50:10 +01002426psa_status_t psa_hash_compute(psa_algorithm_t alg,
2427 const uint8_t *input, size_t input_length,
2428 uint8_t *hash, size_t hash_size,
2429 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002430{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002431 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 if (!PSA_ALG_IS_HASH(alg)) {
2433 return PSA_ERROR_INVALID_ARGUMENT;
2434 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002435
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2437 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002438}
2439
Gilles Peskine449bd832023-01-11 14:50:10 +01002440psa_status_t psa_hash_compare(psa_algorithm_t alg,
2441 const uint8_t *input, size_t input_length,
2442 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002443{
Ronald Cron69a63422021-10-18 09:47:58 +02002444 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002445 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002446
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 if (!PSA_ALG_IS_HASH(alg)) {
2448 return PSA_ERROR_INVALID_ARGUMENT;
2449 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002450
Steven Cooreman1e582352021-02-18 17:24:37 +01002451 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 alg, input, input_length,
2453 actual_hash, sizeof(actual_hash),
2454 &actual_hash_length);
2455 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002456 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 }
2458 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002459 status = PSA_ERROR_INVALID_SIGNATURE;
2460 goto exit;
2461 }
Dave Rodgman78701152023-08-29 14:20:18 +01002462 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002463 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002465
2466exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2468 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002469}
2470
Gilles Peskine449bd832023-01-11 14:50:10 +01002471psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2472 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002473{
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 if (source_operation->id == 0 ||
2475 target_operation->id != 0) {
2476 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002477 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002478
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2480 target_operation);
2481 if (status != PSA_SUCCESS) {
2482 psa_hash_abort(target_operation);
2483 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002484
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002486}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002487
2488
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002489/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002490/* MAC */
2491/****************************************************************/
2492
Gilles Peskine449bd832023-01-11 14:50:10 +01002493psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002494{
Steven Cooremane6804192021-03-19 18:28:56 +01002495 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 if (operation->id == 0) {
2497 return PSA_SUCCESS;
2498 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002499
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002501 operation->mac_size = 0;
2502 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002503 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002504
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002506}
2507
Ronald Cron2dff3b22021-06-17 16:33:22 +02002508static psa_status_t psa_mac_finalize_alg_and_key_validation(
2509 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002510 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002512{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002513 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 psa_key_type_t key_type = psa_get_key_type(attributes);
2515 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002516
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 if (!PSA_ALG_IS_MAC(alg)) {
2518 return PSA_ERROR_INVALID_ARGUMENT;
2519 }
Steven Cooremane6804192021-03-19 18:28:56 +01002520
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002521 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 status = psa_mac_key_can_do(alg, key_type);
2523 if (status != PSA_SUCCESS) {
2524 return status;
2525 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002526
Steven Cooremand1a68f12021-05-10 11:13:41 +02002527 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002529
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002531 /* A very short MAC is too short for security since it can be
2532 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2533 * so we make this our minimum, even though 32 bits is still
2534 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002536 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002537
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2539 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002540 /* It's impossible to "truncate" to a larger length than the full length
2541 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002543 }
2544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002546 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2547 * that is disabled in the compile-time configuration. The result can
2548 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2549 * configuration into account. In this case, force a return of
2550 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2551 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2552 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2553 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2554 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002556 }
2557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002559}
2560
Gilles Peskine449bd832023-01-11 14:50:10 +01002561static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2562 mbedtls_svc_key_id_t key,
2563 psa_algorithm_t alg,
2564 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002565{
2566 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2567 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002568 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002569 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002570
2571 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002573 status = PSA_ERROR_BAD_STATE;
2574 goto exit;
2575 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002576
2577 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 key,
2579 &slot,
2580 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2581 alg);
2582 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002583 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002585
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002586 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002587 .core = slot->attr
2588 };
2589
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2591 &operation->mac_size);
2592 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002593 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002595
2596 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002597 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 if (is_sign) {
2599 status = psa_driver_wrapper_mac_sign_setup(operation,
2600 &attributes,
2601 slot->key.data,
2602 slot->key.bytes,
2603 alg);
2604 } else {
2605 status = psa_driver_wrapper_mac_verify_setup(operation,
2606 &attributes,
2607 slot->key.data,
2608 slot->key.bytes,
2609 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002610 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002611
Gilles Peskinefbfac682018-07-08 20:51:54 +02002612exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 if (status != PSA_SUCCESS) {
2614 psa_mac_abort(operation);
2615 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002616
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002618
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002620}
2621
Gilles Peskine449bd832023-01-11 14:50:10 +01002622psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2623 mbedtls_svc_key_id_t key,
2624 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002625{
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002627}
2628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2630 mbedtls_svc_key_id_t key,
2631 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002632{
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002634}
2635
Gilles Peskine449bd832023-01-11 14:50:10 +01002636psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2637 const uint8_t *input,
2638 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002639{
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 if (operation->id == 0) {
2641 return PSA_ERROR_BAD_STATE;
2642 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002643
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002644 /* Don't require hash implementations to behave correctly on a
2645 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 if (input_length == 0) {
2647 return PSA_SUCCESS;
2648 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002649
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2651 input, input_length);
2652 if (status != PSA_SUCCESS) {
2653 psa_mac_abort(operation);
2654 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002655
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002657}
2658
Gilles Peskine449bd832023-01-11 14:50:10 +01002659psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2660 uint8_t *mac,
2661 size_t mac_size,
2662 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002663{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002664 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2665 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002666
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002668 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002669 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002670 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002673 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002674 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002675 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002676
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002677 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2678 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002680 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002681 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002682 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002685 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002686 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002687 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002688
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 status = psa_driver_wrapper_mac_sign_finish(operation,
2690 mac, operation->mac_size,
2691 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002692
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002693exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002694 /* In case of success, set the potential excess room in the output buffer
2695 * to an invalid value, to avoid potentially leaking a longer MAC.
2696 * In case of error, set the output length and content to a safe default,
2697 * such that in case the caller misses an error check, the output would be
2698 * an unachievable MAC.
2699 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002701 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002702 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002703 }
mohammad16036df908f2018-04-02 08:34:15 -07002704
Paul Elliottdc42ca82023-02-24 18:11:59 +00002705 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002706
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002708
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002710}
2711
Gilles Peskine449bd832023-01-11 14:50:10 +01002712psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2713 const uint8_t *mac,
2714 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002715{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002716 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2717 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002718
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002720 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002721 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002722 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002725 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002726 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002727 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002730 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002731 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002732 }
2733
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 status = psa_driver_wrapper_mac_verify_finish(operation,
2735 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002736
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002737exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002739
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002741}
2742
Gilles Peskine449bd832023-01-11 14:50:10 +01002743static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2744 psa_algorithm_t alg,
2745 const uint8_t *input,
2746 size_t input_length,
2747 uint8_t *mac,
2748 size_t mac_size,
2749 size_t *mac_length,
2750 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002751{
2752 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002753 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2754 psa_key_slot_t *slot;
2755 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002756 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002757
Ronald Cron51131b52021-06-17 17:17:20 +02002758 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 key,
2760 &slot,
2761 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2762 alg);
2763 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002764 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002766
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002767 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002768 .core = slot->attr
2769 };
2770
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2772 &operation_mac_size);
2773 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002774 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002776
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002778 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002779 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002780 }
2781
2782 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 &attributes,
2784 slot->key.data, slot->key.bytes,
2785 alg,
2786 input, input_length,
2787 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002788
2789exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002790 /* In case of success, set the potential excess room in the output buffer
2791 * to an invalid value, to avoid potentially leaking a longer MAC.
2792 * In case of error, set the output length and content to a safe default,
2793 * such that in case the caller misses an error check, the output would be
2794 * an unachievable MAC.
2795 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002797 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002798 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002799 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002800
2801 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002802
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002804
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002806}
2807
Gilles Peskine449bd832023-01-11 14:50:10 +01002808psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002809 psa_algorithm_t alg,
2810 const uint8_t *input,
2811 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 uint8_t *mac,
2813 size_t mac_size,
2814 size_t *mac_length)
2815{
2816 return psa_mac_compute_internal(key, alg,
2817 input, input_length,
2818 mac, mac_size, mac_length, 1);
2819}
2820
2821psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2822 psa_algorithm_t alg,
2823 const uint8_t *input,
2824 size_t input_length,
2825 const uint8_t *mac,
2826 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002827{
2828 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002829 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2830 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002831
Gilles Peskine449bd832023-01-11 14:50:10 +01002832 status = psa_mac_compute_internal(key, alg,
2833 input, input_length,
2834 actual_mac, sizeof(actual_mac),
2835 &actual_mac_length, 0);
2836 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002837 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002839
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002841 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002842 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002843 }
Dave Rodgman78701152023-08-29 14:20:18 +01002844 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002845 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002846 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002847 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002848
2849exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002851
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002853}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002854
Gilles Peskinee59236f2018-01-27 23:32:46 +01002855/****************************************************************/
2856/* Asymmetric cryptography */
2857/****************************************************************/
2858
Gilles Peskine449bd832023-01-11 14:50:10 +01002859static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2860 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002861{
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 if (input_is_message) {
2863 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2864 return PSA_ERROR_INVALID_ARGUMENT;
2865 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002866
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2868 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2869 return PSA_ERROR_INVALID_ARGUMENT;
2870 }
2871 }
2872 } else {
2873 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2874 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002875 }
2876 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002877
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002879}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002880
Gilles Peskine449bd832023-01-11 14:50:10 +01002881static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2882 int input_is_message,
2883 psa_algorithm_t alg,
2884 const uint8_t *input,
2885 size_t input_length,
2886 uint8_t *signature,
2887 size_t signature_size,
2888 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002889{
2890 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2891 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2892 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002893 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002894
2895 *signature_length = 0;
2896
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 status = psa_sign_verify_check_alg(input_is_message, alg);
2898 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002899 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002901
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002902 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002903 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002904 * buffer can in principle be empty since it doesn't actually have
2905 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 if (signature_size == 0) {
2907 return PSA_ERROR_BUFFER_TOO_SMALL;
2908 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002909
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002910 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 key, &slot,
2912 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2913 PSA_KEY_USAGE_SIGN_HASH,
2914 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002915
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002917 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002919
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002921 status = PSA_ERROR_INVALID_ARGUMENT;
2922 goto exit;
2923 }
2924
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002925 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002927 };
2928
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002930 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002931 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002932 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 signature, signature_size, signature_length);
2934 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002935
2936 status = psa_driver_wrapper_sign_hash(
2937 &attributes, slot->key.data, slot->key.bytes,
2938 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002940 }
2941
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002942
2943exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002944 psa_wipe_tag_output_buffer(signature, status, signature_size,
2945 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002946
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002948
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002950}
2951
Gilles Peskine449bd832023-01-11 14:50:10 +01002952static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2953 int input_is_message,
2954 psa_algorithm_t alg,
2955 const uint8_t *input,
2956 size_t input_length,
2957 const uint8_t *signature,
2958 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002959{
2960 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2961 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2962 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002963
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 status = psa_sign_verify_check_alg(input_is_message, alg);
2965 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002966 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002968
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002969 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 key, &slot,
2971 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2972 PSA_KEY_USAGE_VERIFY_HASH,
2973 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002974
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 if (status != PSA_SUCCESS) {
2976 return status;
2977 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002978
2979 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002981 };
2982
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002984 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002985 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002986 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 signature, signature_length);
2988 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002989 status = psa_driver_wrapper_verify_hash(
2990 &attributes, slot->key.data, slot->key.bytes,
2991 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002993 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002994
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002996
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002998
2999}
3000
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003001psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003002 const psa_key_attributes_t *attributes,
3003 const uint8_t *key_buffer,
3004 size_t key_buffer_size,
3005 psa_algorithm_t alg,
3006 const uint8_t *input,
3007 size_t input_length,
3008 uint8_t *signature,
3009 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003011{
3012 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003013
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003015 size_t hash_length;
3016 uint8_t hash[PSA_HASH_MAX_SIZE];
3017
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003018 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 PSA_ALG_SIGN_GET_HASH(alg),
3020 input, input_length,
3021 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003022
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003024 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003026
3027 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 attributes, key_buffer, key_buffer_size,
3029 alg, hash, hash_length,
3030 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003031 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003032
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003034}
3035
Gilles Peskine449bd832023-01-11 14:50:10 +01003036psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3037 psa_algorithm_t alg,
3038 const uint8_t *input,
3039 size_t input_length,
3040 uint8_t *signature,
3041 size_t signature_size,
3042 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003043{
3044 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003045 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003047}
3048
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003049psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003050 const psa_key_attributes_t *attributes,
3051 const uint8_t *key_buffer,
3052 size_t key_buffer_size,
3053 psa_algorithm_t alg,
3054 const uint8_t *input,
3055 size_t input_length,
3056 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003058{
3059 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003060
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003062 size_t hash_length;
3063 uint8_t hash[PSA_HASH_MAX_SIZE];
3064
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003065 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 PSA_ALG_SIGN_GET_HASH(alg),
3067 input, input_length,
3068 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003069
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003071 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003073
3074 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 attributes, key_buffer, key_buffer_size,
3076 alg, hash, hash_length,
3077 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003078 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003079
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003081}
3082
Gilles Peskine449bd832023-01-11 14:50:10 +01003083psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3084 psa_algorithm_t alg,
3085 const uint8_t *input,
3086 size_t input_length,
3087 const uint8_t *signature,
3088 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003089{
3090 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003091 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003093}
3094
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003095psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003096 const psa_key_attributes_t *attributes,
3097 const uint8_t *key_buffer, size_t key_buffer_size,
3098 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003100{
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3102 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3103 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003104#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3106 return mbedtls_psa_rsa_sign_hash(
3107 attributes,
3108 key_buffer, key_buffer_size,
3109 alg, hash, hash_length,
3110 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003111#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3112 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 } else {
3114 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003115 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3117 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003118#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3120 return mbedtls_psa_ecdsa_sign_hash(
3121 attributes,
3122 key_buffer, key_buffer_size,
3123 alg, hash, hash_length,
3124 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003125#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3126 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 } else {
3128 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003129 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003130 }
Ronald Cron4501c982021-03-19 20:09:32 +01003131
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 (void) key_buffer;
3133 (void) key_buffer_size;
3134 (void) hash;
3135 (void) hash_length;
3136 (void) signature;
3137 (void) signature_size;
3138 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003139
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003141}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003142
Gilles Peskine449bd832023-01-11 14:50:10 +01003143psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3144 psa_algorithm_t alg,
3145 const uint8_t *hash,
3146 size_t hash_length,
3147 uint8_t *signature,
3148 size_t signature_size,
3149 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003150{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003151 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003152 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003154}
3155
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003156psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003157 const psa_key_attributes_t *attributes,
3158 const uint8_t *key_buffer, size_t key_buffer_size,
3159 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003161{
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3163 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3164 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003165#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3167 return mbedtls_psa_rsa_verify_hash(
3168 attributes,
3169 key_buffer, key_buffer_size,
3170 alg, hash, hash_length,
3171 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003172#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3173 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003174 } else {
3175 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003176 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003177 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3178 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003179#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3181 return mbedtls_psa_ecdsa_verify_hash(
3182 attributes,
3183 key_buffer, key_buffer_size,
3184 alg, hash, hash_length,
3185 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003186#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3187 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003188 } else {
3189 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003190 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003191 }
Ronald Cron4501c982021-03-19 20:09:32 +01003192
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 (void) key_buffer;
3194 (void) key_buffer_size;
3195 (void) hash;
3196 (void) hash_length;
3197 (void) signature;
3198 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003199
Gilles Peskine449bd832023-01-11 14:50:10 +01003200 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003201}
3202
Gilles Peskine449bd832023-01-11 14:50:10 +01003203psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3204 psa_algorithm_t alg,
3205 const uint8_t *hash,
3206 size_t hash_length,
3207 const uint8_t *signature,
3208 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003209{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003210 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003211 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003213}
3214
Gilles Peskine449bd832023-01-11 14:50:10 +01003215psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3216 psa_algorithm_t alg,
3217 const uint8_t *input,
3218 size_t input_length,
3219 const uint8_t *salt,
3220 size_t salt_length,
3221 uint8_t *output,
3222 size_t output_size,
3223 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003224{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003225 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003226 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003227 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003228 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003229
Darryl Green5cc689a2018-07-24 15:34:10 +01003230 (void) input;
3231 (void) input_length;
3232 (void) salt;
3233 (void) output;
3234 (void) output_size;
3235
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003236 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003237
Gilles Peskine449bd832023-01-11 14:50:10 +01003238 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3239 return PSA_ERROR_INVALID_ARGUMENT;
3240 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003241
Ronald Cron5c522922020-11-14 16:35:34 +01003242 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003243 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3244 if (status != PSA_SUCCESS) {
3245 return status;
3246 }
3247 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3248 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003249 status = PSA_ERROR_INVALID_ARGUMENT;
3250 goto exit;
3251 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003252
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003253 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003255 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003256
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003257 status = psa_driver_wrapper_asymmetric_encrypt(
3258 &attributes, slot->key.data, slot->key.bytes,
3259 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003261exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003262 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003263
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003265}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003266
Gilles Peskine449bd832023-01-11 14:50:10 +01003267psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3268 psa_algorithm_t alg,
3269 const uint8_t *input,
3270 size_t input_length,
3271 const uint8_t *salt,
3272 size_t salt_length,
3273 uint8_t *output,
3274 size_t output_size,
3275 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003276{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003277 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003278 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003279 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003280 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003281
Darryl Green5cc689a2018-07-24 15:34:10 +01003282 (void) input;
3283 (void) input_length;
3284 (void) salt;
3285 (void) output;
3286 (void) output_size;
3287
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003288 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003289
Gilles Peskine449bd832023-01-11 14:50:10 +01003290 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3291 return PSA_ERROR_INVALID_ARGUMENT;
3292 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003293
Ronald Cron5c522922020-11-14 16:35:34 +01003294 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003295 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3296 if (status != PSA_SUCCESS) {
3297 return status;
3298 }
3299 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003300 status = PSA_ERROR_INVALID_ARGUMENT;
3301 goto exit;
3302 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003303
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003304 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003306 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003307
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003308 status = psa_driver_wrapper_asymmetric_decrypt(
3309 &attributes, slot->key.data, slot->key.bytes,
3310 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003312
3313exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003314 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003315
Gilles Peskine449bd832023-01-11 14:50:10 +01003316 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003317}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003318
Paul Elliott9fe12f62022-11-30 19:16:02 +00003319/****************************************************************/
3320/* Asymmetric interruptible cryptography */
3321/****************************************************************/
3322
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003323static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3324
Paul Elliott9fe12f62022-11-30 19:16:02 +00003325void psa_interruptible_set_max_ops(uint32_t max_ops)
3326{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003327 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003328}
3329
3330uint32_t psa_interruptible_get_max_ops(void)
3331{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003332 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003333}
3334
Paul Elliott9fe12f62022-11-30 19:16:02 +00003335uint32_t psa_sign_hash_get_num_ops(
3336 const psa_sign_hash_interruptible_operation_t *operation)
3337{
Paul Elliott296ede92022-12-15 17:00:30 +00003338 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003339}
3340
3341uint32_t psa_verify_hash_get_num_ops(
3342 const psa_verify_hash_interruptible_operation_t *operation)
3343{
Paul Elliott296ede92022-12-15 17:00:30 +00003344 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003345}
3346
Paul Elliott59ad9452022-12-18 15:09:02 +00003347static psa_status_t psa_sign_hash_abort_internal(
3348 psa_sign_hash_interruptible_operation_t *operation)
3349{
3350 if (operation->id == 0) {
3351 /* The object has (apparently) been initialized but it is not (yet)
3352 * in use. It's ok to call abort on such an object, and there's
3353 * nothing to do. */
3354 return PSA_SUCCESS;
3355 }
3356
3357 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3358
3359 status = psa_driver_wrapper_sign_hash_abort(operation);
3360
3361 operation->id = 0;
3362
Paul Elliotte6145dc2023-02-07 12:51:21 +00003363 /* Do not clear either the error_occurred or num_ops elements here as they
3364 * only want to be cleared by the application calling abort, not by abort
3365 * being called at completion of an operation. */
3366
Paul Elliott59ad9452022-12-18 15:09:02 +00003367 return status;
3368}
3369
Paul Elliott9fe12f62022-11-30 19:16:02 +00003370psa_status_t psa_sign_hash_start(
3371 psa_sign_hash_interruptible_operation_t *operation,
3372 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3373 const uint8_t *hash, size_t hash_length)
3374{
3375 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3376 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3377 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003378 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003379
Paul Elliottc9774412023-02-06 15:14:07 +00003380 /* Check that start has not been previously called, or operation has not
3381 * previously errored. */
3382 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003383 return PSA_ERROR_BAD_STATE;
3384 }
3385
Paul Elliott9fe12f62022-11-30 19:16:02 +00003386 status = psa_sign_verify_check_alg(0, alg);
3387 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003388 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003389 return status;
3390 }
3391
3392 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3393 PSA_KEY_USAGE_SIGN_HASH,
3394 alg);
3395
3396 if (status != PSA_SUCCESS) {
3397 goto exit;
3398 }
3399
3400 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3401 status = PSA_ERROR_INVALID_ARGUMENT;
3402 goto exit;
3403 }
3404
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003405 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003406 .core = slot->attr
3407 };
3408
Paul Elliott296ede92022-12-15 17:00:30 +00003409 /* Ensure ops count gets reset, in case of operation re-use. */
3410 operation->num_ops = 0;
3411
Paul Elliott9fe12f62022-11-30 19:16:02 +00003412 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3413 slot->key.data,
3414 slot->key.bytes, alg,
3415 hash, hash_length);
3416exit:
3417
3418 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003419 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003420 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003421 }
3422
3423 unlock_status = psa_unlock_key_slot(slot);
3424
Paul Elliottc9774412023-02-06 15:14:07 +00003425 if (unlock_status != PSA_SUCCESS) {
3426 operation->error_occurred = 1;
3427 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003428
Paul Elliottc9774412023-02-06 15:14:07 +00003429 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003430}
3431
3432
3433psa_status_t psa_sign_hash_complete(
3434 psa_sign_hash_interruptible_operation_t *operation,
3435 uint8_t *signature, size_t signature_size,
3436 size_t *signature_length)
3437{
3438 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3439
3440 *signature_length = 0;
3441
Paul Elliottc9774412023-02-06 15:14:07 +00003442 /* Check that start has been called first, and that operation has not
3443 * previously errored. */
3444 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003445 status = PSA_ERROR_BAD_STATE;
3446 goto exit;
3447 }
3448
Paul Elliott096abc42023-02-03 18:33:23 +00003449 /* Immediately reject a zero-length signature buffer. This guarantees that
3450 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003451 if (signature_size == 0) {
3452 status = PSA_ERROR_BUFFER_TOO_SMALL;
3453 goto exit;
3454 }
3455
3456 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3457 signature_size,
3458 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003459
Paul Elliott296ede92022-12-15 17:00:30 +00003460 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003461 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003462
Paul Elliottebe225c2023-02-10 14:32:53 +00003463exit:
3464
Paul Elliott59200a22023-02-21 15:07:40 +00003465 psa_wipe_tag_output_buffer(signature, status, signature_size,
3466 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003467
Paul Elliott53bb3122023-02-10 14:22:22 +00003468 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003469 if (status != PSA_SUCCESS) {
3470 operation->error_occurred = 1;
3471 }
3472
Paul Elliott59ad9452022-12-18 15:09:02 +00003473 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003474 }
3475
3476 return status;
3477}
3478
3479psa_status_t psa_sign_hash_abort(
3480 psa_sign_hash_interruptible_operation_t *operation)
3481{
Paul Elliott59ad9452022-12-18 15:09:02 +00003482 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3483
3484 status = psa_sign_hash_abort_internal(operation);
3485
3486 /* We clear the number of ops done here, so that it is not cleared when
3487 * the operation fails or succeeds, only on manual abort. */
3488 operation->num_ops = 0;
3489
Paul Elliottc9774412023-02-06 15:14:07 +00003490 /* Likewise, failure state. */
3491 operation->error_occurred = 0;
3492
Paul Elliott59ad9452022-12-18 15:09:02 +00003493 return status;
3494}
3495
3496static psa_status_t psa_verify_hash_abort_internal(
3497 psa_verify_hash_interruptible_operation_t *operation)
3498{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003499 if (operation->id == 0) {
3500 /* The object has (apparently) been initialized but it is not (yet)
3501 * in use. It's ok to call abort on such an object, and there's
3502 * nothing to do. */
3503 return PSA_SUCCESS;
3504 }
3505
Paul Elliott59ad9452022-12-18 15:09:02 +00003506 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3507
3508 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003509
3510 operation->id = 0;
3511
Paul Elliotte6145dc2023-02-07 12:51:21 +00003512 /* Do not clear either the error_occurred or num_ops elements here as they
3513 * only want to be cleared by the application calling abort, not by abort
3514 * being called at completion of an operation. */
3515
Paul Elliott59ad9452022-12-18 15:09:02 +00003516 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003517}
3518
3519psa_status_t psa_verify_hash_start(
3520 psa_verify_hash_interruptible_operation_t *operation,
3521 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3522 const uint8_t *hash, size_t hash_length,
3523 const uint8_t *signature, size_t signature_length)
3524{
3525 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3526 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3527 psa_key_slot_t *slot;
3528
Paul Elliottc9774412023-02-06 15:14:07 +00003529 /* Check that start has not been previously called, or operation has not
3530 * previously errored. */
3531 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003532 return PSA_ERROR_BAD_STATE;
3533 }
3534
3535 status = psa_sign_verify_check_alg(0, alg);
3536 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003537 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003538 return status;
3539 }
3540
3541 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3542 PSA_KEY_USAGE_VERIFY_HASH,
3543 alg);
3544
3545 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003546 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003547 return status;
3548 }
3549
3550 psa_key_attributes_t attributes = {
3551 .core = slot->attr
3552 };
3553
Paul Elliott296ede92022-12-15 17:00:30 +00003554 /* Ensure ops count gets reset, in case of operation re-use. */
3555 operation->num_ops = 0;
3556
Paul Elliott9fe12f62022-11-30 19:16:02 +00003557 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3558 slot->key.data,
3559 slot->key.bytes,
3560 alg, hash, hash_length,
3561 signature, signature_length);
3562
3563 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003564 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003565 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003566 }
3567
3568 unlock_status = psa_unlock_key_slot(slot);
3569
Paul Elliottc9774412023-02-06 15:14:07 +00003570 if (unlock_status != PSA_SUCCESS) {
3571 operation->error_occurred = 1;
3572 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003573
Paul Elliottc9774412023-02-06 15:14:07 +00003574 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003575}
3576
3577psa_status_t psa_verify_hash_complete(
3578 psa_verify_hash_interruptible_operation_t *operation)
3579{
3580 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3581
Paul Elliottc9774412023-02-06 15:14:07 +00003582 /* Check that start has been called first, and that operation has not
3583 * previously errored. */
3584 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003585 status = PSA_ERROR_BAD_STATE;
3586 goto exit;
3587 }
3588
3589 status = psa_driver_wrapper_verify_hash_complete(operation);
3590
Paul Elliott296ede92022-12-15 17:00:30 +00003591 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003592 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003593 operation);
3594
Paul Elliottebe225c2023-02-10 14:32:53 +00003595exit:
3596
Paul Elliott9fe12f62022-11-30 19:16:02 +00003597 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003598 if (status != PSA_SUCCESS) {
3599 operation->error_occurred = 1;
3600 }
3601
Paul Elliott59ad9452022-12-18 15:09:02 +00003602 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003603 }
3604
3605 return status;
3606}
3607
3608psa_status_t psa_verify_hash_abort(
3609 psa_verify_hash_interruptible_operation_t *operation)
3610{
Paul Elliott59ad9452022-12-18 15:09:02 +00003611 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003612
Paul Elliott59ad9452022-12-18 15:09:02 +00003613 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003614
Paul Elliott59ad9452022-12-18 15:09:02 +00003615 /* We clear the number of ops done here, so that it is not cleared when
3616 * the operation fails or succeeds, only on manual abort. */
3617 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003618
Paul Elliottc9774412023-02-06 15:14:07 +00003619 /* Likewise, failure state. */
3620 operation->error_occurred = 0;
3621
Paul Elliott59ad9452022-12-18 15:09:02 +00003622 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003623}
3624
Paul Elliott588f8ed2022-12-02 18:10:26 +00003625/****************************************************************/
3626/* Asymmetric interruptible cryptography internal */
3627/* implementations */
3628/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003629
Paul Elliott588f8ed2022-12-02 18:10:26 +00003630void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3631{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003632
Paul Elliott588f8ed2022-12-02 18:10:26 +00003633#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3634 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3635 defined(MBEDTLS_ECP_RESTARTABLE)
3636
3637 /* Internal implementation uses zero to indicate infinite number max ops,
3638 * therefore avoid this value, and set to minimum possible. */
3639 if (max_ops == 0) {
3640 max_ops = 1;
3641 }
3642
Paul Elliott588f8ed2022-12-02 18:10:26 +00003643 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003644#else
3645 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003646#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3647 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3648 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3649}
3650
Paul Elliott588f8ed2022-12-02 18:10:26 +00003651uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003652 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003653{
3654#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3655 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3656 defined(MBEDTLS_ECP_RESTARTABLE)
3657
Paul Elliott93d9ca82023-02-15 18:14:21 +00003658 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003659#else
3660 (void) operation;
3661 return 0;
3662#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3663 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3664 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3665}
3666
3667uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003668 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003669{
3670 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3671 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3672 defined(MBEDTLS_ECP_RESTARTABLE)
3673
Paul Elliott93d9ca82023-02-15 18:14:21 +00003674 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003675#else
3676 (void) operation;
3677 return 0;
3678#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3679 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3680 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3681}
3682
3683psa_status_t mbedtls_psa_sign_hash_start(
3684 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3685 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3686 size_t key_buffer_size, psa_algorithm_t alg,
3687 const uint8_t *hash, size_t hash_length)
3688{
3689 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003690 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003691
Paul Elliott068fe072023-01-16 13:59:15 +00003692 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3693 return PSA_ERROR_NOT_SUPPORTED;
3694 }
3695
3696 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003697 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003698 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003699
3700#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003701 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3702 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003703
Paul Elliotta1c94092023-02-15 16:38:04 +00003704 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3705
Paul Elliott93d9ca82023-02-15 18:14:21 +00003706 /* Ensure num_ops is zero'ed in case of context re-use. */
3707 operation->num_ops = 0;
3708
Paul Elliott068fe072023-01-16 13:59:15 +00003709 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3710 attributes->core.bits,
3711 key_buffer,
3712 key_buffer_size,
3713 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003714
Paul Elliott068fe072023-01-16 13:59:15 +00003715 if (status != PSA_SUCCESS) {
3716 return status;
3717 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003718
Paul Elliott1bc59df2023-02-05 13:41:57 +00003719 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003720 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003721
Paul Elliott068fe072023-01-16 13:59:15 +00003722 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003723 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003724 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003725
Paul Elliott0290a762023-02-09 14:30:24 +00003726 /* We only need to store the same length of hash as the private key size
3727 * here, it would be truncated by the internal implementation anyway. */
3728 required_hash_length = (hash_length < operation->coordinate_bytes ?
3729 hash_length : operation->coordinate_bytes);
3730
Paul Elliottba70ad42023-02-15 18:23:53 +00003731 if (required_hash_length > sizeof(operation->hash)) {
3732 /* Shouldn't happen, but better safe than sorry. */
3733 return PSA_ERROR_CORRUPTION_DETECTED;
3734 }
3735
Paul Elliott0290a762023-02-09 14:30:24 +00003736 memcpy(operation->hash, hash, required_hash_length);
3737 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003738
3739 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003740
3741#else
Paul Elliott068fe072023-01-16 13:59:15 +00003742 (void) operation;
3743 (void) key_buffer;
3744 (void) key_buffer_size;
3745 (void) alg;
3746 (void) hash;
3747 (void) hash_length;
3748 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003749 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003750
Paul Elliott068fe072023-01-16 13:59:15 +00003751 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003752#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3753 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3754 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003755}
3756
3757psa_status_t mbedtls_psa_sign_hash_complete(
3758 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3759 uint8_t *signature, size_t signature_size,
3760 size_t *signature_length)
3761{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003762#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3763 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3764 defined(MBEDTLS_ECP_RESTARTABLE)
3765
Paul Elliotta1c94092023-02-15 16:38:04 +00003766 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003767 mbedtls_mpi r;
3768 mbedtls_mpi s;
3769
Paul Elliott46845252023-02-03 14:59:11 +00003770 mbedtls_mpi_init(&r);
3771 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003772
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003773 /* Ensure max_ops is set to the current value (or default). */
3774 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3775
Paul Elliotta1c94092023-02-15 16:38:04 +00003776 if (signature_size < 2 * operation->coordinate_bytes) {
3777 status = PSA_ERROR_BUFFER_TOO_SMALL;
3778 goto exit;
3779 }
3780
Paul Elliott588f8ed2022-12-02 18:10:26 +00003781 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003782
Paul Elliott588f8ed2022-12-02 18:10:26 +00003783#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3784 status = mbedtls_to_psa_error(
3785 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003786 &r,
3787 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003788 &operation->ctx->d,
3789 operation->hash,
3790 operation->hash_length,
3791 operation->md_alg,
3792 mbedtls_psa_get_random,
3793 MBEDTLS_PSA_RANDOM_STATE,
3794 &operation->restart_ctx));
3795#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003796 status = PSA_ERROR_NOT_SUPPORTED;
3797 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003798#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3799 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003800 status = mbedtls_to_psa_error(
3801 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003802 &r,
3803 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003804 &operation->ctx->d,
3805 operation->hash,
3806 operation->hash_length,
3807 mbedtls_psa_get_random,
3808 MBEDTLS_PSA_RANDOM_STATE,
3809 mbedtls_psa_get_random,
3810 MBEDTLS_PSA_RANDOM_STATE,
3811 &operation->restart_ctx));
3812 }
3813
Paul Elliottf8e5b562023-02-19 18:43:45 +00003814 /* Hide the fact that the restart context only holds a delta of number of
3815 * ops done during the last operation, not an absolute value. */
3816 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3817
Paul Elliott724bd252023-02-08 12:35:08 +00003818 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003819 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003820 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003821 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003822 operation->coordinate_bytes)
3823 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003824
3825 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003826 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003827 }
3828
3829 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003830 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003831 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003832 operation->coordinate_bytes,
3833 operation->coordinate_bytes)
3834 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003835
3836 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003837 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003838 }
3839
Paul Elliott1bc59df2023-02-05 13:41:57 +00003840 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003841
Paul Elliott724bd252023-02-08 12:35:08 +00003842 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003843 }
Paul Elliott724bd252023-02-08 12:35:08 +00003844
3845exit:
3846
3847 mbedtls_mpi_free(&r);
3848 mbedtls_mpi_free(&s);
3849 return status;
3850
Paul Elliott588f8ed2022-12-02 18:10:26 +00003851 #else
3852
3853 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003854 (void) signature;
3855 (void) signature_size;
3856 (void) signature_length;
3857
3858 return PSA_ERROR_NOT_SUPPORTED;
3859
3860#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3861 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3862 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3863}
3864
3865psa_status_t mbedtls_psa_sign_hash_abort(
3866 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3867{
3868
3869#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3870 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3871 defined(MBEDTLS_ECP_RESTARTABLE)
3872
3873 if (operation->ctx) {
3874 mbedtls_ecdsa_free(operation->ctx);
3875 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003876 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003877 }
3878
3879 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3880
Paul Elliott93d9ca82023-02-15 18:14:21 +00003881 operation->num_ops = 0;
3882
Paul Elliott588f8ed2022-12-02 18:10:26 +00003883 return PSA_SUCCESS;
3884
3885#else
3886
3887 (void) operation;
3888
3889 return PSA_ERROR_NOT_SUPPORTED;
3890
3891#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3892 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3893 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3894}
3895
3896psa_status_t mbedtls_psa_verify_hash_start(
3897 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3898 const psa_key_attributes_t *attributes,
3899 const uint8_t *key_buffer, size_t key_buffer_size,
3900 psa_algorithm_t alg,
3901 const uint8_t *hash, size_t hash_length,
3902 const uint8_t *signature, size_t signature_length)
3903{
3904 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003905 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003906 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003907
Paul Elliott068fe072023-01-16 13:59:15 +00003908 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3909 return PSA_ERROR_NOT_SUPPORTED;
3910 }
3911
3912 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003913 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003914 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003915
3916#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003917 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3918 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003919
Paul Elliotta1c94092023-02-15 16:38:04 +00003920 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3921 mbedtls_mpi_init(&operation->r);
3922 mbedtls_mpi_init(&operation->s);
3923
Paul Elliott93d9ca82023-02-15 18:14:21 +00003924 /* Ensure num_ops is zero'ed in case of context re-use. */
3925 operation->num_ops = 0;
3926
Paul Elliott068fe072023-01-16 13:59:15 +00003927 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3928 attributes->core.bits,
3929 key_buffer,
3930 key_buffer_size,
3931 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003932
Paul Elliott068fe072023-01-16 13:59:15 +00003933 if (status != PSA_SUCCESS) {
3934 return status;
3935 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003936
Paul Elliottc569fc22023-02-10 13:02:54 +00003937 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003938
Paul Elliott1bc59df2023-02-05 13:41:57 +00003939 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003940 return PSA_ERROR_INVALID_SIGNATURE;
3941 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003942
Paul Elliott068fe072023-01-16 13:59:15 +00003943 status = mbedtls_to_psa_error(
3944 mbedtls_mpi_read_binary(&operation->r,
3945 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003946 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003947
Paul Elliott068fe072023-01-16 13:59:15 +00003948 if (status != PSA_SUCCESS) {
3949 return status;
3950 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003951
Paul Elliott068fe072023-01-16 13:59:15 +00003952 status = mbedtls_to_psa_error(
3953 mbedtls_mpi_read_binary(&operation->s,
3954 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003955 coordinate_bytes,
3956 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003957
Paul Elliott068fe072023-01-16 13:59:15 +00003958 if (status != PSA_SUCCESS) {
3959 return status;
3960 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003961
Paul Elliott2c9843f2023-02-15 17:32:42 +00003962 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003963
Paul Elliott2c9843f2023-02-15 17:32:42 +00003964 if (status != PSA_SUCCESS) {
3965 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003966 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003967
Paul Elliott0290a762023-02-09 14:30:24 +00003968 /* We only need to store the same length of hash as the private key size
3969 * here, it would be truncated by the internal implementation anyway. */
3970 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3971 coordinate_bytes);
3972
Paul Elliottba70ad42023-02-15 18:23:53 +00003973 if (required_hash_length > sizeof(operation->hash)) {
3974 /* Shouldn't happen, but better safe than sorry. */
3975 return PSA_ERROR_CORRUPTION_DETECTED;
3976 }
3977
Paul Elliott0290a762023-02-09 14:30:24 +00003978 memcpy(operation->hash, hash, required_hash_length);
3979 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003980
3981 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003982#else
Paul Elliott068fe072023-01-16 13:59:15 +00003983 (void) operation;
3984 (void) key_buffer;
3985 (void) key_buffer_size;
3986 (void) alg;
3987 (void) hash;
3988 (void) hash_length;
3989 (void) signature;
3990 (void) signature_length;
3991 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003992 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003993 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003994
Paul Elliott068fe072023-01-16 13:59:15 +00003995 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003996#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3997 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3998 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003999}
4000
4001psa_status_t mbedtls_psa_verify_hash_complete(
4002 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4003{
4004
4005#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4006 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4007 defined(MBEDTLS_ECP_RESTARTABLE)
4008
Paul Elliottf8e5b562023-02-19 18:43:45 +00004009 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4010
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004011 /* Ensure max_ops is set to the current value (or default). */
4012 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4013
Paul Elliottf8e5b562023-02-19 18:43:45 +00004014 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004015 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4016 operation->hash,
4017 operation->hash_length,
4018 &operation->ctx->Q,
4019 &operation->r,
4020 &operation->s,
4021 &operation->restart_ctx));
4022
Paul Elliottf8e5b562023-02-19 18:43:45 +00004023 /* Hide the fact that the restart context only holds a delta of number of
4024 * ops done during the last operation, not an absolute value. */
4025 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4026
4027 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004028#else
4029 (void) operation;
4030
4031 return PSA_ERROR_NOT_SUPPORTED;
4032
4033#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4034 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4035 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4036}
4037
4038psa_status_t mbedtls_psa_verify_hash_abort(
4039 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4040{
4041
4042#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4043 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4044 defined(MBEDTLS_ECP_RESTARTABLE)
4045
4046 if (operation->ctx) {
4047 mbedtls_ecdsa_free(operation->ctx);
4048 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004049 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004050 }
4051
4052 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4053
Paul Elliott93d9ca82023-02-15 18:14:21 +00004054 operation->num_ops = 0;
4055
Paul Elliott588f8ed2022-12-02 18:10:26 +00004056 mbedtls_mpi_free(&operation->r);
4057 mbedtls_mpi_free(&operation->s);
4058
4059 return PSA_SUCCESS;
4060
4061#else
4062 (void) operation;
4063
4064 return PSA_ERROR_NOT_SUPPORTED;
4065
4066#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4067 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4068 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4069}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004070
mohammad1603503973b2018-03-12 15:59:30 +02004071/****************************************************************/
4072/* Symmetric cryptography */
4073/****************************************************************/
4074
Gilles Peskine449bd832023-01-11 14:50:10 +01004075static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4076 mbedtls_svc_key_id_t key,
4077 psa_algorithm_t alg,
4078 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004079{
4080 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4081 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004082 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004083 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4084 PSA_KEY_USAGE_ENCRYPT :
4085 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004086 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004087
4088 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004089 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004090 status = PSA_ERROR_BAD_STATE;
4091 goto exit;
4092 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004093
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004095 status = PSA_ERROR_INVALID_ARGUMENT;
4096 goto exit;
4097 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004098
Gilles Peskine449bd832023-01-11 14:50:10 +01004099 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4100 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004101 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004102 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004103
Ronald Cron49fafa92021-03-10 08:34:23 +01004104 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004105 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004106 * so we only set it (in the driver wrapper) after resources have been
4107 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004108 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004109 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004110 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004111 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004112 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 }
4114 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004115
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004116 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004117 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004118 };
4119
Ronald Cronab99ac22020-10-01 16:38:28 +02004120 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004121 if (cipher_operation == MBEDTLS_ENCRYPT) {
4122 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4123 &attributes,
4124 slot->key.data,
4125 slot->key.bytes,
4126 alg);
4127 } else {
4128 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4129 &attributes,
4130 slot->key.data,
4131 slot->key.bytes,
4132 alg);
4133 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004134
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004135exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 if (status != PSA_SUCCESS) {
4137 psa_cipher_abort(operation);
4138 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004139
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004141
Gilles Peskine449bd832023-01-11 14:50:10 +01004142 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004143}
4144
Gilles Peskine449bd832023-01-11 14:50:10 +01004145psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4146 mbedtls_svc_key_id_t key,
4147 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004148{
Gilles Peskine449bd832023-01-11 14:50:10 +01004149 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004150}
4151
Gilles Peskine449bd832023-01-11 14:50:10 +01004152psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4153 mbedtls_svc_key_id_t key,
4154 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004155{
Gilles Peskine449bd832023-01-11 14:50:10 +01004156 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004157}
4158
Gilles Peskine449bd832023-01-11 14:50:10 +01004159psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4160 uint8_t *iv,
4161 size_t iv_size,
4162 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004163{
Ronald Cron6d051732020-10-01 14:10:20 +02004164 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004165 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004166 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004167
Gilles Peskine449bd832023-01-11 14:50:10 +01004168 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004169 status = PSA_ERROR_BAD_STATE;
4170 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004171 }
4172
Gilles Peskine449bd832023-01-11 14:50:10 +01004173 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004174 status = PSA_ERROR_BAD_STATE;
4175 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004176 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004177
Ronald Cron2fb90522021-07-05 12:31:44 +02004178 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004179 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004180 status = PSA_ERROR_BUFFER_TOO_SMALL;
4181 goto exit;
4182 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004183
Gilles Peskine449bd832023-01-11 14:50:10 +01004184 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004185 status = PSA_ERROR_GENERIC_ERROR;
4186 goto exit;
4187 }
4188
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 status = psa_generate_random(local_iv, default_iv_length);
4190 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004191 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 }
Ronald Cron5618a392021-03-26 09:52:26 +01004193
Gilles Peskine449bd832023-01-11 14:50:10 +01004194 status = psa_driver_wrapper_cipher_set_iv(operation,
4195 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004196
4197exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004198 if (status == PSA_SUCCESS) {
4199 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004200 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004201 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004203 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004204 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004205 }
Ronald Cron6d051732020-10-01 14:10:20 +02004206
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004208}
4209
Gilles Peskine449bd832023-01-11 14:50:10 +01004210psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4211 const uint8_t *iv,
4212 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004213{
Ronald Cron6d051732020-10-01 14:10:20 +02004214 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004215
Gilles Peskine449bd832023-01-11 14:50:10 +01004216 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004217 status = PSA_ERROR_BAD_STATE;
4218 goto exit;
4219 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004220
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004222 status = PSA_ERROR_BAD_STATE;
4223 goto exit;
4224 }
Ronald Crona0d68172021-03-26 10:15:08 +01004225
Gilles Peskine449bd832023-01-11 14:50:10 +01004226 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004227 status = PSA_ERROR_INVALID_ARGUMENT;
4228 goto exit;
4229 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004230
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 status = psa_driver_wrapper_cipher_set_iv(operation,
4232 iv,
4233 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004234
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004235exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004236 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004237 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004238 } else {
4239 psa_cipher_abort(operation);
4240 }
4241 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004242}
4243
Gilles Peskine449bd832023-01-11 14:50:10 +01004244psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4245 const uint8_t *input,
4246 size_t input_length,
4247 uint8_t *output,
4248 size_t output_size,
4249 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004250{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004251 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004252
Gilles Peskine449bd832023-01-11 14:50:10 +01004253 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004254 status = PSA_ERROR_BAD_STATE;
4255 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004256 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004257
Gilles Peskine449bd832023-01-11 14:50:10 +01004258 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004259 status = PSA_ERROR_BAD_STATE;
4260 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004261 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004262
Gilles Peskine449bd832023-01-11 14:50:10 +01004263 status = psa_driver_wrapper_cipher_update(operation,
4264 input,
4265 input_length,
4266 output,
4267 output_size,
4268 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004269
4270exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004271 if (status != PSA_SUCCESS) {
4272 psa_cipher_abort(operation);
4273 }
Ronald Cron6d051732020-10-01 14:10:20 +02004274
Gilles Peskine449bd832023-01-11 14:50:10 +01004275 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004276}
4277
Gilles Peskine449bd832023-01-11 14:50:10 +01004278psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4279 uint8_t *output,
4280 size_t output_size,
4281 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004282{
David Saadab4ecc272019-02-14 13:48:10 +02004283 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004284
Gilles Peskine449bd832023-01-11 14:50:10 +01004285 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004286 status = PSA_ERROR_BAD_STATE;
4287 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004288 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004289
Gilles Peskine449bd832023-01-11 14:50:10 +01004290 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004291 status = PSA_ERROR_BAD_STATE;
4292 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004293 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004294
Gilles Peskine449bd832023-01-11 14:50:10 +01004295 status = psa_driver_wrapper_cipher_finish(operation,
4296 output,
4297 output_size,
4298 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004299
4300exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004301 if (status == PSA_SUCCESS) {
4302 return psa_cipher_abort(operation);
4303 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004304 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004305 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004306
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004308 }
mohammad1603503973b2018-03-12 15:59:30 +02004309}
4310
Gilles Peskine449bd832023-01-11 14:50:10 +01004311psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004312{
Gilles Peskine449bd832023-01-11 14:50:10 +01004313 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004314 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004315 * in use. It's ok to call abort on such an object, and there's
4316 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004318 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004319
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004321
Ronald Cron49fafa92021-03-10 08:34:23 +01004322 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004323 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004324 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004325
Gilles Peskine449bd832023-01-11 14:50:10 +01004326 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004327}
4328
Gilles Peskine449bd832023-01-11 14:50:10 +01004329psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4330 psa_algorithm_t alg,
4331 const uint8_t *input,
4332 size_t input_length,
4333 uint8_t *output,
4334 size_t output_size,
4335 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004336{
4337 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004338 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004339 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004340 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4341 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004342 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004343
Gilles Peskine449bd832023-01-11 14:50:10 +01004344 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004345 status = PSA_ERROR_INVALID_ARGUMENT;
4346 goto exit;
4347 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004348
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4350 PSA_KEY_USAGE_ENCRYPT,
4351 alg);
4352 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004353 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004354 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004355
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004356 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004357 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004358 };
4359
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4361 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004362 status = PSA_ERROR_GENERIC_ERROR;
4363 goto exit;
4364 }
4365
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 if (default_iv_length > 0) {
4367 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004368 status = PSA_ERROR_BUFFER_TOO_SMALL;
4369 goto exit;
4370 }
4371
Gilles Peskine449bd832023-01-11 14:50:10 +01004372 status = psa_generate_random(local_iv, default_iv_length);
4373 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004374 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004376 }
4377
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004378 status = psa_driver_wrapper_cipher_encrypt(
4379 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004380 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004381 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004383
4384exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 unlock_status = psa_unlock_key_slot(slot);
4386 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004387 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004388 }
Ronald Cron23919522021-07-08 19:00:07 +02004389
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 if (status == PSA_SUCCESS) {
4391 if (default_iv_length > 0) {
4392 memcpy(output, local_iv, default_iv_length);
4393 }
4394 *output_length += default_iv_length;
4395 } else {
4396 *output_length = 0;
4397 }
4398
4399 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004400}
4401
Gilles Peskine449bd832023-01-11 14:50:10 +01004402psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4403 psa_algorithm_t alg,
4404 const uint8_t *input,
4405 size_t input_length,
4406 uint8_t *output,
4407 size_t output_size,
4408 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004409{
4410 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004411 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004412 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004413 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004414
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004416 status = PSA_ERROR_INVALID_ARGUMENT;
4417 goto exit;
4418 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004419
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4421 PSA_KEY_USAGE_DECRYPT,
4422 alg);
4423 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004424 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004425 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004426
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004427 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004428 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004429 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004430
Gilles Peskine449bd832023-01-11 14:50:10 +01004431 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4432 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004433 status = PSA_ERROR_INVALID_ARGUMENT;
4434 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004436 status = PSA_ERROR_INVALID_ARGUMENT;
4437 goto exit;
4438 }
4439
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004440 status = psa_driver_wrapper_cipher_decrypt(
4441 &attributes, slot->key.data, slot->key.bytes,
4442 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004443 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004444
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004445exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 unlock_status = psa_unlock_key_slot(slot);
4447 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004448 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004449 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004450
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004452 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 }
Ronald Cron23919522021-07-08 19:00:07 +02004454
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004456}
4457
4458
mohammad16035955c982018-04-26 00:53:03 +03004459/****************************************************************/
4460/* AEAD */
4461/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004462
Paul Elliottbaff51c2021-09-28 17:44:45 +01004463/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004464static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004465{
Gilles Peskine449bd832023-01-11 14:50:10 +01004466 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004467}
4468
4469/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004470static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4471 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004472{
Gilles Peskine449bd832023-01-11 14:50:10 +01004473 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004474
Gilles Peskine449bd832023-01-11 14:50:10 +01004475 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004476#if defined(PSA_WANT_ALG_GCM)
4477 case PSA_ALG_GCM:
4478 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 * arbitrarily large nonces. Please note that we do not generally
4480 * recommend the usage of nonces of greater length than
4481 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4482 * size, which can then lead to collisions if you encrypt a very
4483 * large number of messages.*/
4484 if (nonce_length != 0) {
4485 return PSA_SUCCESS;
4486 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004487 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004488#endif /* PSA_WANT_ALG_GCM */
4489#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004490 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 if (nonce_length >= 7 && nonce_length <= 13) {
4492 return PSA_SUCCESS;
4493 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004494 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004495#endif /* PSA_WANT_ALG_CCM */
4496#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004497 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004498 if (nonce_length == 12) {
4499 return PSA_SUCCESS;
4500 } else if (nonce_length == 8) {
4501 return PSA_ERROR_NOT_SUPPORTED;
4502 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004503 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004504#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004505 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004506 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004508 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004509
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004511}
4512
Gilles Peskine449bd832023-01-11 14:50:10 +01004513static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004514{
Gilles Peskine449bd832023-01-11 14:50:10 +01004515 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4516 return PSA_ERROR_INVALID_ARGUMENT;
4517 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004518
Gilles Peskine449bd832023-01-11 14:50:10 +01004519 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004520}
4521
Gilles Peskine449bd832023-01-11 14:50:10 +01004522psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4523 psa_algorithm_t alg,
4524 const uint8_t *nonce,
4525 size_t nonce_length,
4526 const uint8_t *additional_data,
4527 size_t additional_data_length,
4528 const uint8_t *plaintext,
4529 size_t plaintext_length,
4530 uint8_t *ciphertext,
4531 size_t ciphertext_size,
4532 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004533{
Ronald Cron215633c2021-03-16 17:15:37 +01004534 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4535 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004536
mohammad1603f08a5502018-06-03 15:05:47 +03004537 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004538
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 status = psa_aead_check_algorithm(alg);
4540 if (status != PSA_SUCCESS) {
4541 return status;
4542 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004543
Ronald Cron9a986162021-03-26 12:40:07 +01004544 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004545 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4546 if (status != PSA_SUCCESS) {
4547 return status;
4548 }
mohammad16035955c982018-04-26 00:53:03 +03004549
Ronald Cron215633c2021-03-16 17:15:37 +01004550 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004551 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004552 };
mohammad16035955c982018-04-26 00:53:03 +03004553
Gilles Peskine449bd832023-01-11 14:50:10 +01004554 status = psa_aead_check_nonce_length(alg, nonce_length);
4555 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004556 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004557 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004558
Ronald Cronde822812021-03-17 16:08:20 +01004559 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004560 &attributes, slot->key.data, slot->key.bytes,
4561 alg,
4562 nonce, nonce_length,
4563 additional_data, additional_data_length,
4564 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004566
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4568 memset(ciphertext, 0, ciphertext_size);
4569 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004570
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004571exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004573
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004575}
4576
Gilles Peskine449bd832023-01-11 14:50:10 +01004577psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4578 psa_algorithm_t alg,
4579 const uint8_t *nonce,
4580 size_t nonce_length,
4581 const uint8_t *additional_data,
4582 size_t additional_data_length,
4583 const uint8_t *ciphertext,
4584 size_t ciphertext_length,
4585 uint8_t *plaintext,
4586 size_t plaintext_size,
4587 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004588{
Ronald Cron215633c2021-03-16 17:15:37 +01004589 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4590 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004591
Gilles Peskineee652a32018-06-01 19:23:52 +02004592 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004593
Gilles Peskine449bd832023-01-11 14:50:10 +01004594 status = psa_aead_check_algorithm(alg);
4595 if (status != PSA_SUCCESS) {
4596 return status;
4597 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004598
Ronald Cron9a986162021-03-26 12:40:07 +01004599 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004600 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4601 if (status != PSA_SUCCESS) {
4602 return status;
4603 }
mohammad16035955c982018-04-26 00:53:03 +03004604
Ronald Cron215633c2021-03-16 17:15:37 +01004605 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004606 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004607 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004608
Gilles Peskine449bd832023-01-11 14:50:10 +01004609 status = psa_aead_check_nonce_length(alg, nonce_length);
4610 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004611 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004613
Ronald Cronde822812021-03-17 16:08:20 +01004614 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004615 &attributes, slot->key.data, slot->key.bytes,
4616 alg,
4617 nonce, nonce_length,
4618 additional_data, additional_data_length,
4619 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004621
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 if (status != PSA_SUCCESS && plaintext_size != 0) {
4623 memset(plaintext, 0, plaintext_size);
4624 }
mohammad160360a64d02018-06-03 17:20:42 +03004625
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004626exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004628
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 return status;
mohammad16035955c982018-04-26 00:53:03 +03004630}
4631
Gilles Peskine449bd832023-01-11 14:50:10 +01004632static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4633{
4634 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004635
Gilles Peskine449bd832023-01-11 14:50:10 +01004636 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004637#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004638 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004639 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4641 return PSA_ERROR_INVALID_ARGUMENT;
4642 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004643 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004644#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004645
Przemek Stekiel86679c72022-10-06 17:06:56 +02004646#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004648 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4650 return PSA_ERROR_INVALID_ARGUMENT;
4651 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004652 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004653#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004654
Przemek Stekiel86679c72022-10-06 17:06:56 +02004655#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004656 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004657 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 if (tag_len != 16) {
4659 return PSA_ERROR_INVALID_ARGUMENT;
4660 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004661 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004662#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004663
4664 default:
4665 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004666 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004667 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004669}
4670
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004671/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004672static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4673 int is_encrypt,
4674 mbedtls_svc_key_id_t key,
4675 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004676{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004677 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4678 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4679 psa_key_slot_t *slot = NULL;
4680 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004681 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004682
Gilles Peskine449bd832023-01-11 14:50:10 +01004683 status = psa_aead_check_algorithm(alg);
4684 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004685 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004686 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004687
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004689 status = PSA_ERROR_BAD_STATE;
4690 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004691 }
4692
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 if (operation->nonce_set || operation->lengths_set ||
4694 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004695 status = PSA_ERROR_BAD_STATE;
4696 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004697 }
4698
Gilles Peskine449bd832023-01-11 14:50:10 +01004699 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004700 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004702 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004703 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004704
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4706 alg);
4707 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004708 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004710
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004711 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004712 .core = slot->attr
4713 };
4714
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004716 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004718
Gilles Peskine449bd832023-01-11 14:50:10 +01004719 if (is_encrypt) {
4720 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4721 &attributes,
4722 slot->key.data,
4723 slot->key.bytes,
4724 alg);
4725 } else {
4726 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4727 &attributes,
4728 slot->key.data,
4729 slot->key.bytes,
4730 alg);
4731 }
4732 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004733 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004735
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004737
4738exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004739 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004740
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004742 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004743 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004744 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004745 } else {
4746 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004747 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004748
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004750}
4751
Paul Elliott302ff6b2021-04-20 18:10:30 +01004752/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004753psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4754 mbedtls_svc_key_id_t key,
4755 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004756{
Gilles Peskine449bd832023-01-11 14:50:10 +01004757 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004758}
4759
4760/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004761psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4762 mbedtls_svc_key_id_t key,
4763 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004764{
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004766}
4767
4768/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004769psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4770 uint8_t *nonce,
4771 size_t nonce_size,
4772 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004773{
4774 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004775 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004776 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004777
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004778 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004779
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004781 status = PSA_ERROR_BAD_STATE;
4782 goto exit;
4783 }
4784
Gilles Peskine449bd832023-01-11 14:50:10 +01004785 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004786 status = PSA_ERROR_BAD_STATE;
4787 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004788 }
4789
Paul Elliotte193ea82021-10-01 13:00:16 +01004790 /* For CCM, this size may not be correct according to the PSA
4791 * specification. The PSA Crypto 1.0.1 specification states:
4792 *
4793 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4794 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4795 *
4796 * However this restriction that L has to be the smallest integer is not
4797 * applied in practice, and it is not implementable here since the
4798 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4800 operation->alg);
4801 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004802 status = PSA_ERROR_BUFFER_TOO_SMALL;
4803 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004804 }
4805
Gilles Peskine449bd832023-01-11 14:50:10 +01004806 status = psa_generate_random(local_nonce, required_nonce_size);
4807 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004808 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004810
Gilles Peskine449bd832023-01-11 14:50:10 +01004811 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004812
Paul Elliott39dc6b82021-05-11 19:16:09 +01004813exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 if (status == PSA_SUCCESS) {
4815 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004816 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 } else {
4818 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004819 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004820
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004822}
4823
4824/* Set the nonce for a multipart authenticated encryption or decryption
4825 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004826psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4827 const uint8_t *nonce,
4828 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004829{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004830 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4831
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004833 status = PSA_ERROR_BAD_STATE;
4834 goto exit;
4835 }
4836
Gilles Peskine449bd832023-01-11 14:50:10 +01004837 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004838 status = PSA_ERROR_BAD_STATE;
4839 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004840 }
4841
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4843 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004844 status = PSA_ERROR_INVALID_ARGUMENT;
4845 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004846 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004847
Gilles Peskine449bd832023-01-11 14:50:10 +01004848 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4849 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004850
Paul Elliott39dc6b82021-05-11 19:16:09 +01004851exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004853 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004854 } else {
4855 psa_aead_abort(operation);
4856 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004857
Gilles Peskine449bd832023-01-11 14:50:10 +01004858 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004859}
4860
4861/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004862psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4863 size_t ad_length,
4864 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004865{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004866 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004869 status = PSA_ERROR_BAD_STATE;
4870 goto exit;
4871 }
4872
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 if (operation->lengths_set || operation->ad_started ||
4874 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004875 status = PSA_ERROR_BAD_STATE;
4876 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004877 }
4878
Gilles Peskine449bd832023-01-11 14:50:10 +01004879 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004880#if defined(PSA_WANT_ALG_GCM)
4881 case PSA_ALG_GCM:
4882 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 * bits. Without the guard this code will generate warnings on 32bit
4884 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004885#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 if (((uint64_t) ad_length) >> 61 != 0 ||
4887 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004888 status = PSA_ERROR_INVALID_ARGUMENT;
4889 goto exit;
4890 }
Paul Elliott325d3742021-09-27 17:56:28 +01004891#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004892 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004893#endif /* PSA_WANT_ALG_GCM */
4894#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004895 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004897 status = PSA_ERROR_INVALID_ARGUMENT;
4898 goto exit;
4899 }
4900 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004901#endif /* PSA_WANT_ALG_CCM */
4902#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004903 case PSA_ALG_CHACHA20_POLY1305:
4904 /* No length restrictions for ChaChaPoly. */
4905 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004906#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004907 default:
4908 break;
4909 }
Paul Elliott325d3742021-09-27 17:56:28 +01004910
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4912 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004913
Paul Elliott39dc6b82021-05-11 19:16:09 +01004914exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004915 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004916 operation->ad_remaining = ad_length;
4917 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004918 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004919 } else {
4920 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004921 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004922
Gilles Peskine449bd832023-01-11 14:50:10 +01004923 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004924}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004925
4926/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004927psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4928 const uint8_t *input,
4929 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004930{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004931 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4932
Gilles Peskine449bd832023-01-11 14:50:10 +01004933 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004934 status = PSA_ERROR_BAD_STATE;
4935 goto exit;
4936 }
4937
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004939 status = PSA_ERROR_BAD_STATE;
4940 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004941 }
4942
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 if (operation->lengths_set) {
4944 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004945 status = PSA_ERROR_INVALID_ARGUMENT;
4946 goto exit;
4947 }
4948
4949 operation->ad_remaining -= input_length;
4950 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004951#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004952 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004953 status = PSA_ERROR_BAD_STATE;
4954 goto exit;
4955 }
4956#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004957
Gilles Peskine449bd832023-01-11 14:50:10 +01004958 status = psa_driver_wrapper_aead_update_ad(operation, input,
4959 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004960
Paul Elliott39dc6b82021-05-11 19:16:09 +01004961exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004962 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004963 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004964 } else {
4965 psa_aead_abort(operation);
4966 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004967
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004969}
4970
4971/* Encrypt or decrypt a message fragment in an active multipart AEAD
4972 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004973psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4974 const uint8_t *input,
4975 size_t input_length,
4976 uint8_t *output,
4977 size_t output_size,
4978 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004979{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004980 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004981
4982 *output_length = 0;
4983
Gilles Peskine449bd832023-01-11 14:50:10 +01004984 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004985 status = PSA_ERROR_BAD_STATE;
4986 goto exit;
4987 }
4988
Gilles Peskine449bd832023-01-11 14:50:10 +01004989 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004990 status = PSA_ERROR_BAD_STATE;
4991 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004992 }
4993
Gilles Peskine449bd832023-01-11 14:50:10 +01004994 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004995 /* Additional data length was supplied, but not all the additional
4996 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004997 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004998 status = PSA_ERROR_INVALID_ARGUMENT;
4999 goto exit;
5000 }
5001
5002 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005003 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005004 status = PSA_ERROR_INVALID_ARGUMENT;
5005 goto exit;
5006 }
5007
5008 operation->body_remaining -= input_length;
5009 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005010#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005011 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005012 status = PSA_ERROR_BAD_STATE;
5013 goto exit;
5014 }
5015#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005016
Gilles Peskine449bd832023-01-11 14:50:10 +01005017 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5018 output, output_size,
5019 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005020
Paul Elliott39dc6b82021-05-11 19:16:09 +01005021exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005022 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005023 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 } else {
5025 psa_aead_abort(operation);
5026 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005027
Gilles Peskine449bd832023-01-11 14:50:10 +01005028 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005029}
5030
Gilles Peskine449bd832023-01-11 14:50:10 +01005031static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005032{
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 if (operation->id == 0 || !operation->nonce_set) {
5034 return PSA_ERROR_BAD_STATE;
5035 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005036
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5038 operation->body_remaining != 0)) {
5039 return PSA_ERROR_INVALID_ARGUMENT;
5040 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005041
Gilles Peskine449bd832023-01-11 14:50:10 +01005042 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005043}
5044
Paul Elliott302ff6b2021-04-20 18:10:30 +01005045/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005046psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5047 uint8_t *ciphertext,
5048 size_t ciphertext_size,
5049 size_t *ciphertext_length,
5050 uint8_t *tag,
5051 size_t tag_size,
5052 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005053{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005054 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5055
Paul Elliott302ff6b2021-04-20 18:10:30 +01005056 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005057 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005058
Gilles Peskine449bd832023-01-11 14:50:10 +01005059 status = psa_aead_final_checks(operation);
5060 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005061 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005062 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005063
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005065 status = PSA_ERROR_BAD_STATE;
5066 goto exit;
5067 }
5068
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5070 ciphertext_size,
5071 ciphertext_length,
5072 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005073
Paul Elliott39dc6b82021-05-11 19:16:09 +01005074exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005075
5076
Paul Elliottf47b0952021-05-21 18:02:33 +01005077 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005078 * the zero tag size, make sure the tag is set to something implausible.
5079 * Even if the operation succeeds, make sure we clear the rest of the
5080 * buffer to prevent potential leakage of anything previously placed in
5081 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005082 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005083
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005085
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005087}
5088
5089/* Finish authenticating and decrypting a message in a multipart AEAD
5090 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005091psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5092 uint8_t *plaintext,
5093 size_t plaintext_size,
5094 size_t *plaintext_length,
5095 const uint8_t *tag,
5096 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005097{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005098 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5099
Paul Elliott302ff6b2021-04-20 18:10:30 +01005100 *plaintext_length = 0;
5101
Gilles Peskine449bd832023-01-11 14:50:10 +01005102 status = psa_aead_final_checks(operation);
5103 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005104 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005106
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005108 status = PSA_ERROR_BAD_STATE;
5109 goto exit;
5110 }
5111
Gilles Peskine449bd832023-01-11 14:50:10 +01005112 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5113 plaintext_size,
5114 plaintext_length,
5115 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005116
5117exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005118 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005119
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005121}
5122
5123/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005124psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005125{
5126 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5127
Gilles Peskine449bd832023-01-11 14:50:10 +01005128 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005129 /* The object has (apparently) been initialized but it is not (yet)
5130 * in use. It's ok to call abort on such an object, and there's
5131 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005132 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005133 }
5134
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005136
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005138
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005140}
5141
Gilles Peskinee59236f2018-01-27 23:32:46 +01005142/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005143/* Generators */
5144/****************************************************************/
5145
Przemek Stekiel69c46792022-06-10 12:59:51 +02005146#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005147 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005148 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305149 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305150 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005151#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005152#endif /* At least one builtin KDF */
5153
Przemek Stekiel69c46792022-06-10 12:59:51 +02005154#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005155 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5156 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5157static psa_status_t psa_key_derivation_start_hmac(
5158 psa_mac_operation_t *operation,
5159 psa_algorithm_t hash_alg,
5160 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005162{
5163 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5164 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005165 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5166 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5167 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005168
Steven Cooreman72f736a2021-05-07 14:14:37 +02005169 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005170 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005171
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 status = psa_driver_wrapper_mac_sign_setup(operation,
5173 &attributes,
5174 hmac_key, hmac_key_length,
5175 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005176
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 psa_reset_key_attributes(&attributes);
5178 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005179}
5180#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005181
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005182#define HKDF_STATE_INIT 0 /* no input yet */
5183#define HKDF_STATE_STARTED 1 /* got salt */
5184#define HKDF_STATE_KEYED 2 /* got key */
5185#define HKDF_STATE_OUTPUT 3 /* output started */
5186
Gilles Peskinecbe66502019-05-16 16:59:18 +02005187static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005189{
Gilles Peskine449bd832023-01-11 14:50:10 +01005190 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5191 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5192 } else {
5193 return operation->alg;
5194 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005195}
5196
Gilles Peskine449bd832023-01-11 14:50:10 +01005197psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005198{
5199 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005200 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5201 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005202 /* The object has (apparently) been initialized but it is not
5203 * in use. It's ok to call abort on such an object, and there's
5204 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005205 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005206#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005207 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5208 mbedtls_free(operation->ctx.hkdf.info);
5209 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5210 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005211#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005212#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5213 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5215 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5216 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5217 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005218 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005219 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005220 }
5221
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005223 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005224 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005225 }
5226
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005228 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005230 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005231#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005233 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005235 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005236#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005237 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005238
5239 /* We leave the fields Ai and output_block to be erased safely by the
5240 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005242#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5243 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005244#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5246 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5247 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5248 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005249#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305250#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305251 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305252 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005253 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305254 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305255 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305256
5257 status = PSA_SUCCESS;
5258 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305259#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005260 {
5261 status = PSA_ERROR_BAD_STATE;
5262 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 mbedtls_platform_zeroize(operation, sizeof(*operation));
5264 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005265}
5266
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005267psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005269{
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005271 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005272 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005273 }
5274
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005275 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005277}
5278
Gilles Peskine449bd832023-01-11 14:50:10 +01005279psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5280 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005281{
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 if (operation->alg == 0) {
5283 return PSA_ERROR_BAD_STATE;
5284 }
5285 if (capacity > operation->capacity) {
5286 return PSA_ERROR_INVALID_ARGUMENT;
5287 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005288 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005290}
5291
Przemek Stekiel69c46792022-06-10 12:59:51 +02005292#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005293/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005294static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5295 psa_algorithm_t kdf_alg,
5296 uint8_t *output,
5297 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005298{
Gilles Peskine449bd832023-01-11 14:50:10 +01005299 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5300 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005301 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005302 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005303#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005305#else
5306 const uint8_t last_block = 0xff;
5307#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005308
Gilles Peskine449bd832023-01-11 14:50:10 +01005309 if (hkdf->state < HKDF_STATE_KEYED ||
5310 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005311#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005312 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005313#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005314 )) {
5315 return PSA_ERROR_BAD_STATE;
5316 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005317 hkdf->state = HKDF_STATE_OUTPUT;
5318
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005320 /* Copy what remains of the current block */
5321 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005323 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 }
5325 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005326 output += n;
5327 output_length -= n;
5328 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005330 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005332 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005333 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005334 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005335 * object was corrupted or if this function is called directly
5336 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 if (hkdf->block_number == last_block) {
5338 return PSA_ERROR_BAD_STATE;
5339 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005340
5341 /* We need a new block */
5342 ++hkdf->block_number;
5343 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005344
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5346 hash_alg,
5347 hkdf->prk,
5348 hash_length);
5349 if (status != PSA_SUCCESS) {
5350 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005351 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005352
5353 if (hkdf->block_number != 1) {
5354 status = psa_mac_update(&hkdf->hmac,
5355 hkdf->output_block,
5356 hash_length);
5357 if (status != PSA_SUCCESS) {
5358 return status;
5359 }
5360 }
5361 status = psa_mac_update(&hkdf->hmac,
5362 hkdf->info,
5363 hkdf->info_length);
5364 if (status != PSA_SUCCESS) {
5365 return status;
5366 }
5367 status = psa_mac_update(&hkdf->hmac,
5368 &hkdf->block_number, 1);
5369 if (status != PSA_SUCCESS) {
5370 return status;
5371 }
5372 status = psa_mac_sign_finish(&hkdf->hmac,
5373 hkdf->output_block,
5374 sizeof(hkdf->output_block),
5375 &hmac_output_length);
5376 if (status != PSA_SUCCESS) {
5377 return status;
5378 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005379 }
5380
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005382}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005383#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005384
John Durkop07cc04a2020-11-16 22:08:34 -08005385#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5386 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005387static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5388 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005389 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005390{
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5392 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005393 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5394 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005395 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005396
Janos Follath7742fee2019-06-17 12:58:10 +01005397 /* We can't be wanting more output after block 0xff, otherwise
5398 * the capacity check in psa_key_derivation_output_bytes() would have
5399 * prevented this call. It could happen only if the operation
5400 * object was corrupted or if this function is called directly
5401 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 if (tls12_prf->block_number == 0xff) {
5403 return PSA_ERROR_CORRUPTION_DETECTED;
5404 }
Janos Follath7742fee2019-06-17 12:58:10 +01005405
5406 /* We need a new block */
5407 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005408 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005409
5410 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5411 *
5412 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5413 *
5414 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5415 * HMAC_hash(secret, A(2) + seed) +
5416 * HMAC_hash(secret, A(3) + seed) + ...
5417 *
5418 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005419 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005420 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005421 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005422 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005423 * is currently extracted as `output_block` and where i is
5424 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005425 */
5426
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 status = psa_key_derivation_start_hmac(&hmac,
5428 hash_alg,
5429 tls12_prf->secret,
5430 tls12_prf->secret_length);
5431 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005432 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005433 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005434
5435 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005436 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005437 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5438 * the variable seed and in this instance means it in the context of the
5439 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005440 status = psa_mac_update(&hmac,
5441 tls12_prf->label,
5442 tls12_prf->label_length);
5443 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005444 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 }
5446 status = psa_mac_update(&hmac,
5447 tls12_prf->seed,
5448 tls12_prf->seed_length);
5449 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005450 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 }
5452 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005453 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5455 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005456 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005458 }
5459
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 status = psa_mac_sign_finish(&hmac,
5461 tls12_prf->Ai, hash_length,
5462 &hmac_output_length);
5463 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005464 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 }
5466 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005467 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005468 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005469
5470 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 status = psa_key_derivation_start_hmac(&hmac,
5472 hash_alg,
5473 tls12_prf->secret,
5474 tls12_prf->secret_length);
5475 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005476 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 }
5478 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5479 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005480 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 }
5482 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5483 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005484 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005485 }
5486 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5487 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005488 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 }
5490 status = psa_mac_sign_finish(&hmac,
5491 tls12_prf->output_block, hash_length,
5492 &hmac_output_length);
5493 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005494 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005496
Janos Follath7742fee2019-06-17 12:58:10 +01005497
5498cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 cleanup_status = psa_mac_abort(&hmac);
5500 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005501 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005503
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005505}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005506
Janos Follath844eb0e2019-06-19 12:10:49 +01005507static psa_status_t psa_key_derivation_tls12_prf_read(
5508 psa_tls12_prf_key_derivation_t *tls12_prf,
5509 psa_algorithm_t alg,
5510 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005512{
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5514 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005515 psa_status_t status;
5516 uint8_t offset, length;
5517
Gilles Peskine449bd832023-01-11 14:50:10 +01005518 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005519 case PSA_TLS12_PRF_STATE_LABEL_SET:
5520 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5521 break;
5522 case PSA_TLS12_PRF_STATE_OUTPUT:
5523 break;
5524 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005526 }
5527
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005529 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005530 if (tls12_prf->left_in_block == 0) {
5531 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5532 alg);
5533 if (status != PSA_SUCCESS) {
5534 return status;
5535 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005536
5537 continue;
5538 }
5539
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005541 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005543 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005545
5546 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005548 output += length;
5549 output_length -= length;
5550 tls12_prf->left_in_block -= length;
5551 }
5552
Gilles Peskine449bd832023-01-11 14:50:10 +01005553 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005554}
John Durkop07cc04a2020-11-16 22:08:34 -08005555#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5556 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005557
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005558#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5559static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5560 psa_tls12_ecjpake_to_pms_t *ecjpake,
5561 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005563{
5564 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005565 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005566
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 if (output_length != 32) {
5568 return PSA_ERROR_INVALID_ARGUMENT;
5569 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005570
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5572 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5573 &output_size);
5574 if (status != PSA_SUCCESS) {
5575 return status;
5576 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005577
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 if (output_size != output_length) {
5579 return PSA_ERROR_GENERIC_ERROR;
5580 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005581
Gilles Peskine449bd832023-01-11 14:50:10 +01005582 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005583}
5584#endif
5585
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305586#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305587static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5588 psa_pbkdf2_key_derivation_t *pbkdf2,
5589 psa_algorithm_t prf_alg,
5590 uint8_t prf_output_length,
5591 psa_key_attributes_t *attributes)
5592{
5593 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305594 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305595 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305596 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305597 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305598 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305599 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305600
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305601 mac_operation.is_sign = 1;
5602 mac_operation.mac_size = prf_output_length;
5603 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305604
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305605 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5606 attributes,
5607 pbkdf2->password,
5608 pbkdf2->password_length,
5609 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305610 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305611 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305612 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305613 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5614 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305615 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305616 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305617 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305618 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305619 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305620 }
5621 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5622 &mac_output_length);
5623 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305624 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305625 }
5626
5627 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305628 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305629 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305630 }
5631
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305632 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305633
5634 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305635 /* We are passing prf_output_length as mac_size because the driver
5636 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305637 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305638 status = psa_driver_wrapper_mac_compute(attributes,
5639 pbkdf2->password,
5640 pbkdf2->password_length,
5641 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305642 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305643 &mac_output_length);
5644 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305645 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305646 }
5647
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305648 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305649 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305650
5651cleanup:
5652 /* Zeroise buffers to clear sensitive data from memory. */
5653 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5654 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305655}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305656
5657static psa_status_t psa_key_derivation_pbkdf2_read(
5658 psa_pbkdf2_key_derivation_t *pbkdf2,
5659 psa_algorithm_t kdf_alg,
5660 uint8_t *output,
5661 size_t output_length)
5662{
5663 psa_status_t status;
5664 psa_algorithm_t prf_alg;
5665 uint8_t prf_output_length;
5666 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5667 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5668 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5669
5670 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5671 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5672 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5673 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305674 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5675 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305676 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305677 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305678 } else {
5679 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305680 }
5681
5682 switch (pbkdf2->state) {
5683 case PSA_PBKDF2_STATE_PASSWORD_SET:
5684 /* Initially we need a new block so bytes_used is equal to block size*/
5685 pbkdf2->bytes_used = prf_output_length;
5686 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5687 break;
5688 case PSA_PBKDF2_STATE_OUTPUT:
5689 break;
5690 default:
5691 return PSA_ERROR_BAD_STATE;
5692 }
5693
5694 while (output_length != 0) {
5695 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5696 if (n > output_length) {
5697 n = (uint8_t) output_length;
5698 }
5699 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5700 output += n;
5701 output_length -= n;
5702 pbkdf2->bytes_used += n;
5703
5704 if (output_length == 0) {
5705 break;
5706 }
5707
5708 /* We need a new block */
5709 pbkdf2->bytes_used = 0;
5710 pbkdf2->block_number++;
5711
5712 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5713 prf_output_length,
5714 &attributes);
5715 if (status != PSA_SUCCESS) {
5716 return status;
5717 }
5718 }
5719
5720 return PSA_SUCCESS;
5721}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305722#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305723
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005724psa_status_t psa_key_derivation_output_bytes(
5725 psa_key_derivation_operation_t *operation,
5726 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005727 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005728{
5729 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005730 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005731
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005733 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005735 }
5736
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005738 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005739 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005740 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005741 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005742 goto exit;
5743 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005745 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005746 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005747 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5748 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005749 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005750 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005751 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005752 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005753 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005754
Przemek Stekiel69c46792022-06-10 12:59:51 +02005755#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5757 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5758 output, output_length);
5759 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005760#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005761#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5762 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005763 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5764 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5765 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5766 kdf_alg, output,
5767 output_length);
5768 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005769#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5770 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005771#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005772 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005773 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5775 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005776#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305777#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305778 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305779 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5780 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305781 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305782#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005783
Gilles Peskineeab56e42018-07-12 17:12:33 +02005784 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005785 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005787 }
5788
5789exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005791 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005792 * This allows us to differentiate between exhausted operations and
5793 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5794 * operations. */
5795 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005797 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005799 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005801}
5802
David Brown7807bf72021-02-09 16:28:23 -07005803#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005804static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005805{
Gilles Peskine449bd832023-01-11 14:50:10 +01005806 if (data_size >= 8) {
5807 mbedtls_des_key_set_parity(data);
5808 }
5809 if (data_size >= 16) {
5810 mbedtls_des_key_set_parity(data + 8);
5811 }
5812 if (data_size >= 24) {
5813 mbedtls_des_key_set_parity(data + 16);
5814 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005815}
David Brown7807bf72021-02-09 16:28:23 -07005816#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005817
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005818/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005819 * ECC keys on a Weierstrass elliptic curve require the generation
5820 * of a private key which is an integer
5821 * in the range [1, N - 1], where N is the boundary of the private key domain:
5822 * N is the prime p for Diffie-Hellman, or the order of the
5823 * curve’s base point for ECC.
5824 *
5825 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5826 * This function generates the private key using the following process:
5827 *
5828 * 1. Draw a byte string of length ceiling(m/8) bytes.
5829 * 2. If m is not a multiple of 8, set the most significant
5830 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5831 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5832 * 4. If k > N - 2, discard the result and return to step 1.
5833 * 5. Output k + 1 as the private key.
5834 *
5835 * This method allows compliance to NIST standards, specifically the methods titled
5836 * Key-Pair Generation by Testing Candidates in the following publications:
5837 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5838 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5839 * Diffie-Hellman keys.
5840 *
5841 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5842 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5843 *
5844 * Note: Function allocates memory for *data buffer, so given *data should be
5845 * always NULL.
5846 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005847#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5848#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005849static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005850 psa_key_slot_t *slot,
5851 size_t bits,
5852 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005853 uint8_t **data
5854 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005855{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005856 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005857 mbedtls_mpi k;
5858 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005859 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005860 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005861 size_t m;
5862 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005863
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 mbedtls_mpi_init(&k);
5865 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005866
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005867 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005869 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005870 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005871
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005873 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005874 goto cleanup;
5875 }
5876
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005877 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005879
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005881
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005882 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005883 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005884 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005885
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005886 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005887
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005888 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005889 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005890
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005891 /* Note: This function is always called with *data == NULL and it
5892 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 *data = mbedtls_calloc(1, m_bytes);
5894 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005895 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005896 goto cleanup;
5897 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005898
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005900 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005901 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005902 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005903 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005904
5905 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005907 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005908 * (8 * ceiling(m/8) - m) bits of the first byte in
5909 * the string to zero.
5910 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005911 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005912 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005913 }
5914
5915 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 * big-endian byte string.
5917 */
5918 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005919
5920 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 * Result of comparison is returned. When it indicates error
5922 * then this function is called again.
5923 */
5924 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005925 }
5926
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005927 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5929 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005930cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 if (ret != 0) {
5932 status = mbedtls_to_psa_error(ret);
5933 }
5934 if (status != PSA_SUCCESS) {
5935 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005936 *data = NULL;
5937 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 mbedtls_mpi_free(&k);
5939 mbedtls_mpi_free(&diff_N_2);
5940 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005941}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005942
5943/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5944 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5945 *
5946 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5947 * draw a 32-byte string and process it as specified in
5948 * Elliptic Curves for Security [RFC7748] §5.
5949 *
5950 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5951 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5952 *
5953 * Note: Function allocates memory for *data buffer, so given *data should be
5954 * always NULL.
5955 */
5956
5957static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5958 size_t bits,
5959 psa_key_derivation_operation_t *operation,
5960 uint8_t **data
5961 )
5962{
5963 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005964 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005965
Gilles Peskine449bd832023-01-11 14:50:10 +01005966 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005967 case 255:
5968 output_length = 32;
5969 break;
5970 case 448:
5971 output_length = 56;
5972 break;
5973 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005975 break;
5976 }
5977
Gilles Peskine449bd832023-01-11 14:50:10 +01005978 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005979
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 if (*data == NULL) {
5981 return PSA_ERROR_INSUFFICIENT_MEMORY;
5982 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005983
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005985
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005987 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005989
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005991 case 255:
5992 (*data)[0] &= 248;
5993 (*data)[31] &= 127;
5994 (*data)[31] |= 64;
5995 break;
5996 case 448:
5997 (*data)[0] &= 252;
5998 (*data)[55] |= 128;
5999 break;
6000 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006002 break;
6003 }
6004
6005 return status;
6006}
Valerio Setti86587ab2023-06-27 13:09:30 +02006007#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6008static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6009 psa_key_slot_t *slot, size_t bits,
6010 psa_key_derivation_operation_t *operation, uint8_t **data)
6011{
6012 (void) slot;
6013 (void) bits;
6014 (void) operation;
6015 (void) data;
6016 return PSA_ERROR_NOT_SUPPORTED;
6017}
6018
6019static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6020 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6021{
6022 (void) bits;
6023 (void) operation;
6024 (void) data;
6025 return PSA_ERROR_NOT_SUPPORTED;
6026}
6027#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6028#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006029
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006030static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006031 psa_key_slot_t *slot,
6032 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006034{
6035 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306037 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006038 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006039 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006040
Gilles Peskine449bd832023-01-11 14:50:10 +01006041 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6042 return PSA_ERROR_INVALID_ARGUMENT;
6043 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006044
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006045#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006046 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006047 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6048 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6049 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006050 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6052 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006053 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 }
6055 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006056 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6058 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006059 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006061 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006062 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006063#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006064 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006065 if (key_type_is_raw_bytes(slot->attr.type)) {
6066 if (bits % 8 != 0) {
6067 return PSA_ERROR_INVALID_ARGUMENT;
6068 }
6069 data = mbedtls_calloc(1, bytes);
6070 if (data == NULL) {
6071 return PSA_ERROR_INSUFFICIENT_MEMORY;
6072 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006073
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 status = psa_key_derivation_output_bytes(operation, data, bytes);
6075 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006076 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 }
David Brown12ca5032021-02-11 11:02:00 -07006078#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6080 psa_des_set_key_parity(data, bytes);
6081 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006082#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 } else {
6084 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006085 }
Ronald Crondd04d422020-11-28 15:14:42 +01006086
Ronald Cronbf33c932020-11-28 18:06:53 +01006087 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006088 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006090 };
6091
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6093 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6094 &storage_size);
6095 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306096 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 }
Archanad8a83dc2021-06-14 10:04:16 +05306098 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 status = psa_allocate_buffer_to_slot(slot, storage_size);
6100 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306101 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 }
Archanad8a83dc2021-06-14 10:04:16 +05306103
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 status = psa_driver_wrapper_import_key(&attributes,
6105 data, bytes,
6106 slot->key.data,
6107 slot->key.bytes,
6108 &slot->key.bytes, &bits);
6109 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006110 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006112
6113exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 mbedtls_free(data);
6115 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006116}
6117
Gilles Peskine449bd832023-01-11 14:50:10 +01006118psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6119 psa_key_derivation_operation_t *operation,
6120 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006121{
6122 psa_status_t status;
6123 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006124 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006125
Ronald Cron81709fc2020-11-14 12:10:32 +01006126 *key = MBEDTLS_SVC_KEY_ID_INIT;
6127
Gilles Peskine0f84d622019-09-12 19:03:13 +02006128 /* Reject any attempt to create a zero-length key so that we don't
6129 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 if (psa_get_key_bits(attributes) == 0) {
6131 return PSA_ERROR_INVALID_ARGUMENT;
6132 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006133
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 if (operation->alg == PSA_ALG_NONE) {
6135 return PSA_ERROR_BAD_STATE;
6136 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006137
Gilles Peskine449bd832023-01-11 14:50:10 +01006138 if (!operation->can_output_key) {
6139 return PSA_ERROR_NOT_PERMITTED;
6140 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006141
Gilles Peskine449bd832023-01-11 14:50:10 +01006142 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6143 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006144#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006146 /* Deriving a key in a secure element is not implemented yet. */
6147 status = PSA_ERROR_NOT_SUPPORTED;
6148 }
6149#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 if (status == PSA_SUCCESS) {
6151 status = psa_generate_derived_key_internal(slot,
6152 attributes->core.bits,
6153 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006154 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 if (status == PSA_SUCCESS) {
6156 status = psa_finish_key_creation(slot, driver, key);
6157 }
6158 if (status != PSA_SUCCESS) {
6159 psa_fail_key_creation(slot, driver);
6160 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006161
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006163}
6164
Gilles Peskineeab56e42018-07-12 17:12:33 +02006165
6166
6167/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006168/* Key derivation */
6169/****************************************************************/
6170
Steven Cooreman932ffb72021-02-15 12:14:32 +01006171#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006172static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006173{
6174#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006175 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6176 return 1;
6177 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006178#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006179#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6181 return 1;
6182 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006183#endif
6184#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6186 return 1;
6187 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006188#endif
6189#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6191 return 1;
6192 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006193#endif
6194#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6196 return 1;
6197 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006198#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006199#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6201 return 1;
6202 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006203#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306204#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6205 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6206 return 1;
6207 }
6208#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306209#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6210 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6211 return 1;
6212 }
6213#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006214 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006215}
6216
Gilles Peskine449bd832023-01-11 14:50:10 +01006217static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006218{
6219 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006220 psa_status_t status = psa_hash_setup(&operation, alg);
6221 psa_hash_abort(&operation);
6222 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006223}
6224
Gilles Peskine969c5d62019-01-16 15:53:06 +01006225static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006226 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006228{
Janos Follath5fe19732019-06-20 15:09:30 +01006229 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6230 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006232
Gilles Peskine969c5d62019-01-16 15:53:06 +01006233 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 if (!is_kdf_alg_supported(kdf_alg)) {
6235 return PSA_ERROR_NOT_SUPPORTED;
6236 }
John Durkop07cc04a2020-11-16 22:08:34 -08006237
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006238 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306239 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006240 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6241 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306242 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6243 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6244 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306245 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306246 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 if (hash_size == 0) {
6248 return PSA_ERROR_NOT_SUPPORTED;
6249 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006250
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006251 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6252 * we might fail later, which is somewhat unfriendly and potentially
6253 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006254 psa_status_t status = psa_hash_try_support(hash_alg);
6255 if (status != PSA_SUCCESS) {
6256 return status;
6257 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006258 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6261 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6262 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6263 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006264 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006265#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006266 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6268 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006269 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006270 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006271#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6272 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 operation->capacity = 255 * hash_size;
6274 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006275}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006276
Gilles Peskine449bd832023-01-11 14:50:10 +01006277static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006278{
6279#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 if (alg == PSA_ALG_ECDH) {
6281 return PSA_SUCCESS;
6282 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006283#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006284#if defined(PSA_WANT_ALG_FFDH)
6285 if (alg == PSA_ALG_FFDH) {
6286 return PSA_SUCCESS;
6287 }
6288#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006289 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006290 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006291}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006292
6293static int psa_key_derivation_allows_free_form_secret_input(
6294 psa_algorithm_t kdf_alg)
6295{
6296#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6297 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6298 return 0;
6299 }
6300#endif
6301 (void) kdf_alg;
6302 return 1;
6303}
John Durkop07cc04a2020-11-16 22:08:34 -08006304#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006305
Gilles Peskine449bd832023-01-11 14:50:10 +01006306psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6307 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006308{
6309 psa_status_t status;
6310
Gilles Peskine449bd832023-01-11 14:50:10 +01006311 if (operation->alg != 0) {
6312 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006313 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006314
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6316 return PSA_ERROR_INVALID_ARGUMENT;
6317 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6318#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6319 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6320 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6321 status = psa_key_agreement_try_support(ka_alg);
6322 if (status != PSA_SUCCESS) {
6323 return status;
6324 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006325 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6326 return PSA_ERROR_INVALID_ARGUMENT;
6327 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006328 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6329#else
6330 return PSA_ERROR_NOT_SUPPORTED;
6331#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6332 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6333#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6334 status = psa_key_derivation_setup_kdf(operation, alg);
6335#else
6336 return PSA_ERROR_NOT_SUPPORTED;
6337#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6338 } else {
6339 return PSA_ERROR_INVALID_ARGUMENT;
6340 }
6341
6342 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006343 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 }
6345 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006346}
6347
Przemek Stekiel69c46792022-06-10 12:59:51 +02006348#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006349static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6350 psa_algorithm_t kdf_alg,
6351 psa_key_derivation_step_t step,
6352 const uint8_t *data,
6353 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006354{
Gilles Peskine449bd832023-01-11 14:50:10 +01006355 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006356 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006358 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006359#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6361 return PSA_ERROR_INVALID_ARGUMENT;
6362 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006363#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 if (hkdf->state != HKDF_STATE_INIT) {
6365 return PSA_ERROR_BAD_STATE;
6366 } else {
6367 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6368 hash_alg,
6369 data, data_length);
6370 if (status != PSA_SUCCESS) {
6371 return status;
6372 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006373 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006374 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006375 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006376 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006377#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006378 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006379 /* We shouldn't be in different state as HKDF_EXPAND only allows
6380 * two inputs: SECRET (this case) and INFO which does not modify
6381 * the state. It could happen only if the hkdf
6382 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 if (hkdf->state != HKDF_STATE_INIT) {
6384 return PSA_ERROR_BAD_STATE;
6385 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006386
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006387 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6389 return PSA_ERROR_INVALID_ARGUMENT;
6390 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006391
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 memcpy(hkdf->prk, data, data_length);
6393 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006394#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006395 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006396 /* HKDF: If no salt was provided, use an empty salt.
6397 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006398 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006399#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006400 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6401 return PSA_ERROR_BAD_STATE;
6402 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006403#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006404 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6405 hash_alg,
6406 NULL, 0);
6407 if (status != PSA_SUCCESS) {
6408 return status;
6409 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006410 hkdf->state = HKDF_STATE_STARTED;
6411 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 if (hkdf->state != HKDF_STATE_STARTED) {
6413 return PSA_ERROR_BAD_STATE;
6414 }
6415 status = psa_mac_update(&hkdf->hmac,
6416 data, data_length);
6417 if (status != PSA_SUCCESS) {
6418 return status;
6419 }
6420 status = psa_mac_sign_finish(&hkdf->hmac,
6421 hkdf->prk,
6422 sizeof(hkdf->prk),
6423 &data_length);
6424 if (status != PSA_SUCCESS) {
6425 return status;
6426 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006427 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006428
Gilles Peskine2b522db2019-04-12 15:11:49 +02006429 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006430 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006431#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006432 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006433 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006435 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006437#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006438 {
6439 /* Block 0 is empty, and the next block will be
6440 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006442 }
6443
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006445 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006446#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006447 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6448 return PSA_ERROR_INVALID_ARGUMENT;
6449 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006450#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006451#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6453 hkdf->state == HKDF_STATE_INIT) {
6454 return PSA_ERROR_BAD_STATE;
6455 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006456#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 if (hkdf->state == HKDF_STATE_OUTPUT) {
6458 return PSA_ERROR_BAD_STATE;
6459 }
6460 if (hkdf->info_set) {
6461 return PSA_ERROR_BAD_STATE;
6462 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006463 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006464 if (data_length != 0) {
6465 hkdf->info = mbedtls_calloc(1, data_length);
6466 if (hkdf->info == NULL) {
6467 return PSA_ERROR_INSUFFICIENT_MEMORY;
6468 }
6469 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006470 }
6471 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006472 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006473 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006474 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006475 }
6476}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006477#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006478
John Durkop07cc04a2020-11-16 22:08:34 -08006479#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6480 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006481static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6482 const uint8_t *data,
6483 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006484{
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6486 return PSA_ERROR_BAD_STATE;
6487 }
Janos Follathf08e2652019-06-13 09:05:41 +01006488
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 if (data_length != 0) {
6490 prf->seed = mbedtls_calloc(1, data_length);
6491 if (prf->seed == NULL) {
6492 return PSA_ERROR_INSUFFICIENT_MEMORY;
6493 }
Janos Follathf08e2652019-06-13 09:05:41 +01006494
Gilles Peskine449bd832023-01-11 14:50:10 +01006495 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006496 prf->seed_length = data_length;
6497 }
Janos Follathf08e2652019-06-13 09:05:41 +01006498
Gilles Peskine43f958b2020-12-13 14:55:14 +01006499 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006500
Gilles Peskine449bd832023-01-11 14:50:10 +01006501 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006502}
6503
Gilles Peskine449bd832023-01-11 14:50:10 +01006504static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6505 const uint8_t *data,
6506 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006507{
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6509 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6510 return PSA_ERROR_BAD_STATE;
6511 }
Janos Follath81550542019-06-13 14:26:34 +01006512
Gilles Peskine449bd832023-01-11 14:50:10 +01006513 if (data_length != 0) {
6514 prf->secret = mbedtls_calloc(1, data_length);
6515 if (prf->secret == NULL) {
6516 return PSA_ERROR_INSUFFICIENT_MEMORY;
6517 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006518
Gilles Peskine449bd832023-01-11 14:50:10 +01006519 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006520 prf->secret_length = data_length;
6521 }
Janos Follath81550542019-06-13 14:26:34 +01006522
Gilles Peskine43f958b2020-12-13 14:55:14 +01006523 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006524
Gilles Peskine449bd832023-01-11 14:50:10 +01006525 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006526}
6527
Gilles Peskine449bd832023-01-11 14:50:10 +01006528static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6529 const uint8_t *data,
6530 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006531{
Gilles Peskine449bd832023-01-11 14:50:10 +01006532 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6533 return PSA_ERROR_BAD_STATE;
6534 }
Janos Follath63028dd2019-06-13 09:15:47 +01006535
Gilles Peskine449bd832023-01-11 14:50:10 +01006536 if (data_length != 0) {
6537 prf->label = mbedtls_calloc(1, data_length);
6538 if (prf->label == NULL) {
6539 return PSA_ERROR_INSUFFICIENT_MEMORY;
6540 }
Janos Follath63028dd2019-06-13 09:15:47 +01006541
Gilles Peskine449bd832023-01-11 14:50:10 +01006542 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006543 prf->label_length = data_length;
6544 }
Janos Follath63028dd2019-06-13 09:15:47 +01006545
Gilles Peskine43f958b2020-12-13 14:55:14 +01006546 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006547
Gilles Peskine449bd832023-01-11 14:50:10 +01006548 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006549}
6550
Gilles Peskine449bd832023-01-11 14:50:10 +01006551static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6552 psa_key_derivation_step_t step,
6553 const uint8_t *data,
6554 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006555{
Gilles Peskine449bd832023-01-11 14:50:10 +01006556 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006557 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006558 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006559 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006560 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006561 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006562 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006563 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006564 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006565 }
6566}
John Durkop07cc04a2020-11-16 22:08:34 -08006567#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6568 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6569
6570#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6571static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6572 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006573 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006574 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006575{
6576 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006577 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6578 4 + data_length + prf->other_secret_length :
6579 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006580
Gilles Peskine449bd832023-01-11 14:50:10 +01006581 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6582 return PSA_ERROR_INVALID_ARGUMENT;
6583 }
John Durkop07cc04a2020-11-16 22:08:34 -08006584
Gilles Peskine449bd832023-01-11 14:50:10 +01006585 uint8_t *pms = mbedtls_calloc(1, pms_len);
6586 if (pms == NULL) {
6587 return PSA_ERROR_INSUFFICIENT_MEMORY;
6588 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006589 uint8_t *cur = pms;
6590
6591 /* pure-PSK:
6592 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006593 *
6594 * The premaster secret is formed as follows: if the PSK is N octets
6595 * long, concatenate a uint16 with the value N, N zero octets, a second
6596 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006597 *
6598 * mixed-PSK:
6599 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6600 * follows: concatenate a uint16 with the length of the other secret,
6601 * the other secret itself, uint16 with the length of PSK, and the
6602 * PSK itself.
6603 * For details please check:
6604 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6605 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6606 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006607 */
6608
Gilles Peskine449bd832023-01-11 14:50:10 +01006609 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6610 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6611 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6612 if (prf->other_secret_length != 0) {
6613 memcpy(cur, prf->other_secret, prf->other_secret_length);
6614 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006615 cur += prf->other_secret_length;
6616 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006617 } else {
6618 *cur++ = MBEDTLS_BYTE_1(data_length);
6619 *cur++ = MBEDTLS_BYTE_0(data_length);
6620 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006621 cur += data_length;
6622 }
6623
Gilles Peskine449bd832023-01-11 14:50:10 +01006624 *cur++ = MBEDTLS_BYTE_1(data_length);
6625 *cur++ = MBEDTLS_BYTE_0(data_length);
6626 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006627 cur += data_length;
6628
Gilles Peskine449bd832023-01-11 14:50:10 +01006629 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006630
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006631 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006632 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006633}
Janos Follath6660f0e2019-06-17 08:44:03 +01006634
Przemek Stekiele47201b2022-04-19 13:53:28 +02006635static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6636 psa_tls12_prf_key_derivation_t *prf,
6637 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006638 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006639{
Gilles Peskine449bd832023-01-11 14:50:10 +01006640 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6641 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006642 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006643
6644 if (data_length != 0) {
6645 prf->other_secret = mbedtls_calloc(1, data_length);
6646 if (prf->other_secret == NULL) {
6647 return PSA_ERROR_INSUFFICIENT_MEMORY;
6648 }
6649
6650 memcpy(prf->other_secret, data, data_length);
6651 prf->other_secret_length = data_length;
6652 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006653 prf->other_secret_length = 0;
6654 }
6655
6656 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6657
Gilles Peskine449bd832023-01-11 14:50:10 +01006658 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006659}
6660
Janos Follath6660f0e2019-06-17 08:44:03 +01006661static psa_status_t psa_tls12_prf_psk_to_ms_input(
6662 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006663 psa_key_derivation_step_t step,
6664 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006665 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006666{
Gilles Peskine449bd832023-01-11 14:50:10 +01006667 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006668 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006669 return psa_tls12_prf_psk_to_ms_set_key(prf,
6670 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006671 break;
6672 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006673 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6674 data,
6675 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006676 break;
6677 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006678 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006679 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006680
Przemek Stekiele47201b2022-04-19 13:53:28 +02006681 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006682}
John Durkop07cc04a2020-11-16 22:08:34 -08006683#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006684
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006685#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6686static psa_status_t psa_tls12_ecjpake_to_pms_input(
6687 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006688 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006689 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006690 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006691{
Gilles Peskine449bd832023-01-11 14:50:10 +01006692 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6693 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6694 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006695 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006696
6697 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006698 if (data[0] != 0x04) {
6699 return PSA_ERROR_INVALID_ARGUMENT;
6700 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006701
6702 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006703 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006704
Gilles Peskine449bd832023-01-11 14:50:10 +01006705 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006706}
6707#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306708
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306709#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306710static psa_status_t psa_pbkdf2_set_input_cost(
6711 psa_pbkdf2_key_derivation_t *pbkdf2,
6712 psa_key_derivation_step_t step,
6713 uint64_t data)
6714{
6715 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6716 return PSA_ERROR_INVALID_ARGUMENT;
6717 }
6718
6719 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6720 return PSA_ERROR_BAD_STATE;
6721 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306722
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306723 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306724 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306725 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306726
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306727 if (data == 0) {
6728 return PSA_ERROR_INVALID_ARGUMENT;
6729 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306730
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306731 pbkdf2->input_cost = data;
6732 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6733
6734 return PSA_SUCCESS;
6735}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306736
6737static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6738 const uint8_t *data,
6739 size_t data_length)
6740{
Gilles Peskineead17662023-08-20 22:02:51 +02006741 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6742 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6743 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6744 /* Appending to existing salt. No state change. */
6745 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306746 return PSA_ERROR_BAD_STATE;
6747 }
6748
Gilles Peskineca57d782023-07-20 19:08:06 +02006749 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006750 /* Appending an empty string, nothing to do. */
6751 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306752 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306753
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306754 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6755 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306756 return PSA_ERROR_INSUFFICIENT_MEMORY;
6757 }
6758
Gilles Peskineead17662023-08-20 22:02:51 +02006759 if (pbkdf2->salt_length != 0) {
6760 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6761 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306762 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306763 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306764 mbedtls_free(pbkdf2->salt);
6765 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306766 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306767 return PSA_SUCCESS;
6768}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306769
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306770#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306771static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306772 const uint8_t *input,
6773 size_t input_len,
6774 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306775 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306776{
6777 psa_status_t status = PSA_SUCCESS;
6778 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6779 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306780 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306781 } else {
6782 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306783 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306784 }
6785 return status;
6786}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306787#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306788
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306789#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306790static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6791 size_t input_len,
6792 uint8_t *output,
6793 size_t *output_len)
6794{
6795 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306796 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306797 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306798 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306799 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6800 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6801 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306802 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306803 * mac_size as the driver function sets mac_output_length = mac_size
6804 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306805 status = psa_driver_wrapper_mac_compute(&attributes,
6806 zeros, sizeof(zeros),
6807 PSA_ALG_CMAC, input, input_len,
6808 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306809 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6810 128U,
6811 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306812 output_len);
6813 } else {
6814 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306815 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306816 }
6817 return status;
6818}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306819#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306820
6821static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306822 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306823 const uint8_t *data,
6824 size_t data_length)
6825{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306826 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306827 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6828 return PSA_ERROR_BAD_STATE;
6829 }
6830
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306831#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306832 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6833 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6834 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6835 pbkdf2->password,
6836 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306837 } else
6838#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6839#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6840 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306841 status = psa_pbkdf2_cmac_set_password(data, data_length,
6842 pbkdf2->password,
6843 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306844 } else
6845#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6846 {
6847 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306848 }
6849
6850 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6851
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306852 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306853}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306854
6855static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306856 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306857 psa_key_derivation_step_t step,
6858 const uint8_t *data,
6859 size_t data_length)
6860{
6861 switch (step) {
6862 case PSA_KEY_DERIVATION_INPUT_SALT:
6863 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6864 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306865 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306866 default:
6867 return PSA_ERROR_INVALID_ARGUMENT;
6868 }
6869}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306870#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306871
Gilles Peskineb8965192019-09-24 16:21:10 +02006872/** Check whether the given key type is acceptable for the given
6873 * input step of a key derivation.
6874 *
6875 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6876 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6877 * Both secret and non-secret inputs can alternatively have the type
6878 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6879 * that the input was passed as a buffer rather than via a key object.
6880 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006881static int psa_key_derivation_check_input_type(
6882 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006883 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006884{
Gilles Peskine449bd832023-01-11 14:50:10 +01006885 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006886 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006887 if (key_type == PSA_KEY_TYPE_DERIVE) {
6888 return PSA_SUCCESS;
6889 }
6890 if (key_type == PSA_KEY_TYPE_NONE) {
6891 return PSA_SUCCESS;
6892 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006893 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006894 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006895 if (key_type == PSA_KEY_TYPE_DERIVE) {
6896 return PSA_SUCCESS;
6897 }
6898 if (key_type == PSA_KEY_TYPE_NONE) {
6899 return PSA_SUCCESS;
6900 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006901 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006902 case PSA_KEY_DERIVATION_INPUT_LABEL:
6903 case PSA_KEY_DERIVATION_INPUT_SALT:
6904 case PSA_KEY_DERIVATION_INPUT_INFO:
6905 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006906 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6907 return PSA_SUCCESS;
6908 }
6909 if (key_type == PSA_KEY_TYPE_NONE) {
6910 return PSA_SUCCESS;
6911 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006912 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306913 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6914 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6915 return PSA_SUCCESS;
6916 }
6917 if (key_type == PSA_KEY_TYPE_DERIVE) {
6918 return PSA_SUCCESS;
6919 }
6920 if (key_type == PSA_KEY_TYPE_NONE) {
6921 return PSA_SUCCESS;
6922 }
6923 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006924 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006925 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006926}
6927
Janos Follathb80a94e2019-06-12 15:54:46 +01006928static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006929 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006930 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006931 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006932 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006933 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006934{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006935 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006936 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006937
Gilles Peskine449bd832023-01-11 14:50:10 +01006938 status = psa_key_derivation_check_input_type(step, key_type);
6939 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006940 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006942
Przemek Stekiel69c46792022-06-10 12:59:51 +02006943#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006944 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6945 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6946 step, data, data_length);
6947 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006948#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006949#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006950 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6951 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6952 step, data, data_length);
6953 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006954#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6955#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006956 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6957 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6958 step, data, data_length);
6959 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006960#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006961#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006962 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006963 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006964 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6965 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006966#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306967#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306968 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306969 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6970 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306971 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306972#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006973 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006974 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006975 (void) data;
6976 (void) data_length;
6977 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006978 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006979 }
6980
Gilles Peskine224b0d62019-09-23 18:13:17 +02006981exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006982 if (status != PSA_SUCCESS) {
6983 psa_key_derivation_abort(operation);
6984 }
6985 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006986}
6987
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306988static psa_status_t psa_key_derivation_input_integer_internal(
6989 psa_key_derivation_operation_t *operation,
6990 psa_key_derivation_step_t step,
6991 uint64_t value)
6992{
6993 psa_status_t status;
6994 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6995
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306996#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306997 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306998 status = psa_pbkdf2_set_input_cost(
6999 &operation->ctx.pbkdf2, step, value);
7000 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307001#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307002 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307003 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307004 (void) value;
7005 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307006 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307007 }
7008
7009 if (status != PSA_SUCCESS) {
7010 psa_key_derivation_abort(operation);
7011 }
7012 return status;
7013}
7014
Janos Follath51f4a0f2019-06-14 11:35:55 +01007015psa_status_t psa_key_derivation_input_bytes(
7016 psa_key_derivation_operation_t *operation,
7017 psa_key_derivation_step_t step,
7018 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007019 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007020{
Gilles Peskine449bd832023-01-11 14:50:10 +01007021 return psa_key_derivation_input_internal(operation, step,
7022 PSA_KEY_TYPE_NONE,
7023 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007024}
7025
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307026psa_status_t psa_key_derivation_input_integer(
7027 psa_key_derivation_operation_t *operation,
7028 psa_key_derivation_step_t step,
7029 uint64_t value)
7030{
7031 return psa_key_derivation_input_integer_internal(operation, step, value);
7032}
7033
Janos Follath51f4a0f2019-06-14 11:35:55 +01007034psa_status_t psa_key_derivation_input_key(
7035 psa_key_derivation_operation_t *operation,
7036 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007037 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007038{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007039 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007040 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007041 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007042
Ronald Cron5c522922020-11-14 16:35:34 +01007043 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007044 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7045 if (status != PSA_SUCCESS) {
7046 psa_key_derivation_abort(operation);
7047 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007048 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007049
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307050 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7051 * permission to output to a key object. */
7052 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7053 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007054 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007055 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007056
Gilles Peskine449bd832023-01-11 14:50:10 +01007057 status = psa_key_derivation_input_internal(operation,
7058 step, slot->attr.type,
7059 slot->key.data,
7060 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007061
Gilles Peskine449bd832023-01-11 14:50:10 +01007062 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007063
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007065}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007066
7067
7068
7069/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007070/* Key agreement */
7071/****************************************************************/
7072
Gilles Peskine449bd832023-01-11 14:50:10 +01007073psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7074 const uint8_t *key_buffer,
7075 size_t key_buffer_size,
7076 psa_algorithm_t alg,
7077 const uint8_t *peer_key,
7078 size_t peer_key_length,
7079 uint8_t *shared_secret,
7080 size_t shared_secret_size,
7081 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007082{
Gilles Peskine449bd832023-01-11 14:50:10 +01007083 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007084#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007085 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7087 key_buffer_size, alg,
7088 peer_key, peer_key_length,
7089 shared_secret,
7090 shared_secret_size,
7091 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007092#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007093
7094#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7095 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007096 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007097 peer_key,
7098 peer_key_length,
7099 key_buffer,
7100 key_buffer_size,
7101 shared_secret,
7102 shared_secret_size,
7103 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007104#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7105
Gilles Peskine01d718c2018-09-18 12:01:02 +02007106 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007107 (void) attributes;
7108 (void) key_buffer;
7109 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007110 (void) peer_key;
7111 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007112 (void) shared_secret;
7113 (void) shared_secret_size;
7114 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007115 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007116 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007117}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007118
Aditya Deshpande17845b82022-10-13 17:21:01 +01007119/** Internal function for raw key agreement
7120 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007121 * to the driver's implementation if a driver is present.
7122 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007123 * (psa_key_agreement_raw_builtin).
7124 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007125static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7126 psa_key_slot_t *private_key,
7127 const uint8_t *peer_key,
7128 size_t peer_key_length,
7129 uint8_t *shared_secret,
7130 size_t shared_secret_size,
7131 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007132{
Gilles Peskine449bd832023-01-11 14:50:10 +01007133 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7134 return PSA_ERROR_NOT_SUPPORTED;
7135 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007136
7137 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007138 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007139 };
7140
Gilles Peskine449bd832023-01-11 14:50:10 +01007141 return psa_driver_wrapper_key_agreement(&attributes,
7142 private_key->key.data,
7143 private_key->key.bytes, alg,
7144 peer_key, peer_key_length,
7145 shared_secret,
7146 shared_secret_size,
7147 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007148}
7149
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007150/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007151 * to potentially free embedded data structures and wipe confidential data.
7152 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007153static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7154 psa_key_derivation_step_t step,
7155 psa_key_slot_t *private_key,
7156 const uint8_t *peer_key,
7157 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007158{
7159 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007160 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007161 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007162 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007163
7164 /* Step 1: run the secret agreement algorithm to generate the shared
7165 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007166 status = psa_key_agreement_raw_internal(ka_alg,
7167 private_key,
7168 peer_key, peer_key_length,
7169 shared_secret,
7170 sizeof(shared_secret),
7171 &shared_secret_length);
7172 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007173 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007174 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007175
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007176 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007177 * the shared secret. A shared secret is permitted wherever a key
7178 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007179 status = psa_key_derivation_input_internal(operation, step,
7180 PSA_KEY_TYPE_DERIVE,
7181 shared_secret,
7182 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007183exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007184 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7185 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007186}
7187
Gilles Peskine449bd832023-01-11 14:50:10 +01007188psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7189 psa_key_derivation_step_t step,
7190 mbedtls_svc_key_id_t private_key,
7191 const uint8_t *peer_key,
7192 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007193{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007194 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007195 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007196 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007197
Gilles Peskine449bd832023-01-11 14:50:10 +01007198 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7199 return PSA_ERROR_INVALID_ARGUMENT;
7200 }
Ronald Cron5c522922020-11-14 16:35:34 +01007201 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007202 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7203 if (status != PSA_SUCCESS) {
7204 return status;
7205 }
7206 status = psa_key_agreement_internal(operation, step,
7207 slot,
7208 peer_key, peer_key_length);
7209 if (status != PSA_SUCCESS) {
7210 psa_key_derivation_abort(operation);
7211 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007212 /* If a private key has been added as SECRET, we allow the derived
7213 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007214 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007215 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007216 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007217 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007218
Gilles Peskine449bd832023-01-11 14:50:10 +01007219 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007220
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007222}
7223
Gilles Peskine449bd832023-01-11 14:50:10 +01007224psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7225 mbedtls_svc_key_id_t private_key,
7226 const uint8_t *peer_key,
7227 size_t peer_key_length,
7228 uint8_t *output,
7229 size_t output_size,
7230 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007231{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007232 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007233 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007234 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007235 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007236
Gilles Peskine449bd832023-01-11 14:50:10 +01007237 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007238 status = PSA_ERROR_INVALID_ARGUMENT;
7239 goto exit;
7240 }
Ronald Cron5c522922020-11-14 16:35:34 +01007241 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007242 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7243 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007244 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007245 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007246
Gilles Peskine3e561302022-04-14 00:17:15 +02007247 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7248 * for the output size. The PSA specification only guarantees that this
7249 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7250 * but it might be nice to allow smaller buffers if the output fits.
7251 * At the time of writing this comment, with only ECDH implemented,
7252 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7253 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7254 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007255 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007256 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7257 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007258 status = PSA_ERROR_BUFFER_TOO_SMALL;
7259 goto exit;
7260 }
7261
Gilles Peskine449bd832023-01-11 14:50:10 +01007262 status = psa_key_agreement_raw_internal(alg, slot,
7263 peer_key, peer_key_length,
7264 output, output_size,
7265 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007266
7267exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007268 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007269 /* If an error happens and is not handled properly, the output
7270 * may be used as a key to protect sensitive data. Arrange for such
7271 * a key to be random, which is likely to result in decryption or
7272 * verification errors. This is better than filling the buffer with
7273 * some constant data such as zeros, which would result in the data
7274 * being protected with a reproducible, easily knowable key.
7275 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007276 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007277 *output_length = output_size;
7278 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007279
Gilles Peskine449bd832023-01-11 14:50:10 +01007280 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007281
Gilles Peskine449bd832023-01-11 14:50:10 +01007282 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007283}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007284
7285
Gilles Peskine30524eb2020-11-13 17:02:26 +01007286
Gilles Peskine86a440b2018-11-12 18:39:40 +01007287/****************************************************************/
7288/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007289/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007290
Gilles Peskine672a7712023-04-28 21:00:28 +02007291#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7292#include "entropy_poll.h"
7293#endif
7294
Gilles Peskine30524eb2020-11-13 17:02:26 +01007295/** Initialize the PSA random generator.
7296 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007297static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007298{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007299#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007300 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007301#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7302
Gilles Peskine30524eb2020-11-13 17:02:26 +01007303 /* Set default configuration if
7304 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007305 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007306 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007307 }
7308 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007309 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007310 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007311
Gilles Peskine449bd832023-01-11 14:50:10 +01007312 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007313#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7314 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7315 /* The PSA entropy injection feature depends on using NV seed as an entropy
7316 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007317 mbedtls_entropy_add_source(&rng->entropy,
7318 mbedtls_nv_seed_poll, NULL,
7319 MBEDTLS_ENTROPY_BLOCK_SIZE,
7320 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007321#endif
7322
Gilles Peskine449bd832023-01-11 14:50:10 +01007323 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007324#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007325}
7326
7327/** Deinitialize the PSA random generator.
7328 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007329static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007330{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007331#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007332 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007333#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007334 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7335 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007336#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007337}
7338
7339/** Seed the PSA random generator.
7340 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007341static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007342{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007343#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7344 /* Do nothing: the external RNG seeds itself. */
7345 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007346 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007347#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007348 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007349 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7350 drbg_seed, sizeof(drbg_seed) - 1);
7351 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007352#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007353}
7354
Gilles Peskine449bd832023-01-11 14:50:10 +01007355psa_status_t psa_generate_random(uint8_t *output,
7356 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007357{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007358 GUARD_MODULE_INITIALIZED;
7359
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007360#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7361
7362 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007363 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7364 output, output_size,
7365 &output_length);
7366 if (status != PSA_SUCCESS) {
7367 return status;
7368 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007369 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007370 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007371 if (output_length != output_size) {
7372 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7373 }
7374 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007375
7376#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7377
Gilles Peskine449bd832023-01-11 14:50:10 +01007378 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007379 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007380 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7381 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7382 output_size);
7383 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7384 output, request_size);
7385 if (ret != 0) {
7386 return mbedtls_to_psa_error(ret);
7387 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007388 output_size -= request_size;
7389 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007390 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007391 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007392#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007393}
7394
Gilles Peskine8814fc42020-12-14 15:33:44 +01007395/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007396 *
7397 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7398 * `psa_generate_random(...)`. The state parameter is ignored since the
7399 * PSA API doesn't support passing an explicit state.
7400 *
7401 * In the non-external case, psa_generate_random() calls an
7402 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7403 * and semantics as mbedtls_psa_get_random(). As an optimization,
7404 * instead of doing this back-and-forth between the PSA API and the
7405 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7406 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007407 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007408#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7409int mbedtls_psa_get_random(void *p_rng,
7410 unsigned char *output,
7411 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007412{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007413 /* This function takes a pointer to the RNG state because that's what
7414 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7415 * its own state internally and doesn't let the caller access that state.
7416 * So we just ignore the state parameter, and in practice we'll pass
7417 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007418 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007419 psa_status_t status = psa_generate_random(output, output_size);
7420 if (status == PSA_SUCCESS) {
7421 return 0;
7422 } else {
7423 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7424 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007425}
7426#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7427
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007428#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007429psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7430 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007431{
Gilles Peskine449bd832023-01-11 14:50:10 +01007432 if (global_data.initialized) {
7433 return PSA_ERROR_NOT_PERMITTED;
7434 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007435
Gilles Peskine449bd832023-01-11 14:50:10 +01007436 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7437 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7438 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7439 return PSA_ERROR_INVALID_ARGUMENT;
7440 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007441
Gilles Peskine449bd832023-01-11 14:50:10 +01007442 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007443}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007444#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007445
Ronald Cron3772afe2021-02-08 16:10:05 +01007446/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007447 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007448 * \param type The key type
7449 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007450 *
7451 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007452 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007453 * \retval #PSA_ERROR_INVALID_ARGUMENT
7454 * The size in bits of the key is not valid.
7455 * \retval #PSA_ERROR_NOT_SUPPORTED
7456 * The type and/or the size in bits of the key or the combination of
7457 * the two is not supported.
7458 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007459static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007460 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007461{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007462 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007463
Gilles Peskine449bd832023-01-11 14:50:10 +01007464 if (key_type_is_raw_bytes(type)) {
7465 status = psa_validate_unstructured_key_bit_size(type, bits);
7466 if (status != PSA_SUCCESS) {
7467 return status;
7468 }
7469 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007470#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007471 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7472 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7473 return PSA_ERROR_NOT_SUPPORTED;
7474 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007475 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007476 return PSA_ERROR_NOT_SUPPORTED;
7477 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007478
Ronald Cron01b2aba2020-10-05 09:42:02 +02007479 /* Accept only byte-aligned keys, for the same reasons as
7480 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007481 if (bits % 8 != 0) {
7482 return PSA_ERROR_NOT_SUPPORTED;
7483 }
7484 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007485#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007486
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007487#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007488 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007489 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007490 return PSA_SUCCESS;
7491 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007492#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007493
Valerio Settia55f0422023-07-10 15:34:41 +02007494#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007495 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007496 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007497 return PSA_ERROR_NOT_SUPPORTED;
7498 }
7499 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007500#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007501 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007502 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007503 }
7504
Gilles Peskine449bd832023-01-11 14:50:10 +01007505 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007506}
7507
Ronald Cron55ed0592020-10-05 10:30:40 +02007508psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007509 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007510 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007511{
7512 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007513 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007514
Gilles Peskine449bd832023-01-11 14:50:10 +01007515 if ((attributes->domain_parameters == NULL) &&
7516 (attributes->domain_parameters_size != 0)) {
7517 return PSA_ERROR_INVALID_ARGUMENT;
7518 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007519
Gilles Peskine449bd832023-01-11 14:50:10 +01007520 if (key_type_is_raw_bytes(type)) {
7521 status = psa_generate_random(key_buffer, key_buffer_size);
7522 if (status != PSA_SUCCESS) {
7523 return status;
7524 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007525
David Brown12ca5032021-02-11 11:02:00 -07007526#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007527 if (type == PSA_KEY_TYPE_DES) {
7528 psa_des_set_key_parity(key_buffer, key_buffer_size);
7529 }
David Brown8107e312021-02-12 08:08:46 -07007530#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007531 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007532
Valerio Setti76df8c12023-07-11 14:11:28 +02007533#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007534 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7535 return mbedtls_psa_rsa_generate_key(attributes,
7536 key_buffer,
7537 key_buffer_size,
7538 key_buffer_length);
7539 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007540#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007541
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007542#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007543 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7544 return mbedtls_psa_ecp_generate_key(attributes,
7545 key_buffer,
7546 key_buffer_size,
7547 key_buffer_length);
7548 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007549#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007550
Valerio Settia55f0422023-07-10 15:34:41 +02007551#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007552 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7553 return mbedtls_psa_ffdh_generate_key(attributes,
7554 key_buffer,
7555 key_buffer_size,
7556 key_buffer_length);
7557 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007558#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007559 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007560 (void) key_buffer_length;
7561 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007562 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007563
Gilles Peskine449bd832023-01-11 14:50:10 +01007564 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007565}
Darryl Green0c6575a2018-11-07 16:05:30 +00007566
Gilles Peskine449bd832023-01-11 14:50:10 +01007567psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7568 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007569{
7570 psa_status_t status;
7571 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007572 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007573 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007574
Ronald Cron81709fc2020-11-14 12:10:32 +01007575 *key = MBEDTLS_SVC_KEY_ID_INIT;
7576
Gilles Peskine0f84d622019-09-12 19:03:13 +02007577 /* Reject any attempt to create a zero-length key so that we don't
7578 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007579 if (psa_get_key_bits(attributes) == 0) {
7580 return PSA_ERROR_INVALID_ARGUMENT;
7581 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007582
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007583 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007584 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7585 return PSA_ERROR_INVALID_ARGUMENT;
7586 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007587
Gilles Peskine449bd832023-01-11 14:50:10 +01007588 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7589 &slot, &driver);
7590 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007591 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007592 }
Gilles Peskine11792082019-08-06 18:36:36 +02007593
Ronald Cron977c2472020-10-13 08:32:21 +02007594 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307595 * storage ( thus not in the case of generating a key in a secure element
7596 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7597 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007598 if (slot->key.data == NULL) {
7599 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7600 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007601 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007602 attributes->core.type, attributes->core.bits);
7603 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007604 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007605 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007606
7607 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007608 attributes->core.type,
7609 attributes->core.bits);
7610 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007611 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007612 attributes, &key_buffer_size);
7613 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007614 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007615 }
Ronald Cron977c2472020-10-13 08:32:21 +02007616 }
Ronald Cron977c2472020-10-13 08:32:21 +02007617
Gilles Peskine449bd832023-01-11 14:50:10 +01007618 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7619 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007620 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007621 }
Ronald Cron977c2472020-10-13 08:32:21 +02007622 }
7623
Gilles Peskine449bd832023-01-11 14:50:10 +01007624 status = psa_driver_wrapper_generate_key(attributes,
7625 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007626
Gilles Peskine449bd832023-01-11 14:50:10 +01007627 if (status != PSA_SUCCESS) {
7628 psa_remove_key_data_from_memory(slot);
7629 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007630
Gilles Peskine11792082019-08-06 18:36:36 +02007631exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007632 if (status == PSA_SUCCESS) {
7633 status = psa_finish_key_creation(slot, driver, key);
7634 }
7635 if (status != PSA_SUCCESS) {
7636 psa_fail_key_creation(slot, driver);
7637 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007638
Gilles Peskine449bd832023-01-11 14:50:10 +01007639 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007640}
7641
Gilles Peskinee59236f2018-01-27 23:32:46 +01007642/****************************************************************/
7643/* Module setup */
7644/****************************************************************/
7645
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007646#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007647psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007648 void (* entropy_init)(mbedtls_entropy_context *ctx),
7649 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007650{
Gilles Peskine449bd832023-01-11 14:50:10 +01007651 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7652 return PSA_ERROR_BAD_STATE;
7653 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007654 global_data.rng.entropy_init = entropy_init;
7655 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007656 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007657}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007658#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007659
Gilles Peskine449bd832023-01-11 14:50:10 +01007660void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007661{
Gilles Peskine449bd832023-01-11 14:50:10 +01007662 psa_wipe_all_key_slots();
7663 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7664 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007665 }
7666 /* Wipe all remaining data, including configuration.
7667 * In particular, this sets all state indicator to the value
7668 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007669 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007670
7671 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007672 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007673}
7674
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007675#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7676/** Recover a transaction that was interrupted by a power failure.
7677 *
7678 * This function is called during initialization, before psa_crypto_init()
7679 * returns. If this function returns a failure status, the initialization
7680 * fails.
7681 */
7682static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007683 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007684{
Gilles Peskine449bd832023-01-11 14:50:10 +01007685 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007686 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7687 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007688 /* TODO - fall through to the failure case until this
7689 * is implemented.
7690 * https://github.com/ARMmbed/mbed-crypto/issues/218
7691 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007692 default:
7693 /* We found an unsupported transaction in the storage.
7694 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007695 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007696 }
7697}
7698#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7699
Gilles Peskine449bd832023-01-11 14:50:10 +01007700psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007701{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007702 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007703
Gilles Peskinec6b69072018-11-20 21:42:52 +01007704 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007705 if (global_data.initialized != 0) {
7706 return PSA_SUCCESS;
7707 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007708
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007709 /* Init drivers */
7710 status = psa_driver_wrapper_init();
7711 if (status != PSA_SUCCESS) {
7712 goto exit;
7713 }
7714 global_data.drivers_initialized = 1;
7715
Gilles Peskine30524eb2020-11-13 17:02:26 +01007716 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007717 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007718 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007719 status = mbedtls_psa_random_seed(&global_data.rng);
7720 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007721 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007722 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007723 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007724
Gilles Peskine449bd832023-01-11 14:50:10 +01007725 status = psa_initialize_key_slots();
7726 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007727 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007728 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007729
Gilles Peskine4b734222019-07-24 15:56:31 +02007730#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007731 status = psa_crypto_load_transaction();
7732 if (status == PSA_SUCCESS) {
7733 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7734 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007735 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007736 }
7737 status = psa_crypto_stop_transaction();
7738 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007739 /* There's no transaction to complete. It's all good. */
7740 status = PSA_SUCCESS;
7741 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007742#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007743
Gilles Peskinec6b69072018-11-20 21:42:52 +01007744 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007745 global_data.initialized = 1;
7746
7747exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007748 if (status != PSA_SUCCESS) {
7749 mbedtls_psa_crypto_free();
7750 }
7751 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007752}
7753
Yanray Wangffc3c482023-07-11 12:01:04 +08007754#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007755psa_status_t psa_crypto_driver_pake_get_password_len(
7756 const psa_crypto_driver_pake_inputs_t *inputs,
7757 size_t *password_len)
7758{
7759 if (inputs->password_len == 0) {
7760 return PSA_ERROR_BAD_STATE;
7761 }
7762
7763 *password_len = inputs->password_len;
7764
7765 return PSA_SUCCESS;
7766}
7767
7768psa_status_t psa_crypto_driver_pake_get_password(
7769 const psa_crypto_driver_pake_inputs_t *inputs,
7770 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7771{
7772 if (inputs->password_len == 0) {
7773 return PSA_ERROR_BAD_STATE;
7774 }
7775
7776 if (buffer_size < inputs->password_len) {
7777 return PSA_ERROR_BUFFER_TOO_SMALL;
7778 }
7779
7780 memcpy(buffer, inputs->password, inputs->password_len);
7781 *buffer_length = inputs->password_len;
7782
7783 return PSA_SUCCESS;
7784}
7785
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007786psa_status_t psa_crypto_driver_pake_get_user_len(
7787 const psa_crypto_driver_pake_inputs_t *inputs,
7788 size_t *user_len)
7789{
7790 if (inputs->user_len == 0) {
7791 return PSA_ERROR_BAD_STATE;
7792 }
7793
7794 *user_len = inputs->user_len;
7795
7796 return PSA_SUCCESS;
7797}
7798
7799psa_status_t psa_crypto_driver_pake_get_user(
7800 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007801 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007802{
7803 if (inputs->user_len == 0) {
7804 return PSA_ERROR_BAD_STATE;
7805 }
7806
Przemek Stekielc0e62502023-03-14 11:49:36 +01007807 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007808 return PSA_ERROR_BUFFER_TOO_SMALL;
7809 }
7810
Przemek Stekielc0e62502023-03-14 11:49:36 +01007811 memcpy(user_id, inputs->user, inputs->user_len);
7812 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007813
7814 return PSA_SUCCESS;
7815}
7816
7817psa_status_t psa_crypto_driver_pake_get_peer_len(
7818 const psa_crypto_driver_pake_inputs_t *inputs,
7819 size_t *peer_len)
7820{
7821 if (inputs->peer_len == 0) {
7822 return PSA_ERROR_BAD_STATE;
7823 }
7824
7825 *peer_len = inputs->peer_len;
7826
7827 return PSA_SUCCESS;
7828}
7829
7830psa_status_t psa_crypto_driver_pake_get_peer(
7831 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007832 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007833{
7834 if (inputs->peer_len == 0) {
7835 return PSA_ERROR_BAD_STATE;
7836 }
7837
Przemek Stekielc0e62502023-03-14 11:49:36 +01007838 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007839 return PSA_ERROR_BUFFER_TOO_SMALL;
7840 }
7841
Przemek Stekielc0e62502023-03-14 11:49:36 +01007842 memcpy(peer_id, inputs->peer, inputs->peer_len);
7843 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007844
7845 return PSA_SUCCESS;
7846}
7847
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007848psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7849 const psa_crypto_driver_pake_inputs_t *inputs,
7850 psa_pake_cipher_suite_t *cipher_suite)
7851{
7852 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7853 return PSA_ERROR_BAD_STATE;
7854 }
7855
7856 *cipher_suite = inputs->cipher_suite;
7857
7858 return PSA_SUCCESS;
7859}
7860
Neil Armstronga7d08c32022-06-01 18:21:20 +02007861psa_status_t psa_pake_setup(
7862 psa_pake_operation_t *operation,
7863 const psa_pake_cipher_suite_t *cipher_suite)
7864{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007865 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007866
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007867 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007868 status = PSA_ERROR_BAD_STATE;
7869 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007870 }
7871
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007872 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007873 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007874 status = PSA_ERROR_INVALID_ARGUMENT;
7875 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007876 }
7877
Przemek Stekiel51eac532022-12-07 11:04:51 +01007878 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7879
Przemek Stekiele12ed362022-12-21 12:54:46 +01007880 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007881 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7882 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007883 operation->data.inputs.cipher_suite = *cipher_suite;
7884
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007885#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007886 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007887 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007888 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007889
David Horstmann16f01512023-06-14 17:21:07 +01007890 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007891 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007892 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007893#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007894 {
7895 status = PSA_ERROR_NOT_SUPPORTED;
7896 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007897 }
7898
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007899 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7900
Przemek Stekiel51eac532022-12-07 11:04:51 +01007901 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007902exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007903 psa_pake_abort(operation);
7904 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007905}
7906
7907psa_status_t psa_pake_set_password_key(
7908 psa_pake_operation_t *operation,
7909 mbedtls_svc_key_id_t password)
7910{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007911 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007912 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7913 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007914 psa_key_attributes_t attributes;
7915 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007916
Przemek Stekiel51eac532022-12-07 11:04:51 +01007917 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007918 status = PSA_ERROR_BAD_STATE;
7919 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007920 }
7921
Przemek Stekiel6c764412022-11-22 14:05:12 +01007922 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7923 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007924 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007925 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007926 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007927 }
7928
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007929 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007930 .core = slot->attr
7931 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007932
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007933 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007934
Przemek Stekiel51eac532022-12-07 11:04:51 +01007935 if (type != PSA_KEY_TYPE_PASSWORD &&
7936 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7937 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007938 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007939 }
7940
Przemek Stekiel51eac532022-12-07 11:04:51 +01007941 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7942 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007943 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007944 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007945 }
7946
7947 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7948 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007949 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007950exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007951 if (status != PSA_SUCCESS) {
7952 psa_pake_abort(operation);
7953 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007954 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007955 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007956}
7957
7958psa_status_t psa_pake_set_user(
7959 psa_pake_operation_t *operation,
7960 const uint8_t *user_id,
7961 size_t user_id_len)
7962{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007963 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007964
7965 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007966 status = PSA_ERROR_BAD_STATE;
7967 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007968 }
7969
Przemek Stekiel51eac532022-12-07 11:04:51 +01007970 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007971 status = PSA_ERROR_INVALID_ARGUMENT;
7972 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007973 }
7974
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007975 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007976 status = PSA_ERROR_BAD_STATE;
7977 goto exit;
7978 }
7979
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007980 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7981 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007982 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7983 goto exit;
7984 }
7985
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007986 memcpy(operation->data.inputs.user, user_id, user_id_len);
7987 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007988
7989 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007990exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007991 psa_pake_abort(operation);
7992 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007993}
7994
7995psa_status_t psa_pake_set_peer(
7996 psa_pake_operation_t *operation,
7997 const uint8_t *peer_id,
7998 size_t peer_id_len)
7999{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008000 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008001
8002 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008003 status = PSA_ERROR_BAD_STATE;
8004 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008005 }
8006
Przemek Stekiel51eac532022-12-07 11:04:51 +01008007 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008008 status = PSA_ERROR_INVALID_ARGUMENT;
8009 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008010 }
8011
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008012 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008013 status = PSA_ERROR_BAD_STATE;
8014 goto exit;
8015 }
8016
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008017 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8018 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008019 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8020 goto exit;
8021 }
8022
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008023 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8024 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008025
8026 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008027exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008028 psa_pake_abort(operation);
8029 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008030}
8031
8032psa_status_t psa_pake_set_role(
8033 psa_pake_operation_t *operation,
8034 psa_pake_role_t role)
8035{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008036 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008037
Przemek Stekiel51eac532022-12-07 11:04:51 +01008038 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008039 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008040 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008041 }
8042
Przemek Stekiel09104b82023-03-06 14:11:51 +01008043 switch (operation->alg) {
8044#if defined(PSA_WANT_ALG_JPAKE)
8045 case PSA_ALG_JPAKE:
8046 if (role == PSA_PAKE_ROLE_NONE) {
8047 return PSA_SUCCESS;
8048 }
8049 status = PSA_ERROR_INVALID_ARGUMENT;
8050 break;
8051#endif
8052 default:
8053 (void) role;
8054 status = PSA_ERROR_NOT_SUPPORTED;
8055 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008056 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008057exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008058 psa_pake_abort(operation);
8059 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008060}
8061
David Horstmanne7f21e62023-05-12 18:17:21 +01008062/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008063#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008064static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008065 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008066{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008067 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008068 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008069 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008070
David Horstmann024e5c52023-06-14 15:48:21 +01008071 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008072 is_x1 = (stage->outputs < 1);
8073 } else {
8074 is_x1 = (stage->inputs < 1);
8075 }
8076
David Horstmann74a3d8c2023-06-14 18:28:19 +01008077 key_share_step = is_x1 ?
8078 PSA_JPAKE_X1_STEP_KEY_SHARE :
8079 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008080 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008081 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8082 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8083 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8084 } else {
8085 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008086 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008087 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008088}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008089#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008090
Przemek Stekiel2797d372022-12-22 11:19:22 +01008091static psa_status_t psa_pake_complete_inputs(
8092 psa_pake_operation_t *operation)
8093{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008094 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008095 /* Create copy of the inputs on stack as inputs share memory
8096 with the driver context which will be setup by the driver. */
8097 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008098
Przemek Stekielfde11282023-03-13 16:06:09 +01008099 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008100 return PSA_ERROR_BAD_STATE;
8101 }
8102
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008103 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008104 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8105 return PSA_ERROR_BAD_STATE;
8106 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008107 }
8108
Przemek Stekiel18620a32023-01-17 16:34:52 +01008109 /* Clear driver context */
8110 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8111
8112 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008113
8114 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008115 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008116
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008117 /* User and peer are translated to role. */
8118 mbedtls_free(inputs.user);
8119 mbedtls_free(inputs.peer);
8120
Przemek Stekiel2797d372022-12-22 11:19:22 +01008121 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008122#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008123 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008124 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008125 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008126#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008127 {
8128 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008129 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008130 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008131 return status;
8132}
8133
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008134#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008135static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008136 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008137 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008138 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008139{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008140 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8141 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8142 step != PSA_PAKE_STEP_ZK_PROOF) {
8143 return PSA_ERROR_INVALID_ARGUMENT;
8144 }
8145
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008146 psa_jpake_computation_stage_t *computation_stage =
8147 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008148
David Horstmann5da95602023-06-08 15:37:12 +01008149 if (computation_stage->round != PSA_JPAKE_FIRST &&
8150 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008151 return PSA_ERROR_BAD_STATE;
8152 }
8153
David Horstmanne7f21e62023-05-12 18:17:21 +01008154 /* Check that the step we are given is the one we were expecting */
8155 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008156 return PSA_ERROR_BAD_STATE;
8157 }
8158
David Horstmanne7f21e62023-05-12 18:17:21 +01008159 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8160 computation_stage->inputs == 0 &&
8161 computation_stage->outputs == 0) {
8162 /* Start of the round, so function decides whether we are inputting
8163 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008164 computation_stage->io_mode = io_mode;
8165 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008166 /* Middle of the round so the mode we are in must match the function
8167 * called by the user */
8168 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008169 }
8170
8171 return PSA_SUCCESS;
8172}
8173
David Horstmanne7f21e62023-05-12 18:17:21 +01008174static psa_status_t psa_jpake_epilogue(
8175 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008176 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008177{
David Horstmanne7f21e62023-05-12 18:17:21 +01008178 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008179 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008180
David Horstmanne7f21e62023-05-12 18:17:21 +01008181 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8182 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008183 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008184 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008185 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008186 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008187 }
8188 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008189 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008190 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008191 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008192 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008193 }
8194 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008195 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8196 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008197 /* End of a round, move to the next round */
8198 stage->inputs = 0;
8199 stage->outputs = 0;
8200 stage->round++;
8201 }
8202 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008203 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008204 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008205 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008206 return PSA_SUCCESS;
8207}
David Horstmanne7f21e62023-05-12 18:17:21 +01008208
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008209#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008210
Neil Armstronga7d08c32022-06-01 18:21:20 +02008211psa_status_t psa_pake_output(
8212 psa_pake_operation_t *operation,
8213 psa_pake_step_t step,
8214 uint8_t *output,
8215 size_t output_size,
8216 size_t *output_length)
8217{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008218 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008219 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008220 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008221
8222 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008223 status = psa_pake_complete_inputs(operation);
8224 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008225 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008226 }
8227 }
8228
8229 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008230 status = PSA_ERROR_BAD_STATE;
8231 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008232 }
8233
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008234 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008235 status = PSA_ERROR_INVALID_ARGUMENT;
8236 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008237 }
8238
Przemek Stekiele12ed362022-12-21 12:54:46 +01008239 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008240#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008241 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008242 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008243 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008244 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008245 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008246 driver_step = convert_jpake_computation_stage_to_driver_step(
8247 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008248 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008249#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008250 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008251 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008252 status = PSA_ERROR_NOT_SUPPORTED;
8253 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008254 }
8255
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008256 status = psa_driver_wrapper_pake_output(operation, driver_step,
8257 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008258
8259 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008260 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008261 }
8262
8263 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008264#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008265 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008266 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008267 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008268 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008269 }
8270 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008271#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008272 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008273 status = PSA_ERROR_NOT_SUPPORTED;
8274 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008275 }
8276
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008277 return PSA_SUCCESS;
8278exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008279 psa_pake_abort(operation);
8280 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008281}
8282
8283psa_status_t psa_pake_input(
8284 psa_pake_operation_t *operation,
8285 psa_pake_step_t step,
8286 const uint8_t *input,
8287 size_t input_length)
8288{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008289 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008290 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008291 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8292 operation->primitive,
8293 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008294
8295 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008296 status = psa_pake_complete_inputs(operation);
8297 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008298 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008299 }
8300 }
8301
8302 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008303 status = PSA_ERROR_BAD_STATE;
8304 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008305 }
8306
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008307 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008308 status = PSA_ERROR_INVALID_ARGUMENT;
8309 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008310 }
8311
Przemek Stekiele12ed362022-12-21 12:54:46 +01008312 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008313#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008314 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008315 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008316 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008317 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008318 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008319 driver_step = convert_jpake_computation_stage_to_driver_step(
8320 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008321 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008322#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008323 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008324 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008325 status = PSA_ERROR_NOT_SUPPORTED;
8326 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008327 }
8328
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008329 status = psa_driver_wrapper_pake_input(operation, driver_step,
8330 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008331
8332 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008333 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008334 }
8335
8336 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008337#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008338 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008339 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008340 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008341 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008342 }
8343 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008344#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008345 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008346 status = PSA_ERROR_NOT_SUPPORTED;
8347 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008348 }
8349
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008350 return PSA_SUCCESS;
8351exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008352 psa_pake_abort(operation);
8353 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008354}
8355
8356psa_status_t psa_pake_get_implicit_key(
8357 psa_pake_operation_t *operation,
8358 psa_key_derivation_operation_t *output)
8359{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008360 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008361 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008362 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008363 size_t shared_key_len = 0;
8364
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008365 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008366 status = PSA_ERROR_BAD_STATE;
8367 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008368 }
8369
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008370#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008371 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008372 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008373 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008374 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008375 status = PSA_ERROR_BAD_STATE;
8376 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008377 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008378 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008379#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008380 {
8381 status = PSA_ERROR_NOT_SUPPORTED;
8382 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008383 }
8384
Przemek Stekiel0c781802022-11-29 14:53:13 +01008385 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8386 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008387 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008388 &shared_key_len);
8389
8390 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008391 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008392 }
8393
8394 status = psa_key_derivation_input_bytes(output,
8395 PSA_KEY_DERIVATION_INPUT_SECRET,
8396 shared_key,
8397 shared_key_len);
8398
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008399 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008400exit:
8401 abort_status = psa_pake_abort(operation);
8402 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008403}
8404
8405psa_status_t psa_pake_abort(
8406 psa_pake_operation_t *operation)
8407{
Przemek Stekield69dca92023-01-31 19:59:20 +01008408 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008409
Przemek Stekield69dca92023-01-31 19:59:20 +01008410 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008411 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008412 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008413
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008414 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8415 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008416 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008417 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008418 }
8419 if (operation->data.inputs.user != NULL) {
8420 mbedtls_free(operation->data.inputs.user);
8421 }
8422 if (operation->data.inputs.peer != NULL) {
8423 mbedtls_free(operation->data.inputs.peer);
8424 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008425 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008426 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008427
Przemek Stekield69dca92023-01-31 19:59:20 +01008428 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008429}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008430#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008431
Gilles Peskinee59236f2018-01-27 23:32:46 +01008432#endif /* MBEDTLS_PSA_CRYPTO_C */