blob: 9e6c2efc39e807032cc0bd18ad2ce319ebbfb135 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskinee59236f2018-01-27 23:32:46 +01007 */
8
Gilles Peskinedb09ef62020-06-03 01:43:33 +02009#include "common.h"
Ronald Cronafbc7ed2023-01-24 16:02:04 +010010#include "psa_crypto_core_common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010011
12#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030013
John Durkop07cc04a2020-11-16 22:08:34 -080014#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
15#include "check_crypto_config.h"
16#endif
17
Gilles Peskinee59236f2018-01-27 23:32:46 +010018#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010019#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010020
Ronald Crond6d28882020-12-14 14:56:02 +010021#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010022#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010023#include "psa_crypto_invasive.h"
Xiaokang Qiane9c39c42023-08-09 08:50:19 +000024#include "psa_crypto_driver_wrappers.h"
Xiaokang Qianfe9666b2023-09-11 10:36:20 +000025#include "psa_crypto_driver_wrappers_no_static.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010026#include "psa_crypto_ecp.h"
Przemek Stekiel359f4622022-12-05 14:11:55 +010027#include "psa_crypto_ffdh.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010028#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010029#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010030#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010031#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020032#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020033#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020034#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010035#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010036/* Include internal declarations that are useful for implementing persistently
37 * stored keys. */
38#include "psa_crypto_storage.h"
39
Gilles Peskineb2b64d32020-12-14 16:43:58 +010040#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010041
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010042#include <stdlib.h>
43#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010046#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020047#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000048#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020049#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010050#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020051#include "mbedtls/chacha20.h"
52#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/cipher.h"
54#include "mbedtls/ccm.h"
55#include "mbedtls/cmac.h"
Dave Rodgman78701152023-08-29 14:20:18 +010056#include "mbedtls/constant_time.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010057#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020058#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010059#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010060#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/error.h"
62#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/md5.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010064#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000065#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010066#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000067#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010069#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/sha1.h"
71#include "mbedtls/sha256.h"
72#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020073#include "md_psa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010074
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020075#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
76 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
77 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020078#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020079#endif
80
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010081/****************************************************************/
82/* Global data, support functions and library management */
83/****************************************************************/
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020086{
Gilles Peskine449bd832023-01-11 14:50:10 +010087 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020088}
89
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010090/* Values for psa_global_data_t::rng_state */
91#define RNG_NOT_INITIALIZED 0
92#define RNG_INITIALIZED 1
93#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +010094
Gilles Peskine449bd832023-01-11 14:50:10 +010095typedef struct {
Dave Rodgman864f5942023-08-16 18:04:44 +010096 uint8_t initialized;
97 uint8_t rng_state;
98 uint8_t drivers_initialized;
Gilles Peskine2d8a1822021-11-08 22:20:03 +010099 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100100} psa_global_data_t;
101
102static psa_global_data_t global_data;
103
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100104#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
105mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
106 &global_data.rng.drbg;
107#endif
108
itayzafrir0adf0fc2018-09-06 16:24:41 +0300109#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if (global_data.initialized == 0) \
111 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300112
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100113int psa_can_do_hash(psa_algorithm_t hash_alg)
114{
115 (void) hash_alg;
116 return global_data.drivers_initialized;
117}
Valerio Settia55f0422023-07-10 15:34:41 +0200118#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200119 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200120 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200121static int psa_is_dh_key_size_valid(size_t bits)
122{
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200123 if (bits != 2048 && bits != 3072 && bits != 4096 &&
124 bits != 6144 && bits != 8192) {
125 return 0;
126 }
127
128 return 1;
129}
Valerio Settia55f0422023-07-10 15:34:41 +0200130#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200131 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200132 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100135{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100136 /* Mbed TLS error codes can combine a high-level error code and a
137 * low-level error code. The low-level error usually reflects the
138 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 int low_level_ret = -(-ret & 0x007f);
140 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100141 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100143
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100144#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100145 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
146 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100148 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
149 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100150#endif
151
152#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200153 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
154 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
155 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
156 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
157 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200159 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200161 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100163#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200164
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100165#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000166 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100167 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100169#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100170
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100171#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100172 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100176#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100177
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100178#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200179 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100181#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200182
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100183#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200184 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200186 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100188#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200189
Dave Rodgman509b5672023-08-16 19:26:23 +0100190#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100191 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100199 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100201 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100203 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100205#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
208 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100209 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
210 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100211 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
214 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100216 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100218#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100219
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100220#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100221 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100223#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100224
Gilles Peskinee59236f2018-01-27 23:32:46 +0100225 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
226 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
227 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100229
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100230#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100231 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200233 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100235 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100237#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100238
Gilles Peskine82e57d12020-11-13 21:31:17 +0100239#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100241 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
242 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100243 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100245 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
246 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100248 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100250#endif
251
Dave Rodgmandea266f2023-08-31 11:52:43 +0100252#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100253 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100255 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100257 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100259#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100260 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100262#endif
263#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100264
Dave Rodgman509b5672023-08-16 19:26:23 +0100265#if defined(MBEDTLS_BIGNUM_C)
266#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100267 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100269#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100270 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100272 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100274 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100276 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100278 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100280 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100282 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100284#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100285
Dave Rodgman509b5672023-08-16 19:26:23 +0100286#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100287 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100289 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
290 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100292#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
293 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100294 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100296#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100297 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
298 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100300 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100302 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
303 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100305 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100307 case MBEDTLS_ERR_PK_INVALID_ALG:
308 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
309 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100311 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200313 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100315#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100316
Gilles Peskineff2d2002019-05-06 15:26:23 +0200317 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200319 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200321
Dave Rodgman509b5672023-08-16 19:26:23 +0100322#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100323 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100325 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100327 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100329 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100331 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
332 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100334 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100336 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100338 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100340#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200341
Dave Rodgman1dab4452023-09-01 09:59:51 +0100342#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300343 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300344 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300346 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300348 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300350 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
351 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300353 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100355 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100357
Dave Rodgman509b5672023-08-16 19:26:23 +0100358#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000359 case MBEDTLS_ERR_ECP_IN_PROGRESS:
360 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100361#endif
362#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000363
Janos Follatha13b9052019-11-22 12:48:59 +0000364 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300366
Gilles Peskinee59236f2018-01-27 23:32:46 +0100367 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100369 }
370}
371
Paul Elliottdc42ca82023-02-24 18:11:59 +0000372/**
373 * \brief For output buffers which contain "tags"
374 * (outputs that may be checked for validity like
375 * hashes, MACs and signatures), fill the unused
376 * part of the output buffer (the whole buffer on
377 * error, the trailing part on success) with
378 * something that isn't a valid tag (barring an
379 * attack on the tag and deliberately-crafted
380 * input), in case the caller doesn't check the
381 * return status properly.
382 *
383 * \param output_buffer Pointer to buffer to wipe. May not be NULL
384 * unless \p output_buffer_size is zero.
385 * \param status Status of function called to generate
386 * output_buffer originally
387 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
388 * could be NULL.
389 * \param output_buffer_length Length of data written to output_buffer, must be
390 * less than \p output_buffer_size
391 */
392static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000393 size_t output_buffer_size, size_t output_buffer_length)
394{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000395 size_t offset = 0;
396
397 if (output_buffer_size == 0) {
398 /* If output_buffer_size is 0 then we have nothing to do. We must not
399 call memset because output_buffer may be NULL in this case */
400 return;
401 }
402
403 if (status == PSA_SUCCESS) {
404 offset = output_buffer_length;
405 }
406
407 memset(output_buffer + offset, '!', output_buffer_size - offset);
408}
409
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200410
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200411
412
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100413/****************************************************************/
414/* Key management */
415/****************************************************************/
416
Valerio Settia9aab1a2023-06-19 13:39:54 +0200417#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200418psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
419 size_t *bits)
420{
421 switch (grpid) {
Valerio Settic437fae2023-09-08 14:58:03 +0200422#if defined(MBEDTLS_ECP_HAVE_SECP192R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200423 case MBEDTLS_ECP_DP_SECP192R1:
424 *bits = 192;
425 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100426#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200427#if defined(MBEDTLS_ECP_HAVE_SECP224R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200428 case MBEDTLS_ECP_DP_SECP224R1:
429 *bits = 224;
430 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100431#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200432#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200433 case MBEDTLS_ECP_DP_SECP256R1:
434 *bits = 256;
435 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100436#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200437#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200438 case MBEDTLS_ECP_DP_SECP384R1:
439 *bits = 384;
440 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100441#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200442#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200443 case MBEDTLS_ECP_DP_SECP521R1:
444 *bits = 521;
445 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100446#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200447#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200448 case MBEDTLS_ECP_DP_BP256R1:
449 *bits = 256;
450 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100451#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200452#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200453 case MBEDTLS_ECP_DP_BP384R1:
454 *bits = 384;
455 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100456#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200457#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200458 case MBEDTLS_ECP_DP_BP512R1:
459 *bits = 512;
460 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100461#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200462#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200463 case MBEDTLS_ECP_DP_CURVE25519:
464 *bits = 255;
465 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100466#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200467#if defined(MBEDTLS_ECP_HAVE_SECP192K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200468 case MBEDTLS_ECP_DP_SECP192K1:
469 *bits = 192;
470 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100471#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200472#if defined(MBEDTLS_ECP_HAVE_SECP224K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200473 case MBEDTLS_ECP_DP_SECP224K1:
474 *bits = 224;
475 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100476#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200477#if defined(MBEDTLS_ECP_HAVE_SECP256K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200478 case MBEDTLS_ECP_DP_SECP256K1:
479 *bits = 256;
480 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100481#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200482#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200483 case MBEDTLS_ECP_DP_CURVE448:
484 *bits = 448;
485 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100486#endif
Valerio Settibc2b1d32023-06-19 12:15:13 +0200487 default:
488 *bits = 0;
489 return 0;
490 }
491}
492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
494 size_t bits,
495 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200496{
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100498 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100500#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100501 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100503#endif
504#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100505 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100507#endif
508#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100509 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100511#endif
512#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100513 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100515#endif
516#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100517 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100519 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 if (bits_is_sloppy) {
521 return MBEDTLS_ECP_DP_SECP521R1;
522 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100523 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100524#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100525 }
526 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100527
Paul Elliott8ff510a2020-06-02 17:19:28 +0100528 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100530#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100531 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100533#endif
534#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100535 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100537#endif
538#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100539 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100541#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100542 }
543 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100544
Paul Elliott8ff510a2020-06-02 17:19:28 +0100545 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100547#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100548 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100550 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if (bits_is_sloppy) {
552 return MBEDTLS_ECP_DP_CURVE25519;
553 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100554 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100555#endif
556#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100557 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100559#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100560 }
561 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100562
Paul Elliott8ff510a2020-06-02 17:19:28 +0100563 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100565#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100566 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100568#endif
569#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100570 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100572#endif
573#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100574 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100576#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100577 }
578 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200579 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100580
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100581 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200583}
Valerio Settia9aab1a2023-06-19 13:39:54 +0200584#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
587 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200588{
589 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200591 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200592 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200593 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200594 case PSA_KEY_TYPE_PASSWORD:
595 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200596 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100597#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200598 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 if (bits != 128 && bits != 192 && bits != 256) {
600 return PSA_ERROR_INVALID_ARGUMENT;
601 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200602 break;
603#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200604#if defined(PSA_WANT_KEY_TYPE_ARIA)
605 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 if (bits != 128 && bits != 192 && bits != 256) {
607 return PSA_ERROR_INVALID_ARGUMENT;
608 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200609 break;
610#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100611#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200612 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 if (bits != 128 && bits != 192 && bits != 256) {
614 return PSA_ERROR_INVALID_ARGUMENT;
615 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200616 break;
617#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100618#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200619 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 if (bits != 64 && bits != 128 && bits != 192) {
621 return PSA_ERROR_INVALID_ARGUMENT;
622 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200623 break;
624#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100625#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200626 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 if (bits != 256) {
628 return PSA_ERROR_INVALID_ARGUMENT;
629 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200630 break;
631#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200632 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200634 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 if (bits % 8 != 0) {
636 return PSA_ERROR_INVALID_ARGUMENT;
637 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200640}
641
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100642/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100643 *
Steven Cooremancd640932021-03-08 12:00:27 +0100644 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
645 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100646 *
647 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100648 * \param[in] key_type The key type of the key to be used with the
649 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100650 *
651 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100652 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100653 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100654 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100655 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100656MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100657 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100659{
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 if (PSA_ALG_IS_HMAC(algorithm)) {
661 if (key_type == PSA_KEY_TYPE_HMAC) {
662 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100663 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100664 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
667 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
668 * key. */
669 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
670 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
671 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
672 * the block length (larger than 1) for block ciphers. */
673 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
674 return PSA_SUCCESS;
675 }
676 }
677 }
678
679 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100680}
681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
683 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200684{
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (slot->key.data != NULL) {
686 return PSA_ERROR_ALREADY_EXISTS;
687 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 slot->key.data = mbedtls_calloc(1, buffer_length);
690 if (slot->key.data == NULL) {
691 return PSA_ERROR_INSUFFICIENT_MEMORY;
692 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200693
Ronald Cronea0f8a62020-11-25 17:52:23 +0100694 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200696}
697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
699 const uint8_t *data,
700 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200701{
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 psa_status_t status = psa_allocate_buffer_to_slot(slot,
703 data_length);
704 if (status != PSA_SUCCESS) {
705 return status;
706 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200707
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 memcpy(slot->key.data, data, data_length);
709 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200710}
711
Ronald Crona0fe59f2020-11-29 11:14:53 +0100712psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100713 const psa_key_attributes_t *attributes,
714 const uint8_t *data, size_t data_length,
715 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100717{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100718 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
719 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100720
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200721 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 if (data_length == 0) {
723 return PSA_ERROR_NOT_SUPPORTED;
724 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200725
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 if (key_type_is_raw_bytes(type)) {
727 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
730 *bits);
731 if (status != PSA_SUCCESS) {
732 return status;
733 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200734
Ronald Crondd04d422020-11-28 15:14:42 +0100735 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100737 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 return PSA_SUCCESS;
741 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200742#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200743 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100744 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200745 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100746 return PSA_ERROR_INVALID_ARGUMENT;
747 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200748 return mbedtls_psa_ffdh_import_key(attributes,
749 data, data_length,
750 key_buffer, key_buffer_size,
751 key_buffer_length,
752 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100753 }
Valerio Settia55f0422023-07-10 15:34:41 +0200754#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200755 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200756#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
758 if (PSA_KEY_TYPE_IS_ECC(type)) {
759 return mbedtls_psa_ecp_import_key(attributes,
760 data, data_length,
761 key_buffer, key_buffer_size,
762 key_buffer_length,
763 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100764 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200765#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800766 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200767#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
768 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
770 if (PSA_KEY_TYPE_IS_RSA(type)) {
771 return mbedtls_psa_rsa_import_key(attributes,
772 data, data_length,
773 key_buffer, key_buffer_size,
774 key_buffer_length,
775 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200776 }
Valerio Settife478902023-07-25 12:27:19 +0200777#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
778 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800779 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100780 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100783}
784
Gilles Peskinef603c712019-01-19 13:40:11 +0100785/** Calculate the intersection of two algorithm usage policies.
786 *
787 * Return 0 (which allows no operation) on incompatibility.
788 */
789static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100790 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100791 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100793{
Gilles Peskine549ea862019-05-22 11:45:59 +0200794 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if (alg1 == alg2) {
796 return alg1;
797 }
Steven Cooreman03488022021-02-18 12:07:20 +0100798 /* If the policies are from the same hash-and-sign family, check
799 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
801 PSA_ALG_IS_SIGN_HASH(alg2) &&
802 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
803 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
804 return alg2;
805 }
806 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
807 return alg1;
808 }
Steven Cooreman03488022021-02-18 12:07:20 +0100809 }
810 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100811 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100812 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
814 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
815 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
816 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
817 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100818 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100819
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100820 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
822 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
823 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
824 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100825 }
826 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
828 (alg1_len <= alg2_len)) {
829 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100830 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
832 (alg2_len <= alg1_len)) {
833 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100834 }
835 }
836 /* If the policies are from the same MAC family, check whether one
837 * of them is a minimum-MAC-length policy. Calculate the most
838 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
840 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
841 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100842 /* Validate the combination of key type and algorithm. Since the base
843 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
845 return 0;
846 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100847
Steven Cooreman31a876d2021-03-03 20:47:40 +0100848 /* Get the (exact or at-least) output lengths for both sides of the
849 * requested intersection. None of the currently supported algorithms
850 * have an output length dependent on the actual key size, so setting it
851 * to a bogus value of 0 is currently OK.
852 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100853 * Note that for at-least-this-length wildcard algorithms, the output
854 * length is set to the shortest allowed length, which allows us to
855 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
857 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100858 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100859
Steven Cooremana1d83222021-02-25 10:20:29 +0100860 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
862 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
863 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100864 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100865
866 /* If only one is an at-least-this-length policy, the intersection would
867 * be the other (fixed-length) policy as long as said fixed length is
868 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
870 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100871 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
873 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100874 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100875
Steven Cooremancd640932021-03-08 12:00:27 +0100876 /* If none of them are wildcards, check whether they define the same tag
877 * length. This is still possible here when one is default-length and
878 * the other specific-length. Ensure to always return the
879 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 if (alg1_len == alg2_len) {
881 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
882 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100883 }
884 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100886}
887
Gilles Peskine449bd832023-01-11 14:50:10 +0100888static int psa_key_algorithm_permits(psa_key_type_t key_type,
889 psa_algorithm_t policy_alg,
890 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200891{
Gilles Peskine549ea862019-05-22 11:45:59 +0200892 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (requested_alg == policy_alg) {
894 return 1;
895 }
Steven Cooreman03488022021-02-18 12:07:20 +0100896 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
897 * and requested_alg is the same hash-and-sign family with any hash,
898 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
900 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
901 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
902 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100903 }
904 /* If policy_alg is a wildcard AEAD algorithm of the same base as
905 * the requested algorithm, check the requested tag length to be
906 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 if (PSA_ALG_IS_AEAD(policy_alg) &&
908 PSA_ALG_IS_AEAD(requested_alg) &&
909 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
910 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
911 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
912 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
913 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100914 }
Steven Cooremancd640932021-03-08 12:00:27 +0100915 /* If policy_alg is a MAC algorithm of the same base as the requested
916 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 if (PSA_ALG_IS_MAC(policy_alg) &&
918 PSA_ALG_IS_MAC(requested_alg) &&
919 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
920 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100921 /* Validate the combination of key type and algorithm. Since the policy
922 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
924 return 0;
925 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100926
Steven Cooreman31a876d2021-03-03 20:47:40 +0100927 /* Get both the requested output length for the algorithm which is to be
928 * verified, and the default output length for the base algorithm.
929 * Note that none of the currently supported algorithms have an output
930 * length dependent on actual key size, so setting it to a bogus value
931 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100932 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100934 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 key_type, 0,
936 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100937
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100938 /* If the policy is default-length, only allow an algorithm with
939 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
941 return requested_output_length == default_output_length;
942 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100943
944 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100945 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
947 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
948 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100949 }
950
Steven Cooremancd640932021-03-08 12:00:27 +0100951 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
952 * check for the requested MAC length to be equal to or longer than the
953 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
955 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
956 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100957 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200958 }
Steven Cooremance48e852020-10-05 16:02:45 +0200959 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200960 * a key derivation with that key agreement should also be allowed. This
961 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
963 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
964 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
965 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200966 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100967 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200969}
970
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100971/** Test whether a policy permits an algorithm.
972 *
973 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100974 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100975 * \note This function requires providing the key type for which the policy is
976 * being validated, since some algorithm policy definitions (e.g. MAC)
977 * have different properties depending on what kind of cipher it is
978 * combined with.
979 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100980 * \retval PSA_SUCCESS When \p alg is a specific algorithm
981 * allowed by the \p policy.
982 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
983 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
984 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100985 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100986static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
987 psa_key_type_t key_type,
988 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100989{
Steven Cooremand788fab2021-02-25 11:29:17 +0100990 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 if (alg == 0) {
992 return PSA_ERROR_INVALID_ARGUMENT;
993 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100994
Steven Cooreman7e39f052021-02-23 14:18:32 +0100995 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 if (PSA_ALG_IS_WILDCARD(alg)) {
997 return PSA_ERROR_INVALID_ARGUMENT;
998 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
1001 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
1002 return PSA_SUCCESS;
1003 } else {
1004 return PSA_ERROR_NOT_PERMITTED;
1005 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001006}
1007
Gilles Peskinef603c712019-01-19 13:40:11 +01001008/** Restrict a key policy based on a constraint.
1009 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001010 * \note This function requires providing the key type for which the policy is
1011 * being restricted, since some algorithm policy definitions (e.g. MAC)
1012 * have different properties depending on what kind of cipher it is
1013 * combined with.
1014 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001015 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +01001016 * \param[in,out] policy The policy to restrict.
1017 * \param[in] constraint The policy constraint to apply.
1018 *
1019 * \retval #PSA_SUCCESS
1020 * \c *policy contains the intersection of the original value of
1021 * \c *policy and \c *constraint.
1022 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001023 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001024 * \c *policy is unchanged.
1025 */
1026static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001027 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001028 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001030{
1031 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1033 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001034 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1036 constraint->alg2);
1037 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1038 return PSA_ERROR_INVALID_ARGUMENT;
1039 }
1040 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1041 return PSA_ERROR_INVALID_ARGUMENT;
1042 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001043 policy->usage &= constraint->usage;
1044 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001045 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001047}
1048
Przemek Stekiel061f6942022-11-30 10:51:35 +01001049/** Get the description of a key given its identifier and policy constraints
1050 * and lock it.
1051 *
1052 * The key must have allow all the usage flags set in \p usage. If \p alg is
1053 * nonzero, the key must allow operations with this algorithm. If \p alg is
1054 * zero, the algorithm is not checked.
1055 *
1056 * In case of a persistent key, the function loads the description of the key
1057 * into a key slot if not already done.
1058 *
1059 * On success, the returned key slot is locked. It is the responsibility of
1060 * the caller to unlock the key slot when it does not access it anymore.
1061 */
1062static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001063 mbedtls_svc_key_id_t key,
1064 psa_key_slot_t **p_slot,
1065 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001067{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001068 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001069 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 status = psa_get_and_lock_key_slot(key, p_slot);
1072 if (status != PSA_SUCCESS) {
1073 return status;
1074 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001075 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001076
1077 /* Enforce that usage policy for the key slot contains all the flags
1078 * required by the usage parameter. There is one exception: public
1079 * keys can always be exported, so we treat public key objects as
1080 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001082 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001086 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001087 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001088 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001089
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001090 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (alg != 0) {
1092 status = psa_key_policy_permits(&slot->attr.policy,
1093 slot->attr.type,
1094 alg);
1095 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001096 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001098 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001099
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001101
1102error:
1103 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001107}
Darryl Green940d72c2018-07-13 13:18:51 +01001108
Ronald Cron5c522922020-11-14 16:35:34 +01001109/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001110 *
1111 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001112 * available, as opposed to a key in a secure element and/or to be used
1113 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001114 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001115 * This is a temporary function that may be used instead of
1116 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1117 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001118 *
Ronald Cron5c522922020-11-14 16:35:34 +01001119 * On success, the returned key slot is locked. It is the responsibility of the
1120 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001121 */
Ronald Cron5c522922020-11-14 16:35:34 +01001122static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1123 mbedtls_svc_key_id_t key,
1124 psa_key_slot_t **p_slot,
1125 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001127{
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1129 usage, alg);
1130 if (status != PSA_SUCCESS) {
1131 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001132 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1135 psa_unlock_key_slot(*p_slot);
1136 *p_slot = NULL;
1137 return PSA_ERROR_NOT_SUPPORTED;
1138 }
1139
1140 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001141}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001144{
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001146 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001148
Ronald Cronea0f8a62020-11-25 17:52:23 +01001149 slot->key.data = NULL;
1150 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001153}
1154
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001155/** Completely wipe a slot in memory, including its policy.
1156 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001157psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001158{
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 /*
1162 * As the return error code may not be handled in case of multiple errors,
1163 * do our best to report an unexpected lock counter. Assert with
1164 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1165 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1166 * function is called as part of the execution of a test suite, the
1167 * execution of the test suite is stopped in error if the assertion fails.
1168 */
1169 if (slot->lock_count != 1) {
1170 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001171 status = PSA_ERROR_CORRUPTION_DETECTED;
1172 }
1173
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001174 /* Multipart operations may still be using the key. This is safe
1175 * because all multipart operation objects are independent from
1176 * the key slot: if they need to access the key after the setup
1177 * phase, they have a copy of the key. Note that this means that
1178 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001179 /* At this point, key material and other type-specific content has
1180 * been wiped. Clear remaining metadata. We can call memset and not
1181 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 memset(slot, 0, sizeof(*slot));
1183 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001184}
1185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001187{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001188 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001189 psa_status_t status; /* status of the last operation */
1190 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001191#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1192 psa_se_drv_table_entry_t *driver;
1193#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 if (mbedtls_svc_key_id_is_null(key)) {
1196 return PSA_SUCCESS;
1197 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001198
Ronald Cronf2911112020-10-29 17:51:10 +01001199 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001200 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001201 * key, this will load the key description from persistent memory if not
1202 * done yet. We cannot avoid this loading as without it we don't know if
1203 * the key is operated by an SE or not and this information is needed by
1204 * the current implementation.
1205 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 status = psa_get_and_lock_key_slot(key, &slot);
1207 if (status != PSA_SUCCESS) {
1208 return status;
1209 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001210
Ronald Cronf2911112020-10-29 17:51:10 +01001211 /*
1212 * If the key slot containing the key description is under access by the
1213 * library (apart from the present access), the key cannot be destroyed
1214 * yet. For the time being, just return in error. Eventually (to be
1215 * implemented), the key should be destroyed when all accesses have
1216 * stopped.
1217 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 if (slot->lock_count > 1) {
1219 psa_unlock_key_slot(slot);
1220 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001221 }
1222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001224 /* Refuse the destruction of a read-only key (which may or may not work
1225 * if we attempt it, depending on whether the key is merely read-only
1226 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001227 * Just do the best we can, which is to wipe the copy in memory
1228 * (done in this function's cleanup code). */
1229 overall_status = PSA_ERROR_NOT_PERMITTED;
1230 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001231 }
1232
Gilles Peskine354f7672019-07-12 23:46:38 +02001233#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1235 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001236 /* For a key in a secure element, we need to do three things:
1237 * remove the key file in internal storage, destroy the
1238 * key inside the secure element, and update the driver's
1239 * persistent data. Start a transaction that will encompass these
1240 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001242 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001244 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 status = psa_crypto_save_transaction();
1246 if (status != PSA_SUCCESS) {
1247 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001248 /* We should still try to destroy the key in the secure
1249 * element and the key metadata in storage. This is especially
1250 * important if the error is that the storage is full.
1251 * But how to do it exactly without risking an inconsistent
1252 * state after a reset?
1253 * https://github.com/ARMmbed/mbed-crypto/issues/215
1254 */
1255 overall_status = status;
1256 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001257 }
1258
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 status = psa_destroy_se_key(driver,
1260 psa_key_slot_get_slot_number(slot));
1261 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001262 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001264 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001265#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1266
Darryl Greend49a4992018-06-18 17:27:26 +01001267#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1269 status = psa_destroy_persistent_key(slot->attr.id);
1270 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001271 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001273
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001274 /* TODO: other slots may have a copy of the same key. We should
1275 * invalidate them.
1276 * https://github.com/ARMmbed/mbed-crypto/issues/214
1277 */
Darryl Greend49a4992018-06-18 17:27:26 +01001278 }
1279#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001280
Gilles Peskinefc762652019-07-22 19:30:34 +02001281#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 if (driver != NULL) {
1283 status = psa_save_se_persistent_data(driver);
1284 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001285 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 }
1287 status = psa_crypto_stop_transaction();
1288 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001289 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001291 }
1292#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1293
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001294exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001296 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001298 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 }
1300 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001301}
1302
Valerio Settib2bcedb2023-07-10 11:24:00 +02001303#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001304 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001305static psa_status_t psa_get_rsa_public_exponent(
1306 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001308{
1309 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001310 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001311 uint8_t *buffer = NULL;
1312 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1316 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001317 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 }
1319 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001320 /* It's the default value, which is reported as an empty string,
1321 * so there's nothing to do. */
1322 goto exit;
1323 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 buflen = mbedtls_mpi_size(&mpi);
1326 buffer = mbedtls_calloc(1, buflen);
1327 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001328 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1329 goto exit;
1330 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1332 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001333 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001335 attributes->domain_parameters = buffer;
1336 attributes->domain_parameters_size = buflen;
1337
1338exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 mbedtls_mpi_free(&mpi);
1340 if (ret != 0) {
1341 mbedtls_free(buffer);
1342 }
1343 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001344}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001345#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001346 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001347
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001348/** Retrieve all the publicly-accessible attributes of a key.
1349 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001350psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1351 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001352{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001353 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001354 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001355 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1360 if (status != PSA_SUCCESS) {
1361 return status;
1362 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001363
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001364 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1366 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001367
1368#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1370 psa_set_key_slot_number(attributes,
1371 psa_key_slot_get_slot_number(slot));
1372 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001373#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001376#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1377 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001378 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001379 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001380 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001381 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001382 * is not yet implemented.
1383 * https://github.com/ARMmbed/mbed-crypto/issues/216
1384 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001386 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001387
Ronald Cron90857082020-11-25 14:58:33 +01001388 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 slot->attr.type,
1390 slot->key.data,
1391 slot->key.bytes,
1392 &rsa);
1393 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001394 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 status = psa_get_rsa_public_exponent(rsa,
1398 attributes);
1399 mbedtls_rsa_free(rsa);
1400 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001401 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001402 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001403#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1404 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001405 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001406 default:
1407 /* Nothing else to do. */
1408 break;
1409 }
1410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 if (status != PSA_SUCCESS) {
1412 psa_reset_key_attributes(attributes);
1413 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001416
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001418}
1419
Gilles Peskinec8000c02019-08-02 20:15:51 +02001420#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1421psa_status_t psa_get_key_slot_number(
1422 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001424{
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001426 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 return PSA_SUCCESS;
1428 } else {
1429 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001430 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001431}
1432#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1435 size_t key_buffer_size,
1436 uint8_t *data,
1437 size_t data_size,
1438 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001439{
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 if (key_buffer_size > data_size) {
1441 return PSA_ERROR_BUFFER_TOO_SMALL;
1442 }
1443 memcpy(data, key_buffer, key_buffer_size);
1444 memset(data + key_buffer_size, 0,
1445 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001446 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001448}
1449
Ronald Cron7285cda2020-11-26 14:46:37 +01001450psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001451 const psa_key_attributes_t *attributes,
1452 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001454{
Ronald Crond18b5f82020-11-25 16:46:05 +01001455 psa_key_type_t type = attributes->core.type;
1456
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 if (key_type_is_raw_bytes(type) ||
1458 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001459 PSA_KEY_TYPE_IS_ECC(type) ||
1460 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 return psa_export_key_buffer_internal(
1462 key_buffer, key_buffer_size,
1463 data, data_size, data_length);
1464 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001465 /* This shouldn't happen in the reference implementation, but
1466 it is valid for a special-purpose implementation to omit
1467 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001469 }
1470}
1471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1473 uint8_t *data,
1474 size_t data_size,
1475 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001476{
1477 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1478 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1479 psa_key_slot_t *slot;
1480
Ronald Crone907e552021-01-18 13:22:38 +01001481 /* Reject a zero-length output buffer now, since this can never be a
1482 * valid key representation. This way we know that data must be a valid
1483 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 if (data_size == 0) {
1485 return PSA_ERROR_BUFFER_TOO_SMALL;
1486 }
Ronald Crone907e552021-01-18 13:22:38 +01001487
Ronald Cron9486f9d2020-11-26 13:36:23 +01001488 /* Set the key to empty now, so that even when there are errors, we always
1489 * set data_length to a value between 0 and data_size. On error, setting
1490 * the key to empty is a good choice because an empty key representation is
1491 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001492 *data_length = 0;
1493
Ronald Cron9486f9d2020-11-26 13:36:23 +01001494 /* Export requires the EXPORT flag. There is an exception for public keys,
1495 * which don't require any flag, but
1496 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1497 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1499 PSA_KEY_USAGE_EXPORT, 0);
1500 if (status != PSA_SUCCESS) {
1501 return status;
1502 }
mohammad160306e79202018-03-28 13:17:44 +03001503
Ronald Crond18b5f82020-11-25 16:46:05 +01001504 psa_key_attributes_t attributes = {
1505 .core = slot->attr
1506 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 status = psa_driver_wrapper_export_key(&attributes,
1508 slot->key.data, slot->key.bytes,
1509 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001510
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001512
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001514}
1515
Ronald Cron7285cda2020-11-26 14:46:37 +01001516psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001517 const psa_key_attributes_t *attributes,
1518 const uint8_t *key_buffer,
1519 size_t key_buffer_size,
1520 uint8_t *data,
1521 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001523{
Ronald Crond18b5f82020-11-25 16:46:05 +01001524 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001525
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001526 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001527 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1528 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001529 /* Exporting public -> public */
1530 return psa_export_key_buffer_internal(
1531 key_buffer, key_buffer_size,
1532 data, data_size, data_length);
1533 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001534#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001535 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1536 return mbedtls_psa_rsa_export_public_key(attributes,
1537 key_buffer,
1538 key_buffer_size,
1539 data,
1540 data_size,
1541 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001542#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001543 /* We don't know how to convert a private RSA key to public. */
1544 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001545#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001546 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001547 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001548#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001549 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1550 return mbedtls_psa_ecp_export_public_key(attributes,
1551 key_buffer,
1552 key_buffer_size,
1553 data,
1554 data_size,
1555 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001556#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001557 /* We don't know how to convert a private ECC key to public */
1558 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001559#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001560 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001561 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001562#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001563 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001564 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001565 key_buffer,
1566 key_buffer_size,
1567 data, data_size,
1568 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001569#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001570 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001571#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001572 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001574 (void) key_buffer;
1575 (void) key_buffer_size;
1576 (void) data;
1577 (void) data_size;
1578 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001580 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001581}
Ronald Crond18b5f82020-11-25 16:46:05 +01001582
Gilles Peskine449bd832023-01-11 14:50:10 +01001583psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1584 uint8_t *data,
1585 size_t data_size,
1586 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001587{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001588 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001589 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001590 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001591 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001592
Ronald Crone907e552021-01-18 13:22:38 +01001593 /* Reject a zero-length output buffer now, since this can never be a
1594 * valid key representation. This way we know that data must be a valid
1595 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 if (data_size == 0) {
1597 return PSA_ERROR_BUFFER_TOO_SMALL;
1598 }
Ronald Crone907e552021-01-18 13:22:38 +01001599
Darryl Greendd8fb772018-11-07 16:00:44 +00001600 /* Set the key to empty now, so that even when there are errors, we always
1601 * set data_length to a value between 0 and data_size. On error, setting
1602 * the key to empty is a good choice because an empty key representation is
1603 * unlikely to be accepted anywhere. */
1604 *data_length = 0;
1605
1606 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1608 if (status != PSA_SUCCESS) {
1609 return status;
1610 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001611
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1613 status = PSA_ERROR_INVALID_ARGUMENT;
1614 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001615 }
1616
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001617 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001618 .core = slot->attr
1619 };
Ronald Cron67227982020-11-26 15:16:05 +01001620 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001621 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001623
1624exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001628}
1629
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001630MBEDTLS_STATIC_ASSERT(
1631 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1632 "One or more key attribute flag is listed as both external-only and dual-use")
1633MBEDTLS_STATIC_ASSERT(
1634 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1635 "One or more key attribute flag is listed as both internal-only and dual-use")
1636MBEDTLS_STATIC_ASSERT(
1637 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1638 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001639
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001640/** Validate that a key policy is internally well-formed.
1641 *
1642 * This function only rejects invalid policies. It does not validate the
1643 * consistency of the policy with respect to other attributes of the key
1644 * such as the key type.
1645 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001646static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001647{
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1649 PSA_KEY_USAGE_COPY |
1650 PSA_KEY_USAGE_ENCRYPT |
1651 PSA_KEY_USAGE_DECRYPT |
1652 PSA_KEY_USAGE_SIGN_MESSAGE |
1653 PSA_KEY_USAGE_VERIFY_MESSAGE |
1654 PSA_KEY_USAGE_SIGN_HASH |
1655 PSA_KEY_USAGE_VERIFY_HASH |
1656 PSA_KEY_USAGE_VERIFY_DERIVATION |
1657 PSA_KEY_USAGE_DERIVE)) != 0) {
1658 return PSA_ERROR_INVALID_ARGUMENT;
1659 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001660
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001662}
1663
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001664/** Validate the internal consistency of key attributes.
1665 *
1666 * This function only rejects invalid attribute values. If does not
1667 * validate the consistency of the attributes with any key data that may
1668 * be involved in the creation of the key.
1669 *
1670 * Call this function early in the key creation process.
1671 *
1672 * \param[in] attributes Key attributes for the new key.
1673 * \param[out] p_drv On any return, the driver for the key, if any.
1674 * NULL for a transparent key.
1675 *
1676 */
1677static psa_status_t psa_validate_key_attributes(
1678 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001680{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001681 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1683 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 status = psa_validate_key_location(lifetime, p_drv);
1686 if (status != PSA_SUCCESS) {
1687 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001688 }
1689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 status = psa_validate_key_persistence(lifetime);
1691 if (status != PSA_SUCCESS) {
1692 return status;
1693 }
1694
1695 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1696 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1697 return PSA_ERROR_INVALID_ARGUMENT;
1698 }
1699 } else {
1700 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1701 return PSA_ERROR_INVALID_ARGUMENT;
1702 }
1703 }
1704
1705 status = psa_validate_key_policy(&attributes->core.policy);
1706 if (status != PSA_SUCCESS) {
1707 return status;
1708 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001709
1710 /* Refuse to create overly large keys.
1711 * Note that this doesn't trigger on import if the attributes don't
1712 * explicitly specify a size (so psa_get_key_bits returns 0), so
1713 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1715 return PSA_ERROR_NOT_SUPPORTED;
1716 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001717
Gilles Peskine91e8c332019-08-02 19:19:39 +02001718 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1720 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1721 return PSA_ERROR_INVALID_ARGUMENT;
1722 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001723
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001725}
1726
Gilles Peskine4747d192019-04-17 15:05:45 +02001727/** Prepare a key slot to receive key material.
1728 *
1729 * This function allocates a key slot and sets its metadata.
1730 *
1731 * If this function fails, call psa_fail_key_creation().
1732 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001733 * This function is intended to be used as follows:
1734 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001735 * it with the specified attributes, and in case of a volatile key assign it
1736 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001737 * -# Populate the slot with the key material.
1738 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1739 * In case of failure at any step, stop the sequence and call
1740 * psa_fail_key_creation().
1741 *
Ronald Cron5c522922020-11-14 16:35:34 +01001742 * On success, the key slot is locked. It is the responsibility of the caller
1743 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001744 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001745 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001746 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001747 * \param[out] p_slot On success, a pointer to the prepared slot.
1748 * \param[out] p_drv On any return, the driver for the key, if any.
1749 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001750 *
1751 * \retval #PSA_SUCCESS
1752 * The key slot is ready to receive key material.
1753 * \return If this function fails, the key slot is an invalid state.
1754 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001755 */
1756static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001757 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001758 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001759 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001761{
1762 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001763 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001764 psa_key_slot_t *slot;
1765
Gilles Peskinedf179142019-07-15 22:02:14 +02001766 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001767 *p_drv = NULL;
1768
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 status = psa_validate_key_attributes(attributes, p_drv);
1770 if (status != PSA_SUCCESS) {
1771 return status;
1772 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001773
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1775 if (status != PSA_SUCCESS) {
1776 return status;
1777 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001778 slot = *p_slot;
1779
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001780 /* We're storing the declared bit-size of the key. It's up to each
1781 * creation mechanism to verify that this information is correct.
1782 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001783 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001784 * is optional (import, copy). In case of a volatile key, assign it the
1785 * volatile key identifier associated to the slot returned to contain its
1786 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001787
1788 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001790#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1791 slot->attr.id = volatile_key_id;
1792#else
1793 slot->attr.id.key_id = volatile_key_id;
1794#endif
1795 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001796
Gilles Peskine91e8c332019-08-02 19:19:39 +02001797 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001798 * external-only flags, query `attributes`. Thanks to the check
1799 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001800 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001801 * may have set. */
1802 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001803
Gilles Peskinecbaff462019-07-12 23:46:04 +02001804#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001805 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001806 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001807 * create the key file in internal storage, create the
1808 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001809 * persistent data. This is done by starting a transaction that will
1810 * encompass these three actions.
1811 * For registering a volatile key, we just need to find an appropriate
1812 * slot number inside the SE. Since the key is designated volatile, creating
1813 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001814 /* The first thing to do is to find a slot number for the new key.
1815 * We save the slot number in persistent storage as part of the
1816 * transaction data. It will be needed to recover if the power
1817 * fails during the key creation process, to clean up on the secure
1818 * element side after restarting. Obtaining a slot number from the
1819 * secure element driver updates its persistent state, but we do not yet
1820 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001821 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001823 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1825 &slot_number);
1826 if (status != PSA_SUCCESS) {
1827 return status;
1828 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1831 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001832 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001833 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001834 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 status = psa_crypto_save_transaction();
1836 if (status != PSA_SUCCESS) {
1837 (void) psa_crypto_stop_transaction();
1838 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001839 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001840 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001841
1842 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001844 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001845
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001847 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001849 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001850#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1851
Ryan Everett975d4112023-11-16 13:37:51 +00001852 slot->status = PSA_SLOT_OCCUPIED;
1853
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001855}
Gilles Peskine4747d192019-04-17 15:05:45 +02001856
1857/** Finalize the creation of a key once its key material has been set.
1858 *
1859 * This entails writing the key to persistent storage.
1860 *
1861 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001862 * See the documentation of psa_start_key_creation() for the intended use
1863 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001864 *
Ronald Cron5c522922020-11-14 16:35:34 +01001865 * If the finalization succeeds, the function unlocks the key slot (it was
1866 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1867 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001868 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001869 * \param[in,out] slot Pointer to the slot with key material.
1870 * \param[in] driver The secure element driver for the key,
1871 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001872 * \param[out] key On success, identifier of the key. Note that the
1873 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001874 *
1875 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001876 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001877 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1878 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1879 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1880 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1881 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1882 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001883 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001884 * \return If this function fails, the key slot is an invalid state.
1885 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001886 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001887static psa_status_t psa_finish_key_creation(
1888 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001889 psa_se_drv_table_entry_t *driver,
1890 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001891{
1892 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001893 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001894 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001895
1896#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001898#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001900 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001901 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001903
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001904 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1905 sizeof(data.slot_number),
1906 "Slot number size does not match psa_se_key_data_storage_t");
1907
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1909 status = psa_save_persistent_key(&slot->attr,
1910 (uint8_t *) &data,
1911 sizeof(data));
1912 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001913#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1914 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001915 /* Key material is saved in export representation in the slot, so
1916 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 status = psa_save_persistent_key(&slot->attr,
1918 slot->key.data,
1919 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001920 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001921 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001922#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1923
Gilles Peskinecbaff462019-07-12 23:46:04 +02001924#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001925 /* Finish the transaction for a key creation. This does not
1926 * happen when registering an existing key. Detect this case
1927 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001928 * creation of a persistent key in a secure element requires a transaction,
1929 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 if (driver != NULL &&
1931 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1932 status = psa_save_se_persistent_data(driver);
1933 if (status != PSA_SUCCESS) {
1934 psa_destroy_persistent_key(slot->attr.id);
1935 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001936 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001938 }
1939#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1940
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001942 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 status = psa_unlock_key_slot(slot);
1944 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001945 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001947 }
Ronald Cron50972942020-11-14 11:28:25 +01001948
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001950}
1951
1952/** Abort the creation of a key.
1953 *
1954 * You may call this function after calling psa_start_key_creation(),
1955 * or after psa_finish_key_creation() fails. In other circumstances, this
1956 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001957 * See the documentation of psa_start_key_creation() for the intended use
1958 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001959 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001960 * \param[in,out] slot Pointer to the slot with key material.
1961 * \param[in] driver The secure element driver for the key,
1962 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001963 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001964static void psa_fail_key_creation(psa_key_slot_t *slot,
1965 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001966{
Gilles Peskine011e4282019-06-26 18:34:38 +02001967 (void) driver;
1968
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001970 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001972
1973#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001974 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001975 * element, and the failure happened later (when saving metadata
1976 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001977 * element.
1978 * https://github.com/ARMmbed/mbed-crypto/issues/217
1979 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001980
Gilles Peskined7729582019-08-05 15:55:54 +02001981 /* Abort the ongoing transaction if any (there may not be one if
1982 * the creation process failed before starting one, or if the
1983 * key creation is a registration of a key in a secure element).
1984 * Earlier functions must already have done what it takes to undo any
1985 * partial creation. All that's left is to update the transaction data
1986 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001988#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1989
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001991}
1992
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001993/** Validate optional attributes during key creation.
1994 *
1995 * Some key attributes are optional during key creation. If they are
1996 * specified in the attributes structure, check that they are consistent
1997 * with the data in the slot.
1998 *
1999 * This function should be called near the end of key creation, after
2000 * the slot in memory is fully populated but before saving persistent data.
2001 */
2002static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002003 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002005{
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 if (attributes->core.type != 0) {
2007 if (attributes->core.type != slot->attr.type) {
2008 return PSA_ERROR_INVALID_ARGUMENT;
2009 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002010 }
2011
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02002013#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
2014 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2016 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002017 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002018 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002019 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002020
Ronald Cron90857082020-11-25 14:58:33 +01002021 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 slot->attr.type,
2023 slot->key.data,
2024 slot->key.bytes,
2025 &rsa);
2026 if (status != PSA_SUCCESS) {
2027 return status;
2028 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002029
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 mbedtls_mpi_init(&actual);
2031 mbedtls_mpi_init(&required);
2032 ret = mbedtls_rsa_export(rsa,
2033 NULL, NULL, NULL, NULL, &actual);
2034 mbedtls_rsa_free(rsa);
2035 mbedtls_free(rsa);
2036 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002037 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 }
2039 ret = mbedtls_mpi_read_binary(&required,
2040 attributes->domain_parameters,
2041 attributes->domain_parameters_size);
2042 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002043 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 }
2045 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002046 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 }
2048rsa_exit:
2049 mbedtls_mpi_free(&actual);
2050 mbedtls_mpi_free(&required);
2051 if (ret != 0) {
2052 return mbedtls_to_psa_error(ret);
2053 }
2054 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02002055#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
2056 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08002057 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002058 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002060 }
2061 }
2062
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 if (attributes->core.bits != 0) {
2064 if (attributes->core.bits != slot->attr.bits) {
2065 return PSA_ERROR_INVALID_ARGUMENT;
2066 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002067 }
2068
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002070}
2071
Gilles Peskine449bd832023-01-11 14:50:10 +01002072psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2073 const uint8_t *data,
2074 size_t data_length,
2075 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002076{
2077 psa_status_t status;
2078 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002079 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002080 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302081 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002082
Ronald Cron81709fc2020-11-14 12:10:32 +01002083 *key = MBEDTLS_SVC_KEY_ID_INIT;
2084
Gilles Peskine0f84d622019-09-12 19:03:13 +02002085 /* Reject zero-length symmetric keys (including raw data key objects).
2086 * This also rejects any key which might be encoded as an empty string,
2087 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 if (data_length == 0) {
2089 return PSA_ERROR_INVALID_ARGUMENT;
2090 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002091
Archana449608b2021-09-08 15:36:05 +05302092 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 if (data_length > SIZE_MAX / 8) {
2094 return PSA_ERROR_NOT_SUPPORTED;
2095 }
Archana6ed4bda2021-08-04 10:47:15 +05302096
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2098 &slot, &driver);
2099 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002100 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002102
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002103 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302104 * storage ( thus not in the case of importing a key in a secure element
2105 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2106 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 if (slot->key.data == NULL) {
2108 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302109 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 attributes, data, data_length, &storage_size);
2111 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302112 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 }
Archanad8a83dc2021-06-14 10:04:16 +05302114 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 status = psa_allocate_buffer_to_slot(slot, storage_size);
2116 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002117 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002119 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002120
2121 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 status = psa_driver_wrapper_import_key(attributes,
2123 data, data_length,
2124 slot->key.data,
2125 slot->key.bytes,
2126 &slot->key.bytes, &bits);
2127 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002128 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002130
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002132 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002134 status = PSA_ERROR_INVALID_ARGUMENT;
2135 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002136 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002137
Archana6ed4bda2021-08-04 10:47:15 +05302138 /* Enforce a size limit, and in particular ensure that the bit
2139 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302141 status = PSA_ERROR_NOT_SUPPORTED;
2142 goto exit;
2143 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 status = psa_validate_optional_attributes(slot, attributes);
2145 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002146 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002150exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 if (status != PSA_SUCCESS) {
2152 psa_fail_key_creation(slot, driver);
2153 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002156}
2157
Gilles Peskined7729582019-08-05 15:55:54 +02002158#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2159psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002161{
2162 psa_status_t status;
2163 psa_key_slot_t *slot = NULL;
2164 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002165 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002166
2167 /* Leaving attributes unspecified is not currently supported.
2168 * It could make sense to query the key type and size from the
2169 * secure element, but not all secure elements support this
2170 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2172 return PSA_ERROR_NOT_SUPPORTED;
2173 }
2174 if (psa_get_key_bits(attributes) == 0) {
2175 return PSA_ERROR_NOT_SUPPORTED;
2176 }
Gilles Peskined7729582019-08-05 15:55:54 +02002177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2179 &slot, &driver);
2180 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002181 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 }
Gilles Peskined7729582019-08-05 15:55:54 +02002183
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002185
2186exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 if (status != PSA_SUCCESS) {
2188 psa_fail_key_creation(slot, driver);
2189 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002190
Gilles Peskined7729582019-08-05 15:55:54 +02002191 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 psa_close_key(key);
2193 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002194}
2195#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2196
Gilles Peskine449bd832023-01-11 14:50:10 +01002197psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2198 const psa_key_attributes_t *specified_attributes,
2199 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002200{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002201 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002202 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002203 psa_key_slot_t *source_slot = NULL;
2204 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002205 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002206 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302207 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002208
Ronald Cron81709fc2020-11-14 12:10:32 +01002209 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2210
Archana8a180362021-07-05 02:18:48 +05302211 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2213 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002214 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 status = psa_validate_optional_attributes(source_slot,
2218 specified_attributes);
2219 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002220 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002222
Archana9d17bf42021-09-10 06:22:44 +05302223 /* The target key type and number of bits have been validated by
2224 * psa_validate_optional_attributes() to be either equal to zero or
2225 * equal to the ones of the source key. So it is safe to inherit
2226 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302227 * */
Archana9d17bf42021-09-10 06:22:44 +05302228 actual_attributes.core.bits = source_slot->attr.bits;
2229 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302230
2231
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 status = psa_restrict_key_policy(source_slot->attr.type,
2233 &actual_attributes.core.policy,
2234 &source_slot->attr.policy);
2235 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002236 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002238
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2240 &target_slot, &driver);
2241 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002242 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 }
2244 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2245 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302246 /*
Archana9d17bf42021-09-10 06:22:44 +05302247 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302248 * the source key would need to be exported as plaintext and re-imported
2249 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302250 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302251 * appropriate API invocations from the application, if needed.
2252 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002253 status = PSA_ERROR_NOT_SUPPORTED;
2254 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002255 }
Archana8a180362021-07-05 02:18:48 +05302256 /*
2257 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302258 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302259 * - For opaque keys this translates to an invocation of the drivers'
2260 * copy_key entry point through the dispatch layer.
2261 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2263 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2264 &storage_size);
2265 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302266 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 }
Archana374fe5b2021-09-08 15:50:28 +05302268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2270 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302271 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 }
Archana374fe5b2021-09-08 15:50:28 +05302273
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 status = psa_driver_wrapper_copy_key(&actual_attributes,
2275 source_slot->key.data,
2276 source_slot->key.bytes,
2277 target_slot->key.data,
2278 target_slot->key.bytes,
2279 &target_slot->key.bytes);
2280 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302281 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 }
2283 } else {
2284 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302285 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 source_slot->key.bytes);
2287 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302288 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 }
Archana8a180362021-07-05 02:18:48 +05302290 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002292exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 if (status != PSA_SUCCESS) {
2294 psa_fail_key_creation(target_slot, driver);
2295 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002300}
2301
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002302
2303
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002304/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002305/* Message digests */
2306/****************************************************************/
2307
Gilles Peskine449bd832023-01-11 14:50:10 +01002308psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002309{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002310 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 if (operation->id == 0) {
2312 return PSA_SUCCESS;
2313 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002314
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002316 operation->id = 0;
2317
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002319}
2320
Gilles Peskine449bd832023-01-11 14:50:10 +01002321psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2322 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002323{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002324 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002325
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002326 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002328 status = PSA_ERROR_BAD_STATE;
2329 goto exit;
2330 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002333 status = PSA_ERROR_INVALID_ARGUMENT;
2334 goto exit;
2335 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002336
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002337 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2338 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002342
2343exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 if (status != PSA_SUCCESS) {
2345 psa_hash_abort(operation);
2346 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002347
2348 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002349}
2350
Gilles Peskine449bd832023-01-11 14:50:10 +01002351psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2352 const uint8_t *input,
2353 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002354{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002355 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002358 status = PSA_ERROR_BAD_STATE;
2359 goto exit;
2360 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002361
Steven Cooremanc8288352021-03-04 14:02:19 +01002362 /* Don't require hash implementations to behave correctly on a
2363 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 if (input_length == 0) {
2365 return PSA_SUCCESS;
2366 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002369
2370exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 if (status != PSA_SUCCESS) {
2372 psa_hash_abort(operation);
2373 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002374
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002376}
2377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2379 uint8_t *hash,
2380 size_t hash_size,
2381 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002382{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002383 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 if (operation->id == 0) {
2385 return PSA_ERROR_BAD_STATE;
2386 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002387
Steven Cooreman1e582352021-02-18 17:24:37 +01002388 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 operation, hash, hash_size, hash_length);
2390 psa_hash_abort(operation);
2391 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002392}
2393
Gilles Peskine449bd832023-01-11 14:50:10 +01002394psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2395 const uint8_t *hash,
2396 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002397{
Ronald Cron69a63422021-10-18 09:47:58 +02002398 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002399 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002400 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 operation,
2402 actual_hash, sizeof(actual_hash),
2403 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002404
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002406 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002408
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002410 status = PSA_ERROR_INVALID_SIGNATURE;
2411 goto exit;
2412 }
2413
Dave Rodgman78701152023-08-29 14:20:18 +01002414 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002415 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002417
2418exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2420 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002421 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002425}
2426
Gilles Peskine449bd832023-01-11 14:50:10 +01002427psa_status_t psa_hash_compute(psa_algorithm_t alg,
2428 const uint8_t *input, size_t input_length,
2429 uint8_t *hash, size_t hash_size,
2430 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002431{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002432 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 if (!PSA_ALG_IS_HASH(alg)) {
2434 return PSA_ERROR_INVALID_ARGUMENT;
2435 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2438 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002439}
2440
Gilles Peskine449bd832023-01-11 14:50:10 +01002441psa_status_t psa_hash_compare(psa_algorithm_t alg,
2442 const uint8_t *input, size_t input_length,
2443 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002444{
Ronald Cron69a63422021-10-18 09:47:58 +02002445 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002446 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002447
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 if (!PSA_ALG_IS_HASH(alg)) {
2449 return PSA_ERROR_INVALID_ARGUMENT;
2450 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002451
Steven Cooreman1e582352021-02-18 17:24:37 +01002452 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 alg, input, input_length,
2454 actual_hash, sizeof(actual_hash),
2455 &actual_hash_length);
2456 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002457 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 }
2459 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002460 status = PSA_ERROR_INVALID_SIGNATURE;
2461 goto exit;
2462 }
Dave Rodgman78701152023-08-29 14:20:18 +01002463 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002464 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002466
2467exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2469 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002470}
2471
Gilles Peskine449bd832023-01-11 14:50:10 +01002472psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2473 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002474{
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 if (source_operation->id == 0 ||
2476 target_operation->id != 0) {
2477 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002478 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002479
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2481 target_operation);
2482 if (status != PSA_SUCCESS) {
2483 psa_hash_abort(target_operation);
2484 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002487}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002488
2489
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002490/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002491/* MAC */
2492/****************************************************************/
2493
Gilles Peskine449bd832023-01-11 14:50:10 +01002494psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002495{
Steven Cooremane6804192021-03-19 18:28:56 +01002496 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 if (operation->id == 0) {
2498 return PSA_SUCCESS;
2499 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002500
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002502 operation->mac_size = 0;
2503 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002504 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002507}
2508
Ronald Cron2dff3b22021-06-17 16:33:22 +02002509static psa_status_t psa_mac_finalize_alg_and_key_validation(
2510 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002511 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002513{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002514 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 psa_key_type_t key_type = psa_get_key_type(attributes);
2516 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 if (!PSA_ALG_IS_MAC(alg)) {
2519 return PSA_ERROR_INVALID_ARGUMENT;
2520 }
Steven Cooremane6804192021-03-19 18:28:56 +01002521
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002522 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 status = psa_mac_key_can_do(alg, key_type);
2524 if (status != PSA_SUCCESS) {
2525 return status;
2526 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002527
Steven Cooremand1a68f12021-05-10 11:13:41 +02002528 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002530
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002532 /* A very short MAC is too short for security since it can be
2533 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2534 * so we make this our minimum, even though 32 bits is still
2535 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002537 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002538
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2540 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002541 /* It's impossible to "truncate" to a larger length than the full length
2542 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002544 }
2545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002547 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2548 * that is disabled in the compile-time configuration. The result can
2549 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2550 * configuration into account. In this case, force a return of
2551 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2552 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2553 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2554 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2555 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002557 }
2558
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002560}
2561
Gilles Peskine449bd832023-01-11 14:50:10 +01002562static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2563 mbedtls_svc_key_id_t key,
2564 psa_algorithm_t alg,
2565 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002566{
2567 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2568 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002569 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002570 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002571
2572 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002574 status = PSA_ERROR_BAD_STATE;
2575 goto exit;
2576 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002577
2578 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 key,
2580 &slot,
2581 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2582 alg);
2583 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002584 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002586
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002587 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002588 .core = slot->attr
2589 };
2590
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2592 &operation->mac_size);
2593 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002594 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002596
2597 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002598 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 if (is_sign) {
2600 status = psa_driver_wrapper_mac_sign_setup(operation,
2601 &attributes,
2602 slot->key.data,
2603 slot->key.bytes,
2604 alg);
2605 } else {
2606 status = psa_driver_wrapper_mac_verify_setup(operation,
2607 &attributes,
2608 slot->key.data,
2609 slot->key.bytes,
2610 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002611 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002612
Gilles Peskinefbfac682018-07-08 20:51:54 +02002613exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 if (status != PSA_SUCCESS) {
2615 psa_mac_abort(operation);
2616 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002619
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002621}
2622
Gilles Peskine449bd832023-01-11 14:50:10 +01002623psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2624 mbedtls_svc_key_id_t key,
2625 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002626{
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002628}
2629
Gilles Peskine449bd832023-01-11 14:50:10 +01002630psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2631 mbedtls_svc_key_id_t key,
2632 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002633{
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002635}
2636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2638 const uint8_t *input,
2639 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002640{
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 if (operation->id == 0) {
2642 return PSA_ERROR_BAD_STATE;
2643 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002644
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002645 /* Don't require hash implementations to behave correctly on a
2646 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 if (input_length == 0) {
2648 return PSA_SUCCESS;
2649 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002650
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2652 input, input_length);
2653 if (status != PSA_SUCCESS) {
2654 psa_mac_abort(operation);
2655 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002656
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002658}
2659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2661 uint8_t *mac,
2662 size_t mac_size,
2663 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002664{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002665 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2666 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002667
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002669 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002670 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002671 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002672
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002674 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002675 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002676 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002677
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002678 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2679 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002681 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002682 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002683 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002684
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002686 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002687 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002688 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002689
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 status = psa_driver_wrapper_mac_sign_finish(operation,
2691 mac, operation->mac_size,
2692 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002693
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002694exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002695 /* In case of success, set the potential excess room in the output buffer
2696 * to an invalid value, to avoid potentially leaking a longer MAC.
2697 * In case of error, set the output length and content to a safe default,
2698 * such that in case the caller misses an error check, the output would be
2699 * an unachievable MAC.
2700 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002702 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002703 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002704 }
mohammad16036df908f2018-04-02 08:34:15 -07002705
Paul Elliottdc42ca82023-02-24 18:11:59 +00002706 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002707
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002709
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002711}
2712
Gilles Peskine449bd832023-01-11 14:50:10 +01002713psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2714 const uint8_t *mac,
2715 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002716{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002717 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2718 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002721 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002722 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002723 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002724
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002726 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002727 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002728 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002729
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002731 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002732 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002733 }
2734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 status = psa_driver_wrapper_mac_verify_finish(operation,
2736 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002737
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002738exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002740
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002742}
2743
Gilles Peskine449bd832023-01-11 14:50:10 +01002744static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2745 psa_algorithm_t alg,
2746 const uint8_t *input,
2747 size_t input_length,
2748 uint8_t *mac,
2749 size_t mac_size,
2750 size_t *mac_length,
2751 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002752{
2753 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002754 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2755 psa_key_slot_t *slot;
2756 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002757 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002758
Ronald Cron51131b52021-06-17 17:17:20 +02002759 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002760 key,
2761 &slot,
2762 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2763 alg);
2764 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002765 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002767
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002768 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002769 .core = slot->attr
2770 };
2771
Gilles Peskine449bd832023-01-11 14:50:10 +01002772 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2773 &operation_mac_size);
2774 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002775 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002777
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002779 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002780 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002781 }
2782
2783 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 &attributes,
2785 slot->key.data, slot->key.bytes,
2786 alg,
2787 input, input_length,
2788 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002789
2790exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002791 /* In case of success, set the potential excess room in the output buffer
2792 * to an invalid value, to avoid potentially leaking a longer MAC.
2793 * In case of error, set the output length and content to a safe default,
2794 * such that in case the caller misses an error check, the output would be
2795 * an unachievable MAC.
2796 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002797 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002798 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002799 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002800 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002801
2802 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002803
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002805
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002807}
2808
Gilles Peskine449bd832023-01-11 14:50:10 +01002809psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002810 psa_algorithm_t alg,
2811 const uint8_t *input,
2812 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 uint8_t *mac,
2814 size_t mac_size,
2815 size_t *mac_length)
2816{
2817 return psa_mac_compute_internal(key, alg,
2818 input, input_length,
2819 mac, mac_size, mac_length, 1);
2820}
2821
2822psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2823 psa_algorithm_t alg,
2824 const uint8_t *input,
2825 size_t input_length,
2826 const uint8_t *mac,
2827 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002828{
2829 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002830 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2831 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002832
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 status = psa_mac_compute_internal(key, alg,
2834 input, input_length,
2835 actual_mac, sizeof(actual_mac),
2836 &actual_mac_length, 0);
2837 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002838 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002840
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002842 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002843 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002844 }
Dave Rodgman78701152023-08-29 14:20:18 +01002845 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002846 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002847 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002848 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002849
2850exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002852
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002854}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002855
Gilles Peskinee59236f2018-01-27 23:32:46 +01002856/****************************************************************/
2857/* Asymmetric cryptography */
2858/****************************************************************/
2859
Gilles Peskine449bd832023-01-11 14:50:10 +01002860static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2861 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002862{
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 if (input_is_message) {
2864 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2865 return PSA_ERROR_INVALID_ARGUMENT;
2866 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002867
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2869 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2870 return PSA_ERROR_INVALID_ARGUMENT;
2871 }
2872 }
2873 } else {
2874 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2875 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002876 }
2877 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002878
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002880}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002881
Gilles Peskine449bd832023-01-11 14:50:10 +01002882static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2883 int input_is_message,
2884 psa_algorithm_t alg,
2885 const uint8_t *input,
2886 size_t input_length,
2887 uint8_t *signature,
2888 size_t signature_size,
2889 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002890{
2891 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2892 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2893 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002894 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002895
2896 *signature_length = 0;
2897
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 status = psa_sign_verify_check_alg(input_is_message, alg);
2899 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002900 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002902
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002903 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002904 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002905 * buffer can in principle be empty since it doesn't actually have
2906 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 if (signature_size == 0) {
2908 return PSA_ERROR_BUFFER_TOO_SMALL;
2909 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002910
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002911 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 key, &slot,
2913 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2914 PSA_KEY_USAGE_SIGN_HASH,
2915 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002916
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002918 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002920
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002922 status = PSA_ERROR_INVALID_ARGUMENT;
2923 goto exit;
2924 }
2925
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002926 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002928 };
2929
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002931 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002932 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002933 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 signature, signature_size, signature_length);
2935 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002936
2937 status = psa_driver_wrapper_sign_hash(
2938 &attributes, slot->key.data, slot->key.bytes,
2939 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002941 }
2942
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002943
2944exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002945 psa_wipe_tag_output_buffer(signature, status, signature_size,
2946 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002947
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002949
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002951}
2952
Gilles Peskine449bd832023-01-11 14:50:10 +01002953static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2954 int input_is_message,
2955 psa_algorithm_t alg,
2956 const uint8_t *input,
2957 size_t input_length,
2958 const uint8_t *signature,
2959 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002960{
2961 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2962 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2963 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002964
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 status = psa_sign_verify_check_alg(input_is_message, alg);
2966 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002967 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002969
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002970 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 key, &slot,
2972 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2973 PSA_KEY_USAGE_VERIFY_HASH,
2974 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002975
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 if (status != PSA_SUCCESS) {
2977 return status;
2978 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002979
2980 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002982 };
2983
Gilles Peskine449bd832023-01-11 14:50:10 +01002984 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002985 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002986 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002987 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 signature, signature_length);
2989 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002990 status = psa_driver_wrapper_verify_hash(
2991 &attributes, slot->key.data, slot->key.bytes,
2992 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002994 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002995
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002997
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002999
3000}
3001
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003002psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003003 const psa_key_attributes_t *attributes,
3004 const uint8_t *key_buffer,
3005 size_t key_buffer_size,
3006 psa_algorithm_t alg,
3007 const uint8_t *input,
3008 size_t input_length,
3009 uint8_t *signature,
3010 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003012{
3013 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003014
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003016 size_t hash_length;
3017 uint8_t hash[PSA_HASH_MAX_SIZE];
3018
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003019 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 PSA_ALG_SIGN_GET_HASH(alg),
3021 input, input_length,
3022 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003023
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003025 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003027
3028 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 attributes, key_buffer, key_buffer_size,
3030 alg, hash, hash_length,
3031 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003032 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003033
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003035}
3036
Gilles Peskine449bd832023-01-11 14:50:10 +01003037psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3038 psa_algorithm_t alg,
3039 const uint8_t *input,
3040 size_t input_length,
3041 uint8_t *signature,
3042 size_t signature_size,
3043 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003044{
3045 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003046 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003048}
3049
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003050psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003051 const psa_key_attributes_t *attributes,
3052 const uint8_t *key_buffer,
3053 size_t key_buffer_size,
3054 psa_algorithm_t alg,
3055 const uint8_t *input,
3056 size_t input_length,
3057 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003059{
3060 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003061
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003063 size_t hash_length;
3064 uint8_t hash[PSA_HASH_MAX_SIZE];
3065
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003066 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 PSA_ALG_SIGN_GET_HASH(alg),
3068 input, input_length,
3069 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003070
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003072 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003074
3075 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 attributes, key_buffer, key_buffer_size,
3077 alg, hash, hash_length,
3078 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003079 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003080
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003082}
3083
Gilles Peskine449bd832023-01-11 14:50:10 +01003084psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3085 psa_algorithm_t alg,
3086 const uint8_t *input,
3087 size_t input_length,
3088 const uint8_t *signature,
3089 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003090{
3091 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003092 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003094}
3095
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003096psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003097 const psa_key_attributes_t *attributes,
3098 const uint8_t *key_buffer, size_t key_buffer_size,
3099 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003101{
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3103 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3104 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003105#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3107 return mbedtls_psa_rsa_sign_hash(
3108 attributes,
3109 key_buffer, key_buffer_size,
3110 alg, hash, hash_length,
3111 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003112#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3113 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 } else {
3115 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003116 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3118 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003119#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3121 return mbedtls_psa_ecdsa_sign_hash(
3122 attributes,
3123 key_buffer, key_buffer_size,
3124 alg, hash, hash_length,
3125 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003126#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3127 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 } else {
3129 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003130 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003131 }
Ronald Cron4501c982021-03-19 20:09:32 +01003132
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 (void) key_buffer;
3134 (void) key_buffer_size;
3135 (void) hash;
3136 (void) hash_length;
3137 (void) signature;
3138 (void) signature_size;
3139 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003140
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003142}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003143
Gilles Peskine449bd832023-01-11 14:50:10 +01003144psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3145 psa_algorithm_t alg,
3146 const uint8_t *hash,
3147 size_t hash_length,
3148 uint8_t *signature,
3149 size_t signature_size,
3150 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003151{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003152 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003153 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003155}
3156
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003157psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003158 const psa_key_attributes_t *attributes,
3159 const uint8_t *key_buffer, size_t key_buffer_size,
3160 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003162{
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3164 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3165 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003166#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3168 return mbedtls_psa_rsa_verify_hash(
3169 attributes,
3170 key_buffer, key_buffer_size,
3171 alg, hash, hash_length,
3172 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003173#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3174 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 } else {
3176 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003177 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3179 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003180#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3182 return mbedtls_psa_ecdsa_verify_hash(
3183 attributes,
3184 key_buffer, key_buffer_size,
3185 alg, hash, hash_length,
3186 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003187#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3188 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 } else {
3190 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003191 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003192 }
Ronald Cron4501c982021-03-19 20:09:32 +01003193
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 (void) key_buffer;
3195 (void) key_buffer_size;
3196 (void) hash;
3197 (void) hash_length;
3198 (void) signature;
3199 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003200
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003202}
3203
Gilles Peskine449bd832023-01-11 14:50:10 +01003204psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3205 psa_algorithm_t alg,
3206 const uint8_t *hash,
3207 size_t hash_length,
3208 const uint8_t *signature,
3209 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003210{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003211 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003212 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003213 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003214}
3215
Gilles Peskine449bd832023-01-11 14:50:10 +01003216psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3217 psa_algorithm_t alg,
3218 const uint8_t *input,
3219 size_t input_length,
3220 const uint8_t *salt,
3221 size_t salt_length,
3222 uint8_t *output,
3223 size_t output_size,
3224 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003225{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003226 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003227 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003228 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003229 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003230
Darryl Green5cc689a2018-07-24 15:34:10 +01003231 (void) input;
3232 (void) input_length;
3233 (void) salt;
3234 (void) output;
3235 (void) output_size;
3236
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003237 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003238
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3240 return PSA_ERROR_INVALID_ARGUMENT;
3241 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003242
Ronald Cron5c522922020-11-14 16:35:34 +01003243 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003244 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3245 if (status != PSA_SUCCESS) {
3246 return status;
3247 }
3248 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3249 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003250 status = PSA_ERROR_INVALID_ARGUMENT;
3251 goto exit;
3252 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003253
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003254 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003255 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003256 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003257
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003258 status = psa_driver_wrapper_asymmetric_encrypt(
3259 &attributes, slot->key.data, slot->key.bytes,
3260 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003262exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003264
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003266}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003267
Gilles Peskine449bd832023-01-11 14:50:10 +01003268psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3269 psa_algorithm_t alg,
3270 const uint8_t *input,
3271 size_t input_length,
3272 const uint8_t *salt,
3273 size_t salt_length,
3274 uint8_t *output,
3275 size_t output_size,
3276 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003277{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003278 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003279 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003280 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003281 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003282
Darryl Green5cc689a2018-07-24 15:34:10 +01003283 (void) input;
3284 (void) input_length;
3285 (void) salt;
3286 (void) output;
3287 (void) output_size;
3288
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003289 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003290
Gilles Peskine449bd832023-01-11 14:50:10 +01003291 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3292 return PSA_ERROR_INVALID_ARGUMENT;
3293 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003294
Ronald Cron5c522922020-11-14 16:35:34 +01003295 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3297 if (status != PSA_SUCCESS) {
3298 return status;
3299 }
3300 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003301 status = PSA_ERROR_INVALID_ARGUMENT;
3302 goto exit;
3303 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003304
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003305 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003307 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003308
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003309 status = psa_driver_wrapper_asymmetric_decrypt(
3310 &attributes, slot->key.data, slot->key.bytes,
3311 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003312 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003313
3314exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003315 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003316
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003318}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003319
Paul Elliott9fe12f62022-11-30 19:16:02 +00003320/****************************************************************/
3321/* Asymmetric interruptible cryptography */
3322/****************************************************************/
3323
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003324static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3325
Paul Elliott9fe12f62022-11-30 19:16:02 +00003326void psa_interruptible_set_max_ops(uint32_t max_ops)
3327{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003328 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003329}
3330
3331uint32_t psa_interruptible_get_max_ops(void)
3332{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003333 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003334}
3335
Paul Elliott9fe12f62022-11-30 19:16:02 +00003336uint32_t psa_sign_hash_get_num_ops(
3337 const psa_sign_hash_interruptible_operation_t *operation)
3338{
Paul Elliott296ede92022-12-15 17:00:30 +00003339 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003340}
3341
3342uint32_t psa_verify_hash_get_num_ops(
3343 const psa_verify_hash_interruptible_operation_t *operation)
3344{
Paul Elliott296ede92022-12-15 17:00:30 +00003345 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003346}
3347
Paul Elliott59ad9452022-12-18 15:09:02 +00003348static psa_status_t psa_sign_hash_abort_internal(
3349 psa_sign_hash_interruptible_operation_t *operation)
3350{
3351 if (operation->id == 0) {
3352 /* The object has (apparently) been initialized but it is not (yet)
3353 * in use. It's ok to call abort on such an object, and there's
3354 * nothing to do. */
3355 return PSA_SUCCESS;
3356 }
3357
3358 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3359
3360 status = psa_driver_wrapper_sign_hash_abort(operation);
3361
3362 operation->id = 0;
3363
Paul Elliotte6145dc2023-02-07 12:51:21 +00003364 /* Do not clear either the error_occurred or num_ops elements here as they
3365 * only want to be cleared by the application calling abort, not by abort
3366 * being called at completion of an operation. */
3367
Paul Elliott59ad9452022-12-18 15:09:02 +00003368 return status;
3369}
3370
Paul Elliott9fe12f62022-11-30 19:16:02 +00003371psa_status_t psa_sign_hash_start(
3372 psa_sign_hash_interruptible_operation_t *operation,
3373 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3374 const uint8_t *hash, size_t hash_length)
3375{
3376 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3377 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3378 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003379 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003380
Paul Elliottc9774412023-02-06 15:14:07 +00003381 /* Check that start has not been previously called, or operation has not
3382 * previously errored. */
3383 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003384 return PSA_ERROR_BAD_STATE;
3385 }
3386
Paul Elliott9fe12f62022-11-30 19:16:02 +00003387 status = psa_sign_verify_check_alg(0, alg);
3388 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003389 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003390 return status;
3391 }
3392
3393 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3394 PSA_KEY_USAGE_SIGN_HASH,
3395 alg);
3396
3397 if (status != PSA_SUCCESS) {
3398 goto exit;
3399 }
3400
3401 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3402 status = PSA_ERROR_INVALID_ARGUMENT;
3403 goto exit;
3404 }
3405
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003406 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003407 .core = slot->attr
3408 };
3409
Paul Elliott296ede92022-12-15 17:00:30 +00003410 /* Ensure ops count gets reset, in case of operation re-use. */
3411 operation->num_ops = 0;
3412
Paul Elliott9fe12f62022-11-30 19:16:02 +00003413 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3414 slot->key.data,
3415 slot->key.bytes, alg,
3416 hash, hash_length);
3417exit:
3418
3419 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003420 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003421 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003422 }
3423
3424 unlock_status = psa_unlock_key_slot(slot);
3425
Paul Elliottc9774412023-02-06 15:14:07 +00003426 if (unlock_status != PSA_SUCCESS) {
3427 operation->error_occurred = 1;
3428 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003429
Paul Elliottc9774412023-02-06 15:14:07 +00003430 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003431}
3432
3433
3434psa_status_t psa_sign_hash_complete(
3435 psa_sign_hash_interruptible_operation_t *operation,
3436 uint8_t *signature, size_t signature_size,
3437 size_t *signature_length)
3438{
3439 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3440
3441 *signature_length = 0;
3442
Paul Elliottc9774412023-02-06 15:14:07 +00003443 /* Check that start has been called first, and that operation has not
3444 * previously errored. */
3445 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003446 status = PSA_ERROR_BAD_STATE;
3447 goto exit;
3448 }
3449
Paul Elliott096abc42023-02-03 18:33:23 +00003450 /* Immediately reject a zero-length signature buffer. This guarantees that
3451 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003452 if (signature_size == 0) {
3453 status = PSA_ERROR_BUFFER_TOO_SMALL;
3454 goto exit;
3455 }
3456
3457 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3458 signature_size,
3459 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003460
Paul Elliott296ede92022-12-15 17:00:30 +00003461 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003462 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003463
Paul Elliottebe225c2023-02-10 14:32:53 +00003464exit:
3465
Paul Elliott59200a22023-02-21 15:07:40 +00003466 psa_wipe_tag_output_buffer(signature, status, signature_size,
3467 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003468
Paul Elliott53bb3122023-02-10 14:22:22 +00003469 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003470 if (status != PSA_SUCCESS) {
3471 operation->error_occurred = 1;
3472 }
3473
Paul Elliott59ad9452022-12-18 15:09:02 +00003474 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003475 }
3476
3477 return status;
3478}
3479
3480psa_status_t psa_sign_hash_abort(
3481 psa_sign_hash_interruptible_operation_t *operation)
3482{
Paul Elliott59ad9452022-12-18 15:09:02 +00003483 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3484
3485 status = psa_sign_hash_abort_internal(operation);
3486
3487 /* We clear the number of ops done here, so that it is not cleared when
3488 * the operation fails or succeeds, only on manual abort. */
3489 operation->num_ops = 0;
3490
Paul Elliottc9774412023-02-06 15:14:07 +00003491 /* Likewise, failure state. */
3492 operation->error_occurred = 0;
3493
Paul Elliott59ad9452022-12-18 15:09:02 +00003494 return status;
3495}
3496
3497static psa_status_t psa_verify_hash_abort_internal(
3498 psa_verify_hash_interruptible_operation_t *operation)
3499{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003500 if (operation->id == 0) {
3501 /* The object has (apparently) been initialized but it is not (yet)
3502 * in use. It's ok to call abort on such an object, and there's
3503 * nothing to do. */
3504 return PSA_SUCCESS;
3505 }
3506
Paul Elliott59ad9452022-12-18 15:09:02 +00003507 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3508
3509 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003510
3511 operation->id = 0;
3512
Paul Elliotte6145dc2023-02-07 12:51:21 +00003513 /* Do not clear either the error_occurred or num_ops elements here as they
3514 * only want to be cleared by the application calling abort, not by abort
3515 * being called at completion of an operation. */
3516
Paul Elliott59ad9452022-12-18 15:09:02 +00003517 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003518}
3519
3520psa_status_t psa_verify_hash_start(
3521 psa_verify_hash_interruptible_operation_t *operation,
3522 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3523 const uint8_t *hash, size_t hash_length,
3524 const uint8_t *signature, size_t signature_length)
3525{
3526 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3527 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3528 psa_key_slot_t *slot;
3529
Paul Elliottc9774412023-02-06 15:14:07 +00003530 /* Check that start has not been previously called, or operation has not
3531 * previously errored. */
3532 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003533 return PSA_ERROR_BAD_STATE;
3534 }
3535
3536 status = psa_sign_verify_check_alg(0, alg);
3537 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003538 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003539 return status;
3540 }
3541
3542 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3543 PSA_KEY_USAGE_VERIFY_HASH,
3544 alg);
3545
3546 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003547 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003548 return status;
3549 }
3550
3551 psa_key_attributes_t attributes = {
3552 .core = slot->attr
3553 };
3554
Paul Elliott296ede92022-12-15 17:00:30 +00003555 /* Ensure ops count gets reset, in case of operation re-use. */
3556 operation->num_ops = 0;
3557
Paul Elliott9fe12f62022-11-30 19:16:02 +00003558 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3559 slot->key.data,
3560 slot->key.bytes,
3561 alg, hash, hash_length,
3562 signature, signature_length);
3563
3564 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003565 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003566 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003567 }
3568
3569 unlock_status = psa_unlock_key_slot(slot);
3570
Paul Elliottc9774412023-02-06 15:14:07 +00003571 if (unlock_status != PSA_SUCCESS) {
3572 operation->error_occurred = 1;
3573 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003574
Paul Elliottc9774412023-02-06 15:14:07 +00003575 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003576}
3577
3578psa_status_t psa_verify_hash_complete(
3579 psa_verify_hash_interruptible_operation_t *operation)
3580{
3581 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3582
Paul Elliottc9774412023-02-06 15:14:07 +00003583 /* Check that start has been called first, and that operation has not
3584 * previously errored. */
3585 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003586 status = PSA_ERROR_BAD_STATE;
3587 goto exit;
3588 }
3589
3590 status = psa_driver_wrapper_verify_hash_complete(operation);
3591
Paul Elliott296ede92022-12-15 17:00:30 +00003592 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003593 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003594 operation);
3595
Paul Elliottebe225c2023-02-10 14:32:53 +00003596exit:
3597
Paul Elliott9fe12f62022-11-30 19:16:02 +00003598 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003599 if (status != PSA_SUCCESS) {
3600 operation->error_occurred = 1;
3601 }
3602
Paul Elliott59ad9452022-12-18 15:09:02 +00003603 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003604 }
3605
3606 return status;
3607}
3608
3609psa_status_t psa_verify_hash_abort(
3610 psa_verify_hash_interruptible_operation_t *operation)
3611{
Paul Elliott59ad9452022-12-18 15:09:02 +00003612 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003613
Paul Elliott59ad9452022-12-18 15:09:02 +00003614 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003615
Paul Elliott59ad9452022-12-18 15:09:02 +00003616 /* We clear the number of ops done here, so that it is not cleared when
3617 * the operation fails or succeeds, only on manual abort. */
3618 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003619
Paul Elliottc9774412023-02-06 15:14:07 +00003620 /* Likewise, failure state. */
3621 operation->error_occurred = 0;
3622
Paul Elliott59ad9452022-12-18 15:09:02 +00003623 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003624}
3625
Paul Elliott588f8ed2022-12-02 18:10:26 +00003626/****************************************************************/
3627/* Asymmetric interruptible cryptography internal */
3628/* implementations */
3629/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003630
Paul Elliott588f8ed2022-12-02 18:10:26 +00003631void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3632{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003633
Paul Elliott588f8ed2022-12-02 18:10:26 +00003634#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3635 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3636 defined(MBEDTLS_ECP_RESTARTABLE)
3637
3638 /* Internal implementation uses zero to indicate infinite number max ops,
3639 * therefore avoid this value, and set to minimum possible. */
3640 if (max_ops == 0) {
3641 max_ops = 1;
3642 }
3643
Paul Elliott588f8ed2022-12-02 18:10:26 +00003644 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003645#else
3646 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003647#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3648 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3649 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3650}
3651
Paul Elliott588f8ed2022-12-02 18:10:26 +00003652uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003653 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003654{
3655#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3656 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3657 defined(MBEDTLS_ECP_RESTARTABLE)
3658
Paul Elliott93d9ca82023-02-15 18:14:21 +00003659 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003660#else
3661 (void) operation;
3662 return 0;
3663#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3664 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3665 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3666}
3667
3668uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003669 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003670{
3671 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3672 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3673 defined(MBEDTLS_ECP_RESTARTABLE)
3674
Paul Elliott93d9ca82023-02-15 18:14:21 +00003675 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003676#else
3677 (void) operation;
3678 return 0;
3679#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3680 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3681 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3682}
3683
3684psa_status_t mbedtls_psa_sign_hash_start(
3685 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3686 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3687 size_t key_buffer_size, psa_algorithm_t alg,
3688 const uint8_t *hash, size_t hash_length)
3689{
3690 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003691 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003692
Paul Elliott068fe072023-01-16 13:59:15 +00003693 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3694 return PSA_ERROR_NOT_SUPPORTED;
3695 }
3696
3697 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003698 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003699 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003700
3701#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003702 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3703 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003704
Paul Elliotta1c94092023-02-15 16:38:04 +00003705 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3706
Paul Elliott93d9ca82023-02-15 18:14:21 +00003707 /* Ensure num_ops is zero'ed in case of context re-use. */
3708 operation->num_ops = 0;
3709
Paul Elliott068fe072023-01-16 13:59:15 +00003710 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3711 attributes->core.bits,
3712 key_buffer,
3713 key_buffer_size,
3714 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003715
Paul Elliott068fe072023-01-16 13:59:15 +00003716 if (status != PSA_SUCCESS) {
3717 return status;
3718 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003719
Paul Elliott1bc59df2023-02-05 13:41:57 +00003720 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003721 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003722
Paul Elliott068fe072023-01-16 13:59:15 +00003723 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003724 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003725 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003726
Paul Elliott0290a762023-02-09 14:30:24 +00003727 /* We only need to store the same length of hash as the private key size
3728 * here, it would be truncated by the internal implementation anyway. */
3729 required_hash_length = (hash_length < operation->coordinate_bytes ?
3730 hash_length : operation->coordinate_bytes);
3731
Paul Elliottba70ad42023-02-15 18:23:53 +00003732 if (required_hash_length > sizeof(operation->hash)) {
3733 /* Shouldn't happen, but better safe than sorry. */
3734 return PSA_ERROR_CORRUPTION_DETECTED;
3735 }
3736
Paul Elliott0290a762023-02-09 14:30:24 +00003737 memcpy(operation->hash, hash, required_hash_length);
3738 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003739
3740 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003741
3742#else
Paul Elliott068fe072023-01-16 13:59:15 +00003743 (void) operation;
3744 (void) key_buffer;
3745 (void) key_buffer_size;
3746 (void) alg;
3747 (void) hash;
3748 (void) hash_length;
3749 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003750 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003751
Paul Elliott068fe072023-01-16 13:59:15 +00003752 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003753#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3754 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3755 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003756}
3757
3758psa_status_t mbedtls_psa_sign_hash_complete(
3759 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3760 uint8_t *signature, size_t signature_size,
3761 size_t *signature_length)
3762{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003763#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3764 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3765 defined(MBEDTLS_ECP_RESTARTABLE)
3766
Paul Elliotta1c94092023-02-15 16:38:04 +00003767 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003768 mbedtls_mpi r;
3769 mbedtls_mpi s;
3770
Paul Elliott46845252023-02-03 14:59:11 +00003771 mbedtls_mpi_init(&r);
3772 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003773
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003774 /* Ensure max_ops is set to the current value (or default). */
3775 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3776
Paul Elliotta1c94092023-02-15 16:38:04 +00003777 if (signature_size < 2 * operation->coordinate_bytes) {
3778 status = PSA_ERROR_BUFFER_TOO_SMALL;
3779 goto exit;
3780 }
3781
Paul Elliott588f8ed2022-12-02 18:10:26 +00003782 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003783
Paul Elliott588f8ed2022-12-02 18:10:26 +00003784#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3785 status = mbedtls_to_psa_error(
3786 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003787 &r,
3788 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003789 &operation->ctx->d,
3790 operation->hash,
3791 operation->hash_length,
3792 operation->md_alg,
3793 mbedtls_psa_get_random,
3794 MBEDTLS_PSA_RANDOM_STATE,
3795 &operation->restart_ctx));
3796#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003797 status = PSA_ERROR_NOT_SUPPORTED;
3798 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003799#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3800 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003801 status = mbedtls_to_psa_error(
3802 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003803 &r,
3804 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003805 &operation->ctx->d,
3806 operation->hash,
3807 operation->hash_length,
3808 mbedtls_psa_get_random,
3809 MBEDTLS_PSA_RANDOM_STATE,
3810 mbedtls_psa_get_random,
3811 MBEDTLS_PSA_RANDOM_STATE,
3812 &operation->restart_ctx));
3813 }
3814
Paul Elliottf8e5b562023-02-19 18:43:45 +00003815 /* Hide the fact that the restart context only holds a delta of number of
3816 * ops done during the last operation, not an absolute value. */
3817 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3818
Paul Elliott724bd252023-02-08 12:35:08 +00003819 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003820 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003821 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003822 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003823 operation->coordinate_bytes)
3824 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003825
3826 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003827 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003828 }
3829
3830 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003831 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003832 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003833 operation->coordinate_bytes,
3834 operation->coordinate_bytes)
3835 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003836
3837 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003838 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003839 }
3840
Paul Elliott1bc59df2023-02-05 13:41:57 +00003841 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003842
Paul Elliott724bd252023-02-08 12:35:08 +00003843 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003844 }
Paul Elliott724bd252023-02-08 12:35:08 +00003845
3846exit:
3847
3848 mbedtls_mpi_free(&r);
3849 mbedtls_mpi_free(&s);
3850 return status;
3851
Paul Elliott588f8ed2022-12-02 18:10:26 +00003852 #else
3853
3854 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003855 (void) signature;
3856 (void) signature_size;
3857 (void) signature_length;
3858
3859 return PSA_ERROR_NOT_SUPPORTED;
3860
3861#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3862 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3863 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3864}
3865
3866psa_status_t mbedtls_psa_sign_hash_abort(
3867 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3868{
3869
3870#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3871 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3872 defined(MBEDTLS_ECP_RESTARTABLE)
3873
3874 if (operation->ctx) {
3875 mbedtls_ecdsa_free(operation->ctx);
3876 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003877 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003878 }
3879
3880 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3881
Paul Elliott93d9ca82023-02-15 18:14:21 +00003882 operation->num_ops = 0;
3883
Paul Elliott588f8ed2022-12-02 18:10:26 +00003884 return PSA_SUCCESS;
3885
3886#else
3887
3888 (void) operation;
3889
3890 return PSA_ERROR_NOT_SUPPORTED;
3891
3892#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3893 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3894 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3895}
3896
3897psa_status_t mbedtls_psa_verify_hash_start(
3898 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3899 const psa_key_attributes_t *attributes,
3900 const uint8_t *key_buffer, size_t key_buffer_size,
3901 psa_algorithm_t alg,
3902 const uint8_t *hash, size_t hash_length,
3903 const uint8_t *signature, size_t signature_length)
3904{
3905 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003906 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003907 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003908
Paul Elliott068fe072023-01-16 13:59:15 +00003909 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3910 return PSA_ERROR_NOT_SUPPORTED;
3911 }
3912
3913 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003914 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003915 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003916
3917#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003918 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3919 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003920
Paul Elliotta1c94092023-02-15 16:38:04 +00003921 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3922 mbedtls_mpi_init(&operation->r);
3923 mbedtls_mpi_init(&operation->s);
3924
Paul Elliott93d9ca82023-02-15 18:14:21 +00003925 /* Ensure num_ops is zero'ed in case of context re-use. */
3926 operation->num_ops = 0;
3927
Paul Elliott068fe072023-01-16 13:59:15 +00003928 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3929 attributes->core.bits,
3930 key_buffer,
3931 key_buffer_size,
3932 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003933
Paul Elliott068fe072023-01-16 13:59:15 +00003934 if (status != PSA_SUCCESS) {
3935 return status;
3936 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003937
Paul Elliottc569fc22023-02-10 13:02:54 +00003938 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003939
Paul Elliott1bc59df2023-02-05 13:41:57 +00003940 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003941 return PSA_ERROR_INVALID_SIGNATURE;
3942 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003943
Paul Elliott068fe072023-01-16 13:59:15 +00003944 status = mbedtls_to_psa_error(
3945 mbedtls_mpi_read_binary(&operation->r,
3946 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003947 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003948
Paul Elliott068fe072023-01-16 13:59:15 +00003949 if (status != PSA_SUCCESS) {
3950 return status;
3951 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003952
Paul Elliott068fe072023-01-16 13:59:15 +00003953 status = mbedtls_to_psa_error(
3954 mbedtls_mpi_read_binary(&operation->s,
3955 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003956 coordinate_bytes,
3957 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003958
Paul Elliott068fe072023-01-16 13:59:15 +00003959 if (status != PSA_SUCCESS) {
3960 return status;
3961 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003962
Paul Elliott2c9843f2023-02-15 17:32:42 +00003963 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003964
Paul Elliott2c9843f2023-02-15 17:32:42 +00003965 if (status != PSA_SUCCESS) {
3966 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003967 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003968
Paul Elliott0290a762023-02-09 14:30:24 +00003969 /* We only need to store the same length of hash as the private key size
3970 * here, it would be truncated by the internal implementation anyway. */
3971 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3972 coordinate_bytes);
3973
Paul Elliottba70ad42023-02-15 18:23:53 +00003974 if (required_hash_length > sizeof(operation->hash)) {
3975 /* Shouldn't happen, but better safe than sorry. */
3976 return PSA_ERROR_CORRUPTION_DETECTED;
3977 }
3978
Paul Elliott0290a762023-02-09 14:30:24 +00003979 memcpy(operation->hash, hash, required_hash_length);
3980 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003981
3982 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003983#else
Paul Elliott068fe072023-01-16 13:59:15 +00003984 (void) operation;
3985 (void) key_buffer;
3986 (void) key_buffer_size;
3987 (void) alg;
3988 (void) hash;
3989 (void) hash_length;
3990 (void) signature;
3991 (void) signature_length;
3992 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003993 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003994 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003995
Paul Elliott068fe072023-01-16 13:59:15 +00003996 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003997#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3998 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3999 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004000}
4001
4002psa_status_t mbedtls_psa_verify_hash_complete(
4003 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4004{
4005
4006#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4007 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4008 defined(MBEDTLS_ECP_RESTARTABLE)
4009
Paul Elliottf8e5b562023-02-19 18:43:45 +00004010 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4011
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004012 /* Ensure max_ops is set to the current value (or default). */
4013 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4014
Paul Elliottf8e5b562023-02-19 18:43:45 +00004015 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004016 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4017 operation->hash,
4018 operation->hash_length,
4019 &operation->ctx->Q,
4020 &operation->r,
4021 &operation->s,
4022 &operation->restart_ctx));
4023
Paul Elliottf8e5b562023-02-19 18:43:45 +00004024 /* Hide the fact that the restart context only holds a delta of number of
4025 * ops done during the last operation, not an absolute value. */
4026 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4027
4028 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004029#else
4030 (void) operation;
4031
4032 return PSA_ERROR_NOT_SUPPORTED;
4033
4034#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4035 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4036 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4037}
4038
4039psa_status_t mbedtls_psa_verify_hash_abort(
4040 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4041{
4042
4043#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4044 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4045 defined(MBEDTLS_ECP_RESTARTABLE)
4046
4047 if (operation->ctx) {
4048 mbedtls_ecdsa_free(operation->ctx);
4049 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004050 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004051 }
4052
4053 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4054
Paul Elliott93d9ca82023-02-15 18:14:21 +00004055 operation->num_ops = 0;
4056
Paul Elliott588f8ed2022-12-02 18:10:26 +00004057 mbedtls_mpi_free(&operation->r);
4058 mbedtls_mpi_free(&operation->s);
4059
4060 return PSA_SUCCESS;
4061
4062#else
4063 (void) operation;
4064
4065 return PSA_ERROR_NOT_SUPPORTED;
4066
4067#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4068 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4069 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4070}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004071
mohammad1603503973b2018-03-12 15:59:30 +02004072/****************************************************************/
4073/* Symmetric cryptography */
4074/****************************************************************/
4075
Gilles Peskine449bd832023-01-11 14:50:10 +01004076static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4077 mbedtls_svc_key_id_t key,
4078 psa_algorithm_t alg,
4079 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004080{
4081 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4082 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004083 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004084 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4085 PSA_KEY_USAGE_ENCRYPT :
4086 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004087 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004088
4089 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004090 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004091 status = PSA_ERROR_BAD_STATE;
4092 goto exit;
4093 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004094
Gilles Peskine449bd832023-01-11 14:50:10 +01004095 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004096 status = PSA_ERROR_INVALID_ARGUMENT;
4097 goto exit;
4098 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004099
Gilles Peskine449bd832023-01-11 14:50:10 +01004100 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4101 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004102 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004103 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004104
Ronald Cron49fafa92021-03-10 08:34:23 +01004105 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004106 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004107 * so we only set it (in the driver wrapper) after resources have been
4108 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004109 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004110 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004111 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004112 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004113 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 }
4115 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004116
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004117 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004119 };
4120
Ronald Cronab99ac22020-10-01 16:38:28 +02004121 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 if (cipher_operation == MBEDTLS_ENCRYPT) {
4123 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4124 &attributes,
4125 slot->key.data,
4126 slot->key.bytes,
4127 alg);
4128 } else {
4129 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4130 &attributes,
4131 slot->key.data,
4132 slot->key.bytes,
4133 alg);
4134 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004135
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004136exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004137 if (status != PSA_SUCCESS) {
4138 psa_cipher_abort(operation);
4139 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004140
Gilles Peskine449bd832023-01-11 14:50:10 +01004141 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004142
Gilles Peskine449bd832023-01-11 14:50:10 +01004143 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004144}
4145
Gilles Peskine449bd832023-01-11 14:50:10 +01004146psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4147 mbedtls_svc_key_id_t key,
4148 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004149{
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004151}
4152
Gilles Peskine449bd832023-01-11 14:50:10 +01004153psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4154 mbedtls_svc_key_id_t key,
4155 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004156{
Gilles Peskine449bd832023-01-11 14:50:10 +01004157 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004158}
4159
Gilles Peskine449bd832023-01-11 14:50:10 +01004160psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4161 uint8_t *iv,
4162 size_t iv_size,
4163 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004164{
Ronald Cron6d051732020-10-01 14:10:20 +02004165 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004166 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004167 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004168
Gilles Peskine449bd832023-01-11 14:50:10 +01004169 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004170 status = PSA_ERROR_BAD_STATE;
4171 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004172 }
4173
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004175 status = PSA_ERROR_BAD_STATE;
4176 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004177 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004178
Ronald Cron2fb90522021-07-05 12:31:44 +02004179 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004180 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004181 status = PSA_ERROR_BUFFER_TOO_SMALL;
4182 goto exit;
4183 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004184
Gilles Peskine449bd832023-01-11 14:50:10 +01004185 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004186 status = PSA_ERROR_GENERIC_ERROR;
4187 goto exit;
4188 }
4189
Gilles Peskine449bd832023-01-11 14:50:10 +01004190 status = psa_generate_random(local_iv, default_iv_length);
4191 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004192 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004193 }
Ronald Cron5618a392021-03-26 09:52:26 +01004194
Gilles Peskine449bd832023-01-11 14:50:10 +01004195 status = psa_driver_wrapper_cipher_set_iv(operation,
4196 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004197
4198exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 if (status == PSA_SUCCESS) {
4200 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004201 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004202 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004204 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004205 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004206 }
Ronald Cron6d051732020-10-01 14:10:20 +02004207
Gilles Peskine449bd832023-01-11 14:50:10 +01004208 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004209}
4210
Gilles Peskine449bd832023-01-11 14:50:10 +01004211psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4212 const uint8_t *iv,
4213 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004214{
Ronald Cron6d051732020-10-01 14:10:20 +02004215 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004216
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004218 status = PSA_ERROR_BAD_STATE;
4219 goto exit;
4220 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004221
Gilles Peskine449bd832023-01-11 14:50:10 +01004222 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004223 status = PSA_ERROR_BAD_STATE;
4224 goto exit;
4225 }
Ronald Crona0d68172021-03-26 10:15:08 +01004226
Gilles Peskine449bd832023-01-11 14:50:10 +01004227 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004228 status = PSA_ERROR_INVALID_ARGUMENT;
4229 goto exit;
4230 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004231
Gilles Peskine449bd832023-01-11 14:50:10 +01004232 status = psa_driver_wrapper_cipher_set_iv(operation,
4233 iv,
4234 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004235
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004236exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004238 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004239 } else {
4240 psa_cipher_abort(operation);
4241 }
4242 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004243}
4244
Gilles Peskine449bd832023-01-11 14:50:10 +01004245psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4246 const uint8_t *input,
4247 size_t input_length,
4248 uint8_t *output,
4249 size_t output_size,
4250 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004251{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004252 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004253
Gilles Peskine449bd832023-01-11 14:50:10 +01004254 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004255 status = PSA_ERROR_BAD_STATE;
4256 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004257 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004258
Gilles Peskine449bd832023-01-11 14:50:10 +01004259 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004260 status = PSA_ERROR_BAD_STATE;
4261 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004262 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004263
Gilles Peskine449bd832023-01-11 14:50:10 +01004264 status = psa_driver_wrapper_cipher_update(operation,
4265 input,
4266 input_length,
4267 output,
4268 output_size,
4269 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004270
4271exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004272 if (status != PSA_SUCCESS) {
4273 psa_cipher_abort(operation);
4274 }
Ronald Cron6d051732020-10-01 14:10:20 +02004275
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004277}
4278
Gilles Peskine449bd832023-01-11 14:50:10 +01004279psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4280 uint8_t *output,
4281 size_t output_size,
4282 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004283{
David Saadab4ecc272019-02-14 13:48:10 +02004284 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004285
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004287 status = PSA_ERROR_BAD_STATE;
4288 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004289 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004290
Gilles Peskine449bd832023-01-11 14:50:10 +01004291 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004292 status = PSA_ERROR_BAD_STATE;
4293 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004294 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004295
Gilles Peskine449bd832023-01-11 14:50:10 +01004296 status = psa_driver_wrapper_cipher_finish(operation,
4297 output,
4298 output_size,
4299 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004300
4301exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004302 if (status == PSA_SUCCESS) {
4303 return psa_cipher_abort(operation);
4304 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004305 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004306 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004307
Gilles Peskine449bd832023-01-11 14:50:10 +01004308 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004309 }
mohammad1603503973b2018-03-12 15:59:30 +02004310}
4311
Gilles Peskine449bd832023-01-11 14:50:10 +01004312psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004313{
Gilles Peskine449bd832023-01-11 14:50:10 +01004314 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004315 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004316 * in use. It's ok to call abort on such an object, and there's
4317 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004319 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004320
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004322
Ronald Cron49fafa92021-03-10 08:34:23 +01004323 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004324 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004325 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004326
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004328}
4329
Gilles Peskine449bd832023-01-11 14:50:10 +01004330psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4331 psa_algorithm_t alg,
4332 const uint8_t *input,
4333 size_t input_length,
4334 uint8_t *output,
4335 size_t output_size,
4336 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004337{
4338 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004339 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004340 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004341 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4342 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004343 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004344
David Horstmannc6977b42023-11-27 17:43:05 +00004345 psa_crypto_local_input_t local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
4346 status = psa_crypto_local_input_alloc(input, input_length, &local_input);
4347 if (status != PSA_SUCCESS) {
4348 goto exit;
4349 }
4350 input = local_input.buffer;
4351
4352 psa_crypto_local_output_t local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
4353 status = psa_crypto_local_output_alloc(output, output_size, &local_output);
4354 if (status != PSA_SUCCESS) {
4355 goto exit;
4356 }
4357 output = local_output.buffer;
4358
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004360 status = PSA_ERROR_INVALID_ARGUMENT;
4361 goto exit;
4362 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004363
Gilles Peskine449bd832023-01-11 14:50:10 +01004364 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4365 PSA_KEY_USAGE_ENCRYPT,
4366 alg);
4367 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004368 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004369 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004370
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004371 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004372 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004373 };
4374
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4376 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004377 status = PSA_ERROR_GENERIC_ERROR;
4378 goto exit;
4379 }
4380
Gilles Peskine449bd832023-01-11 14:50:10 +01004381 if (default_iv_length > 0) {
4382 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004383 status = PSA_ERROR_BUFFER_TOO_SMALL;
4384 goto exit;
4385 }
4386
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 status = psa_generate_random(local_iv, default_iv_length);
4388 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004389 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004391 }
4392
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004393 status = psa_driver_wrapper_cipher_encrypt(
4394 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004395 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004396 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004398
4399exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 unlock_status = psa_unlock_key_slot(slot);
4401 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004402 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004403 }
Ronald Cron23919522021-07-08 19:00:07 +02004404
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 if (status == PSA_SUCCESS) {
4406 if (default_iv_length > 0) {
4407 memcpy(output, local_iv, default_iv_length);
4408 }
4409 *output_length += default_iv_length;
4410 } else {
4411 *output_length = 0;
4412 }
4413
David Horstmannc6977b42023-11-27 17:43:05 +00004414 psa_crypto_local_input_free(&local_input);
4415 psa_crypto_local_output_free(&local_output);
4416
Gilles Peskine449bd832023-01-11 14:50:10 +01004417 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004418}
4419
Gilles Peskine449bd832023-01-11 14:50:10 +01004420psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4421 psa_algorithm_t alg,
4422 const uint8_t *input,
4423 size_t input_length,
4424 uint8_t *output,
4425 size_t output_size,
4426 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004427{
4428 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004429 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004430 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004431 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004432
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004434 status = PSA_ERROR_INVALID_ARGUMENT;
4435 goto exit;
4436 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004437
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4439 PSA_KEY_USAGE_DECRYPT,
4440 alg);
4441 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004442 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004443 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004444
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004445 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004447 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004448
Gilles Peskine449bd832023-01-11 14:50:10 +01004449 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4450 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004451 status = PSA_ERROR_INVALID_ARGUMENT;
4452 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004454 status = PSA_ERROR_INVALID_ARGUMENT;
4455 goto exit;
4456 }
4457
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004458 status = psa_driver_wrapper_cipher_decrypt(
4459 &attributes, slot->key.data, slot->key.bytes,
4460 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004462
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004463exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 unlock_status = psa_unlock_key_slot(slot);
4465 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004466 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004467 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004468
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004470 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 }
Ronald Cron23919522021-07-08 19:00:07 +02004472
Gilles Peskine449bd832023-01-11 14:50:10 +01004473 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004474}
4475
4476
mohammad16035955c982018-04-26 00:53:03 +03004477/****************************************************************/
4478/* AEAD */
4479/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004480
Paul Elliottbaff51c2021-09-28 17:44:45 +01004481/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004482static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004483{
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004485}
4486
4487/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004488static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4489 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004490{
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004492
Gilles Peskine449bd832023-01-11 14:50:10 +01004493 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004494#if defined(PSA_WANT_ALG_GCM)
4495 case PSA_ALG_GCM:
4496 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 * arbitrarily large nonces. Please note that we do not generally
4498 * recommend the usage of nonces of greater length than
4499 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4500 * size, which can then lead to collisions if you encrypt a very
4501 * large number of messages.*/
4502 if (nonce_length != 0) {
4503 return PSA_SUCCESS;
4504 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004505 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004506#endif /* PSA_WANT_ALG_GCM */
4507#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004508 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 if (nonce_length >= 7 && nonce_length <= 13) {
4510 return PSA_SUCCESS;
4511 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004512 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004513#endif /* PSA_WANT_ALG_CCM */
4514#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004515 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 if (nonce_length == 12) {
4517 return PSA_SUCCESS;
4518 } else if (nonce_length == 8) {
4519 return PSA_ERROR_NOT_SUPPORTED;
4520 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004521 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004522#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004523 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004524 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004525 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004526 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004527
Gilles Peskine449bd832023-01-11 14:50:10 +01004528 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004529}
4530
Gilles Peskine449bd832023-01-11 14:50:10 +01004531static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004532{
Gilles Peskine449bd832023-01-11 14:50:10 +01004533 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4534 return PSA_ERROR_INVALID_ARGUMENT;
4535 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004536
Gilles Peskine449bd832023-01-11 14:50:10 +01004537 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004538}
4539
Gilles Peskine449bd832023-01-11 14:50:10 +01004540psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4541 psa_algorithm_t alg,
4542 const uint8_t *nonce,
4543 size_t nonce_length,
4544 const uint8_t *additional_data,
4545 size_t additional_data_length,
4546 const uint8_t *plaintext,
4547 size_t plaintext_length,
4548 uint8_t *ciphertext,
4549 size_t ciphertext_size,
4550 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004551{
Ronald Cron215633c2021-03-16 17:15:37 +01004552 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4553 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004554
mohammad1603f08a5502018-06-03 15:05:47 +03004555 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004556
Gilles Peskine449bd832023-01-11 14:50:10 +01004557 status = psa_aead_check_algorithm(alg);
4558 if (status != PSA_SUCCESS) {
4559 return status;
4560 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004561
Ronald Cron9a986162021-03-26 12:40:07 +01004562 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4564 if (status != PSA_SUCCESS) {
4565 return status;
4566 }
mohammad16035955c982018-04-26 00:53:03 +03004567
Ronald Cron215633c2021-03-16 17:15:37 +01004568 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004570 };
mohammad16035955c982018-04-26 00:53:03 +03004571
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 status = psa_aead_check_nonce_length(alg, nonce_length);
4573 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004574 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004576
Ronald Cronde822812021-03-17 16:08:20 +01004577 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004578 &attributes, slot->key.data, slot->key.bytes,
4579 alg,
4580 nonce, nonce_length,
4581 additional_data, additional_data_length,
4582 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004584
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4586 memset(ciphertext, 0, ciphertext_size);
4587 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004588
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004589exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004591
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004593}
4594
Gilles Peskine449bd832023-01-11 14:50:10 +01004595psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4596 psa_algorithm_t alg,
4597 const uint8_t *nonce,
4598 size_t nonce_length,
4599 const uint8_t *additional_data,
4600 size_t additional_data_length,
4601 const uint8_t *ciphertext,
4602 size_t ciphertext_length,
4603 uint8_t *plaintext,
4604 size_t plaintext_size,
4605 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004606{
Ronald Cron215633c2021-03-16 17:15:37 +01004607 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4608 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004609
Gilles Peskineee652a32018-06-01 19:23:52 +02004610 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004611
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 status = psa_aead_check_algorithm(alg);
4613 if (status != PSA_SUCCESS) {
4614 return status;
4615 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004616
Ronald Cron9a986162021-03-26 12:40:07 +01004617 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4619 if (status != PSA_SUCCESS) {
4620 return status;
4621 }
mohammad16035955c982018-04-26 00:53:03 +03004622
Ronald Cron215633c2021-03-16 17:15:37 +01004623 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004625 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004626
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 status = psa_aead_check_nonce_length(alg, nonce_length);
4628 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004629 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004631
Ronald Cronde822812021-03-17 16:08:20 +01004632 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004633 &attributes, slot->key.data, slot->key.bytes,
4634 alg,
4635 nonce, nonce_length,
4636 additional_data, additional_data_length,
4637 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004638 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004639
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 if (status != PSA_SUCCESS && plaintext_size != 0) {
4641 memset(plaintext, 0, plaintext_size);
4642 }
mohammad160360a64d02018-06-03 17:20:42 +03004643
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004644exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004645 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004646
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 return status;
mohammad16035955c982018-04-26 00:53:03 +03004648}
4649
Gilles Peskine449bd832023-01-11 14:50:10 +01004650static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4651{
4652 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004653
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004655#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004656 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004657 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
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_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004663
Przemek Stekiel86679c72022-10-06 17:06:56 +02004664#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004665 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004666 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4668 return PSA_ERROR_INVALID_ARGUMENT;
4669 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004670 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004671#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004672
Przemek Stekiel86679c72022-10-06 17:06:56 +02004673#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004674 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004675 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 if (tag_len != 16) {
4677 return PSA_ERROR_INVALID_ARGUMENT;
4678 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004679 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004680#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004681
4682 default:
4683 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004685 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004686 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004687}
4688
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004689/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004690static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4691 int is_encrypt,
4692 mbedtls_svc_key_id_t key,
4693 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004694{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004695 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4696 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4697 psa_key_slot_t *slot = NULL;
4698 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004699 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004700
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 status = psa_aead_check_algorithm(alg);
4702 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004703 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004705
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004707 status = PSA_ERROR_BAD_STATE;
4708 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004709 }
4710
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 if (operation->nonce_set || operation->lengths_set ||
4712 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004713 status = PSA_ERROR_BAD_STATE;
4714 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004715 }
4716
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004718 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004719 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004720 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004722
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4724 alg);
4725 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004726 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004728
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004729 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004730 .core = slot->attr
4731 };
4732
Gilles Peskine449bd832023-01-11 14:50:10 +01004733 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004734 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004736
Gilles Peskine449bd832023-01-11 14:50:10 +01004737 if (is_encrypt) {
4738 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4739 &attributes,
4740 slot->key.data,
4741 slot->key.bytes,
4742 alg);
4743 } else {
4744 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4745 &attributes,
4746 slot->key.data,
4747 slot->key.bytes,
4748 alg);
4749 }
4750 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004751 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004753
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004755
4756exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004757 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004758
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004760 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004762 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 } else {
4764 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004765 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004766
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004768}
4769
Paul Elliott302ff6b2021-04-20 18:10:30 +01004770/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004771psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4772 mbedtls_svc_key_id_t key,
4773 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004774{
Gilles Peskine449bd832023-01-11 14:50:10 +01004775 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004776}
4777
4778/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004779psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4780 mbedtls_svc_key_id_t key,
4781 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004782{
Gilles Peskine449bd832023-01-11 14:50:10 +01004783 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004784}
4785
4786/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004787psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4788 uint8_t *nonce,
4789 size_t nonce_size,
4790 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004791{
4792 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004793 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004794 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004795
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004796 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004797
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004799 status = PSA_ERROR_BAD_STATE;
4800 goto exit;
4801 }
4802
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004804 status = PSA_ERROR_BAD_STATE;
4805 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004806 }
4807
Paul Elliotte193ea82021-10-01 13:00:16 +01004808 /* For CCM, this size may not be correct according to the PSA
4809 * specification. The PSA Crypto 1.0.1 specification states:
4810 *
4811 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4812 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4813 *
4814 * However this restriction that L has to be the smallest integer is not
4815 * applied in practice, and it is not implementable here since the
4816 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4818 operation->alg);
4819 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004820 status = PSA_ERROR_BUFFER_TOO_SMALL;
4821 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004822 }
4823
Gilles Peskine449bd832023-01-11 14:50:10 +01004824 status = psa_generate_random(local_nonce, required_nonce_size);
4825 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004826 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004828
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004830
Paul Elliott39dc6b82021-05-11 19:16:09 +01004831exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 if (status == PSA_SUCCESS) {
4833 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004834 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 } else {
4836 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004837 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004838
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004840}
4841
4842/* Set the nonce for a multipart authenticated encryption or decryption
4843 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004844psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4845 const uint8_t *nonce,
4846 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004847{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004848 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4849
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004851 status = PSA_ERROR_BAD_STATE;
4852 goto exit;
4853 }
4854
Gilles Peskine449bd832023-01-11 14:50:10 +01004855 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004856 status = PSA_ERROR_BAD_STATE;
4857 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004858 }
4859
Gilles Peskine449bd832023-01-11 14:50:10 +01004860 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4861 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004862 status = PSA_ERROR_INVALID_ARGUMENT;
4863 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004864 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004865
Gilles Peskine449bd832023-01-11 14:50:10 +01004866 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4867 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004868
Paul Elliott39dc6b82021-05-11 19:16:09 +01004869exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004870 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004871 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 } else {
4873 psa_aead_abort(operation);
4874 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004875
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004877}
4878
4879/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004880psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4881 size_t ad_length,
4882 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004883{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004884 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4885
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004887 status = PSA_ERROR_BAD_STATE;
4888 goto exit;
4889 }
4890
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 if (operation->lengths_set || operation->ad_started ||
4892 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004893 status = PSA_ERROR_BAD_STATE;
4894 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004895 }
4896
Gilles Peskine449bd832023-01-11 14:50:10 +01004897 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004898#if defined(PSA_WANT_ALG_GCM)
4899 case PSA_ALG_GCM:
4900 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 * bits. Without the guard this code will generate warnings on 32bit
4902 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004903#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004904 if (((uint64_t) ad_length) >> 61 != 0 ||
4905 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004906 status = PSA_ERROR_INVALID_ARGUMENT;
4907 goto exit;
4908 }
Paul Elliott325d3742021-09-27 17:56:28 +01004909#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004910 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004911#endif /* PSA_WANT_ALG_GCM */
4912#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004913 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004914 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004915 status = PSA_ERROR_INVALID_ARGUMENT;
4916 goto exit;
4917 }
4918 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004919#endif /* PSA_WANT_ALG_CCM */
4920#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004921 case PSA_ALG_CHACHA20_POLY1305:
4922 /* No length restrictions for ChaChaPoly. */
4923 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004924#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004925 default:
4926 break;
4927 }
Paul Elliott325d3742021-09-27 17:56:28 +01004928
Gilles Peskine449bd832023-01-11 14:50:10 +01004929 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4930 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004931
Paul Elliott39dc6b82021-05-11 19:16:09 +01004932exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004933 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004934 operation->ad_remaining = ad_length;
4935 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004936 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004937 } else {
4938 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004939 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004940
Gilles Peskine449bd832023-01-11 14:50:10 +01004941 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004942}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004943
4944/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004945psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4946 const uint8_t *input,
4947 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004948{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004949 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4950
Gilles Peskine449bd832023-01-11 14:50:10 +01004951 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004952 status = PSA_ERROR_BAD_STATE;
4953 goto exit;
4954 }
4955
Gilles Peskine449bd832023-01-11 14:50:10 +01004956 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004957 status = PSA_ERROR_BAD_STATE;
4958 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004959 }
4960
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 if (operation->lengths_set) {
4962 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004963 status = PSA_ERROR_INVALID_ARGUMENT;
4964 goto exit;
4965 }
4966
4967 operation->ad_remaining -= input_length;
4968 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004969#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004971 status = PSA_ERROR_BAD_STATE;
4972 goto exit;
4973 }
4974#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004975
Gilles Peskine449bd832023-01-11 14:50:10 +01004976 status = psa_driver_wrapper_aead_update_ad(operation, input,
4977 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004978
Paul Elliott39dc6b82021-05-11 19:16:09 +01004979exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004980 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004981 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 } else {
4983 psa_aead_abort(operation);
4984 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004985
Gilles Peskine449bd832023-01-11 14:50:10 +01004986 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004987}
4988
4989/* Encrypt or decrypt a message fragment in an active multipart AEAD
4990 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004991psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4992 const uint8_t *input,
4993 size_t input_length,
4994 uint8_t *output,
4995 size_t output_size,
4996 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004997{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004998 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004999
5000 *output_length = 0;
5001
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005003 status = PSA_ERROR_BAD_STATE;
5004 goto exit;
5005 }
5006
Gilles Peskine449bd832023-01-11 14:50:10 +01005007 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005008 status = PSA_ERROR_BAD_STATE;
5009 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005010 }
5011
Gilles Peskine449bd832023-01-11 14:50:10 +01005012 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005013 /* Additional data length was supplied, but not all the additional
5014 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005015 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005016 status = PSA_ERROR_INVALID_ARGUMENT;
5017 goto exit;
5018 }
5019
5020 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005022 status = PSA_ERROR_INVALID_ARGUMENT;
5023 goto exit;
5024 }
5025
5026 operation->body_remaining -= input_length;
5027 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005028#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005029 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005030 status = PSA_ERROR_BAD_STATE;
5031 goto exit;
5032 }
5033#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005034
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5036 output, output_size,
5037 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005038
Paul Elliott39dc6b82021-05-11 19:16:09 +01005039exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005040 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005041 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005042 } else {
5043 psa_aead_abort(operation);
5044 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005045
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005047}
5048
Gilles Peskine449bd832023-01-11 14:50:10 +01005049static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005050{
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 if (operation->id == 0 || !operation->nonce_set) {
5052 return PSA_ERROR_BAD_STATE;
5053 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005054
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5056 operation->body_remaining != 0)) {
5057 return PSA_ERROR_INVALID_ARGUMENT;
5058 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005059
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005061}
5062
Paul Elliott302ff6b2021-04-20 18:10:30 +01005063/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005064psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5065 uint8_t *ciphertext,
5066 size_t ciphertext_size,
5067 size_t *ciphertext_length,
5068 uint8_t *tag,
5069 size_t tag_size,
5070 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005071{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005072 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5073
Paul Elliott302ff6b2021-04-20 18:10:30 +01005074 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005075 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005076
Gilles Peskine449bd832023-01-11 14:50:10 +01005077 status = psa_aead_final_checks(operation);
5078 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005079 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005081
Gilles Peskine449bd832023-01-11 14:50:10 +01005082 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005083 status = PSA_ERROR_BAD_STATE;
5084 goto exit;
5085 }
5086
Gilles Peskine449bd832023-01-11 14:50:10 +01005087 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5088 ciphertext_size,
5089 ciphertext_length,
5090 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005091
Paul Elliott39dc6b82021-05-11 19:16:09 +01005092exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005093
5094
Paul Elliottf47b0952021-05-21 18:02:33 +01005095 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005096 * the zero tag size, make sure the tag is set to something implausible.
5097 * Even if the operation succeeds, make sure we clear the rest of the
5098 * buffer to prevent potential leakage of anything previously placed in
5099 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005100 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005101
Gilles Peskine449bd832023-01-11 14:50:10 +01005102 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005103
Gilles Peskine449bd832023-01-11 14:50:10 +01005104 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005105}
5106
5107/* Finish authenticating and decrypting a message in a multipart AEAD
5108 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005109psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5110 uint8_t *plaintext,
5111 size_t plaintext_size,
5112 size_t *plaintext_length,
5113 const uint8_t *tag,
5114 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005115{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005116 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5117
Paul Elliott302ff6b2021-04-20 18:10:30 +01005118 *plaintext_length = 0;
5119
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 status = psa_aead_final_checks(operation);
5121 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005122 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005123 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005124
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005126 status = PSA_ERROR_BAD_STATE;
5127 goto exit;
5128 }
5129
Gilles Peskine449bd832023-01-11 14:50:10 +01005130 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5131 plaintext_size,
5132 plaintext_length,
5133 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005134
5135exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005137
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005139}
5140
5141/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005142psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005143{
5144 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5145
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005147 /* The object has (apparently) been initialized but it is not (yet)
5148 * in use. It's ok to call abort on such an object, and there's
5149 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005151 }
5152
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005154
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005156
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005158}
5159
Gilles Peskinee59236f2018-01-27 23:32:46 +01005160/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005161/* Generators */
5162/****************************************************************/
5163
Przemek Stekiel69c46792022-06-10 12:59:51 +02005164#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005165 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005166 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305167 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305168 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005169#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005170#endif /* At least one builtin KDF */
5171
Przemek Stekiel69c46792022-06-10 12:59:51 +02005172#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005173 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5174 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5175static psa_status_t psa_key_derivation_start_hmac(
5176 psa_mac_operation_t *operation,
5177 psa_algorithm_t hash_alg,
5178 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005180{
5181 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5182 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5184 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5185 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005186
Steven Cooreman72f736a2021-05-07 14:14:37 +02005187 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005189
Gilles Peskine449bd832023-01-11 14:50:10 +01005190 status = psa_driver_wrapper_mac_sign_setup(operation,
5191 &attributes,
5192 hmac_key, hmac_key_length,
5193 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005194
Gilles Peskine449bd832023-01-11 14:50:10 +01005195 psa_reset_key_attributes(&attributes);
5196 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005197}
5198#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005199
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005200#define HKDF_STATE_INIT 0 /* no input yet */
5201#define HKDF_STATE_STARTED 1 /* got salt */
5202#define HKDF_STATE_KEYED 2 /* got key */
5203#define HKDF_STATE_OUTPUT 3 /* output started */
5204
Gilles Peskinecbe66502019-05-16 16:59:18 +02005205static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005207{
Gilles Peskine449bd832023-01-11 14:50:10 +01005208 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5209 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5210 } else {
5211 return operation->alg;
5212 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005213}
5214
Gilles Peskine449bd832023-01-11 14:50:10 +01005215psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005216{
5217 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5219 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005220 /* The object has (apparently) been initialized but it is not
5221 * in use. It's ok to call abort on such an object, and there's
5222 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005223 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005224#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005225 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5226 mbedtls_free(operation->ctx.hkdf.info);
5227 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5228 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005229#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005230#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5231 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5233 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5234 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5235 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005236 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005238 }
5239
Gilles Peskine449bd832023-01-11 14:50:10 +01005240 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005241 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005243 }
5244
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005246 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005247 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005248 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005249#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005251 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005252 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005253 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005254#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005255 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005256
5257 /* We leave the fields Ai and output_block to be erased safely by the
5258 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005260#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5261 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005262#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5264 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5265 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5266 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005267#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305268#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305269 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305270 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005271 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305272 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305273 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305274
5275 status = PSA_SUCCESS;
5276 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305277#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005278 {
5279 status = PSA_ERROR_BAD_STATE;
5280 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 mbedtls_platform_zeroize(operation, sizeof(*operation));
5282 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005283}
5284
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005285psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005287{
Gilles Peskine449bd832023-01-11 14:50:10 +01005288 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005289 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005290 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005291 }
5292
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005293 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005295}
5296
Gilles Peskine449bd832023-01-11 14:50:10 +01005297psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5298 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005299{
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 if (operation->alg == 0) {
5301 return PSA_ERROR_BAD_STATE;
5302 }
5303 if (capacity > operation->capacity) {
5304 return PSA_ERROR_INVALID_ARGUMENT;
5305 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005306 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005307 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005308}
5309
Przemek Stekiel69c46792022-06-10 12:59:51 +02005310#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005311/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005312static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5313 psa_algorithm_t kdf_alg,
5314 uint8_t *output,
5315 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005316{
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5318 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005319 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005320 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005321#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005323#else
5324 const uint8_t last_block = 0xff;
5325#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005326
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 if (hkdf->state < HKDF_STATE_KEYED ||
5328 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005329#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005331#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005332 )) {
5333 return PSA_ERROR_BAD_STATE;
5334 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005335 hkdf->state = HKDF_STATE_OUTPUT;
5336
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005338 /* Copy what remains of the current block */
5339 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005340 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005341 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 }
5343 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005344 output += n;
5345 output_length -= n;
5346 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005347 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005348 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005349 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005350 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005351 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005352 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005353 * object was corrupted or if this function is called directly
5354 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 if (hkdf->block_number == last_block) {
5356 return PSA_ERROR_BAD_STATE;
5357 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005358
5359 /* We need a new block */
5360 ++hkdf->block_number;
5361 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005362
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5364 hash_alg,
5365 hkdf->prk,
5366 hash_length);
5367 if (status != PSA_SUCCESS) {
5368 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005369 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005370
5371 if (hkdf->block_number != 1) {
5372 status = psa_mac_update(&hkdf->hmac,
5373 hkdf->output_block,
5374 hash_length);
5375 if (status != PSA_SUCCESS) {
5376 return status;
5377 }
5378 }
5379 status = psa_mac_update(&hkdf->hmac,
5380 hkdf->info,
5381 hkdf->info_length);
5382 if (status != PSA_SUCCESS) {
5383 return status;
5384 }
5385 status = psa_mac_update(&hkdf->hmac,
5386 &hkdf->block_number, 1);
5387 if (status != PSA_SUCCESS) {
5388 return status;
5389 }
5390 status = psa_mac_sign_finish(&hkdf->hmac,
5391 hkdf->output_block,
5392 sizeof(hkdf->output_block),
5393 &hmac_output_length);
5394 if (status != PSA_SUCCESS) {
5395 return status;
5396 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005397 }
5398
Gilles Peskine449bd832023-01-11 14:50:10 +01005399 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005400}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005401#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005402
John Durkop07cc04a2020-11-16 22:08:34 -08005403#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5404 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005405static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5406 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005408{
Gilles Peskine449bd832023-01-11 14:50:10 +01005409 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5410 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005411 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5412 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005413 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005414
Janos Follath7742fee2019-06-17 12:58:10 +01005415 /* We can't be wanting more output after block 0xff, otherwise
5416 * the capacity check in psa_key_derivation_output_bytes() would have
5417 * prevented this call. It could happen only if the operation
5418 * object was corrupted or if this function is called directly
5419 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005420 if (tls12_prf->block_number == 0xff) {
5421 return PSA_ERROR_CORRUPTION_DETECTED;
5422 }
Janos Follath7742fee2019-06-17 12:58:10 +01005423
5424 /* We need a new block */
5425 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005426 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005427
5428 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5429 *
5430 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5431 *
5432 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5433 * HMAC_hash(secret, A(2) + seed) +
5434 * HMAC_hash(secret, A(3) + seed) + ...
5435 *
5436 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005437 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005438 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005439 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005440 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005441 * is currently extracted as `output_block` and where i is
5442 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005443 */
5444
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 status = psa_key_derivation_start_hmac(&hmac,
5446 hash_alg,
5447 tls12_prf->secret,
5448 tls12_prf->secret_length);
5449 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005450 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005452
5453 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005455 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5456 * the variable seed and in this instance means it in the context of the
5457 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 status = psa_mac_update(&hmac,
5459 tls12_prf->label,
5460 tls12_prf->label_length);
5461 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005462 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005463 }
5464 status = psa_mac_update(&hmac,
5465 tls12_prf->seed,
5466 tls12_prf->seed_length);
5467 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005468 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 }
5470 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005471 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5473 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005474 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005476 }
5477
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 status = psa_mac_sign_finish(&hmac,
5479 tls12_prf->Ai, hash_length,
5480 &hmac_output_length);
5481 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005482 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 }
5484 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005485 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005487
5488 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 status = psa_key_derivation_start_hmac(&hmac,
5490 hash_alg,
5491 tls12_prf->secret,
5492 tls12_prf->secret_length);
5493 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005494 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 }
5496 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5497 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005498 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 }
5500 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5501 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005502 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 }
5504 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5505 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005506 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 }
5508 status = psa_mac_sign_finish(&hmac,
5509 tls12_prf->output_block, hash_length,
5510 &hmac_output_length);
5511 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005512 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005514
Janos Follath7742fee2019-06-17 12:58:10 +01005515
5516cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 cleanup_status = psa_mac_abort(&hmac);
5518 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005519 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005521
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005523}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005524
Janos Follath844eb0e2019-06-19 12:10:49 +01005525static psa_status_t psa_key_derivation_tls12_prf_read(
5526 psa_tls12_prf_key_derivation_t *tls12_prf,
5527 psa_algorithm_t alg,
5528 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005530{
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5532 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005533 psa_status_t status;
5534 uint8_t offset, length;
5535
Gilles Peskine449bd832023-01-11 14:50:10 +01005536 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005537 case PSA_TLS12_PRF_STATE_LABEL_SET:
5538 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5539 break;
5540 case PSA_TLS12_PRF_STATE_OUTPUT:
5541 break;
5542 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005544 }
5545
Gilles Peskine449bd832023-01-11 14:50:10 +01005546 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005547 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 if (tls12_prf->left_in_block == 0) {
5549 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5550 alg);
5551 if (status != PSA_SUCCESS) {
5552 return status;
5553 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005554
5555 continue;
5556 }
5557
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005559 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005561 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005563
5564 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005566 output += length;
5567 output_length -= length;
5568 tls12_prf->left_in_block -= length;
5569 }
5570
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005572}
John Durkop07cc04a2020-11-16 22:08:34 -08005573#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5574 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005575
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005576#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5577static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5578 psa_tls12_ecjpake_to_pms_t *ecjpake,
5579 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005581{
5582 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005583 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005584
Gilles Peskine449bd832023-01-11 14:50:10 +01005585 if (output_length != 32) {
5586 return PSA_ERROR_INVALID_ARGUMENT;
5587 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005588
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5590 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5591 &output_size);
5592 if (status != PSA_SUCCESS) {
5593 return status;
5594 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005595
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 if (output_size != output_length) {
5597 return PSA_ERROR_GENERIC_ERROR;
5598 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005599
Gilles Peskine449bd832023-01-11 14:50:10 +01005600 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005601}
5602#endif
5603
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305604#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305605static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5606 psa_pbkdf2_key_derivation_t *pbkdf2,
5607 psa_algorithm_t prf_alg,
5608 uint8_t prf_output_length,
5609 psa_key_attributes_t *attributes)
5610{
5611 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305612 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305613 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305614 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305615 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305616 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305617 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305618
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305619 mac_operation.is_sign = 1;
5620 mac_operation.mac_size = prf_output_length;
5621 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305622
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305623 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5624 attributes,
5625 pbkdf2->password,
5626 pbkdf2->password_length,
5627 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305628 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305629 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305630 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305631 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5632 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305633 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305634 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305635 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305636 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305637 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305638 }
5639 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5640 &mac_output_length);
5641 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305642 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305643 }
5644
5645 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305646 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305647 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305648 }
5649
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305650 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305651
5652 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305653 /* We are passing prf_output_length as mac_size because the driver
5654 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305655 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305656 status = psa_driver_wrapper_mac_compute(attributes,
5657 pbkdf2->password,
5658 pbkdf2->password_length,
5659 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305660 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305661 &mac_output_length);
5662 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305663 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305664 }
5665
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305666 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305667 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305668
5669cleanup:
5670 /* Zeroise buffers to clear sensitive data from memory. */
5671 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5672 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305673}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305674
5675static psa_status_t psa_key_derivation_pbkdf2_read(
5676 psa_pbkdf2_key_derivation_t *pbkdf2,
5677 psa_algorithm_t kdf_alg,
5678 uint8_t *output,
5679 size_t output_length)
5680{
5681 psa_status_t status;
5682 psa_algorithm_t prf_alg;
5683 uint8_t prf_output_length;
5684 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5685 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5686 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5687
5688 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5689 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5690 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5691 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305692 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5693 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305694 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305695 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305696 } else {
5697 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305698 }
5699
5700 switch (pbkdf2->state) {
5701 case PSA_PBKDF2_STATE_PASSWORD_SET:
5702 /* Initially we need a new block so bytes_used is equal to block size*/
5703 pbkdf2->bytes_used = prf_output_length;
5704 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5705 break;
5706 case PSA_PBKDF2_STATE_OUTPUT:
5707 break;
5708 default:
5709 return PSA_ERROR_BAD_STATE;
5710 }
5711
5712 while (output_length != 0) {
5713 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5714 if (n > output_length) {
5715 n = (uint8_t) output_length;
5716 }
5717 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5718 output += n;
5719 output_length -= n;
5720 pbkdf2->bytes_used += n;
5721
5722 if (output_length == 0) {
5723 break;
5724 }
5725
5726 /* We need a new block */
5727 pbkdf2->bytes_used = 0;
5728 pbkdf2->block_number++;
5729
5730 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5731 prf_output_length,
5732 &attributes);
5733 if (status != PSA_SUCCESS) {
5734 return status;
5735 }
5736 }
5737
5738 return PSA_SUCCESS;
5739}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305740#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305741
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005742psa_status_t psa_key_derivation_output_bytes(
5743 psa_key_derivation_operation_t *operation,
5744 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005746{
5747 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005748 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005749
Gilles Peskine449bd832023-01-11 14:50:10 +01005750 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005751 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005753 }
5754
Gilles Peskine449bd832023-01-11 14:50:10 +01005755 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005756 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005757 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005758 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005759 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005760 goto exit;
5761 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005763 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005764 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005765 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5766 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005767 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005768 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005770 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005771 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005772
Przemek Stekiel69c46792022-06-10 12:59:51 +02005773#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5775 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5776 output, output_length);
5777 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005778#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005779#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5780 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5782 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5783 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5784 kdf_alg, output,
5785 output_length);
5786 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005787#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5788 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005789#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005791 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5793 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005794#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305795#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305796 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305797 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5798 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305799 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305800#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005801
Gilles Peskineeab56e42018-07-12 17:12:33 +02005802 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005803 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005804 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005805 }
5806
5807exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005809 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005810 * This allows us to differentiate between exhausted operations and
5811 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5812 * operations. */
5813 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005814 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005815 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005817 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005819}
5820
David Brown7807bf72021-02-09 16:28:23 -07005821#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005822static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005823{
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 if (data_size >= 8) {
5825 mbedtls_des_key_set_parity(data);
5826 }
5827 if (data_size >= 16) {
5828 mbedtls_des_key_set_parity(data + 8);
5829 }
5830 if (data_size >= 24) {
5831 mbedtls_des_key_set_parity(data + 16);
5832 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005833}
David Brown7807bf72021-02-09 16:28:23 -07005834#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005835
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005836/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 * ECC keys on a Weierstrass elliptic curve require the generation
5838 * of a private key which is an integer
5839 * in the range [1, N - 1], where N is the boundary of the private key domain:
5840 * N is the prime p for Diffie-Hellman, or the order of the
5841 * curve’s base point for ECC.
5842 *
5843 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5844 * This function generates the private key using the following process:
5845 *
5846 * 1. Draw a byte string of length ceiling(m/8) bytes.
5847 * 2. If m is not a multiple of 8, set the most significant
5848 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5849 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5850 * 4. If k > N - 2, discard the result and return to step 1.
5851 * 5. Output k + 1 as the private key.
5852 *
5853 * This method allows compliance to NIST standards, specifically the methods titled
5854 * Key-Pair Generation by Testing Candidates in the following publications:
5855 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5856 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5857 * Diffie-Hellman keys.
5858 *
5859 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5860 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5861 *
5862 * Note: Function allocates memory for *data buffer, so given *data should be
5863 * always NULL.
5864 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005865#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5866#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005867static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005868 psa_key_slot_t *slot,
5869 size_t bits,
5870 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005871 uint8_t **data
5872 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005873{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005874 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005875 mbedtls_mpi k;
5876 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005877 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005878 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005879 size_t m;
5880 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005881
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 mbedtls_mpi_init(&k);
5883 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005884
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005885 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005887 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005888 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005889
Gilles Peskine449bd832023-01-11 14:50:10 +01005890 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005891 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005892 goto cleanup;
5893 }
5894
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005895 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005897
Gilles Peskine449bd832023-01-11 14:50:10 +01005898 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005899
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005900 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005901 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005902 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005903
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005904 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005905
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005906 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005907 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005908
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005909 /* Note: This function is always called with *data == NULL and it
5910 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005911 *data = mbedtls_calloc(1, m_bytes);
5912 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005913 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005914 goto cleanup;
5915 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005916
Gilles Peskine449bd832023-01-11 14:50:10 +01005917 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005918 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005919 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005920 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005922
5923 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005924 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005925 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005926 * (8 * ceiling(m/8) - m) bits of the first byte in
5927 * the string to zero.
5928 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005929 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005930 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005931 }
5932
5933 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 * big-endian byte string.
5935 */
5936 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005937
5938 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 * Result of comparison is returned. When it indicates error
5940 * then this function is called again.
5941 */
5942 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005943 }
5944
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005945 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005946 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5947 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005948cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005949 if (ret != 0) {
5950 status = mbedtls_to_psa_error(ret);
5951 }
5952 if (status != PSA_SUCCESS) {
5953 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005954 *data = NULL;
5955 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005956 mbedtls_mpi_free(&k);
5957 mbedtls_mpi_free(&diff_N_2);
5958 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005959}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005960
5961/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5962 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5963 *
5964 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5965 * draw a 32-byte string and process it as specified in
5966 * Elliptic Curves for Security [RFC7748] §5.
5967 *
5968 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5969 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5970 *
5971 * Note: Function allocates memory for *data buffer, so given *data should be
5972 * always NULL.
5973 */
5974
5975static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5976 size_t bits,
5977 psa_key_derivation_operation_t *operation,
5978 uint8_t **data
5979 )
5980{
5981 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005982 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005983
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005985 case 255:
5986 output_length = 32;
5987 break;
5988 case 448:
5989 output_length = 56;
5990 break;
5991 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005992 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005993 break;
5994 }
5995
Gilles Peskine449bd832023-01-11 14:50:10 +01005996 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005997
Gilles Peskine449bd832023-01-11 14:50:10 +01005998 if (*data == NULL) {
5999 return PSA_ERROR_INSUFFICIENT_MEMORY;
6000 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006001
Gilles Peskine449bd832023-01-11 14:50:10 +01006002 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006003
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006005 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006006 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006007
Gilles Peskine449bd832023-01-11 14:50:10 +01006008 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006009 case 255:
6010 (*data)[0] &= 248;
6011 (*data)[31] &= 127;
6012 (*data)[31] |= 64;
6013 break;
6014 case 448:
6015 (*data)[0] &= 252;
6016 (*data)[55] |= 128;
6017 break;
6018 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006020 break;
6021 }
6022
6023 return status;
6024}
Valerio Setti86587ab2023-06-27 13:09:30 +02006025#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6026static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6027 psa_key_slot_t *slot, size_t bits,
6028 psa_key_derivation_operation_t *operation, uint8_t **data)
6029{
6030 (void) slot;
6031 (void) bits;
6032 (void) operation;
6033 (void) data;
6034 return PSA_ERROR_NOT_SUPPORTED;
6035}
6036
6037static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6038 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6039{
6040 (void) bits;
6041 (void) operation;
6042 (void) data;
6043 return PSA_ERROR_NOT_SUPPORTED;
6044}
6045#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6046#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006047
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006048static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006049 psa_key_slot_t *slot,
6050 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006052{
6053 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306055 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006056 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006057 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006058
Gilles Peskine449bd832023-01-11 14:50:10 +01006059 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6060 return PSA_ERROR_INVALID_ARGUMENT;
6061 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006062
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006063#if 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 (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6066 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6067 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006068 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6070 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006071 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 }
6073 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006074 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6076 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006077 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006079 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006080 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006081#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006082 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 if (key_type_is_raw_bytes(slot->attr.type)) {
6084 if (bits % 8 != 0) {
6085 return PSA_ERROR_INVALID_ARGUMENT;
6086 }
6087 data = mbedtls_calloc(1, bytes);
6088 if (data == NULL) {
6089 return PSA_ERROR_INSUFFICIENT_MEMORY;
6090 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006091
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 status = psa_key_derivation_output_bytes(operation, data, bytes);
6093 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006094 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 }
David Brown12ca5032021-02-11 11:02:00 -07006096#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6098 psa_des_set_key_parity(data, bytes);
6099 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006100#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 } else {
6102 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006103 }
Ronald Crondd04d422020-11-28 15:14:42 +01006104
Ronald Cronbf33c932020-11-28 18:06:53 +01006105 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006106 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006108 };
6109
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6111 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6112 &storage_size);
6113 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306114 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 }
Archanad8a83dc2021-06-14 10:04:16 +05306116 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 status = psa_allocate_buffer_to_slot(slot, storage_size);
6118 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306119 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 }
Archanad8a83dc2021-06-14 10:04:16 +05306121
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 status = psa_driver_wrapper_import_key(&attributes,
6123 data, bytes,
6124 slot->key.data,
6125 slot->key.bytes,
6126 &slot->key.bytes, &bits);
6127 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006128 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006130
6131exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 mbedtls_free(data);
6133 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006134}
6135
Gilles Peskine449bd832023-01-11 14:50:10 +01006136psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6137 psa_key_derivation_operation_t *operation,
6138 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006139{
6140 psa_status_t status;
6141 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006142 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006143
Ronald Cron81709fc2020-11-14 12:10:32 +01006144 *key = MBEDTLS_SVC_KEY_ID_INIT;
6145
Gilles Peskine0f84d622019-09-12 19:03:13 +02006146 /* Reject any attempt to create a zero-length key so that we don't
6147 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006148 if (psa_get_key_bits(attributes) == 0) {
6149 return PSA_ERROR_INVALID_ARGUMENT;
6150 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006151
Gilles Peskine449bd832023-01-11 14:50:10 +01006152 if (operation->alg == PSA_ALG_NONE) {
6153 return PSA_ERROR_BAD_STATE;
6154 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006155
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 if (!operation->can_output_key) {
6157 return PSA_ERROR_NOT_PERMITTED;
6158 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006159
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6161 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006162#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006164 /* Deriving a key in a secure element is not implemented yet. */
6165 status = PSA_ERROR_NOT_SUPPORTED;
6166 }
6167#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 if (status == PSA_SUCCESS) {
6169 status = psa_generate_derived_key_internal(slot,
6170 attributes->core.bits,
6171 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006172 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006173 if (status == PSA_SUCCESS) {
6174 status = psa_finish_key_creation(slot, driver, key);
6175 }
6176 if (status != PSA_SUCCESS) {
6177 psa_fail_key_creation(slot, driver);
6178 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006179
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006181}
6182
Gilles Peskineeab56e42018-07-12 17:12:33 +02006183
6184
6185/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006186/* Key derivation */
6187/****************************************************************/
6188
Steven Cooreman932ffb72021-02-15 12:14:32 +01006189#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006190static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006191{
6192#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6194 return 1;
6195 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006196#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006197#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6199 return 1;
6200 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006201#endif
6202#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6204 return 1;
6205 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006206#endif
6207#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6209 return 1;
6210 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006211#endif
6212#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6214 return 1;
6215 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006216#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006217#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6219 return 1;
6220 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006221#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306222#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6223 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6224 return 1;
6225 }
6226#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306227#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6228 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6229 return 1;
6230 }
6231#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006233}
6234
Gilles Peskine449bd832023-01-11 14:50:10 +01006235static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006236{
6237 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 psa_status_t status = psa_hash_setup(&operation, alg);
6239 psa_hash_abort(&operation);
6240 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006241}
6242
Gilles Peskine969c5d62019-01-16 15:53:06 +01006243static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006244 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006246{
Janos Follath5fe19732019-06-20 15:09:30 +01006247 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6248 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006250
Gilles Peskine969c5d62019-01-16 15:53:06 +01006251 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006252 if (!is_kdf_alg_supported(kdf_alg)) {
6253 return PSA_ERROR_NOT_SUPPORTED;
6254 }
John Durkop07cc04a2020-11-16 22:08:34 -08006255
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006256 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306257 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6259 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306260 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6261 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6262 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306263 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306264 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006265 if (hash_size == 0) {
6266 return PSA_ERROR_NOT_SUPPORTED;
6267 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006268
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006269 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6270 * we might fail later, which is somewhat unfriendly and potentially
6271 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 psa_status_t status = psa_hash_try_support(hash_alg);
6273 if (status != PSA_SUCCESS) {
6274 return status;
6275 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006276 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006277
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6279 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6280 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6281 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006282 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006283#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006284 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6286 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006287 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006289#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6290 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 operation->capacity = 255 * hash_size;
6292 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006293}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006294
Gilles Peskine449bd832023-01-11 14:50:10 +01006295static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006296{
6297#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 if (alg == PSA_ALG_ECDH) {
6299 return PSA_SUCCESS;
6300 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006301#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006302#if defined(PSA_WANT_ALG_FFDH)
6303 if (alg == PSA_ALG_FFDH) {
6304 return PSA_SUCCESS;
6305 }
6306#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006307 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006308 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006309}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006310
6311static int psa_key_derivation_allows_free_form_secret_input(
6312 psa_algorithm_t kdf_alg)
6313{
6314#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6315 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6316 return 0;
6317 }
6318#endif
6319 (void) kdf_alg;
6320 return 1;
6321}
John Durkop07cc04a2020-11-16 22:08:34 -08006322#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006323
Gilles Peskine449bd832023-01-11 14:50:10 +01006324psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6325 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006326{
6327 psa_status_t status;
6328
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 if (operation->alg != 0) {
6330 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006331 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006332
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6334 return PSA_ERROR_INVALID_ARGUMENT;
6335 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6336#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6337 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6338 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6339 status = psa_key_agreement_try_support(ka_alg);
6340 if (status != PSA_SUCCESS) {
6341 return status;
6342 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006343 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6344 return PSA_ERROR_INVALID_ARGUMENT;
6345 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6347#else
6348 return PSA_ERROR_NOT_SUPPORTED;
6349#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6350 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6351#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6352 status = psa_key_derivation_setup_kdf(operation, alg);
6353#else
6354 return PSA_ERROR_NOT_SUPPORTED;
6355#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6356 } else {
6357 return PSA_ERROR_INVALID_ARGUMENT;
6358 }
6359
6360 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006361 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 }
6363 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006364}
6365
Przemek Stekiel69c46792022-06-10 12:59:51 +02006366#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006367static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6368 psa_algorithm_t kdf_alg,
6369 psa_key_derivation_step_t step,
6370 const uint8_t *data,
6371 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006372{
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006374 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006376 case PSA_KEY_DERIVATION_INPUT_SALT:
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)) {
6379 return PSA_ERROR_INVALID_ARGUMENT;
6380 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006381#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 if (hkdf->state != HKDF_STATE_INIT) {
6383 return PSA_ERROR_BAD_STATE;
6384 } else {
6385 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6386 hash_alg,
6387 data, data_length);
6388 if (status != PSA_SUCCESS) {
6389 return status;
6390 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006391 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006393 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006394 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006395#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006397 /* We shouldn't be in different state as HKDF_EXPAND only allows
6398 * two inputs: SECRET (this case) and INFO which does not modify
6399 * the state. It could happen only if the hkdf
6400 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 if (hkdf->state != HKDF_STATE_INIT) {
6402 return PSA_ERROR_BAD_STATE;
6403 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006404
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006405 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6407 return PSA_ERROR_INVALID_ARGUMENT;
6408 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006409
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 memcpy(hkdf->prk, data, data_length);
6411 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006412#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006413 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006414 /* HKDF: If no salt was provided, use an empty salt.
6415 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006417#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6419 return PSA_ERROR_BAD_STATE;
6420 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006421#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6423 hash_alg,
6424 NULL, 0);
6425 if (status != PSA_SUCCESS) {
6426 return status;
6427 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006428 hkdf->state = HKDF_STATE_STARTED;
6429 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 if (hkdf->state != HKDF_STATE_STARTED) {
6431 return PSA_ERROR_BAD_STATE;
6432 }
6433 status = psa_mac_update(&hkdf->hmac,
6434 data, data_length);
6435 if (status != PSA_SUCCESS) {
6436 return status;
6437 }
6438 status = psa_mac_sign_finish(&hkdf->hmac,
6439 hkdf->prk,
6440 sizeof(hkdf->prk),
6441 &data_length);
6442 if (status != PSA_SUCCESS) {
6443 return status;
6444 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006445 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006446
Gilles Peskine2b522db2019-04-12 15:11:49 +02006447 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006448 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006449#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006451 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006453 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006454 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006455#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006456 {
6457 /* Block 0 is empty, and the next block will be
6458 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006460 }
6461
Gilles Peskine449bd832023-01-11 14:50:10 +01006462 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006463 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006464#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6466 return PSA_ERROR_INVALID_ARGUMENT;
6467 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006468#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006469#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006470 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6471 hkdf->state == HKDF_STATE_INIT) {
6472 return PSA_ERROR_BAD_STATE;
6473 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006474#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 if (hkdf->state == HKDF_STATE_OUTPUT) {
6476 return PSA_ERROR_BAD_STATE;
6477 }
6478 if (hkdf->info_set) {
6479 return PSA_ERROR_BAD_STATE;
6480 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006481 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006482 if (data_length != 0) {
6483 hkdf->info = mbedtls_calloc(1, data_length);
6484 if (hkdf->info == NULL) {
6485 return PSA_ERROR_INSUFFICIENT_MEMORY;
6486 }
6487 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006488 }
6489 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006491 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006492 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006493 }
6494}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006495#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006496
John Durkop07cc04a2020-11-16 22:08:34 -08006497#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6498 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006499static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6500 const uint8_t *data,
6501 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006502{
Gilles Peskine449bd832023-01-11 14:50:10 +01006503 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6504 return PSA_ERROR_BAD_STATE;
6505 }
Janos Follathf08e2652019-06-13 09:05:41 +01006506
Gilles Peskine449bd832023-01-11 14:50:10 +01006507 if (data_length != 0) {
6508 prf->seed = mbedtls_calloc(1, data_length);
6509 if (prf->seed == NULL) {
6510 return PSA_ERROR_INSUFFICIENT_MEMORY;
6511 }
Janos Follathf08e2652019-06-13 09:05:41 +01006512
Gilles Peskine449bd832023-01-11 14:50:10 +01006513 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006514 prf->seed_length = data_length;
6515 }
Janos Follathf08e2652019-06-13 09:05:41 +01006516
Gilles Peskine43f958b2020-12-13 14:55:14 +01006517 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006518
Gilles Peskine449bd832023-01-11 14:50:10 +01006519 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006520}
6521
Gilles Peskine449bd832023-01-11 14:50:10 +01006522static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6523 const uint8_t *data,
6524 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006525{
Gilles Peskine449bd832023-01-11 14:50:10 +01006526 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6527 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6528 return PSA_ERROR_BAD_STATE;
6529 }
Janos Follath81550542019-06-13 14:26:34 +01006530
Gilles Peskine449bd832023-01-11 14:50:10 +01006531 if (data_length != 0) {
6532 prf->secret = mbedtls_calloc(1, data_length);
6533 if (prf->secret == NULL) {
6534 return PSA_ERROR_INSUFFICIENT_MEMORY;
6535 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006536
Gilles Peskine449bd832023-01-11 14:50:10 +01006537 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006538 prf->secret_length = data_length;
6539 }
Janos Follath81550542019-06-13 14:26:34 +01006540
Gilles Peskine43f958b2020-12-13 14:55:14 +01006541 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006542
Gilles Peskine449bd832023-01-11 14:50:10 +01006543 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006544}
6545
Gilles Peskine449bd832023-01-11 14:50:10 +01006546static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6547 const uint8_t *data,
6548 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006549{
Gilles Peskine449bd832023-01-11 14:50:10 +01006550 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6551 return PSA_ERROR_BAD_STATE;
6552 }
Janos Follath63028dd2019-06-13 09:15:47 +01006553
Gilles Peskine449bd832023-01-11 14:50:10 +01006554 if (data_length != 0) {
6555 prf->label = mbedtls_calloc(1, data_length);
6556 if (prf->label == NULL) {
6557 return PSA_ERROR_INSUFFICIENT_MEMORY;
6558 }
Janos Follath63028dd2019-06-13 09:15:47 +01006559
Gilles Peskine449bd832023-01-11 14:50:10 +01006560 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006561 prf->label_length = data_length;
6562 }
Janos Follath63028dd2019-06-13 09:15:47 +01006563
Gilles Peskine43f958b2020-12-13 14:55:14 +01006564 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006565
Gilles Peskine449bd832023-01-11 14:50:10 +01006566 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006567}
6568
Gilles Peskine449bd832023-01-11 14:50:10 +01006569static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6570 psa_key_derivation_step_t step,
6571 const uint8_t *data,
6572 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006573{
Gilles Peskine449bd832023-01-11 14:50:10 +01006574 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006575 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006576 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006577 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006578 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006579 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006580 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006581 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006583 }
6584}
John Durkop07cc04a2020-11-16 22:08:34 -08006585#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6586 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6587
6588#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6589static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6590 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006591 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006592 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006593{
6594 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006595 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6596 4 + data_length + prf->other_secret_length :
6597 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006598
Gilles Peskine449bd832023-01-11 14:50:10 +01006599 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6600 return PSA_ERROR_INVALID_ARGUMENT;
6601 }
John Durkop07cc04a2020-11-16 22:08:34 -08006602
Gilles Peskine449bd832023-01-11 14:50:10 +01006603 uint8_t *pms = mbedtls_calloc(1, pms_len);
6604 if (pms == NULL) {
6605 return PSA_ERROR_INSUFFICIENT_MEMORY;
6606 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006607 uint8_t *cur = pms;
6608
6609 /* pure-PSK:
6610 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006611 *
6612 * The premaster secret is formed as follows: if the PSK is N octets
6613 * long, concatenate a uint16 with the value N, N zero octets, a second
6614 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006615 *
6616 * mixed-PSK:
6617 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6618 * follows: concatenate a uint16 with the length of the other secret,
6619 * the other secret itself, uint16 with the length of PSK, and the
6620 * PSK itself.
6621 * For details please check:
6622 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6623 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6624 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006625 */
6626
Gilles Peskine449bd832023-01-11 14:50:10 +01006627 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6628 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6629 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6630 if (prf->other_secret_length != 0) {
6631 memcpy(cur, prf->other_secret, prf->other_secret_length);
6632 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006633 cur += prf->other_secret_length;
6634 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006635 } else {
6636 *cur++ = MBEDTLS_BYTE_1(data_length);
6637 *cur++ = MBEDTLS_BYTE_0(data_length);
6638 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006639 cur += data_length;
6640 }
6641
Gilles Peskine449bd832023-01-11 14:50:10 +01006642 *cur++ = MBEDTLS_BYTE_1(data_length);
6643 *cur++ = MBEDTLS_BYTE_0(data_length);
6644 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006645 cur += data_length;
6646
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006647 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006648
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006649 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006650 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006651}
Janos Follath6660f0e2019-06-17 08:44:03 +01006652
Przemek Stekiele47201b2022-04-19 13:53:28 +02006653static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6654 psa_tls12_prf_key_derivation_t *prf,
6655 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006656 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006657{
Gilles Peskine449bd832023-01-11 14:50:10 +01006658 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6659 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006660 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006661
6662 if (data_length != 0) {
6663 prf->other_secret = mbedtls_calloc(1, data_length);
6664 if (prf->other_secret == NULL) {
6665 return PSA_ERROR_INSUFFICIENT_MEMORY;
6666 }
6667
6668 memcpy(prf->other_secret, data, data_length);
6669 prf->other_secret_length = data_length;
6670 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006671 prf->other_secret_length = 0;
6672 }
6673
6674 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6675
Gilles Peskine449bd832023-01-11 14:50:10 +01006676 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006677}
6678
Janos Follath6660f0e2019-06-17 08:44:03 +01006679static psa_status_t psa_tls12_prf_psk_to_ms_input(
6680 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006681 psa_key_derivation_step_t step,
6682 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006683 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006684{
Gilles Peskine449bd832023-01-11 14:50:10 +01006685 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006686 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006687 return psa_tls12_prf_psk_to_ms_set_key(prf,
6688 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006689 break;
6690 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6692 data,
6693 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006694 break;
6695 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006696 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006697 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006698
Przemek Stekiele47201b2022-04-19 13:53:28 +02006699 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006700}
John Durkop07cc04a2020-11-16 22:08:34 -08006701#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006702
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006703#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6704static psa_status_t psa_tls12_ecjpake_to_pms_input(
6705 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006706 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006707 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006708 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006709{
Gilles Peskine449bd832023-01-11 14:50:10 +01006710 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6711 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6712 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006713 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006714
6715 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006716 if (data[0] != 0x04) {
6717 return PSA_ERROR_INVALID_ARGUMENT;
6718 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006719
6720 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006721 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006722
Gilles Peskine449bd832023-01-11 14:50:10 +01006723 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006724}
6725#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306726
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306727#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306728static psa_status_t psa_pbkdf2_set_input_cost(
6729 psa_pbkdf2_key_derivation_t *pbkdf2,
6730 psa_key_derivation_step_t step,
6731 uint64_t data)
6732{
6733 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6734 return PSA_ERROR_INVALID_ARGUMENT;
6735 }
6736
6737 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6738 return PSA_ERROR_BAD_STATE;
6739 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306740
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306741 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306742 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306743 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306744
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306745 if (data == 0) {
6746 return PSA_ERROR_INVALID_ARGUMENT;
6747 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306748
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306749 pbkdf2->input_cost = data;
6750 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6751
6752 return PSA_SUCCESS;
6753}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306754
6755static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6756 const uint8_t *data,
6757 size_t data_length)
6758{
Gilles Peskineead17662023-08-20 22:02:51 +02006759 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6760 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6761 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6762 /* Appending to existing salt. No state change. */
6763 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306764 return PSA_ERROR_BAD_STATE;
6765 }
6766
Gilles Peskineca57d782023-07-20 19:08:06 +02006767 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006768 /* Appending an empty string, nothing to do. */
6769 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306770 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306771
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306772 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6773 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306774 return PSA_ERROR_INSUFFICIENT_MEMORY;
6775 }
6776
Gilles Peskineead17662023-08-20 22:02:51 +02006777 if (pbkdf2->salt_length != 0) {
6778 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6779 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306780 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306781 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306782 mbedtls_free(pbkdf2->salt);
6783 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306784 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306785 return PSA_SUCCESS;
6786}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306787
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306788#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306789static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306790 const uint8_t *input,
6791 size_t input_len,
6792 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306793 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306794{
6795 psa_status_t status = PSA_SUCCESS;
6796 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6797 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306798 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306799 } else {
6800 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306801 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306802 }
6803 return status;
6804}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306805#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306806
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306807#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306808static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6809 size_t input_len,
6810 uint8_t *output,
6811 size_t *output_len)
6812{
6813 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306814 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306815 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306816 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306817 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6818 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6819 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306820 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306821 * mac_size as the driver function sets mac_output_length = mac_size
6822 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306823 status = psa_driver_wrapper_mac_compute(&attributes,
6824 zeros, sizeof(zeros),
6825 PSA_ALG_CMAC, input, input_len,
6826 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306827 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6828 128U,
6829 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306830 output_len);
6831 } else {
6832 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306833 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306834 }
6835 return status;
6836}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306837#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306838
6839static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306840 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306841 const uint8_t *data,
6842 size_t data_length)
6843{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306844 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306845 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6846 return PSA_ERROR_BAD_STATE;
6847 }
6848
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306849#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306850 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6851 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6852 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6853 pbkdf2->password,
6854 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306855 } else
6856#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6857#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6858 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306859 status = psa_pbkdf2_cmac_set_password(data, data_length,
6860 pbkdf2->password,
6861 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306862 } else
6863#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6864 {
6865 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306866 }
6867
6868 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6869
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306870 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306871}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306872
6873static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306874 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306875 psa_key_derivation_step_t step,
6876 const uint8_t *data,
6877 size_t data_length)
6878{
6879 switch (step) {
6880 case PSA_KEY_DERIVATION_INPUT_SALT:
6881 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6882 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306883 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306884 default:
6885 return PSA_ERROR_INVALID_ARGUMENT;
6886 }
6887}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306888#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306889
Gilles Peskineb8965192019-09-24 16:21:10 +02006890/** Check whether the given key type is acceptable for the given
6891 * input step of a key derivation.
6892 *
6893 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6894 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6895 * Both secret and non-secret inputs can alternatively have the type
6896 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6897 * that the input was passed as a buffer rather than via a key object.
6898 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006899static int psa_key_derivation_check_input_type(
6900 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006901 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006902{
Gilles Peskine449bd832023-01-11 14:50:10 +01006903 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006904 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006905 if (key_type == PSA_KEY_TYPE_DERIVE) {
6906 return PSA_SUCCESS;
6907 }
6908 if (key_type == PSA_KEY_TYPE_NONE) {
6909 return PSA_SUCCESS;
6910 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006911 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006912 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006913 if (key_type == PSA_KEY_TYPE_DERIVE) {
6914 return PSA_SUCCESS;
6915 }
6916 if (key_type == PSA_KEY_TYPE_NONE) {
6917 return PSA_SUCCESS;
6918 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006919 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006920 case PSA_KEY_DERIVATION_INPUT_LABEL:
6921 case PSA_KEY_DERIVATION_INPUT_SALT:
6922 case PSA_KEY_DERIVATION_INPUT_INFO:
6923 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006924 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6925 return PSA_SUCCESS;
6926 }
6927 if (key_type == PSA_KEY_TYPE_NONE) {
6928 return PSA_SUCCESS;
6929 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006930 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306931 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6932 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6933 return PSA_SUCCESS;
6934 }
6935 if (key_type == PSA_KEY_TYPE_DERIVE) {
6936 return PSA_SUCCESS;
6937 }
6938 if (key_type == PSA_KEY_TYPE_NONE) {
6939 return PSA_SUCCESS;
6940 }
6941 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006942 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006944}
6945
Janos Follathb80a94e2019-06-12 15:54:46 +01006946static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006947 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006948 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006949 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006950 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006951 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006952{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006953 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006954 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006955
Gilles Peskine449bd832023-01-11 14:50:10 +01006956 status = psa_key_derivation_check_input_type(step, key_type);
6957 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006958 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006959 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006960
Przemek Stekiel69c46792022-06-10 12:59:51 +02006961#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006962 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6963 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6964 step, data, data_length);
6965 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006966#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006967#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006968 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6969 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6970 step, data, data_length);
6971 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006972#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6973#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006974 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6975 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6976 step, data, data_length);
6977 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006978#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006979#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006980 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006981 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006982 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6983 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006984#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306985#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306986 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306987 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6988 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306989 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306990#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006991 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006992 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006993 (void) data;
6994 (void) data_length;
6995 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006996 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006997 }
6998
Gilles Peskine224b0d62019-09-23 18:13:17 +02006999exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007000 if (status != PSA_SUCCESS) {
7001 psa_key_derivation_abort(operation);
7002 }
7003 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007004}
7005
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307006static psa_status_t psa_key_derivation_input_integer_internal(
7007 psa_key_derivation_operation_t *operation,
7008 psa_key_derivation_step_t step,
7009 uint64_t value)
7010{
7011 psa_status_t status;
7012 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
7013
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307014#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307015 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307016 status = psa_pbkdf2_set_input_cost(
7017 &operation->ctx.pbkdf2, step, value);
7018 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307019#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307020 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307021 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307022 (void) value;
7023 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307024 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307025 }
7026
7027 if (status != PSA_SUCCESS) {
7028 psa_key_derivation_abort(operation);
7029 }
7030 return status;
7031}
7032
Janos Follath51f4a0f2019-06-14 11:35:55 +01007033psa_status_t psa_key_derivation_input_bytes(
7034 psa_key_derivation_operation_t *operation,
7035 psa_key_derivation_step_t step,
7036 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007037 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007038{
Gilles Peskine449bd832023-01-11 14:50:10 +01007039 return psa_key_derivation_input_internal(operation, step,
7040 PSA_KEY_TYPE_NONE,
7041 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007042}
7043
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307044psa_status_t psa_key_derivation_input_integer(
7045 psa_key_derivation_operation_t *operation,
7046 psa_key_derivation_step_t step,
7047 uint64_t value)
7048{
7049 return psa_key_derivation_input_integer_internal(operation, step, value);
7050}
7051
Janos Follath51f4a0f2019-06-14 11:35:55 +01007052psa_status_t psa_key_derivation_input_key(
7053 psa_key_derivation_operation_t *operation,
7054 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007055 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007056{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007057 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007058 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007059 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007060
Ronald Cron5c522922020-11-14 16:35:34 +01007061 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007062 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7063 if (status != PSA_SUCCESS) {
7064 psa_key_derivation_abort(operation);
7065 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007066 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007067
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307068 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7069 * permission to output to a key object. */
7070 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7071 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007072 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007073 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007074
Gilles Peskine449bd832023-01-11 14:50:10 +01007075 status = psa_key_derivation_input_internal(operation,
7076 step, slot->attr.type,
7077 slot->key.data,
7078 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007079
Gilles Peskine449bd832023-01-11 14:50:10 +01007080 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007081
Gilles Peskine449bd832023-01-11 14:50:10 +01007082 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007083}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007084
7085
7086
7087/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007088/* Key agreement */
7089/****************************************************************/
7090
Gilles Peskine449bd832023-01-11 14:50:10 +01007091psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7092 const uint8_t *key_buffer,
7093 size_t key_buffer_size,
7094 psa_algorithm_t alg,
7095 const uint8_t *peer_key,
7096 size_t peer_key_length,
7097 uint8_t *shared_secret,
7098 size_t shared_secret_size,
7099 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007100{
Gilles Peskine449bd832023-01-11 14:50:10 +01007101 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007102#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007103 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007104 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7105 key_buffer_size, alg,
7106 peer_key, peer_key_length,
7107 shared_secret,
7108 shared_secret_size,
7109 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007110#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007111
7112#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7113 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007114 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007115 peer_key,
7116 peer_key_length,
7117 key_buffer,
7118 key_buffer_size,
7119 shared_secret,
7120 shared_secret_size,
7121 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007122#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7123
Gilles Peskine01d718c2018-09-18 12:01:02 +02007124 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007125 (void) attributes;
7126 (void) key_buffer;
7127 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007128 (void) peer_key;
7129 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007130 (void) shared_secret;
7131 (void) shared_secret_size;
7132 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007133 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007134 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007135}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007136
Aditya Deshpande17845b82022-10-13 17:21:01 +01007137/** Internal function for raw key agreement
7138 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007139 * to the driver's implementation if a driver is present.
7140 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007141 * (psa_key_agreement_raw_builtin).
7142 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007143static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7144 psa_key_slot_t *private_key,
7145 const uint8_t *peer_key,
7146 size_t peer_key_length,
7147 uint8_t *shared_secret,
7148 size_t shared_secret_size,
7149 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007150{
Gilles Peskine449bd832023-01-11 14:50:10 +01007151 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7152 return PSA_ERROR_NOT_SUPPORTED;
7153 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007154
7155 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007156 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007157 };
7158
Gilles Peskine449bd832023-01-11 14:50:10 +01007159 return psa_driver_wrapper_key_agreement(&attributes,
7160 private_key->key.data,
7161 private_key->key.bytes, alg,
7162 peer_key, peer_key_length,
7163 shared_secret,
7164 shared_secret_size,
7165 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007166}
7167
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007168/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007169 * to potentially free embedded data structures and wipe confidential data.
7170 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007171static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7172 psa_key_derivation_step_t step,
7173 psa_key_slot_t *private_key,
7174 const uint8_t *peer_key,
7175 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007176{
7177 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007178 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007179 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007180 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007181
7182 /* Step 1: run the secret agreement algorithm to generate the shared
7183 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007184 status = psa_key_agreement_raw_internal(ka_alg,
7185 private_key,
7186 peer_key, peer_key_length,
7187 shared_secret,
7188 sizeof(shared_secret),
7189 &shared_secret_length);
7190 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007191 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007192 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007193
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007194 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007195 * the shared secret. A shared secret is permitted wherever a key
7196 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007197 status = psa_key_derivation_input_internal(operation, step,
7198 PSA_KEY_TYPE_DERIVE,
7199 shared_secret,
7200 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007201exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007202 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7203 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007204}
7205
Gilles Peskine449bd832023-01-11 14:50:10 +01007206psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7207 psa_key_derivation_step_t step,
7208 mbedtls_svc_key_id_t private_key,
7209 const uint8_t *peer_key,
7210 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007211{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007212 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007213 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007214 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007215
Gilles Peskine449bd832023-01-11 14:50:10 +01007216 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7217 return PSA_ERROR_INVALID_ARGUMENT;
7218 }
Ronald Cron5c522922020-11-14 16:35:34 +01007219 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007220 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7221 if (status != PSA_SUCCESS) {
7222 return status;
7223 }
7224 status = psa_key_agreement_internal(operation, step,
7225 slot,
7226 peer_key, peer_key_length);
7227 if (status != PSA_SUCCESS) {
7228 psa_key_derivation_abort(operation);
7229 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007230 /* If a private key has been added as SECRET, we allow the derived
7231 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007232 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007233 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007234 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007235 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007236
Gilles Peskine449bd832023-01-11 14:50:10 +01007237 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007238
Gilles Peskine449bd832023-01-11 14:50:10 +01007239 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007240}
7241
Gilles Peskine449bd832023-01-11 14:50:10 +01007242psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7243 mbedtls_svc_key_id_t private_key,
7244 const uint8_t *peer_key,
7245 size_t peer_key_length,
7246 uint8_t *output,
7247 size_t output_size,
7248 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007249{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007250 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007251 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007252 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007253 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007254
Gilles Peskine449bd832023-01-11 14:50:10 +01007255 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007256 status = PSA_ERROR_INVALID_ARGUMENT;
7257 goto exit;
7258 }
Ronald Cron5c522922020-11-14 16:35:34 +01007259 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007260 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7261 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007262 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007263 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007264
Gilles Peskine3e561302022-04-14 00:17:15 +02007265 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7266 * for the output size. The PSA specification only guarantees that this
7267 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7268 * but it might be nice to allow smaller buffers if the output fits.
7269 * At the time of writing this comment, with only ECDH implemented,
7270 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7271 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7272 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007273 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007274 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7275 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007276 status = PSA_ERROR_BUFFER_TOO_SMALL;
7277 goto exit;
7278 }
7279
Gilles Peskine449bd832023-01-11 14:50:10 +01007280 status = psa_key_agreement_raw_internal(alg, slot,
7281 peer_key, peer_key_length,
7282 output, output_size,
7283 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007284
7285exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007286 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007287 /* If an error happens and is not handled properly, the output
7288 * may be used as a key to protect sensitive data. Arrange for such
7289 * a key to be random, which is likely to result in decryption or
7290 * verification errors. This is better than filling the buffer with
7291 * some constant data such as zeros, which would result in the data
7292 * being protected with a reproducible, easily knowable key.
7293 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007294 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007295 *output_length = output_size;
7296 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007297
Gilles Peskine449bd832023-01-11 14:50:10 +01007298 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007299
Gilles Peskine449bd832023-01-11 14:50:10 +01007300 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007301}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007302
7303
Gilles Peskine30524eb2020-11-13 17:02:26 +01007304
Gilles Peskine86a440b2018-11-12 18:39:40 +01007305/****************************************************************/
7306/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007307/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007308
Gilles Peskine672a7712023-04-28 21:00:28 +02007309#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7310#include "entropy_poll.h"
7311#endif
7312
Gilles Peskine30524eb2020-11-13 17:02:26 +01007313/** Initialize the PSA random generator.
7314 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007315static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007316{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007317#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007318 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007319#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7320
Gilles Peskine30524eb2020-11-13 17:02:26 +01007321 /* Set default configuration if
7322 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007323 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007324 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007325 }
7326 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007327 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007328 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007329
Gilles Peskine449bd832023-01-11 14:50:10 +01007330 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007331#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7332 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7333 /* The PSA entropy injection feature depends on using NV seed as an entropy
7334 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007335 mbedtls_entropy_add_source(&rng->entropy,
7336 mbedtls_nv_seed_poll, NULL,
7337 MBEDTLS_ENTROPY_BLOCK_SIZE,
7338 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007339#endif
7340
Gilles Peskine449bd832023-01-11 14:50:10 +01007341 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007342#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007343}
7344
7345/** Deinitialize the PSA random generator.
7346 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007347static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007348{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007349#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007350 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007351#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007352 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7353 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007354#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007355}
7356
7357/** Seed the PSA random generator.
7358 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007359static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007360{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007361#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7362 /* Do nothing: the external RNG seeds itself. */
7363 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007364 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007365#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007366 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007367 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7368 drbg_seed, sizeof(drbg_seed) - 1);
7369 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007370#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007371}
7372
Gilles Peskine449bd832023-01-11 14:50:10 +01007373psa_status_t psa_generate_random(uint8_t *output,
7374 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007375{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007376 GUARD_MODULE_INITIALIZED;
7377
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007378#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7379
7380 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007381 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7382 output, output_size,
7383 &output_length);
7384 if (status != PSA_SUCCESS) {
7385 return status;
7386 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007387 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007388 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007389 if (output_length != output_size) {
7390 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7391 }
7392 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007393
7394#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7395
Gilles Peskine449bd832023-01-11 14:50:10 +01007396 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007397 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007398 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7399 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7400 output_size);
7401 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7402 output, request_size);
7403 if (ret != 0) {
7404 return mbedtls_to_psa_error(ret);
7405 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007406 output_size -= request_size;
7407 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007408 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007409 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007410#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007411}
7412
Gilles Peskine8814fc42020-12-14 15:33:44 +01007413/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007414 *
7415 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7416 * `psa_generate_random(...)`. The state parameter is ignored since the
7417 * PSA API doesn't support passing an explicit state.
7418 *
7419 * In the non-external case, psa_generate_random() calls an
7420 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7421 * and semantics as mbedtls_psa_get_random(). As an optimization,
7422 * instead of doing this back-and-forth between the PSA API and the
7423 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7424 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007425 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007426#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7427int mbedtls_psa_get_random(void *p_rng,
7428 unsigned char *output,
7429 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007430{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007431 /* This function takes a pointer to the RNG state because that's what
7432 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7433 * its own state internally and doesn't let the caller access that state.
7434 * So we just ignore the state parameter, and in practice we'll pass
7435 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007436 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007437 psa_status_t status = psa_generate_random(output, output_size);
7438 if (status == PSA_SUCCESS) {
7439 return 0;
7440 } else {
7441 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7442 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007443}
7444#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7445
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007446#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007447psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7448 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007449{
Gilles Peskine449bd832023-01-11 14:50:10 +01007450 if (global_data.initialized) {
7451 return PSA_ERROR_NOT_PERMITTED;
7452 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007453
Gilles Peskine449bd832023-01-11 14:50:10 +01007454 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7455 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7456 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7457 return PSA_ERROR_INVALID_ARGUMENT;
7458 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007459
Gilles Peskine449bd832023-01-11 14:50:10 +01007460 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007461}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007462#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007463
Ronald Cron3772afe2021-02-08 16:10:05 +01007464/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007465 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007466 * \param type The key type
7467 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007468 *
7469 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007470 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007471 * \retval #PSA_ERROR_INVALID_ARGUMENT
7472 * The size in bits of the key is not valid.
7473 * \retval #PSA_ERROR_NOT_SUPPORTED
7474 * The type and/or the size in bits of the key or the combination of
7475 * the two is not supported.
7476 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007477static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007478 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007479{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007480 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007481
Gilles Peskine449bd832023-01-11 14:50:10 +01007482 if (key_type_is_raw_bytes(type)) {
7483 status = psa_validate_unstructured_key_bit_size(type, bits);
7484 if (status != PSA_SUCCESS) {
7485 return status;
7486 }
7487 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007488#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007489 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7490 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7491 return PSA_ERROR_NOT_SUPPORTED;
7492 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007493 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007494 return PSA_ERROR_NOT_SUPPORTED;
7495 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007496
Ronald Cron01b2aba2020-10-05 09:42:02 +02007497 /* Accept only byte-aligned keys, for the same reasons as
7498 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007499 if (bits % 8 != 0) {
7500 return PSA_ERROR_NOT_SUPPORTED;
7501 }
7502 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007503#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007504
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007505#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007506 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007507 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007508 return PSA_SUCCESS;
7509 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007510#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007511
Valerio Settia55f0422023-07-10 15:34:41 +02007512#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007513 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007514 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007515 return PSA_ERROR_NOT_SUPPORTED;
7516 }
7517 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007518#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007519 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007520 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007521 }
7522
Gilles Peskine449bd832023-01-11 14:50:10 +01007523 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007524}
7525
Ronald Cron55ed0592020-10-05 10:30:40 +02007526psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007527 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007528 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007529{
7530 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007531 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007532
Gilles Peskine449bd832023-01-11 14:50:10 +01007533 if ((attributes->domain_parameters == NULL) &&
7534 (attributes->domain_parameters_size != 0)) {
7535 return PSA_ERROR_INVALID_ARGUMENT;
7536 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007537
Gilles Peskine449bd832023-01-11 14:50:10 +01007538 if (key_type_is_raw_bytes(type)) {
7539 status = psa_generate_random(key_buffer, key_buffer_size);
7540 if (status != PSA_SUCCESS) {
7541 return status;
7542 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007543
David Brown12ca5032021-02-11 11:02:00 -07007544#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007545 if (type == PSA_KEY_TYPE_DES) {
7546 psa_des_set_key_parity(key_buffer, key_buffer_size);
7547 }
David Brown8107e312021-02-12 08:08:46 -07007548#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007549 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007550
Valerio Setti76df8c12023-07-11 14:11:28 +02007551#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007552 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7553 return mbedtls_psa_rsa_generate_key(attributes,
7554 key_buffer,
7555 key_buffer_size,
7556 key_buffer_length);
7557 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007558#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007559
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007560#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007561 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7562 return mbedtls_psa_ecp_generate_key(attributes,
7563 key_buffer,
7564 key_buffer_size,
7565 key_buffer_length);
7566 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007567#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007568
Valerio Settia55f0422023-07-10 15:34:41 +02007569#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007570 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7571 return mbedtls_psa_ffdh_generate_key(attributes,
7572 key_buffer,
7573 key_buffer_size,
7574 key_buffer_length);
7575 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007576#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007577 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007578 (void) key_buffer_length;
7579 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007580 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007581
Gilles Peskine449bd832023-01-11 14:50:10 +01007582 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007583}
Darryl Green0c6575a2018-11-07 16:05:30 +00007584
Gilles Peskine449bd832023-01-11 14:50:10 +01007585psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7586 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007587{
7588 psa_status_t status;
7589 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007590 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007591 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007592
Ronald Cron81709fc2020-11-14 12:10:32 +01007593 *key = MBEDTLS_SVC_KEY_ID_INIT;
7594
Gilles Peskine0f84d622019-09-12 19:03:13 +02007595 /* Reject any attempt to create a zero-length key so that we don't
7596 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007597 if (psa_get_key_bits(attributes) == 0) {
7598 return PSA_ERROR_INVALID_ARGUMENT;
7599 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007600
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007601 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007602 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7603 return PSA_ERROR_INVALID_ARGUMENT;
7604 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007605
Gilles Peskine449bd832023-01-11 14:50:10 +01007606 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7607 &slot, &driver);
7608 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007609 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 }
Gilles Peskine11792082019-08-06 18:36:36 +02007611
Ronald Cron977c2472020-10-13 08:32:21 +02007612 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307613 * storage ( thus not in the case of generating a key in a secure element
7614 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7615 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007616 if (slot->key.data == NULL) {
7617 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7618 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007619 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 attributes->core.type, attributes->core.bits);
7621 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007622 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007623 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007624
7625 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007626 attributes->core.type,
7627 attributes->core.bits);
7628 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007629 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007630 attributes, &key_buffer_size);
7631 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007632 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007633 }
Ronald Cron977c2472020-10-13 08:32:21 +02007634 }
Ronald Cron977c2472020-10-13 08:32:21 +02007635
Gilles Peskine449bd832023-01-11 14:50:10 +01007636 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7637 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007638 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007639 }
Ronald Cron977c2472020-10-13 08:32:21 +02007640 }
7641
Gilles Peskine449bd832023-01-11 14:50:10 +01007642 status = psa_driver_wrapper_generate_key(attributes,
7643 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007644
Gilles Peskine449bd832023-01-11 14:50:10 +01007645 if (status != PSA_SUCCESS) {
7646 psa_remove_key_data_from_memory(slot);
7647 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007648
Gilles Peskine11792082019-08-06 18:36:36 +02007649exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007650 if (status == PSA_SUCCESS) {
7651 status = psa_finish_key_creation(slot, driver, key);
7652 }
7653 if (status != PSA_SUCCESS) {
7654 psa_fail_key_creation(slot, driver);
7655 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007656
Gilles Peskine449bd832023-01-11 14:50:10 +01007657 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007658}
7659
Gilles Peskinee59236f2018-01-27 23:32:46 +01007660/****************************************************************/
7661/* Module setup */
7662/****************************************************************/
7663
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007664#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007665psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007666 void (* entropy_init)(mbedtls_entropy_context *ctx),
7667 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007668{
Gilles Peskine449bd832023-01-11 14:50:10 +01007669 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7670 return PSA_ERROR_BAD_STATE;
7671 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007672 global_data.rng.entropy_init = entropy_init;
7673 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007674 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007675}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007676#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007677
Gilles Peskine449bd832023-01-11 14:50:10 +01007678void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007679{
Gilles Peskine449bd832023-01-11 14:50:10 +01007680 psa_wipe_all_key_slots();
7681 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7682 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007683 }
7684 /* Wipe all remaining data, including configuration.
7685 * In particular, this sets all state indicator to the value
7686 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007687 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007688
7689 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007690 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007691}
7692
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007693#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7694/** Recover a transaction that was interrupted by a power failure.
7695 *
7696 * This function is called during initialization, before psa_crypto_init()
7697 * returns. If this function returns a failure status, the initialization
7698 * fails.
7699 */
7700static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007701 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007702{
Gilles Peskine449bd832023-01-11 14:50:10 +01007703 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007704 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7705 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007706 /* TODO - fall through to the failure case until this
7707 * is implemented.
7708 * https://github.com/ARMmbed/mbed-crypto/issues/218
7709 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007710 default:
7711 /* We found an unsupported transaction in the storage.
7712 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007713 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007714 }
7715}
7716#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7717
Gilles Peskine449bd832023-01-11 14:50:10 +01007718psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007719{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007720 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007721
Gilles Peskinec6b69072018-11-20 21:42:52 +01007722 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007723 if (global_data.initialized != 0) {
7724 return PSA_SUCCESS;
7725 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007726
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007727 /* Init drivers */
7728 status = psa_driver_wrapper_init();
7729 if (status != PSA_SUCCESS) {
7730 goto exit;
7731 }
7732 global_data.drivers_initialized = 1;
7733
Gilles Peskine30524eb2020-11-13 17:02:26 +01007734 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007735 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007736 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007737 status = mbedtls_psa_random_seed(&global_data.rng);
7738 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007739 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007740 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007741 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007742
Gilles Peskine449bd832023-01-11 14:50:10 +01007743 status = psa_initialize_key_slots();
7744 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007745 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007746 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007747
Gilles Peskine4b734222019-07-24 15:56:31 +02007748#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007749 status = psa_crypto_load_transaction();
7750 if (status == PSA_SUCCESS) {
7751 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7752 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007753 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007754 }
7755 status = psa_crypto_stop_transaction();
7756 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007757 /* There's no transaction to complete. It's all good. */
7758 status = PSA_SUCCESS;
7759 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007760#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007761
Gilles Peskinec6b69072018-11-20 21:42:52 +01007762 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007763 global_data.initialized = 1;
7764
7765exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007766 if (status != PSA_SUCCESS) {
7767 mbedtls_psa_crypto_free();
7768 }
7769 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007770}
7771
Yanray Wangffc3c482023-07-11 12:01:04 +08007772#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007773psa_status_t psa_crypto_driver_pake_get_password_len(
7774 const psa_crypto_driver_pake_inputs_t *inputs,
7775 size_t *password_len)
7776{
7777 if (inputs->password_len == 0) {
7778 return PSA_ERROR_BAD_STATE;
7779 }
7780
7781 *password_len = inputs->password_len;
7782
7783 return PSA_SUCCESS;
7784}
7785
7786psa_status_t psa_crypto_driver_pake_get_password(
7787 const psa_crypto_driver_pake_inputs_t *inputs,
7788 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7789{
7790 if (inputs->password_len == 0) {
7791 return PSA_ERROR_BAD_STATE;
7792 }
7793
7794 if (buffer_size < inputs->password_len) {
7795 return PSA_ERROR_BUFFER_TOO_SMALL;
7796 }
7797
7798 memcpy(buffer, inputs->password, inputs->password_len);
7799 *buffer_length = inputs->password_len;
7800
7801 return PSA_SUCCESS;
7802}
7803
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007804psa_status_t psa_crypto_driver_pake_get_user_len(
7805 const psa_crypto_driver_pake_inputs_t *inputs,
7806 size_t *user_len)
7807{
7808 if (inputs->user_len == 0) {
7809 return PSA_ERROR_BAD_STATE;
7810 }
7811
7812 *user_len = inputs->user_len;
7813
7814 return PSA_SUCCESS;
7815}
7816
7817psa_status_t psa_crypto_driver_pake_get_user(
7818 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007819 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007820{
7821 if (inputs->user_len == 0) {
7822 return PSA_ERROR_BAD_STATE;
7823 }
7824
Przemek Stekielc0e62502023-03-14 11:49:36 +01007825 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007826 return PSA_ERROR_BUFFER_TOO_SMALL;
7827 }
7828
Przemek Stekielc0e62502023-03-14 11:49:36 +01007829 memcpy(user_id, inputs->user, inputs->user_len);
7830 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007831
7832 return PSA_SUCCESS;
7833}
7834
7835psa_status_t psa_crypto_driver_pake_get_peer_len(
7836 const psa_crypto_driver_pake_inputs_t *inputs,
7837 size_t *peer_len)
7838{
7839 if (inputs->peer_len == 0) {
7840 return PSA_ERROR_BAD_STATE;
7841 }
7842
7843 *peer_len = inputs->peer_len;
7844
7845 return PSA_SUCCESS;
7846}
7847
7848psa_status_t psa_crypto_driver_pake_get_peer(
7849 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007850 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007851{
7852 if (inputs->peer_len == 0) {
7853 return PSA_ERROR_BAD_STATE;
7854 }
7855
Przemek Stekielc0e62502023-03-14 11:49:36 +01007856 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007857 return PSA_ERROR_BUFFER_TOO_SMALL;
7858 }
7859
Przemek Stekielc0e62502023-03-14 11:49:36 +01007860 memcpy(peer_id, inputs->peer, inputs->peer_len);
7861 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007862
7863 return PSA_SUCCESS;
7864}
7865
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007866psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7867 const psa_crypto_driver_pake_inputs_t *inputs,
7868 psa_pake_cipher_suite_t *cipher_suite)
7869{
7870 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7871 return PSA_ERROR_BAD_STATE;
7872 }
7873
7874 *cipher_suite = inputs->cipher_suite;
7875
7876 return PSA_SUCCESS;
7877}
7878
Neil Armstronga7d08c32022-06-01 18:21:20 +02007879psa_status_t psa_pake_setup(
7880 psa_pake_operation_t *operation,
7881 const psa_pake_cipher_suite_t *cipher_suite)
7882{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007883 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007884
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007885 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007886 status = PSA_ERROR_BAD_STATE;
7887 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007888 }
7889
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007890 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007891 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007892 status = PSA_ERROR_INVALID_ARGUMENT;
7893 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007894 }
7895
Przemek Stekiel51eac532022-12-07 11:04:51 +01007896 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7897
Przemek Stekiele12ed362022-12-21 12:54:46 +01007898 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007899 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7900 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007901 operation->data.inputs.cipher_suite = *cipher_suite;
7902
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007903#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007904 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007905 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007906 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007907
David Horstmann16f01512023-06-14 17:21:07 +01007908 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007909 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007910 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007911#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007912 {
7913 status = PSA_ERROR_NOT_SUPPORTED;
7914 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007915 }
7916
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007917 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7918
Przemek Stekiel51eac532022-12-07 11:04:51 +01007919 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007920exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007921 psa_pake_abort(operation);
7922 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007923}
7924
7925psa_status_t psa_pake_set_password_key(
7926 psa_pake_operation_t *operation,
7927 mbedtls_svc_key_id_t password)
7928{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007929 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007930 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7931 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007932 psa_key_attributes_t attributes;
7933 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007934
Przemek Stekiel51eac532022-12-07 11:04:51 +01007935 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007936 status = PSA_ERROR_BAD_STATE;
7937 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007938 }
7939
Przemek Stekiel6c764412022-11-22 14:05:12 +01007940 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7941 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007942 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007943 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007944 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007945 }
7946
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007947 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007948 .core = slot->attr
7949 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007950
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007951 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007952
Przemek Stekiel51eac532022-12-07 11:04:51 +01007953 if (type != PSA_KEY_TYPE_PASSWORD &&
7954 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7955 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007956 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007957 }
7958
Przemek Stekiel51eac532022-12-07 11:04:51 +01007959 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7960 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007961 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007962 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007963 }
7964
7965 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7966 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007967 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007968exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007969 if (status != PSA_SUCCESS) {
7970 psa_pake_abort(operation);
7971 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007972 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007973 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007974}
7975
7976psa_status_t psa_pake_set_user(
7977 psa_pake_operation_t *operation,
7978 const uint8_t *user_id,
7979 size_t user_id_len)
7980{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007981 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007982
7983 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007984 status = PSA_ERROR_BAD_STATE;
7985 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007986 }
7987
Przemek Stekiel51eac532022-12-07 11:04:51 +01007988 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007989 status = PSA_ERROR_INVALID_ARGUMENT;
7990 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007991 }
7992
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007993 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007994 status = PSA_ERROR_BAD_STATE;
7995 goto exit;
7996 }
7997
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007998 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7999 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008000 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8001 goto exit;
8002 }
8003
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008004 memcpy(operation->data.inputs.user, user_id, user_id_len);
8005 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008006
8007 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008008exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008009 psa_pake_abort(operation);
8010 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008011}
8012
8013psa_status_t psa_pake_set_peer(
8014 psa_pake_operation_t *operation,
8015 const uint8_t *peer_id,
8016 size_t peer_id_len)
8017{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008018 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008019
8020 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008021 status = PSA_ERROR_BAD_STATE;
8022 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008023 }
8024
Przemek Stekiel51eac532022-12-07 11:04:51 +01008025 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008026 status = PSA_ERROR_INVALID_ARGUMENT;
8027 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008028 }
8029
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008030 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008031 status = PSA_ERROR_BAD_STATE;
8032 goto exit;
8033 }
8034
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008035 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8036 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008037 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8038 goto exit;
8039 }
8040
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008041 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8042 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008043
8044 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008045exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008046 psa_pake_abort(operation);
8047 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008048}
8049
8050psa_status_t psa_pake_set_role(
8051 psa_pake_operation_t *operation,
8052 psa_pake_role_t role)
8053{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008054 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008055
Przemek Stekiel51eac532022-12-07 11:04:51 +01008056 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008057 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008058 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008059 }
8060
Przemek Stekiel09104b82023-03-06 14:11:51 +01008061 switch (operation->alg) {
8062#if defined(PSA_WANT_ALG_JPAKE)
8063 case PSA_ALG_JPAKE:
8064 if (role == PSA_PAKE_ROLE_NONE) {
8065 return PSA_SUCCESS;
8066 }
8067 status = PSA_ERROR_INVALID_ARGUMENT;
8068 break;
8069#endif
8070 default:
8071 (void) role;
8072 status = PSA_ERROR_NOT_SUPPORTED;
8073 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008074 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008075exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008076 psa_pake_abort(operation);
8077 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008078}
8079
David Horstmanne7f21e62023-05-12 18:17:21 +01008080/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008081#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008082static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008083 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008084{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008085 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008086 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008087 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008088
David Horstmann024e5c52023-06-14 15:48:21 +01008089 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008090 is_x1 = (stage->outputs < 1);
8091 } else {
8092 is_x1 = (stage->inputs < 1);
8093 }
8094
David Horstmann74a3d8c2023-06-14 18:28:19 +01008095 key_share_step = is_x1 ?
8096 PSA_JPAKE_X1_STEP_KEY_SHARE :
8097 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008098 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008099 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8100 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8101 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8102 } else {
8103 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008104 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008105 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008106}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008107#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008108
Przemek Stekiel2797d372022-12-22 11:19:22 +01008109static psa_status_t psa_pake_complete_inputs(
8110 psa_pake_operation_t *operation)
8111{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008112 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008113 /* Create copy of the inputs on stack as inputs share memory
8114 with the driver context which will be setup by the driver. */
8115 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008116
Przemek Stekielfde11282023-03-13 16:06:09 +01008117 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008118 return PSA_ERROR_BAD_STATE;
8119 }
8120
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008121 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008122 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8123 return PSA_ERROR_BAD_STATE;
8124 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008125 }
8126
Przemek Stekiel18620a32023-01-17 16:34:52 +01008127 /* Clear driver context */
8128 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8129
8130 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008131
8132 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008133 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008134
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008135 /* User and peer are translated to role. */
8136 mbedtls_free(inputs.user);
8137 mbedtls_free(inputs.peer);
8138
Przemek Stekiel2797d372022-12-22 11:19:22 +01008139 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008140#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008141 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008142 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008143 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008144#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008145 {
8146 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008147 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008148 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008149 return status;
8150}
8151
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008152#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008153static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008154 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008155 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008156 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008157{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008158 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8159 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8160 step != PSA_PAKE_STEP_ZK_PROOF) {
8161 return PSA_ERROR_INVALID_ARGUMENT;
8162 }
8163
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008164 psa_jpake_computation_stage_t *computation_stage =
8165 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008166
David Horstmann5da95602023-06-08 15:37:12 +01008167 if (computation_stage->round != PSA_JPAKE_FIRST &&
8168 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008169 return PSA_ERROR_BAD_STATE;
8170 }
8171
David Horstmanne7f21e62023-05-12 18:17:21 +01008172 /* Check that the step we are given is the one we were expecting */
8173 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008174 return PSA_ERROR_BAD_STATE;
8175 }
8176
David Horstmanne7f21e62023-05-12 18:17:21 +01008177 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8178 computation_stage->inputs == 0 &&
8179 computation_stage->outputs == 0) {
8180 /* Start of the round, so function decides whether we are inputting
8181 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008182 computation_stage->io_mode = io_mode;
8183 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008184 /* Middle of the round so the mode we are in must match the function
8185 * called by the user */
8186 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008187 }
8188
8189 return PSA_SUCCESS;
8190}
8191
David Horstmanne7f21e62023-05-12 18:17:21 +01008192static psa_status_t psa_jpake_epilogue(
8193 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008194 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008195{
David Horstmanne7f21e62023-05-12 18:17:21 +01008196 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008197 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008198
David Horstmanne7f21e62023-05-12 18:17:21 +01008199 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8200 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008201 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008202 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008203 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008204 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008205 }
8206 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008207 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008208 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008209 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008210 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008211 }
8212 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008213 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8214 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008215 /* End of a round, move to the next round */
8216 stage->inputs = 0;
8217 stage->outputs = 0;
8218 stage->round++;
8219 }
8220 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008221 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008222 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008223 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008224 return PSA_SUCCESS;
8225}
David Horstmanne7f21e62023-05-12 18:17:21 +01008226
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008227#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008228
Neil Armstronga7d08c32022-06-01 18:21:20 +02008229psa_status_t psa_pake_output(
8230 psa_pake_operation_t *operation,
8231 psa_pake_step_t step,
8232 uint8_t *output,
8233 size_t output_size,
8234 size_t *output_length)
8235{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008236 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008237 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008238 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008239
8240 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008241 status = psa_pake_complete_inputs(operation);
8242 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008243 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008244 }
8245 }
8246
8247 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008248 status = PSA_ERROR_BAD_STATE;
8249 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008250 }
8251
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008252 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008253 status = PSA_ERROR_INVALID_ARGUMENT;
8254 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008255 }
8256
Przemek Stekiele12ed362022-12-21 12:54:46 +01008257 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008258#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008259 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008260 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008261 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008262 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008263 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008264 driver_step = convert_jpake_computation_stage_to_driver_step(
8265 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008266 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008267#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008268 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008269 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008270 status = PSA_ERROR_NOT_SUPPORTED;
8271 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008272 }
8273
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008274 status = psa_driver_wrapper_pake_output(operation, driver_step,
8275 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008276
8277 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008278 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008279 }
8280
8281 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008282#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008283 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008284 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008285 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008286 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008287 }
8288 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008289#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008290 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008291 status = PSA_ERROR_NOT_SUPPORTED;
8292 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008293 }
8294
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008295 return PSA_SUCCESS;
8296exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008297 psa_pake_abort(operation);
8298 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008299}
8300
8301psa_status_t psa_pake_input(
8302 psa_pake_operation_t *operation,
8303 psa_pake_step_t step,
8304 const uint8_t *input,
8305 size_t input_length)
8306{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008307 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008308 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008309 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8310 operation->primitive,
8311 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008312
8313 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008314 status = psa_pake_complete_inputs(operation);
8315 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008316 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008317 }
8318 }
8319
8320 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008321 status = PSA_ERROR_BAD_STATE;
8322 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008323 }
8324
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008325 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008326 status = PSA_ERROR_INVALID_ARGUMENT;
8327 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008328 }
8329
Przemek Stekiele12ed362022-12-21 12:54:46 +01008330 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008331#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008332 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008333 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008334 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008335 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008336 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008337 driver_step = convert_jpake_computation_stage_to_driver_step(
8338 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008339 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008340#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008341 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008342 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008343 status = PSA_ERROR_NOT_SUPPORTED;
8344 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008345 }
8346
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008347 status = psa_driver_wrapper_pake_input(operation, driver_step,
8348 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008349
8350 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008351 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008352 }
8353
8354 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008355#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008356 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008357 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008358 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008359 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008360 }
8361 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008362#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008363 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008364 status = PSA_ERROR_NOT_SUPPORTED;
8365 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008366 }
8367
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008368 return PSA_SUCCESS;
8369exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008370 psa_pake_abort(operation);
8371 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008372}
8373
8374psa_status_t psa_pake_get_implicit_key(
8375 psa_pake_operation_t *operation,
8376 psa_key_derivation_operation_t *output)
8377{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008378 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008379 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008380 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008381 size_t shared_key_len = 0;
8382
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008383 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008384 status = PSA_ERROR_BAD_STATE;
8385 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008386 }
8387
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008388#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008389 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008390 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008391 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008392 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008393 status = PSA_ERROR_BAD_STATE;
8394 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008395 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008396 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008397#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008398 {
8399 status = PSA_ERROR_NOT_SUPPORTED;
8400 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008401 }
8402
Przemek Stekiel0c781802022-11-29 14:53:13 +01008403 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8404 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008405 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008406 &shared_key_len);
8407
8408 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008409 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008410 }
8411
8412 status = psa_key_derivation_input_bytes(output,
8413 PSA_KEY_DERIVATION_INPUT_SECRET,
8414 shared_key,
8415 shared_key_len);
8416
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008417 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008418exit:
8419 abort_status = psa_pake_abort(operation);
8420 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008421}
8422
8423psa_status_t psa_pake_abort(
8424 psa_pake_operation_t *operation)
8425{
Przemek Stekield69dca92023-01-31 19:59:20 +01008426 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008427
Przemek Stekield69dca92023-01-31 19:59:20 +01008428 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008429 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008430 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008431
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008432 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8433 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008434 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008435 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008436 }
8437 if (operation->data.inputs.user != NULL) {
8438 mbedtls_free(operation->data.inputs.user);
8439 }
8440 if (operation->data.inputs.peer != NULL) {
8441 mbedtls_free(operation->data.inputs.peer);
8442 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008443 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008444 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008445
Przemek Stekield69dca92023-01-31 19:59:20 +01008446 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008447}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008448#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008449
David Horstmanne138dce2023-11-28 15:21:56 +00008450/* Memory copying test hooks */
8451#if defined(MBEDTLS_TEST_HOOKS)
8452void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
8453void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
8454void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
8455void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
8456#endif
David Horstmannfde97392023-10-30 20:29:43 +00008457
David Horstmann1b7279a2023-11-15 15:18:30 +00008458/** Copy from an input buffer to a local copy.
8459 *
8460 * \param[in] input Pointer to input buffer.
8461 * \param[in] input_len Length of the input buffer.
8462 * \param[out] input_copy Pointer to a local copy in which to store the input data.
8463 * \param[out] input_copy_len Length of the local copy buffer.
8464 * \return #PSA_SUCCESS, if the buffer was successfully
8465 * copied.
8466 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
8467 * copy is too small to hold contents of the
8468 * input buffer.
8469 */
8470MBEDTLS_STATIC_TESTABLE
David Horstmannfde97392023-10-30 20:29:43 +00008471psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
8472 uint8_t *input_copy, size_t input_copy_len)
8473{
8474 if (input_len > input_copy_len) {
David Horstmann49a72762023-11-03 19:51:40 +00008475 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmannfde97392023-10-30 20:29:43 +00008476 }
8477
David Horstmann372b8bb2023-11-24 16:21:04 +00008478#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008479 if (psa_input_pre_copy_hook != NULL) {
8480 psa_input_pre_copy_hook(input, input_len);
8481 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008482#endif
8483
David Horstmann777e7412023-11-15 17:33:47 +00008484 if (input_len > 0) {
8485 memcpy(input_copy, input, input_len);
8486 }
David Horstmannfde97392023-10-30 20:29:43 +00008487
David Horstmann372b8bb2023-11-24 16:21:04 +00008488#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008489 if (psa_input_post_copy_hook != NULL) {
8490 psa_input_post_copy_hook(input, input_len);
8491 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008492#endif
8493
David Horstmannfde97392023-10-30 20:29:43 +00008494 return PSA_SUCCESS;
8495}
8496
David Horstmann1b7279a2023-11-15 15:18:30 +00008497/** Copy from a local output buffer into a user-supplied one.
8498 *
8499 * \param[in] output_copy Pointer to a local buffer containing the output.
8500 * \param[in] output_copy_len Length of the local buffer.
8501 * \param[out] output Pointer to user-supplied output buffer.
8502 * \param[out] output_len Length of the user-supplied output buffer.
8503 * \return #PSA_SUCCESS, if the buffer was successfully
8504 * copied.
David Horstmann671f5f52023-11-20 12:39:38 +00008505 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmann1b7279a2023-11-15 15:18:30 +00008506 * user-supplied output buffer is too small to
8507 * hold the contents of the local buffer.
8508 */
8509MBEDTLS_STATIC_TESTABLE
David Horstmann8978f5c2023-10-30 20:37:12 +00008510psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
8511 uint8_t *output, size_t output_len)
8512{
8513 if (output_len < output_copy_len) {
David Horstmann671f5f52023-11-20 12:39:38 +00008514 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann8978f5c2023-10-30 20:37:12 +00008515 }
David Horstmann777e7412023-11-15 17:33:47 +00008516
David Horstmann372b8bb2023-11-24 16:21:04 +00008517#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008518 if (psa_output_pre_copy_hook != NULL) {
8519 psa_output_pre_copy_hook(output, output_len);
8520 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008521#endif
8522
David Horstmann777e7412023-11-15 17:33:47 +00008523 if (output_copy_len > 0) {
8524 memcpy(output, output_copy, output_copy_len);
8525 }
8526
David Horstmann372b8bb2023-11-24 16:21:04 +00008527#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008528 if (psa_output_post_copy_hook != NULL) {
8529 psa_output_post_copy_hook(output, output_len);
8530 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008531#endif
8532
David Horstmann8978f5c2023-10-30 20:37:12 +00008533 return PSA_SUCCESS;
8534}
8535
David Horstmannf1734052023-11-20 17:15:32 +00008536psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
8537 psa_crypto_local_input_t *local_input)
David Horstmann4ac78852023-11-07 16:36:48 +00008538{
8539 psa_status_t status;
8540
David Horstmann9db14482023-11-23 15:50:37 +00008541 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann4ac78852023-11-07 16:36:48 +00008542
David Horstmannc5cc1c32023-11-15 18:11:26 +00008543 if (input_len == 0) {
David Horstmann4ac78852023-11-07 16:36:48 +00008544 return PSA_SUCCESS;
8545 }
8546
David Horstmannf1734052023-11-20 17:15:32 +00008547 local_input->buffer = mbedtls_calloc(input_len, 1);
8548 if (local_input->buffer == NULL) {
David Horstmann4ac78852023-11-07 16:36:48 +00008549 /* Since we dealt with the zero-length case above, we know that
8550 * a NULL return value means a failure of allocation. */
8551 return PSA_ERROR_INSUFFICIENT_MEMORY;
8552 }
David Horstmannf1734052023-11-20 17:15:32 +00008553 /* From now on, we must free local_input->buffer on error. */
David Horstmann4ac78852023-11-07 16:36:48 +00008554
David Horstmannf1734052023-11-20 17:15:32 +00008555 local_input->length = input_len;
David Horstmann4ac78852023-11-07 16:36:48 +00008556
8557 status = psa_crypto_copy_input(input, input_len,
David Horstmannf1734052023-11-20 17:15:32 +00008558 local_input->buffer, local_input->length);
David Horstmann4ac78852023-11-07 16:36:48 +00008559 if (status != PSA_SUCCESS) {
8560 goto error;
8561 }
8562
8563 return PSA_SUCCESS;
8564
8565error:
David Horstmannf1734052023-11-20 17:15:32 +00008566 mbedtls_free(local_input->buffer);
8567 local_input->buffer = NULL;
8568 local_input->length = 0;
David Horstmann4ac78852023-11-07 16:36:48 +00008569 return status;
8570}
8571
David Horstmannf1734052023-11-20 17:15:32 +00008572void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmanne6042ff2023-11-07 18:00:41 +00008573{
David Horstmannf1734052023-11-20 17:15:32 +00008574 mbedtls_free(local_input->buffer);
8575 local_input->buffer = NULL;
8576 local_input->length = 0;
David Horstmanne6042ff2023-11-07 18:00:41 +00008577}
8578
David Horstmann89875a42023-11-20 12:54:09 +00008579psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
8580 psa_crypto_local_output_t *local_output)
David Horstmannba3c7d62023-11-08 15:19:43 +00008581{
David Horstmann9db14482023-11-23 15:50:37 +00008582 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmannba3c7d62023-11-08 15:19:43 +00008583
David Horstmannc5cc1c32023-11-15 18:11:26 +00008584 if (output_len == 0) {
David Horstmannba3c7d62023-11-08 15:19:43 +00008585 return PSA_SUCCESS;
8586 }
David Horstmann89875a42023-11-20 12:54:09 +00008587 local_output->buffer = mbedtls_calloc(output_len, 1);
8588 if (local_output->buffer == NULL) {
David Horstmannba3c7d62023-11-08 15:19:43 +00008589 /* Since we dealt with the zero-length case above, we know that
8590 * a NULL return value means a failure of allocation. */
8591 return PSA_ERROR_INSUFFICIENT_MEMORY;
8592 }
David Horstmann89875a42023-11-20 12:54:09 +00008593 local_output->length = output_len;
8594 local_output->original = output;
David Horstmannba3c7d62023-11-08 15:19:43 +00008595
8596 return PSA_SUCCESS;
8597}
8598
David Horstmann89875a42023-11-20 12:54:09 +00008599psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmann9467ea32023-11-08 17:44:18 +00008600{
David Horstmannc335a4e2023-11-15 15:57:32 +00008601 psa_status_t status;
8602
David Horstmann89875a42023-11-20 12:54:09 +00008603 if (local_output->buffer == NULL) {
8604 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00008605 return PSA_SUCCESS;
8606 }
David Horstmann89875a42023-11-20 12:54:09 +00008607 if (local_output->original == NULL) {
David Horstmann9467ea32023-11-08 17:44:18 +00008608 /* We have an internal copy but nothing to copy back to. */
8609 return PSA_ERROR_CORRUPTION_DETECTED;
8610 }
8611
David Horstmann89875a42023-11-20 12:54:09 +00008612 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
8613 local_output->original, local_output->length);
David Horstmannc335a4e2023-11-15 15:57:32 +00008614 if (status != PSA_SUCCESS) {
8615 return status;
8616 }
David Horstmann9467ea32023-11-08 17:44:18 +00008617
David Horstmann89875a42023-11-20 12:54:09 +00008618 mbedtls_free(local_output->buffer);
8619 local_output->buffer = NULL;
8620 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00008621
8622 return PSA_SUCCESS;
8623}
8624
Gilles Peskinee59236f2018-01-27 23:32:46 +01008625#endif /* MBEDTLS_PSA_CRYPTO_C */