blob: 9de7cd7db424eb7a55ec8b9d59573948098feb48 [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
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Ronald Cronafbc7ed2023-01-24 16:02:04 +010022#include "psa_crypto_core_common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010023
24#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030025
John Durkop07cc04a2020-11-16 22:08:34 -080026#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
27#include "check_crypto_config.h"
28#endif
29
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010031#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010032
Ronald Crond6d28882020-12-14 14:56:02 +010033#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010034#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010035#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020036#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010037#include "psa_crypto_ecp.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010038#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010039#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010040#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010041#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020042#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020043#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020044#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010045#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010046/* Include internal declarations that are useful for implementing persistently
47 * stored keys. */
48#include "psa_crypto_storage.h"
49
Gilles Peskineb2b64d32020-12-14 16:43:58 +010050#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010051
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010052#include <stdlib.h>
53#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010054#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010055
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010056#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020057#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000058#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020059#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020061#include "mbedtls/chacha20.h"
62#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/cipher.h"
64#include "mbedtls/ccm.h"
65#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020067#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010068#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010069#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/error.h"
71#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010072#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000074#include "md_wrap.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000076#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000078#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010080#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/sha1.h"
82#include "mbedtls/sha256.h"
83#include "mbedtls/sha512.h"
Paul Elliott588f8ed2022-12-02 18:10:26 +000084#include "hash_info.h"
Przemek Stekiel472b3f32022-12-01 14:38:49 +010085#include "mbedtls/dhm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086
Gilles Peskine449bd832023-01-11 14:50:10 +010087#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020088
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020089#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
90 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
91 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020092#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020093#endif
94
Przemek Stekielfde11282023-03-13 16:06:09 +010095/* The only two JPAKE user/peer identifiers supported for the time being. */
96static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
97static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
Przemek Stekiel18cd6c92023-03-06 15:40:35 +010098
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010099/****************************************************************/
100/* Global data, support functions and library management */
101/****************************************************************/
102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200104{
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200106}
107
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100108/* Values for psa_global_data_t::rng_state */
109#define RNG_NOT_INITIALIZED 0
110#define RNG_INITIALIZED 1
111#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100114 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100115 unsigned rng_state : 2;
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100116 unsigned drivers_initialized : 1;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100117 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118} psa_global_data_t;
119
120static psa_global_data_t global_data;
121
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100122#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
123mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
124 &global_data.rng.drbg;
125#endif
126
itayzafrir0adf0fc2018-09-06 16:24:41 +0300127#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 if (global_data.initialized == 0) \
129 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300130
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100131int psa_can_do_hash(psa_algorithm_t hash_alg)
132{
133 (void) hash_alg;
134 return global_data.drivers_initialized;
135}
136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100138{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100139 /* Mbed TLS error codes can combine a high-level error code and a
140 * low-level error code. The low-level error usually reflects the
141 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 int low_level_ret = -(-ret & 0x007f);
143 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100144 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100146
147 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
148 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200150 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
151 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
152 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
153 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
154 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200156 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200158 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200160
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000162 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000163#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100164 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100166
167 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100169 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100171
Gilles Peskine26869f22019-05-06 15:25:00 +0200172 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200174
175 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200177 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200179
Gilles Peskinea5905292018-02-07 20:59:33 +0100180 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100182 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100184 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100186 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100188 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100190 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100192 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
196 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100197 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
198 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100199 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100201 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
202 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100204 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100206#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100207
208 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100210
Gilles Peskinee59236f2018-01-27 23:32:46 +0100211 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
212 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
213 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100215
216 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200218 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100220 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100222
Gilles Peskine82e57d12020-11-13 21:31:17 +0100223#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100225 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
226 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100227 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100229 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
230 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100232 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100234#endif
235
Gilles Peskinea5905292018-02-07 20:59:33 +0100236 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100238 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100240 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100242 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100244
Gilles Peskinef76aa772018-10-29 19:24:33 +0100245 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100247 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100249 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100251 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100253 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100255 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100257 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100259 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100261
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100262 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100264 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
265 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100267 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100269 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
270 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100272 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100274 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
275 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100277 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100279 case MBEDTLS_ERR_PK_INVALID_ALG:
280 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
281 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100283 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200285 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100287
Gilles Peskineff2d2002019-05-06 15:26:23 +0200288 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200290 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200292
Gilles Peskinea5905292018-02-07 20:59:33 +0100293 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100295 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100297 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100299 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100301 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
302 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100308 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200310
itayzafrir5c753392018-05-08 11:18:38 +0300311 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300312 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300314 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300316 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300318 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
319 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300321 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100323 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100325
Paul Elliott588f8ed2022-12-02 18:10:26 +0000326 case MBEDTLS_ERR_ECP_IN_PROGRESS:
327 return PSA_OPERATION_INCOMPLETE;
328
Janos Follatha13b9052019-11-22 12:48:59 +0000329 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300331
Gilles Peskinee59236f2018-01-27 23:32:46 +0100332 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100334 }
335}
336
Paul Elliottdc42ca82023-02-24 18:11:59 +0000337/**
338 * \brief For output buffers which contain "tags"
339 * (outputs that may be checked for validity like
340 * hashes, MACs and signatures), fill the unused
341 * part of the output buffer (the whole buffer on
342 * error, the trailing part on success) with
343 * something that isn't a valid tag (barring an
344 * attack on the tag and deliberately-crafted
345 * input), in case the caller doesn't check the
346 * return status properly.
347 *
348 * \param output_buffer Pointer to buffer to wipe. May not be NULL
349 * unless \p output_buffer_size is zero.
350 * \param status Status of function called to generate
351 * output_buffer originally
352 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
353 * could be NULL.
354 * \param output_buffer_length Length of data written to output_buffer, must be
355 * less than \p output_buffer_size
356 */
357static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000358 size_t output_buffer_size, size_t output_buffer_length)
359{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000360 size_t offset = 0;
361
362 if (output_buffer_size == 0) {
363 /* If output_buffer_size is 0 then we have nothing to do. We must not
364 call memset because output_buffer may be NULL in this case */
365 return;
366 }
367
368 if (status == PSA_SUCCESS) {
369 offset = output_buffer_length;
370 }
371
372 memset(output_buffer + offset, '!', output_buffer_size - offset);
373}
374
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200375
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200376
377
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100378/****************************************************************/
379/* Key management */
380/****************************************************************/
381
Valerio Settid4a5d462023-04-05 18:19:01 +0200382#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100383mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
384 size_t bits,
385 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200386{
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100388 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100390#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100391 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100393#endif
394#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100395 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100397#endif
398#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100399 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100401#endif
402#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100403 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100405#endif
406#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100407 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100409 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 if (bits_is_sloppy) {
411 return MBEDTLS_ECP_DP_SECP521R1;
412 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100413 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100414#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100415 }
416 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100417
Paul Elliott8ff510a2020-06-02 17:19:28 +0100418 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100420#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100421 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100423#endif
424#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100425 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100427#endif
428#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100429 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100431#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100432 }
433 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100434
Paul Elliott8ff510a2020-06-02 17:19:28 +0100435 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100437#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100438 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100440 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 if (bits_is_sloppy) {
442 return MBEDTLS_ECP_DP_CURVE25519;
443 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100444 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100445#endif
446#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100447 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100449#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100450 }
451 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100452
Paul Elliott8ff510a2020-06-02 17:19:28 +0100453 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100455#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100456 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100458#endif
459#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100460 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100462#endif
463#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100464 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100466#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100467 }
468 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200469 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100470
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100471 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200473}
Valerio Settid4a5d462023-04-05 18:19:01 +0200474#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
477 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200478{
479 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200481 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200482 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200483 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200484 case PSA_KEY_TYPE_PASSWORD:
485 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200486 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100487#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200488 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (bits != 128 && bits != 192 && bits != 256) {
490 return PSA_ERROR_INVALID_ARGUMENT;
491 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200492 break;
493#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200494#if defined(PSA_WANT_KEY_TYPE_ARIA)
495 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 if (bits != 128 && bits != 192 && bits != 256) {
497 return PSA_ERROR_INVALID_ARGUMENT;
498 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200499 break;
500#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100501#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200502 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if (bits != 128 && bits != 192 && bits != 256) {
504 return PSA_ERROR_INVALID_ARGUMENT;
505 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200506 break;
507#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100508#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200509 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if (bits != 64 && bits != 128 && bits != 192) {
511 return PSA_ERROR_INVALID_ARGUMENT;
512 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200513 break;
514#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100515#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200516 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if (bits != 256) {
518 return PSA_ERROR_INVALID_ARGUMENT;
519 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200520 break;
521#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200522 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200524 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 if (bits % 8 != 0) {
526 return PSA_ERROR_INVALID_ARGUMENT;
527 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200530}
531
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100532/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100533 *
Steven Cooremancd640932021-03-08 12:00:27 +0100534 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
535 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100536 *
537 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100538 * \param[in] key_type The key type of the key to be used with the
539 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100540 *
541 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100542 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100543 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100544 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100545 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100546MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100547 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100549{
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 if (PSA_ALG_IS_HMAC(algorithm)) {
551 if (key_type == PSA_KEY_TYPE_HMAC) {
552 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100553 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100554 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
557 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
558 * key. */
559 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
560 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
561 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
562 * the block length (larger than 1) for block ciphers. */
563 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
564 return PSA_SUCCESS;
565 }
566 }
567 }
568
569 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100570}
571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
573 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200574{
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 if (slot->key.data != NULL) {
576 return PSA_ERROR_ALREADY_EXISTS;
577 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 slot->key.data = mbedtls_calloc(1, buffer_length);
580 if (slot->key.data == NULL) {
581 return PSA_ERROR_INSUFFICIENT_MEMORY;
582 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200583
Ronald Cronea0f8a62020-11-25 17:52:23 +0100584 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200586}
587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
589 const uint8_t *data,
590 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200591{
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 psa_status_t status = psa_allocate_buffer_to_slot(slot,
593 data_length);
594 if (status != PSA_SUCCESS) {
595 return status;
596 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 memcpy(slot->key.data, data, data_length);
599 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200600}
601
Ronald Crona0fe59f2020-11-29 11:14:53 +0100602psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100603 const psa_key_attributes_t *attributes,
604 const uint8_t *data, size_t data_length,
605 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100607{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100608 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
609 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100610
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200611 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 if (data_length == 0) {
613 return PSA_ERROR_NOT_SUPPORTED;
614 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 if (key_type_is_raw_bytes(type)) {
617 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
620 *bits);
621 if (status != PSA_SUCCESS) {
622 return status;
623 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200624
Ronald Crondd04d422020-11-28 15:14:42 +0100625 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100627 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 return PSA_SUCCESS;
631 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100632#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) || \
633 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY)
634 if (PSA_KEY_TYPE_IS_DH(type)) {
635 if (data_length != 256 && data_length != 384 &&
636 data_length != 512 && data_length != 768 &&
637 data_length != 1024) {
638 return PSA_ERROR_INVALID_ARGUMENT;
639 }
640
641 /* Copy the key material. */
642 memcpy(key_buffer, data, data_length);
643 *key_buffer_length = data_length;
644 *bits = PSA_BYTES_TO_BITS(data_length);
645 (void) key_buffer_size;
646
647 return PSA_SUCCESS;
648 }
649#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) ||
650 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY) */
John Durkop9814fa22020-11-04 12:28:15 -0800651#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
653 if (PSA_KEY_TYPE_IS_ECC(type)) {
654 return mbedtls_psa_ecp_import_key(attributes,
655 data, data_length,
656 key_buffer, key_buffer_size,
657 key_buffer_length,
658 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100659 }
John Durkop9814fa22020-11-04 12:28:15 -0800660#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
661 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700662#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
664 if (PSA_KEY_TYPE_IS_RSA(type)) {
665 return mbedtls_psa_rsa_import_key(attributes,
666 data, data_length,
667 key_buffer, key_buffer_size,
668 key_buffer_length,
669 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200670 }
John Durkop9814fa22020-11-04 12:28:15 -0800671#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
672 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100673 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100676}
677
Gilles Peskinef603c712019-01-19 13:40:11 +0100678/** Calculate the intersection of two algorithm usage policies.
679 *
680 * Return 0 (which allows no operation) on incompatibility.
681 */
682static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100683 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100684 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100686{
Gilles Peskine549ea862019-05-22 11:45:59 +0200687 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 if (alg1 == alg2) {
689 return alg1;
690 }
Steven Cooreman03488022021-02-18 12:07:20 +0100691 /* If the policies are from the same hash-and-sign family, check
692 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
694 PSA_ALG_IS_SIGN_HASH(alg2) &&
695 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
696 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
697 return alg2;
698 }
699 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
700 return alg1;
701 }
Steven Cooreman03488022021-02-18 12:07:20 +0100702 }
703 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100704 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100705 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
707 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
708 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
709 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
710 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100711 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100712
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100713 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
715 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
716 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
717 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100718 }
719 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
721 (alg1_len <= alg2_len)) {
722 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100723 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
725 (alg2_len <= alg1_len)) {
726 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100727 }
728 }
729 /* If the policies are from the same MAC family, check whether one
730 * of them is a minimum-MAC-length policy. Calculate the most
731 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
733 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
734 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100735 /* Validate the combination of key type and algorithm. Since the base
736 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
738 return 0;
739 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100740
Steven Cooreman31a876d2021-03-03 20:47:40 +0100741 /* Get the (exact or at-least) output lengths for both sides of the
742 * requested intersection. None of the currently supported algorithms
743 * have an output length dependent on the actual key size, so setting it
744 * to a bogus value of 0 is currently OK.
745 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100746 * Note that for at-least-this-length wildcard algorithms, the output
747 * length is set to the shortest allowed length, which allows us to
748 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
750 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100751 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100752
Steven Cooremana1d83222021-02-25 10:20:29 +0100753 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
755 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
756 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100757 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100758
759 /* If only one is an at-least-this-length policy, the intersection would
760 * be the other (fixed-length) policy as long as said fixed length is
761 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
763 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100764 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
766 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100767 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100768
Steven Cooremancd640932021-03-08 12:00:27 +0100769 /* If none of them are wildcards, check whether they define the same tag
770 * length. This is still possible here when one is default-length and
771 * the other specific-length. Ensure to always return the
772 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (alg1_len == alg2_len) {
774 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
775 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100776 }
777 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100779}
780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781static int psa_key_algorithm_permits(psa_key_type_t key_type,
782 psa_algorithm_t policy_alg,
783 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200784{
Gilles Peskine549ea862019-05-22 11:45:59 +0200785 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if (requested_alg == policy_alg) {
787 return 1;
788 }
Steven Cooreman03488022021-02-18 12:07:20 +0100789 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
790 * and requested_alg is the same hash-and-sign family with any hash,
791 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
793 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
794 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
795 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100796 }
797 /* If policy_alg is a wildcard AEAD algorithm of the same base as
798 * the requested algorithm, check the requested tag length to be
799 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if (PSA_ALG_IS_AEAD(policy_alg) &&
801 PSA_ALG_IS_AEAD(requested_alg) &&
802 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
803 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
804 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
805 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
806 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100807 }
Steven Cooremancd640932021-03-08 12:00:27 +0100808 /* If policy_alg is a MAC algorithm of the same base as the requested
809 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 if (PSA_ALG_IS_MAC(policy_alg) &&
811 PSA_ALG_IS_MAC(requested_alg) &&
812 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
813 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100814 /* Validate the combination of key type and algorithm. Since the policy
815 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
817 return 0;
818 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100819
Steven Cooreman31a876d2021-03-03 20:47:40 +0100820 /* Get both the requested output length for the algorithm which is to be
821 * verified, and the default output length for the base algorithm.
822 * Note that none of the currently supported algorithms have an output
823 * length dependent on actual key size, so setting it to a bogus value
824 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100825 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100827 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 key_type, 0,
829 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100830
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100831 /* If the policy is default-length, only allow an algorithm with
832 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
834 return requested_output_length == default_output_length;
835 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100836
837 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100838 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
840 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
841 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100842 }
843
Steven Cooremancd640932021-03-08 12:00:27 +0100844 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
845 * check for the requested MAC length to be equal to or longer than the
846 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
848 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
849 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100850 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200851 }
Steven Cooremance48e852020-10-05 16:02:45 +0200852 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200853 * a key derivation with that key agreement should also be allowed. This
854 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
856 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
857 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
858 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200859 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100860 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200862}
863
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100864/** Test whether a policy permits an algorithm.
865 *
866 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100867 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100868 * \note This function requires providing the key type for which the policy is
869 * being validated, since some algorithm policy definitions (e.g. MAC)
870 * have different properties depending on what kind of cipher it is
871 * combined with.
872 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100873 * \retval PSA_SUCCESS When \p alg is a specific algorithm
874 * allowed by the \p policy.
875 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
876 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
877 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100878 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100879static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
880 psa_key_type_t key_type,
881 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100882{
Steven Cooremand788fab2021-02-25 11:29:17 +0100883 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if (alg == 0) {
885 return PSA_ERROR_INVALID_ARGUMENT;
886 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100887
Steven Cooreman7e39f052021-02-23 14:18:32 +0100888 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if (PSA_ALG_IS_WILDCARD(alg)) {
890 return PSA_ERROR_INVALID_ARGUMENT;
891 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
894 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
895 return PSA_SUCCESS;
896 } else {
897 return PSA_ERROR_NOT_PERMITTED;
898 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100899}
900
Gilles Peskinef603c712019-01-19 13:40:11 +0100901/** Restrict a key policy based on a constraint.
902 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100903 * \note This function requires providing the key type for which the policy is
904 * being restricted, since some algorithm policy definitions (e.g. MAC)
905 * have different properties depending on what kind of cipher it is
906 * combined with.
907 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100908 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100909 * \param[in,out] policy The policy to restrict.
910 * \param[in] constraint The policy constraint to apply.
911 *
912 * \retval #PSA_SUCCESS
913 * \c *policy contains the intersection of the original value of
914 * \c *policy and \c *constraint.
915 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100916 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100917 * \c *policy is unchanged.
918 */
919static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100920 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100921 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100923{
924 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 psa_key_policy_algorithm_intersection(key_type, policy->alg,
926 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200927 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
929 constraint->alg2);
930 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
931 return PSA_ERROR_INVALID_ARGUMENT;
932 }
933 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
934 return PSA_ERROR_INVALID_ARGUMENT;
935 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100936 policy->usage &= constraint->usage;
937 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200938 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100940}
941
Przemek Stekiel061f6942022-11-30 10:51:35 +0100942/** Get the description of a key given its identifier and policy constraints
943 * and lock it.
944 *
945 * The key must have allow all the usage flags set in \p usage. If \p alg is
946 * nonzero, the key must allow operations with this algorithm. If \p alg is
947 * zero, the algorithm is not checked.
948 *
949 * In case of a persistent key, the function loads the description of the key
950 * into a key slot if not already done.
951 *
952 * On success, the returned key slot is locked. It is the responsibility of
953 * the caller to unlock the key slot when it does not access it anymore.
954 */
955static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100956 mbedtls_svc_key_id_t key,
957 psa_key_slot_t **p_slot,
958 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100960{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200961 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100962 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 status = psa_get_and_lock_key_slot(key, p_slot);
965 if (status != PSA_SUCCESS) {
966 return status;
967 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200968 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100969
970 /* Enforce that usage policy for the key slot contains all the flags
971 * required by the usage parameter. There is one exception: public
972 * keys can always be exported, so we treat public key objects as
973 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100975 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100979 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200980 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100981 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100982
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800983 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 if (alg != 0) {
985 status = psa_key_policy_permits(&slot->attr.policy,
986 slot->attr.type,
987 alg);
988 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100989 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100991 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100992
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200994
995error:
996 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001000}
Darryl Green940d72c2018-07-13 13:18:51 +01001001
Ronald Cron5c522922020-11-14 16:35:34 +01001002/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001003 *
1004 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001005 * available, as opposed to a key in a secure element and/or to be used
1006 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001007 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001008 * This is a temporary function that may be used instead of
1009 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1010 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001011 *
Ronald Cron5c522922020-11-14 16:35:34 +01001012 * On success, the returned key slot is locked. It is the responsibility of the
1013 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001014 */
Ronald Cron5c522922020-11-14 16:35:34 +01001015static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1016 mbedtls_svc_key_id_t key,
1017 psa_key_slot_t **p_slot,
1018 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001020{
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1022 usage, alg);
1023 if (status != PSA_SUCCESS) {
1024 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001025 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1028 psa_unlock_key_slot(*p_slot);
1029 *p_slot = NULL;
1030 return PSA_ERROR_NOT_SUPPORTED;
1031 }
1032
1033 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001034}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001035
Gilles Peskine449bd832023-01-11 14:50:10 +01001036psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001037{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001038 /* Data pointer will always be either a valid pointer or NULL in an
1039 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 if (slot->key.data != NULL) {
1041 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1042 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001043
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001045 slot->key.data = NULL;
1046 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001049}
1050
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001051/** Completely wipe a slot in memory, including its policy.
1052 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001053psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001054{
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 /*
1058 * As the return error code may not be handled in case of multiple errors,
1059 * do our best to report an unexpected lock counter. Assert with
1060 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1061 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1062 * function is called as part of the execution of a test suite, the
1063 * execution of the test suite is stopped in error if the assertion fails.
1064 */
1065 if (slot->lock_count != 1) {
1066 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001067 status = PSA_ERROR_CORRUPTION_DETECTED;
1068 }
1069
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001070 /* Multipart operations may still be using the key. This is safe
1071 * because all multipart operation objects are independent from
1072 * the key slot: if they need to access the key after the setup
1073 * phase, they have a copy of the key. Note that this means that
1074 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001075 /* At this point, key material and other type-specific content has
1076 * been wiped. Clear remaining metadata. We can call memset and not
1077 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 memset(slot, 0, sizeof(*slot));
1079 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001080}
1081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001083{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001084 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001085 psa_status_t status; /* status of the last operation */
1086 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001087#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1088 psa_se_drv_table_entry_t *driver;
1089#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (mbedtls_svc_key_id_is_null(key)) {
1092 return PSA_SUCCESS;
1093 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001094
Ronald Cronf2911112020-10-29 17:51:10 +01001095 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001096 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001097 * key, this will load the key description from persistent memory if not
1098 * done yet. We cannot avoid this loading as without it we don't know if
1099 * the key is operated by an SE or not and this information is needed by
1100 * the current implementation.
1101 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 status = psa_get_and_lock_key_slot(key, &slot);
1103 if (status != PSA_SUCCESS) {
1104 return status;
1105 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001106
Ronald Cronf2911112020-10-29 17:51:10 +01001107 /*
1108 * If the key slot containing the key description is under access by the
1109 * library (apart from the present access), the key cannot be destroyed
1110 * yet. For the time being, just return in error. Eventually (to be
1111 * implemented), the key should be destroyed when all accesses have
1112 * stopped.
1113 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 if (slot->lock_count > 1) {
1115 psa_unlock_key_slot(slot);
1116 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001117 }
1118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001120 /* Refuse the destruction of a read-only key (which may or may not work
1121 * if we attempt it, depending on whether the key is merely read-only
1122 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001123 * Just do the best we can, which is to wipe the copy in memory
1124 * (done in this function's cleanup code). */
1125 overall_status = PSA_ERROR_NOT_PERMITTED;
1126 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001127 }
1128
Gilles Peskine354f7672019-07-12 23:46:38 +02001129#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1131 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001132 /* For a key in a secure element, we need to do three things:
1133 * remove the key file in internal storage, destroy the
1134 * key inside the secure element, and update the driver's
1135 * persistent data. Start a transaction that will encompass these
1136 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001138 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001140 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 status = psa_crypto_save_transaction();
1142 if (status != PSA_SUCCESS) {
1143 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001144 /* We should still try to destroy the key in the secure
1145 * element and the key metadata in storage. This is especially
1146 * important if the error is that the storage is full.
1147 * But how to do it exactly without risking an inconsistent
1148 * state after a reset?
1149 * https://github.com/ARMmbed/mbed-crypto/issues/215
1150 */
1151 overall_status = status;
1152 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001153 }
1154
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 status = psa_destroy_se_key(driver,
1156 psa_key_slot_get_slot_number(slot));
1157 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001158 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001160 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001161#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1162
Darryl Greend49a4992018-06-18 17:27:26 +01001163#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1165 status = psa_destroy_persistent_key(slot->attr.id);
1166 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001167 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001169
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001170 /* TODO: other slots may have a copy of the same key. We should
1171 * invalidate them.
1172 * https://github.com/ARMmbed/mbed-crypto/issues/214
1173 */
Darryl Greend49a4992018-06-18 17:27:26 +01001174 }
1175#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001176
Gilles Peskinefc762652019-07-22 19:30:34 +02001177#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 if (driver != NULL) {
1179 status = psa_save_se_persistent_data(driver);
1180 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001181 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 }
1183 status = psa_crypto_stop_transaction();
1184 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001185 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001187 }
1188#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1189
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001190exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001192 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001194 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 }
1196 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001197}
1198
John Durkop07cc04a2020-11-16 22:08:34 -08001199#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001200 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001201static psa_status_t psa_get_rsa_public_exponent(
1202 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001204{
1205 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001207 uint8_t *buffer = NULL;
1208 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001210
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1212 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001213 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 }
1215 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001216 /* It's the default value, which is reported as an empty string,
1217 * so there's nothing to do. */
1218 goto exit;
1219 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 buflen = mbedtls_mpi_size(&mpi);
1222 buffer = mbedtls_calloc(1, buflen);
1223 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001224 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1225 goto exit;
1226 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1228 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001229 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001231 attributes->domain_parameters = buffer;
1232 attributes->domain_parameters_size = buflen;
1233
1234exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 mbedtls_mpi_free(&mpi);
1236 if (ret != 0) {
1237 mbedtls_free(buffer);
1238 }
1239 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001240}
John Durkop07cc04a2020-11-16 22:08:34 -08001241#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001242 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001243
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001244/** Retrieve all the publicly-accessible attributes of a key.
1245 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001246psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1247 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001248{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001249 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001250 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001251 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1256 if (status != PSA_SUCCESS) {
1257 return status;
1258 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001259
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001260 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1262 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001263
1264#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1266 psa_set_key_slot_number(attributes,
1267 psa_key_slot_get_slot_number(slot));
1268 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001269#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001272#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001273 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001274 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001275 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001276 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001277 * is not yet implemented.
1278 * https://github.com/ARMmbed/mbed-crypto/issues/216
1279 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001281 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001282
Ronald Cron90857082020-11-25 14:58:33 +01001283 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 slot->attr.type,
1285 slot->key.data,
1286 slot->key.bytes,
1287 &rsa);
1288 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001289 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 status = psa_get_rsa_public_exponent(rsa,
1293 attributes);
1294 mbedtls_rsa_free(rsa);
1295 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001296 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001297 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001298#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001299 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001300 default:
1301 /* Nothing else to do. */
1302 break;
1303 }
1304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 if (status != PSA_SUCCESS) {
1306 psa_reset_key_attributes(attributes);
1307 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001312}
1313
Gilles Peskinec8000c02019-08-02 20:15:51 +02001314#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1315psa_status_t psa_get_key_slot_number(
1316 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001318{
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001320 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 return PSA_SUCCESS;
1322 } else {
1323 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001324 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001325}
1326#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1329 size_t key_buffer_size,
1330 uint8_t *data,
1331 size_t data_size,
1332 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001333{
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 if (key_buffer_size > data_size) {
1335 return PSA_ERROR_BUFFER_TOO_SMALL;
1336 }
1337 memcpy(data, key_buffer, key_buffer_size);
1338 memset(data + key_buffer_size, 0,
1339 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001340 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001342}
1343
Ronald Cron7285cda2020-11-26 14:46:37 +01001344psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001345 const psa_key_attributes_t *attributes,
1346 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001348{
Ronald Crond18b5f82020-11-25 16:46:05 +01001349 psa_key_type_t type = attributes->core.type;
1350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 if (key_type_is_raw_bytes(type) ||
1352 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001353 PSA_KEY_TYPE_IS_ECC(type) ||
1354 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 return psa_export_key_buffer_internal(
1356 key_buffer, key_buffer_size,
1357 data, data_size, data_length);
1358 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001359 /* This shouldn't happen in the reference implementation, but
1360 it is valid for a special-purpose implementation to omit
1361 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001363 }
1364}
1365
Gilles Peskine449bd832023-01-11 14:50:10 +01001366psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1367 uint8_t *data,
1368 size_t data_size,
1369 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001370{
1371 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1372 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1373 psa_key_slot_t *slot;
1374
Ronald Crone907e552021-01-18 13:22:38 +01001375 /* Reject a zero-length output buffer now, since this can never be a
1376 * valid key representation. This way we know that data must be a valid
1377 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 if (data_size == 0) {
1379 return PSA_ERROR_BUFFER_TOO_SMALL;
1380 }
Ronald Crone907e552021-01-18 13:22:38 +01001381
Ronald Cron9486f9d2020-11-26 13:36:23 +01001382 /* Set the key to empty now, so that even when there are errors, we always
1383 * set data_length to a value between 0 and data_size. On error, setting
1384 * the key to empty is a good choice because an empty key representation is
1385 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001386 *data_length = 0;
1387
Ronald Cron9486f9d2020-11-26 13:36:23 +01001388 /* Export requires the EXPORT flag. There is an exception for public keys,
1389 * which don't require any flag, but
1390 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1391 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1393 PSA_KEY_USAGE_EXPORT, 0);
1394 if (status != PSA_SUCCESS) {
1395 return status;
1396 }
mohammad160306e79202018-03-28 13:17:44 +03001397
Ronald Crond18b5f82020-11-25 16:46:05 +01001398 psa_key_attributes_t attributes = {
1399 .core = slot->attr
1400 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 status = psa_driver_wrapper_export_key(&attributes,
1402 slot->key.data, slot->key.bytes,
1403 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001408}
1409
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001410#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) || \
1411 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY)
1412static psa_status_t psa_ffdh_set_prime_generator(size_t key_size,
1413 mbedtls_mpi *P,
1414 mbedtls_mpi *G)
1415{
1416 const unsigned char *dhm_P = NULL;
1417 const unsigned char *dhm_G = NULL;
1418 size_t dhm_size_P = 0;
1419 size_t dhm_size_G = 0;
1420 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1421
1422 if (P == NULL && G == NULL) {
1423 return PSA_ERROR_INVALID_ARGUMENT;
1424 }
1425
1426 static const unsigned char dhm_P_2048[] =
1427 MBEDTLS_DHM_RFC7919_FFDHE2048_P_BIN;
1428 static const unsigned char dhm_P_3072[] =
1429 MBEDTLS_DHM_RFC7919_FFDHE3072_P_BIN;
1430 static const unsigned char dhm_P_4096[] =
1431 MBEDTLS_DHM_RFC7919_FFDHE4096_P_BIN;
1432 static const unsigned char dhm_P_6144[] =
1433 MBEDTLS_DHM_RFC7919_FFDHE6144_P_BIN;
1434 static const unsigned char dhm_P_8192[] =
1435 MBEDTLS_DHM_RFC7919_FFDHE8192_P_BIN;
1436 static const unsigned char dhm_G_2048[] =
1437 MBEDTLS_DHM_RFC7919_FFDHE2048_G_BIN;
1438 static const unsigned char dhm_G_3072[] =
1439 MBEDTLS_DHM_RFC7919_FFDHE3072_G_BIN;
1440 static const unsigned char dhm_G_4096[] =
1441 MBEDTLS_DHM_RFC7919_FFDHE4096_G_BIN;
1442 static const unsigned char dhm_G_6144[] =
1443 MBEDTLS_DHM_RFC7919_FFDHE6144_G_BIN;
1444 static const unsigned char dhm_G_8192[] =
1445 MBEDTLS_DHM_RFC7919_FFDHE8192_G_BIN;
1446
1447 if (key_size <= 256) {
1448 dhm_P = dhm_P_2048;
1449 dhm_G = dhm_G_2048;
1450 dhm_size_P = sizeof(dhm_P_2048);
1451 dhm_size_G = sizeof(dhm_G_2048);
1452 } else if (key_size <= 384) {
1453 dhm_P = dhm_P_3072;
1454 dhm_G = dhm_G_3072;
1455 dhm_size_P = sizeof(dhm_P_3072);
1456 dhm_size_G = sizeof(dhm_G_3072);
1457 } else if (key_size <= 512) {
1458 dhm_P = dhm_P_4096;
1459 dhm_G = dhm_G_4096;
1460 dhm_size_P = sizeof(dhm_P_4096);
1461 dhm_size_G = sizeof(dhm_G_4096);
1462 } else if (key_size <= 768) {
1463 dhm_P = dhm_P_6144;
1464 dhm_G = dhm_G_6144;
1465 dhm_size_P = sizeof(dhm_P_6144);
1466 dhm_size_G = sizeof(dhm_G_6144);
1467 } else if (key_size <= 1024) {
1468 dhm_P = dhm_P_8192;
1469 dhm_G = dhm_G_8192;
1470 dhm_size_P = sizeof(dhm_P_8192);
1471 dhm_size_G = sizeof(dhm_G_8192);
1472 } else {
1473 return PSA_ERROR_INVALID_ARGUMENT;
1474 }
1475
1476 if (P != NULL) {
1477 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(P, dhm_P,
1478 dhm_size_P));
1479 }
1480 if (G != NULL) {
1481 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(G, dhm_G,
1482 dhm_size_G));
1483 }
1484
1485cleanup:
1486 if (ret != 0) {
1487 return mbedtls_to_psa_error(ret);
1488 }
1489
1490 return PSA_SUCCESS;
1491}
1492
Przemek Stekielfb3dd542022-12-01 14:59:15 +01001493#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
1494static psa_status_t psa_key_agreement_ffdh(const uint8_t *peer_key,
1495 size_t peer_key_length,
1496 const uint8_t *our_key,
1497 size_t our_key_length,
1498 uint8_t *shared_secret,
1499 size_t shared_secret_size,
1500 size_t *shared_secret_length)
1501{
1502 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1503 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1504 mbedtls_mpi P, G, X, GY, K;
1505 const size_t calculated_shared_secret_size = peer_key_length;
1506
1507 if (peer_key_length != our_key_length ||
1508 calculated_shared_secret_size > shared_secret_size) {
1509 return PSA_ERROR_INVALID_ARGUMENT;
1510 }
1511
1512 mbedtls_mpi_init(&P); mbedtls_mpi_init(&G);
1513 mbedtls_mpi_init(&X); mbedtls_mpi_init(&GY);
1514 mbedtls_mpi_init(&K);
1515
1516 status = psa_ffdh_set_prime_generator(peer_key_length, &P, &G);
1517
1518 if (status == PSA_SUCCESS) {
1519 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&X, our_key,
1520 our_key_length));
1521
1522 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&GY, peer_key,
1523 peer_key_length));
1524
1525 /* Calculate shared secret public key: K = G^(XY) mod P */
1526 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&K, &GY, &X, &P, NULL));
1527
1528 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&K, shared_secret,
1529 calculated_shared_secret_size));
1530
1531 *shared_secret_length = calculated_shared_secret_size;
1532 }
1533cleanup:
1534 mbedtls_mpi_free(&P); mbedtls_mpi_free(&G);
1535 mbedtls_mpi_free(&X); mbedtls_mpi_free(&GY);
1536 mbedtls_mpi_free(&K);
1537
1538 if (status == PSA_SUCCESS && ret != 0) {
1539 return mbedtls_to_psa_error(ret);
1540 }
1541
1542 return PSA_SUCCESS;
1543}
1544#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
1545
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001546static psa_status_t psa_export_ffdh_public_key_internal(
1547 const uint8_t *key_buffer,
1548 size_t key_buffer_size,
1549 uint8_t *data,
1550 size_t data_size,
1551 size_t *data_length)
1552{
1553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1554 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1555 mbedtls_mpi GX, G, X, P;
1556
1557 mbedtls_mpi_init(&GX); mbedtls_mpi_init(&G);
1558 mbedtls_mpi_init(&X); mbedtls_mpi_init(&P);
1559
1560 status = psa_ffdh_set_prime_generator(key_buffer_size, &P, &G);
1561
1562 if (status == PSA_SUCCESS) {
1563 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&X, key_buffer,
1564 key_buffer_size));
1565
1566 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&GX, &G, &X, &P, NULL));
1567 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&GX, data, data_size));
1568
1569 *data_length = mbedtls_mpi_size(&GX);
1570 }
1571cleanup:
1572 mbedtls_mpi_free(&P); mbedtls_mpi_free(&G);
1573 mbedtls_mpi_free(&X); mbedtls_mpi_free(&GX);
1574
1575 if (status == PSA_SUCCESS && ret != 0) {
1576 return mbedtls_to_psa_error(ret);
1577 }
1578
1579 return status;
1580}
1581
Przemek Stekielfedd1342022-12-01 15:00:02 +01001582static psa_status_t mbedtls_psa_ffdh_generate_key(
1583 const psa_key_attributes_t *attributes,
1584 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
1585{
1586 mbedtls_mpi X, P;
1587 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1588 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1589 mbedtls_mpi_init(&P); mbedtls_mpi_init(&X);
1590
1591 status = psa_ffdh_set_prime_generator(PSA_BITS_TO_BYTES(attributes->core.bits), &P, NULL);
1592
1593 if (status == PSA_SUCCESS) {
1594 MBEDTLS_MPI_CHK(mbedtls_mpi_random(&X, 4, &P, mbedtls_psa_get_random,
1595 MBEDTLS_PSA_RANDOM_STATE));
1596 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&X, &X, 2));
1597
1598 *key_buffer_length = mbedtls_mpi_size(&X);
1599
1600 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&X, key_buffer,
1601 key_buffer_size));
1602 }
1603
1604cleanup:
1605 mbedtls_mpi_free(&P); mbedtls_mpi_free(&X);
1606 if (status == PSA_SUCCESS && ret != 0) {
1607 return mbedtls_to_psa_error(ret);
1608 }
1609
1610 return status;
1611}
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001612#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR ||
1613 MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY */
1614
Ronald Cron7285cda2020-11-26 14:46:37 +01001615psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001616 const psa_key_attributes_t *attributes,
1617 const uint8_t *key_buffer,
1618 size_t key_buffer_size,
1619 uint8_t *data,
1620 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001622{
Ronald Crond18b5f82020-11-25 16:46:05 +01001623 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001624
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001625 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1626 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001628 /* Exporting public -> public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 return psa_export_key_buffer_internal(
1630 key_buffer, key_buffer_size,
1631 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001632 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001635#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1637 return mbedtls_psa_rsa_export_public_key(attributes,
1638 key_buffer,
1639 key_buffer_size,
1640 data,
1641 data_size,
1642 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001643#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001644 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001646#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1647 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001648 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
John Durkop6ba40d12020-11-10 08:50:04 -08001649#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1651 return mbedtls_psa_ecp_export_public_key(attributes,
1652 key_buffer,
1653 key_buffer_size,
1654 data,
1655 data_size,
1656 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001657#else
1658 /* We don't know how to convert a private ECC key to public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001660#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1661 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001662 }
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001663#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) || \
1664 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY)
1665 else {
1666 return psa_export_ffdh_public_key_internal(key_buffer,
1667 key_buffer_size,
1668 data, data_size,
1669 data_length);
1670 }
1671#else
1672 /* We don't know how to convert a private FFDH key to public */
1673 return PSA_ERROR_NOT_SUPPORTED;
1674#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) ||
1675 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001677 /* This shouldn't happen in the reference implementation, but
1678 it is valid for a special-purpose implementation to omit
1679 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001681 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001682}
Ronald Crond18b5f82020-11-25 16:46:05 +01001683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1685 uint8_t *data,
1686 size_t data_size,
1687 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001688{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001689 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001690 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001691 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001692
Ronald Crone907e552021-01-18 13:22:38 +01001693 /* Reject a zero-length output buffer now, since this can never be a
1694 * valid key representation. This way we know that data must be a valid
1695 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 if (data_size == 0) {
1697 return PSA_ERROR_BUFFER_TOO_SMALL;
1698 }
Ronald Crone907e552021-01-18 13:22:38 +01001699
Darryl Greendd8fb772018-11-07 16:00:44 +00001700 /* Set the key to empty now, so that even when there are errors, we always
1701 * set data_length to a value between 0 and data_size. On error, setting
1702 * the key to empty is a good choice because an empty key representation is
1703 * unlikely to be accepted anywhere. */
1704 *data_length = 0;
1705
1706 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1708 if (status != PSA_SUCCESS) {
1709 return status;
1710 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001711
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1713 status = PSA_ERROR_INVALID_ARGUMENT;
1714 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001715 }
1716
Ronald Crond18b5f82020-11-25 16:46:05 +01001717 psa_key_attributes_t attributes = {
1718 .core = slot->attr
1719 };
Ronald Cron67227982020-11-26 15:16:05 +01001720 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001721 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001723
1724exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001726
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001728}
1729
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001730MBEDTLS_STATIC_ASSERT(
1731 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1732 "One or more key attribute flag is listed as both external-only and dual-use")
1733MBEDTLS_STATIC_ASSERT(
1734 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1735 "One or more key attribute flag is listed as both internal-only and dual-use")
1736MBEDTLS_STATIC_ASSERT(
1737 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1738 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001739
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001740/** Validate that a key policy is internally well-formed.
1741 *
1742 * This function only rejects invalid policies. It does not validate the
1743 * consistency of the policy with respect to other attributes of the key
1744 * such as the key type.
1745 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001746static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001747{
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1749 PSA_KEY_USAGE_COPY |
1750 PSA_KEY_USAGE_ENCRYPT |
1751 PSA_KEY_USAGE_DECRYPT |
1752 PSA_KEY_USAGE_SIGN_MESSAGE |
1753 PSA_KEY_USAGE_VERIFY_MESSAGE |
1754 PSA_KEY_USAGE_SIGN_HASH |
1755 PSA_KEY_USAGE_VERIFY_HASH |
1756 PSA_KEY_USAGE_VERIFY_DERIVATION |
1757 PSA_KEY_USAGE_DERIVE)) != 0) {
1758 return PSA_ERROR_INVALID_ARGUMENT;
1759 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001760
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001762}
1763
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001764/** Validate the internal consistency of key attributes.
1765 *
1766 * This function only rejects invalid attribute values. If does not
1767 * validate the consistency of the attributes with any key data that may
1768 * be involved in the creation of the key.
1769 *
1770 * Call this function early in the key creation process.
1771 *
1772 * \param[in] attributes Key attributes for the new key.
1773 * \param[out] p_drv On any return, the driver for the key, if any.
1774 * NULL for a transparent key.
1775 *
1776 */
1777static psa_status_t psa_validate_key_attributes(
1778 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001780{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001781 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1783 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001784
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 status = psa_validate_key_location(lifetime, p_drv);
1786 if (status != PSA_SUCCESS) {
1787 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001788 }
1789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 status = psa_validate_key_persistence(lifetime);
1791 if (status != PSA_SUCCESS) {
1792 return status;
1793 }
1794
1795 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1796 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1797 return PSA_ERROR_INVALID_ARGUMENT;
1798 }
1799 } else {
1800 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1801 return PSA_ERROR_INVALID_ARGUMENT;
1802 }
1803 }
1804
1805 status = psa_validate_key_policy(&attributes->core.policy);
1806 if (status != PSA_SUCCESS) {
1807 return status;
1808 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001809
1810 /* Refuse to create overly large keys.
1811 * Note that this doesn't trigger on import if the attributes don't
1812 * explicitly specify a size (so psa_get_key_bits returns 0), so
1813 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1815 return PSA_ERROR_NOT_SUPPORTED;
1816 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001817
Gilles Peskine91e8c332019-08-02 19:19:39 +02001818 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1820 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1821 return PSA_ERROR_INVALID_ARGUMENT;
1822 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001823
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001825}
1826
Gilles Peskine4747d192019-04-17 15:05:45 +02001827/** Prepare a key slot to receive key material.
1828 *
1829 * This function allocates a key slot and sets its metadata.
1830 *
1831 * If this function fails, call psa_fail_key_creation().
1832 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001833 * This function is intended to be used as follows:
1834 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001835 * it with the specified attributes, and in case of a volatile key assign it
1836 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001837 * -# Populate the slot with the key material.
1838 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1839 * In case of failure at any step, stop the sequence and call
1840 * psa_fail_key_creation().
1841 *
Ronald Cron5c522922020-11-14 16:35:34 +01001842 * On success, the key slot is locked. It is the responsibility of the caller
1843 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001844 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001845 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001846 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001847 * \param[out] p_slot On success, a pointer to the prepared slot.
1848 * \param[out] p_drv On any return, the driver for the key, if any.
1849 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001850 *
1851 * \retval #PSA_SUCCESS
1852 * The key slot is ready to receive key material.
1853 * \return If this function fails, the key slot is an invalid state.
1854 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001855 */
1856static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001857 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001858 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001859 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001861{
1862 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001863 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001864 psa_key_slot_t *slot;
1865
Gilles Peskinedf179142019-07-15 22:02:14 +02001866 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001867 *p_drv = NULL;
1868
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 status = psa_validate_key_attributes(attributes, p_drv);
1870 if (status != PSA_SUCCESS) {
1871 return status;
1872 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001873
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1875 if (status != PSA_SUCCESS) {
1876 return status;
1877 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001878 slot = *p_slot;
1879
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001880 /* We're storing the declared bit-size of the key. It's up to each
1881 * creation mechanism to verify that this information is correct.
1882 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001883 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001884 * is optional (import, copy). In case of a volatile key, assign it the
1885 * volatile key identifier associated to the slot returned to contain its
1886 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001887
1888 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001890#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1891 slot->attr.id = volatile_key_id;
1892#else
1893 slot->attr.id.key_id = volatile_key_id;
1894#endif
1895 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001896
Gilles Peskine91e8c332019-08-02 19:19:39 +02001897 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001898 * external-only flags, query `attributes`. Thanks to the check
1899 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001900 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001901 * may have set. */
1902 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001903
Gilles Peskinecbaff462019-07-12 23:46:04 +02001904#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001905 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001906 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001907 * create the key file in internal storage, create the
1908 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001909 * persistent data. This is done by starting a transaction that will
1910 * encompass these three actions.
1911 * For registering a volatile key, we just need to find an appropriate
1912 * slot number inside the SE. Since the key is designated volatile, creating
1913 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001914 /* The first thing to do is to find a slot number for the new key.
1915 * We save the slot number in persistent storage as part of the
1916 * transaction data. It will be needed to recover if the power
1917 * fails during the key creation process, to clean up on the secure
1918 * element side after restarting. Obtaining a slot number from the
1919 * secure element driver updates its persistent state, but we do not yet
1920 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001921 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001923 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1925 &slot_number);
1926 if (status != PSA_SUCCESS) {
1927 return status;
1928 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001929
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1931 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001932 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001933 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001934 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 status = psa_crypto_save_transaction();
1936 if (status != PSA_SUCCESS) {
1937 (void) psa_crypto_stop_transaction();
1938 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001939 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001940 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001941
1942 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001944 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001947 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001949 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001950#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001953}
Gilles Peskine4747d192019-04-17 15:05:45 +02001954
1955/** Finalize the creation of a key once its key material has been set.
1956 *
1957 * This entails writing the key to persistent storage.
1958 *
1959 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001960 * See the documentation of psa_start_key_creation() for the intended use
1961 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001962 *
Ronald Cron5c522922020-11-14 16:35:34 +01001963 * If the finalization succeeds, the function unlocks the key slot (it was
1964 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1965 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001966 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001967 * \param[in,out] slot Pointer to the slot with key material.
1968 * \param[in] driver The secure element driver for the key,
1969 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001970 * \param[out] key On success, identifier of the key. Note that the
1971 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001972 *
1973 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001974 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001975 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1976 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1977 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1978 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1979 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1980 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001981 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001982 * \return If this function fails, the key slot is an invalid state.
1983 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001984 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001985static psa_status_t psa_finish_key_creation(
1986 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001987 psa_se_drv_table_entry_t *driver,
1988 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001989{
1990 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001991 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001992 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001993
1994#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001996#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001998 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001999 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01002001
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00002002 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
2003 sizeof(data.slot_number),
2004 "Slot number size does not match psa_se_key_data_storage_t");
2005
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
2007 status = psa_save_persistent_key(&slot->attr,
2008 (uint8_t *) &data,
2009 sizeof(data));
2010 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02002011#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2012 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002013 /* Key material is saved in export representation in the slot, so
2014 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 status = psa_save_persistent_key(&slot->attr,
2016 slot->key.data,
2017 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02002018 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002019 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002020#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2021
Gilles Peskinecbaff462019-07-12 23:46:04 +02002022#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002023 /* Finish the transaction for a key creation. This does not
2024 * happen when registering an existing key. Detect this case
2025 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002026 * creation of a persistent key in a secure element requires a transaction,
2027 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 if (driver != NULL &&
2029 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
2030 status = psa_save_se_persistent_data(driver);
2031 if (status != PSA_SUCCESS) {
2032 psa_destroy_persistent_key(slot->attr.id);
2033 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02002034 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02002036 }
2037#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01002040 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 status = psa_unlock_key_slot(slot);
2042 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01002043 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 }
Ronald Cron81709fc2020-11-14 12:10:32 +01002045 }
Ronald Cron50972942020-11-14 11:28:25 +01002046
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002048}
2049
2050/** Abort the creation of a key.
2051 *
2052 * You may call this function after calling psa_start_key_creation(),
2053 * or after psa_finish_key_creation() fails. In other circumstances, this
2054 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002055 * See the documentation of psa_start_key_creation() for the intended use
2056 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002057 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002058 * \param[in,out] slot Pointer to the slot with key material.
2059 * \param[in] driver The secure element driver for the key,
2060 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002061 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002062static void psa_fail_key_creation(psa_key_slot_t *slot,
2063 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02002064{
Gilles Peskine011e4282019-06-26 18:34:38 +02002065 (void) driver;
2066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02002068 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 }
Gilles Peskine011e4282019-06-26 18:34:38 +02002070
2071#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002072 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002073 * element, and the failure happened later (when saving metadata
2074 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002075 * element.
2076 * https://github.com/ARMmbed/mbed-crypto/issues/217
2077 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002078
Gilles Peskined7729582019-08-05 15:55:54 +02002079 /* Abort the ongoing transaction if any (there may not be one if
2080 * the creation process failed before starting one, or if the
2081 * key creation is a registration of a key in a secure element).
2082 * Earlier functions must already have done what it takes to undo any
2083 * partial creation. All that's left is to update the transaction data
2084 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02002086#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2087
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02002089}
2090
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002091/** Validate optional attributes during key creation.
2092 *
2093 * Some key attributes are optional during key creation. If they are
2094 * specified in the attributes structure, check that they are consistent
2095 * with the data in the slot.
2096 *
2097 * This function should be called near the end of key creation, after
2098 * the slot in memory is fully populated but before saving persistent data.
2099 */
2100static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002101 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002103{
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 if (attributes->core.type != 0) {
2105 if (attributes->core.type != slot->attr.type) {
2106 return PSA_ERROR_INVALID_ARGUMENT;
2107 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002108 }
2109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07002111#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2113 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002114 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002115 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002116 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002117
Ronald Cron90857082020-11-25 14:58:33 +01002118 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 slot->attr.type,
2120 slot->key.data,
2121 slot->key.bytes,
2122 &rsa);
2123 if (status != PSA_SUCCESS) {
2124 return status;
2125 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 mbedtls_mpi_init(&actual);
2128 mbedtls_mpi_init(&required);
2129 ret = mbedtls_rsa_export(rsa,
2130 NULL, NULL, NULL, NULL, &actual);
2131 mbedtls_rsa_free(rsa);
2132 mbedtls_free(rsa);
2133 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002134 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 }
2136 ret = mbedtls_mpi_read_binary(&required,
2137 attributes->domain_parameters,
2138 attributes->domain_parameters_size);
2139 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002140 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 }
2142 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002143 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 }
2145rsa_exit:
2146 mbedtls_mpi_free(&actual);
2147 mbedtls_mpi_free(&required);
2148 if (ret != 0) {
2149 return mbedtls_to_psa_error(ret);
2150 }
2151 } else
John Durkop9814fa22020-11-04 12:28:15 -08002152#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2153 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002154 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002156 }
2157 }
2158
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 if (attributes->core.bits != 0) {
2160 if (attributes->core.bits != slot->attr.bits) {
2161 return PSA_ERROR_INVALID_ARGUMENT;
2162 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002163 }
2164
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002166}
2167
Gilles Peskine449bd832023-01-11 14:50:10 +01002168psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2169 const uint8_t *data,
2170 size_t data_length,
2171 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002172{
2173 psa_status_t status;
2174 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002175 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002176 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302177 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002178
Ronald Cron81709fc2020-11-14 12:10:32 +01002179 *key = MBEDTLS_SVC_KEY_ID_INIT;
2180
Gilles Peskine0f84d622019-09-12 19:03:13 +02002181 /* Reject zero-length symmetric keys (including raw data key objects).
2182 * This also rejects any key which might be encoded as an empty string,
2183 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 if (data_length == 0) {
2185 return PSA_ERROR_INVALID_ARGUMENT;
2186 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002187
Archana449608b2021-09-08 15:36:05 +05302188 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 if (data_length > SIZE_MAX / 8) {
2190 return PSA_ERROR_NOT_SUPPORTED;
2191 }
Archana6ed4bda2021-08-04 10:47:15 +05302192
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2194 &slot, &driver);
2195 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002196 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002198
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002199 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302200 * storage ( thus not in the case of importing a key in a secure element
2201 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2202 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 if (slot->key.data == NULL) {
2204 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302205 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 attributes, data, data_length, &storage_size);
2207 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302208 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 }
Archanad8a83dc2021-06-14 10:04:16 +05302210 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 status = psa_allocate_buffer_to_slot(slot, storage_size);
2212 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002213 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002215 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002216
2217 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 status = psa_driver_wrapper_import_key(attributes,
2219 data, data_length,
2220 slot->key.data,
2221 slot->key.bytes,
2222 &slot->key.bytes, &bits);
2223 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002224 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002226
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002228 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002230 status = PSA_ERROR_INVALID_ARGUMENT;
2231 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002232 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002233
Archana6ed4bda2021-08-04 10:47:15 +05302234 /* Enforce a size limit, and in particular ensure that the bit
2235 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302237 status = PSA_ERROR_NOT_SUPPORTED;
2238 goto exit;
2239 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002240 status = psa_validate_optional_attributes(slot, attributes);
2241 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002242 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002246exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 if (status != PSA_SUCCESS) {
2248 psa_fail_key_creation(slot, driver);
2249 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002252}
2253
Gilles Peskined7729582019-08-05 15:55:54 +02002254#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2255psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002257{
2258 psa_status_t status;
2259 psa_key_slot_t *slot = NULL;
2260 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002261 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002262
2263 /* Leaving attributes unspecified is not currently supported.
2264 * It could make sense to query the key type and size from the
2265 * secure element, but not all secure elements support this
2266 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2268 return PSA_ERROR_NOT_SUPPORTED;
2269 }
2270 if (psa_get_key_bits(attributes) == 0) {
2271 return PSA_ERROR_NOT_SUPPORTED;
2272 }
Gilles Peskined7729582019-08-05 15:55:54 +02002273
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2275 &slot, &driver);
2276 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002277 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 }
Gilles Peskined7729582019-08-05 15:55:54 +02002279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002281
2282exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 if (status != PSA_SUCCESS) {
2284 psa_fail_key_creation(slot, driver);
2285 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002286
Gilles Peskined7729582019-08-05 15:55:54 +02002287 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 psa_close_key(key);
2289 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002290}
2291#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2292
Gilles Peskine449bd832023-01-11 14:50:10 +01002293psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2294 const psa_key_attributes_t *specified_attributes,
2295 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002296{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002297 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002298 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002299 psa_key_slot_t *source_slot = NULL;
2300 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002301 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002302 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302303 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002304
Ronald Cron81709fc2020-11-14 12:10:32 +01002305 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2306
Archana8a180362021-07-05 02:18:48 +05302307 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2309 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002310 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002312
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 status = psa_validate_optional_attributes(source_slot,
2314 specified_attributes);
2315 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002316 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002318
Archana9d17bf42021-09-10 06:22:44 +05302319 /* The target key type and number of bits have been validated by
2320 * psa_validate_optional_attributes() to be either equal to zero or
2321 * equal to the ones of the source key. So it is safe to inherit
2322 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302323 * */
Archana9d17bf42021-09-10 06:22:44 +05302324 actual_attributes.core.bits = source_slot->attr.bits;
2325 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302326
2327
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 status = psa_restrict_key_policy(source_slot->attr.type,
2329 &actual_attributes.core.policy,
2330 &source_slot->attr.policy);
2331 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002332 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2336 &target_slot, &driver);
2337 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002338 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 }
2340 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2341 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302342 /*
Archana9d17bf42021-09-10 06:22:44 +05302343 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302344 * the source key would need to be exported as plaintext and re-imported
2345 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302346 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302347 * appropriate API invocations from the application, if needed.
2348 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002349 status = PSA_ERROR_NOT_SUPPORTED;
2350 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002351 }
Archana8a180362021-07-05 02:18:48 +05302352 /*
2353 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302354 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302355 * - For opaque keys this translates to an invocation of the drivers'
2356 * copy_key entry point through the dispatch layer.
2357 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2359 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2360 &storage_size);
2361 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302362 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 }
Archana374fe5b2021-09-08 15:50:28 +05302364
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2366 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302367 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 }
Archana374fe5b2021-09-08 15:50:28 +05302369
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 status = psa_driver_wrapper_copy_key(&actual_attributes,
2371 source_slot->key.data,
2372 source_slot->key.bytes,
2373 target_slot->key.data,
2374 target_slot->key.bytes,
2375 &target_slot->key.bytes);
2376 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302377 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 }
2379 } else {
2380 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302381 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 source_slot->key.bytes);
2383 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302384 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 }
Archana8a180362021-07-05 02:18:48 +05302386 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002388exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 if (status != PSA_SUCCESS) {
2390 psa_fail_key_creation(target_slot, driver);
2391 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002392
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002394
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002396}
2397
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002398
2399
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002400/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002401/* Message digests */
2402/****************************************************************/
2403
Gilles Peskine449bd832023-01-11 14:50:10 +01002404psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002405{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002406 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 if (operation->id == 0) {
2408 return PSA_SUCCESS;
2409 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002412 operation->id = 0;
2413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002415}
2416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2418 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002419{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002420 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002421
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002422 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002424 status = PSA_ERROR_BAD_STATE;
2425 goto exit;
2426 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002427
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002429 status = PSA_ERROR_INVALID_ARGUMENT;
2430 goto exit;
2431 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002432
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002433 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2434 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002438
2439exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 if (status != PSA_SUCCESS) {
2441 psa_hash_abort(operation);
2442 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002443
2444 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002445}
2446
Gilles Peskine449bd832023-01-11 14:50:10 +01002447psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2448 const uint8_t *input,
2449 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002450{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002451 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2452
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002454 status = PSA_ERROR_BAD_STATE;
2455 goto exit;
2456 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002457
Steven Cooremanc8288352021-03-04 14:02:19 +01002458 /* Don't require hash implementations to behave correctly on a
2459 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 if (input_length == 0) {
2461 return PSA_SUCCESS;
2462 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002463
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002465
2466exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 if (status != PSA_SUCCESS) {
2468 psa_hash_abort(operation);
2469 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002470
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002472}
2473
Gilles Peskine449bd832023-01-11 14:50:10 +01002474psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2475 uint8_t *hash,
2476 size_t hash_size,
2477 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002478{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002479 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 if (operation->id == 0) {
2481 return PSA_ERROR_BAD_STATE;
2482 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002483
Steven Cooreman1e582352021-02-18 17:24:37 +01002484 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 operation, hash, hash_size, hash_length);
2486 psa_hash_abort(operation);
2487 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002488}
2489
Gilles Peskine449bd832023-01-11 14:50:10 +01002490psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2491 const uint8_t *hash,
2492 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002493{
Ronald Cron69a63422021-10-18 09:47:58 +02002494 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002495 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002496 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 operation,
2498 actual_hash, sizeof(actual_hash),
2499 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002500
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002502 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002504
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002506 status = PSA_ERROR_INVALID_SIGNATURE;
2507 goto exit;
2508 }
2509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002511 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002513
2514exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2516 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002517 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002521}
2522
Gilles Peskine449bd832023-01-11 14:50:10 +01002523psa_status_t psa_hash_compute(psa_algorithm_t alg,
2524 const uint8_t *input, size_t input_length,
2525 uint8_t *hash, size_t hash_size,
2526 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002527{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002528 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 if (!PSA_ALG_IS_HASH(alg)) {
2530 return PSA_ERROR_INVALID_ARGUMENT;
2531 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002532
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2534 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002535}
2536
Gilles Peskine449bd832023-01-11 14:50:10 +01002537psa_status_t psa_hash_compare(psa_algorithm_t alg,
2538 const uint8_t *input, size_t input_length,
2539 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002540{
Ronald Cron69a63422021-10-18 09:47:58 +02002541 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002542 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002543
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 if (!PSA_ALG_IS_HASH(alg)) {
2545 return PSA_ERROR_INVALID_ARGUMENT;
2546 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002547
Steven Cooreman1e582352021-02-18 17:24:37 +01002548 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 alg, input, input_length,
2550 actual_hash, sizeof(actual_hash),
2551 &actual_hash_length);
2552 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002553 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 }
2555 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002556 status = PSA_ERROR_INVALID_SIGNATURE;
2557 goto exit;
2558 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002560 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002562
2563exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2565 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002566}
2567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2569 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002570{
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 if (source_operation->id == 0 ||
2572 target_operation->id != 0) {
2573 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002574 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2577 target_operation);
2578 if (status != PSA_SUCCESS) {
2579 psa_hash_abort(target_operation);
2580 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002583}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002584
2585
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002586/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002587/* MAC */
2588/****************************************************************/
2589
Gilles Peskine449bd832023-01-11 14:50:10 +01002590psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002591{
Steven Cooremane6804192021-03-19 18:28:56 +01002592 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 if (operation->id == 0) {
2594 return PSA_SUCCESS;
2595 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002596
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002598 operation->mac_size = 0;
2599 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002600 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002603}
2604
Ronald Cron2dff3b22021-06-17 16:33:22 +02002605static psa_status_t psa_mac_finalize_alg_and_key_validation(
2606 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002607 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002609{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002610 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 psa_key_type_t key_type = psa_get_key_type(attributes);
2612 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002613
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 if (!PSA_ALG_IS_MAC(alg)) {
2615 return PSA_ERROR_INVALID_ARGUMENT;
2616 }
Steven Cooremane6804192021-03-19 18:28:56 +01002617
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002618 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 status = psa_mac_key_can_do(alg, key_type);
2620 if (status != PSA_SUCCESS) {
2621 return status;
2622 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002623
Steven Cooremand1a68f12021-05-10 11:13:41 +02002624 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002626
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002628 /* A very short MAC is too short for security since it can be
2629 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2630 * so we make this our minimum, even though 32 bits is still
2631 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002633 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002634
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2636 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002637 /* It's impossible to "truncate" to a larger length than the full length
2638 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002640 }
2641
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002643 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2644 * that is disabled in the compile-time configuration. The result can
2645 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2646 * configuration into account. In this case, force a return of
2647 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2648 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2649 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2650 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2651 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002653 }
2654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002656}
2657
Gilles Peskine449bd832023-01-11 14:50:10 +01002658static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2659 mbedtls_svc_key_id_t key,
2660 psa_algorithm_t alg,
2661 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002662{
2663 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2664 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002665 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002666
2667 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002669 status = PSA_ERROR_BAD_STATE;
2670 goto exit;
2671 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002672
2673 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 key,
2675 &slot,
2676 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2677 alg);
2678 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002679 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002681
2682 psa_key_attributes_t attributes = {
2683 .core = slot->attr
2684 };
2685
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2687 &operation->mac_size);
2688 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002689 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002691
2692 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002693 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 if (is_sign) {
2695 status = psa_driver_wrapper_mac_sign_setup(operation,
2696 &attributes,
2697 slot->key.data,
2698 slot->key.bytes,
2699 alg);
2700 } else {
2701 status = psa_driver_wrapper_mac_verify_setup(operation,
2702 &attributes,
2703 slot->key.data,
2704 slot->key.bytes,
2705 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002706 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002707
Gilles Peskinefbfac682018-07-08 20:51:54 +02002708exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 if (status != PSA_SUCCESS) {
2710 psa_mac_abort(operation);
2711 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002712
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002714
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002716}
2717
Gilles Peskine449bd832023-01-11 14:50:10 +01002718psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2719 mbedtls_svc_key_id_t key,
2720 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002721{
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002723}
2724
Gilles Peskine449bd832023-01-11 14:50:10 +01002725psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2726 mbedtls_svc_key_id_t key,
2727 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002728{
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002730}
2731
Gilles Peskine449bd832023-01-11 14:50:10 +01002732psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2733 const uint8_t *input,
2734 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002735{
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 if (operation->id == 0) {
2737 return PSA_ERROR_BAD_STATE;
2738 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002739
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002740 /* Don't require hash implementations to behave correctly on a
2741 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 if (input_length == 0) {
2743 return PSA_SUCCESS;
2744 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002745
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2747 input, input_length);
2748 if (status != PSA_SUCCESS) {
2749 psa_mac_abort(operation);
2750 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002751
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002753}
2754
Gilles Peskine449bd832023-01-11 14:50:10 +01002755psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2756 uint8_t *mac,
2757 size_t mac_size,
2758 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002759{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002760 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2761 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002762
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002764 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002765 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002766 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002767
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002769 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002770 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002771 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002772
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002773 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2774 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002776 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002777 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002778 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002779
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002781 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002782 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002783 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002784
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 status = psa_driver_wrapper_mac_sign_finish(operation,
2786 mac, operation->mac_size,
2787 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002788
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002789exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002790 /* In case of success, set the potential excess room in the output buffer
2791 * to an invalid value, to avoid potentially leaking a longer MAC.
2792 * In case of error, set the output length and content to a safe default,
2793 * such that in case the caller misses an error check, the output would be
2794 * an unachievable MAC.
2795 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002797 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002798 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002799 }
mohammad16036df908f2018-04-02 08:34:15 -07002800
Paul Elliottdc42ca82023-02-24 18:11:59 +00002801 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002802
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002804
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002806}
2807
Gilles Peskine449bd832023-01-11 14:50:10 +01002808psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2809 const uint8_t *mac,
2810 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002811{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002812 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2813 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002814
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002816 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002817 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002818 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002819
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002821 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002822 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002823 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002824
Gilles Peskine449bd832023-01-11 14:50:10 +01002825 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002826 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002827 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002828 }
2829
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 status = psa_driver_wrapper_mac_verify_finish(operation,
2831 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002832
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002833exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002835
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002837}
2838
Gilles Peskine449bd832023-01-11 14:50:10 +01002839static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2840 psa_algorithm_t alg,
2841 const uint8_t *input,
2842 size_t input_length,
2843 uint8_t *mac,
2844 size_t mac_size,
2845 size_t *mac_length,
2846 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002847{
2848 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002849 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2850 psa_key_slot_t *slot;
2851 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002852
Ronald Cron51131b52021-06-17 17:17:20 +02002853 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 key,
2855 &slot,
2856 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2857 alg);
2858 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002859 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002861
Ronald Cron51131b52021-06-17 17:17:20 +02002862 psa_key_attributes_t attributes = {
2863 .core = slot->attr
2864 };
2865
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2867 &operation_mac_size);
2868 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002869 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002871
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002873 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002874 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002875 }
2876
2877 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 &attributes,
2879 slot->key.data, slot->key.bytes,
2880 alg,
2881 input, input_length,
2882 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002883
2884exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002885 /* In case of success, set the potential excess room in the output buffer
2886 * to an invalid value, to avoid potentially leaking a longer MAC.
2887 * In case of error, set the output length and content to a safe default,
2888 * such that in case the caller misses an error check, the output would be
2889 * an unachievable MAC.
2890 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002892 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002893 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002894 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002895
2896 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002897
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002899
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002901}
2902
Gilles Peskine449bd832023-01-11 14:50:10 +01002903psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002904 psa_algorithm_t alg,
2905 const uint8_t *input,
2906 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 uint8_t *mac,
2908 size_t mac_size,
2909 size_t *mac_length)
2910{
2911 return psa_mac_compute_internal(key, alg,
2912 input, input_length,
2913 mac, mac_size, mac_length, 1);
2914}
2915
2916psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2917 psa_algorithm_t alg,
2918 const uint8_t *input,
2919 size_t input_length,
2920 const uint8_t *mac,
2921 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002922{
2923 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002924 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2925 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002926
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 status = psa_mac_compute_internal(key, alg,
2928 input, input_length,
2929 actual_mac, sizeof(actual_mac),
2930 &actual_mac_length, 0);
2931 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002932 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002934
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002936 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002937 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002938 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002940 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002941 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002942 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002943
2944exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002946
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002948}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002949
Gilles Peskinee59236f2018-01-27 23:32:46 +01002950/****************************************************************/
2951/* Asymmetric cryptography */
2952/****************************************************************/
2953
Gilles Peskine449bd832023-01-11 14:50:10 +01002954static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2955 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002956{
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 if (input_is_message) {
2958 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2959 return PSA_ERROR_INVALID_ARGUMENT;
2960 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002961
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2963 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2964 return PSA_ERROR_INVALID_ARGUMENT;
2965 }
2966 }
2967 } else {
2968 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2969 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002970 }
2971 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002972
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002974}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002975
Gilles Peskine449bd832023-01-11 14:50:10 +01002976static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2977 int input_is_message,
2978 psa_algorithm_t alg,
2979 const uint8_t *input,
2980 size_t input_length,
2981 uint8_t *signature,
2982 size_t signature_size,
2983 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002984{
2985 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2986 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2987 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002988
2989 *signature_length = 0;
2990
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 status = psa_sign_verify_check_alg(input_is_message, alg);
2992 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002993 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002995
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002996 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002997 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002998 * buffer can in principle be empty since it doesn't actually have
2999 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 if (signature_size == 0) {
3001 return PSA_ERROR_BUFFER_TOO_SMALL;
3002 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003003
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003004 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 key, &slot,
3006 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
3007 PSA_KEY_USAGE_SIGN_HASH,
3008 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003009
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003011 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003013
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003015 status = PSA_ERROR_INVALID_ARGUMENT;
3016 goto exit;
3017 }
3018
3019 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003021 };
3022
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003024 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003025 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003026 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 signature, signature_size, signature_length);
3028 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003029
3030 status = psa_driver_wrapper_sign_hash(
3031 &attributes, slot->key.data, slot->key.bytes,
3032 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003034 }
3035
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003036
3037exit:
Paul Elliott59200a22023-02-21 15:07:40 +00003038 psa_wipe_tag_output_buffer(signature, status, signature_size,
3039 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003040
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003042
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003044}
3045
Gilles Peskine449bd832023-01-11 14:50:10 +01003046static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
3047 int input_is_message,
3048 psa_algorithm_t alg,
3049 const uint8_t *input,
3050 size_t input_length,
3051 const uint8_t *signature,
3052 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003053{
3054 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3055 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3056 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003057
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 status = psa_sign_verify_check_alg(input_is_message, alg);
3059 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003060 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003062
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003063 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 key, &slot,
3065 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
3066 PSA_KEY_USAGE_VERIFY_HASH,
3067 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003068
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 if (status != PSA_SUCCESS) {
3070 return status;
3071 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003072
3073 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003075 };
3076
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003078 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003079 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003080 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 signature, signature_length);
3082 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003083 status = psa_driver_wrapper_verify_hash(
3084 &attributes, slot->key.data, slot->key.bytes,
3085 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003087 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003088
Gilles Peskine449bd832023-01-11 14:50:10 +01003089 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003090
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003092
3093}
3094
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003095psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003096 const psa_key_attributes_t *attributes,
3097 const uint8_t *key_buffer,
3098 size_t key_buffer_size,
3099 psa_algorithm_t alg,
3100 const uint8_t *input,
3101 size_t input_length,
3102 uint8_t *signature,
3103 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003104 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003105{
3106 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003107
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003109 size_t hash_length;
3110 uint8_t hash[PSA_HASH_MAX_SIZE];
3111
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003112 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 PSA_ALG_SIGN_GET_HASH(alg),
3114 input, input_length,
3115 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003116
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003118 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003120
3121 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 attributes, key_buffer, key_buffer_size,
3123 alg, hash, hash_length,
3124 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003125 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003126
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003128}
3129
Gilles Peskine449bd832023-01-11 14:50:10 +01003130psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3131 psa_algorithm_t alg,
3132 const uint8_t *input,
3133 size_t input_length,
3134 uint8_t *signature,
3135 size_t signature_size,
3136 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003137{
3138 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003139 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003141}
3142
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003143psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003144 const psa_key_attributes_t *attributes,
3145 const uint8_t *key_buffer,
3146 size_t key_buffer_size,
3147 psa_algorithm_t alg,
3148 const uint8_t *input,
3149 size_t input_length,
3150 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003152{
3153 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003154
Gilles Peskine449bd832023-01-11 14:50:10 +01003155 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003156 size_t hash_length;
3157 uint8_t hash[PSA_HASH_MAX_SIZE];
3158
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003159 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 PSA_ALG_SIGN_GET_HASH(alg),
3161 input, input_length,
3162 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003163
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003165 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003167
3168 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003169 attributes, key_buffer, key_buffer_size,
3170 alg, hash, hash_length,
3171 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003172 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003173
Gilles Peskine449bd832023-01-11 14:50:10 +01003174 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003175}
3176
Gilles Peskine449bd832023-01-11 14:50:10 +01003177psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3178 psa_algorithm_t alg,
3179 const uint8_t *input,
3180 size_t input_length,
3181 const uint8_t *signature,
3182 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003183{
3184 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003185 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003187}
3188
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003189psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003190 const psa_key_attributes_t *attributes,
3191 const uint8_t *key_buffer, size_t key_buffer_size,
3192 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003194{
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3196 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3197 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003198#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3200 return mbedtls_psa_rsa_sign_hash(
3201 attributes,
3202 key_buffer, key_buffer_size,
3203 alg, hash, hash_length,
3204 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003205#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3206 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 } else {
3208 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003209 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3211 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003212#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003213 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3214 return mbedtls_psa_ecdsa_sign_hash(
3215 attributes,
3216 key_buffer, key_buffer_size,
3217 alg, hash, hash_length,
3218 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003219#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3220 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 } else {
3222 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003223 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003224 }
Ronald Cron4501c982021-03-19 20:09:32 +01003225
Gilles Peskine449bd832023-01-11 14:50:10 +01003226 (void) key_buffer;
3227 (void) key_buffer_size;
3228 (void) hash;
3229 (void) hash_length;
3230 (void) signature;
3231 (void) signature_size;
3232 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003233
Gilles Peskine449bd832023-01-11 14:50:10 +01003234 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003235}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003236
Gilles Peskine449bd832023-01-11 14:50:10 +01003237psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3238 psa_algorithm_t alg,
3239 const uint8_t *hash,
3240 size_t hash_length,
3241 uint8_t *signature,
3242 size_t signature_size,
3243 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003244{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003245 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003246 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003247 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003248}
3249
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003250psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003251 const psa_key_attributes_t *attributes,
3252 const uint8_t *key_buffer, size_t key_buffer_size,
3253 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003255{
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3257 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3258 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003259#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3261 return mbedtls_psa_rsa_verify_hash(
3262 attributes,
3263 key_buffer, key_buffer_size,
3264 alg, hash, hash_length,
3265 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003266#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3267 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 } else {
3269 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003270 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3272 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003273#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3275 return mbedtls_psa_ecdsa_verify_hash(
3276 attributes,
3277 key_buffer, key_buffer_size,
3278 alg, hash, hash_length,
3279 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003280#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3281 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003282 } else {
3283 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003284 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003285 }
Ronald Cron4501c982021-03-19 20:09:32 +01003286
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 (void) key_buffer;
3288 (void) key_buffer_size;
3289 (void) hash;
3290 (void) hash_length;
3291 (void) signature;
3292 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003293
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003295}
3296
Gilles Peskine449bd832023-01-11 14:50:10 +01003297psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3298 psa_algorithm_t alg,
3299 const uint8_t *hash,
3300 size_t hash_length,
3301 const uint8_t *signature,
3302 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003303{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003304 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003305 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003307}
3308
Gilles Peskine449bd832023-01-11 14:50:10 +01003309psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3310 psa_algorithm_t alg,
3311 const uint8_t *input,
3312 size_t input_length,
3313 const uint8_t *salt,
3314 size_t salt_length,
3315 uint8_t *output,
3316 size_t output_size,
3317 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003318{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003319 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003320 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003321 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003322
Darryl Green5cc689a2018-07-24 15:34:10 +01003323 (void) input;
3324 (void) input_length;
3325 (void) salt;
3326 (void) output;
3327 (void) output_size;
3328
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003329 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003330
Gilles Peskine449bd832023-01-11 14:50:10 +01003331 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3332 return PSA_ERROR_INVALID_ARGUMENT;
3333 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003334
Ronald Cron5c522922020-11-14 16:35:34 +01003335 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003336 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3337 if (status != PSA_SUCCESS) {
3338 return status;
3339 }
3340 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3341 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003342 status = PSA_ERROR_INVALID_ARGUMENT;
3343 goto exit;
3344 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003345
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003346 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003347 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003348 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003349
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003350 status = psa_driver_wrapper_asymmetric_encrypt(
3351 &attributes, slot->key.data, slot->key.bytes,
3352 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003354exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003355 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003356
Gilles Peskine449bd832023-01-11 14:50:10 +01003357 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003358}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003359
Gilles Peskine449bd832023-01-11 14:50:10 +01003360psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3361 psa_algorithm_t alg,
3362 const uint8_t *input,
3363 size_t input_length,
3364 const uint8_t *salt,
3365 size_t salt_length,
3366 uint8_t *output,
3367 size_t output_size,
3368 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003369{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003370 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003371 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003372 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003373
Darryl Green5cc689a2018-07-24 15:34:10 +01003374 (void) input;
3375 (void) input_length;
3376 (void) salt;
3377 (void) output;
3378 (void) output_size;
3379
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003380 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003381
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3383 return PSA_ERROR_INVALID_ARGUMENT;
3384 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003385
Ronald Cron5c522922020-11-14 16:35:34 +01003386 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3388 if (status != PSA_SUCCESS) {
3389 return status;
3390 }
3391 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003392 status = PSA_ERROR_INVALID_ARGUMENT;
3393 goto exit;
3394 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003395
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003396 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003397 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003398 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003399
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003400 status = psa_driver_wrapper_asymmetric_decrypt(
3401 &attributes, slot->key.data, slot->key.bytes,
3402 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003403 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003404
3405exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003406 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003407
Gilles Peskine449bd832023-01-11 14:50:10 +01003408 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003409}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003410
Paul Elliott9fe12f62022-11-30 19:16:02 +00003411/****************************************************************/
3412/* Asymmetric interruptible cryptography */
3413/****************************************************************/
3414
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003415static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3416
Paul Elliott9fe12f62022-11-30 19:16:02 +00003417void psa_interruptible_set_max_ops(uint32_t max_ops)
3418{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003419 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003420}
3421
3422uint32_t psa_interruptible_get_max_ops(void)
3423{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003424 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003425}
3426
Paul Elliott9fe12f62022-11-30 19:16:02 +00003427uint32_t psa_sign_hash_get_num_ops(
3428 const psa_sign_hash_interruptible_operation_t *operation)
3429{
Paul Elliott296ede92022-12-15 17:00:30 +00003430 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003431}
3432
3433uint32_t psa_verify_hash_get_num_ops(
3434 const psa_verify_hash_interruptible_operation_t *operation)
3435{
Paul Elliott296ede92022-12-15 17:00:30 +00003436 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003437}
3438
Paul Elliott59ad9452022-12-18 15:09:02 +00003439static psa_status_t psa_sign_hash_abort_internal(
3440 psa_sign_hash_interruptible_operation_t *operation)
3441{
3442 if (operation->id == 0) {
3443 /* The object has (apparently) been initialized but it is not (yet)
3444 * in use. It's ok to call abort on such an object, and there's
3445 * nothing to do. */
3446 return PSA_SUCCESS;
3447 }
3448
3449 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3450
3451 status = psa_driver_wrapper_sign_hash_abort(operation);
3452
3453 operation->id = 0;
3454
Paul Elliotte6145dc2023-02-07 12:51:21 +00003455 /* Do not clear either the error_occurred or num_ops elements here as they
3456 * only want to be cleared by the application calling abort, not by abort
3457 * being called at completion of an operation. */
3458
Paul Elliott59ad9452022-12-18 15:09:02 +00003459 return status;
3460}
3461
Paul Elliott9fe12f62022-11-30 19:16:02 +00003462psa_status_t psa_sign_hash_start(
3463 psa_sign_hash_interruptible_operation_t *operation,
3464 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3465 const uint8_t *hash, size_t hash_length)
3466{
3467 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3468 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3469 psa_key_slot_t *slot;
3470
Paul Elliottc9774412023-02-06 15:14:07 +00003471 /* Check that start has not been previously called, or operation has not
3472 * previously errored. */
3473 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003474 return PSA_ERROR_BAD_STATE;
3475 }
3476
Paul Elliott9fe12f62022-11-30 19:16:02 +00003477 status = psa_sign_verify_check_alg(0, alg);
3478 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003479 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003480 return status;
3481 }
3482
3483 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3484 PSA_KEY_USAGE_SIGN_HASH,
3485 alg);
3486
3487 if (status != PSA_SUCCESS) {
3488 goto exit;
3489 }
3490
3491 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3492 status = PSA_ERROR_INVALID_ARGUMENT;
3493 goto exit;
3494 }
3495
3496 psa_key_attributes_t attributes = {
3497 .core = slot->attr
3498 };
3499
Paul Elliott296ede92022-12-15 17:00:30 +00003500 /* Ensure ops count gets reset, in case of operation re-use. */
3501 operation->num_ops = 0;
3502
Paul Elliott9fe12f62022-11-30 19:16:02 +00003503 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3504 slot->key.data,
3505 slot->key.bytes, alg,
3506 hash, hash_length);
3507exit:
3508
3509 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003510 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003511 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003512 }
3513
3514 unlock_status = psa_unlock_key_slot(slot);
3515
Paul Elliottc9774412023-02-06 15:14:07 +00003516 if (unlock_status != PSA_SUCCESS) {
3517 operation->error_occurred = 1;
3518 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003519
Paul Elliottc9774412023-02-06 15:14:07 +00003520 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003521}
3522
3523
3524psa_status_t psa_sign_hash_complete(
3525 psa_sign_hash_interruptible_operation_t *operation,
3526 uint8_t *signature, size_t signature_size,
3527 size_t *signature_length)
3528{
3529 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3530
3531 *signature_length = 0;
3532
Paul Elliottc9774412023-02-06 15:14:07 +00003533 /* Check that start has been called first, and that operation has not
3534 * previously errored. */
3535 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003536 status = PSA_ERROR_BAD_STATE;
3537 goto exit;
3538 }
3539
Paul Elliott096abc42023-02-03 18:33:23 +00003540 /* Immediately reject a zero-length signature buffer. This guarantees that
3541 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003542 if (signature_size == 0) {
3543 status = PSA_ERROR_BUFFER_TOO_SMALL;
3544 goto exit;
3545 }
3546
3547 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3548 signature_size,
3549 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003550
Paul Elliott296ede92022-12-15 17:00:30 +00003551 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003552 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003553
Paul Elliottebe225c2023-02-10 14:32:53 +00003554exit:
3555
Paul Elliott59200a22023-02-21 15:07:40 +00003556 psa_wipe_tag_output_buffer(signature, status, signature_size,
3557 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003558
Paul Elliott53bb3122023-02-10 14:22:22 +00003559 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003560 if (status != PSA_SUCCESS) {
3561 operation->error_occurred = 1;
3562 }
3563
Paul Elliott59ad9452022-12-18 15:09:02 +00003564 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003565 }
3566
3567 return status;
3568}
3569
3570psa_status_t psa_sign_hash_abort(
3571 psa_sign_hash_interruptible_operation_t *operation)
3572{
Paul Elliott59ad9452022-12-18 15:09:02 +00003573 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3574
3575 status = psa_sign_hash_abort_internal(operation);
3576
3577 /* We clear the number of ops done here, so that it is not cleared when
3578 * the operation fails or succeeds, only on manual abort. */
3579 operation->num_ops = 0;
3580
Paul Elliottc9774412023-02-06 15:14:07 +00003581 /* Likewise, failure state. */
3582 operation->error_occurred = 0;
3583
Paul Elliott59ad9452022-12-18 15:09:02 +00003584 return status;
3585}
3586
3587static psa_status_t psa_verify_hash_abort_internal(
3588 psa_verify_hash_interruptible_operation_t *operation)
3589{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003590 if (operation->id == 0) {
3591 /* The object has (apparently) been initialized but it is not (yet)
3592 * in use. It's ok to call abort on such an object, and there's
3593 * nothing to do. */
3594 return PSA_SUCCESS;
3595 }
3596
Paul Elliott59ad9452022-12-18 15:09:02 +00003597 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3598
3599 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003600
3601 operation->id = 0;
3602
Paul Elliotte6145dc2023-02-07 12:51:21 +00003603 /* Do not clear either the error_occurred or num_ops elements here as they
3604 * only want to be cleared by the application calling abort, not by abort
3605 * being called at completion of an operation. */
3606
Paul Elliott59ad9452022-12-18 15:09:02 +00003607 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003608}
3609
3610psa_status_t psa_verify_hash_start(
3611 psa_verify_hash_interruptible_operation_t *operation,
3612 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3613 const uint8_t *hash, size_t hash_length,
3614 const uint8_t *signature, size_t signature_length)
3615{
3616 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3617 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3618 psa_key_slot_t *slot;
3619
Paul Elliottc9774412023-02-06 15:14:07 +00003620 /* Check that start has not been previously called, or operation has not
3621 * previously errored. */
3622 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003623 return PSA_ERROR_BAD_STATE;
3624 }
3625
3626 status = psa_sign_verify_check_alg(0, alg);
3627 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003628 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003629 return status;
3630 }
3631
3632 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3633 PSA_KEY_USAGE_VERIFY_HASH,
3634 alg);
3635
3636 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003637 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003638 return status;
3639 }
3640
3641 psa_key_attributes_t attributes = {
3642 .core = slot->attr
3643 };
3644
Paul Elliott296ede92022-12-15 17:00:30 +00003645 /* Ensure ops count gets reset, in case of operation re-use. */
3646 operation->num_ops = 0;
3647
Paul Elliott9fe12f62022-11-30 19:16:02 +00003648 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3649 slot->key.data,
3650 slot->key.bytes,
3651 alg, hash, hash_length,
3652 signature, signature_length);
3653
3654 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003655 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003656 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003657 }
3658
3659 unlock_status = psa_unlock_key_slot(slot);
3660
Paul Elliottc9774412023-02-06 15:14:07 +00003661 if (unlock_status != PSA_SUCCESS) {
3662 operation->error_occurred = 1;
3663 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003664
Paul Elliottc9774412023-02-06 15:14:07 +00003665 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003666}
3667
3668psa_status_t psa_verify_hash_complete(
3669 psa_verify_hash_interruptible_operation_t *operation)
3670{
3671 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3672
Paul Elliottc9774412023-02-06 15:14:07 +00003673 /* Check that start has been called first, and that operation has not
3674 * previously errored. */
3675 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003676 status = PSA_ERROR_BAD_STATE;
3677 goto exit;
3678 }
3679
3680 status = psa_driver_wrapper_verify_hash_complete(operation);
3681
Paul Elliott296ede92022-12-15 17:00:30 +00003682 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003683 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003684 operation);
3685
Paul Elliottebe225c2023-02-10 14:32:53 +00003686exit:
3687
Paul Elliott9fe12f62022-11-30 19:16:02 +00003688 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003689 if (status != PSA_SUCCESS) {
3690 operation->error_occurred = 1;
3691 }
3692
Paul Elliott59ad9452022-12-18 15:09:02 +00003693 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003694 }
3695
3696 return status;
3697}
3698
3699psa_status_t psa_verify_hash_abort(
3700 psa_verify_hash_interruptible_operation_t *operation)
3701{
Paul Elliott59ad9452022-12-18 15:09:02 +00003702 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003703
Paul Elliott59ad9452022-12-18 15:09:02 +00003704 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003705
Paul Elliott59ad9452022-12-18 15:09:02 +00003706 /* We clear the number of ops done here, so that it is not cleared when
3707 * the operation fails or succeeds, only on manual abort. */
3708 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003709
Paul Elliottc9774412023-02-06 15:14:07 +00003710 /* Likewise, failure state. */
3711 operation->error_occurred = 0;
3712
Paul Elliott59ad9452022-12-18 15:09:02 +00003713 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003714}
3715
Paul Elliott588f8ed2022-12-02 18:10:26 +00003716/****************************************************************/
3717/* Asymmetric interruptible cryptography internal */
3718/* implementations */
3719/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003720
Paul Elliott588f8ed2022-12-02 18:10:26 +00003721void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3722{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003723
Paul Elliott588f8ed2022-12-02 18:10:26 +00003724#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3725 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3726 defined(MBEDTLS_ECP_RESTARTABLE)
3727
3728 /* Internal implementation uses zero to indicate infinite number max ops,
3729 * therefore avoid this value, and set to minimum possible. */
3730 if (max_ops == 0) {
3731 max_ops = 1;
3732 }
3733
Paul Elliott588f8ed2022-12-02 18:10:26 +00003734 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003735#else
3736 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003737#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3738 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3739 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3740}
3741
Paul Elliott588f8ed2022-12-02 18:10:26 +00003742uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003743 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003744{
3745#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3746 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3747 defined(MBEDTLS_ECP_RESTARTABLE)
3748
Paul Elliott93d9ca82023-02-15 18:14:21 +00003749 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003750#else
3751 (void) operation;
3752 return 0;
3753#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3754 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3755 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3756}
3757
3758uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003759 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003760{
3761 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3762 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3763 defined(MBEDTLS_ECP_RESTARTABLE)
3764
Paul Elliott93d9ca82023-02-15 18:14:21 +00003765 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003766#else
3767 (void) operation;
3768 return 0;
3769#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3770 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3771 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3772}
3773
3774psa_status_t mbedtls_psa_sign_hash_start(
3775 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3776 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3777 size_t key_buffer_size, psa_algorithm_t alg,
3778 const uint8_t *hash, size_t hash_length)
3779{
3780 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003781 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003782
Paul Elliott068fe072023-01-16 13:59:15 +00003783 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3784 return PSA_ERROR_NOT_SUPPORTED;
3785 }
3786
3787 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003788 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003789 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003790
3791#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003792 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3793 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003794
Paul Elliotta1c94092023-02-15 16:38:04 +00003795 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3796
Paul Elliott93d9ca82023-02-15 18:14:21 +00003797 /* Ensure num_ops is zero'ed in case of context re-use. */
3798 operation->num_ops = 0;
3799
Paul Elliott068fe072023-01-16 13:59:15 +00003800 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3801 attributes->core.bits,
3802 key_buffer,
3803 key_buffer_size,
3804 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003805
Paul Elliott068fe072023-01-16 13:59:15 +00003806 if (status != PSA_SUCCESS) {
3807 return status;
3808 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003809
Paul Elliott1bc59df2023-02-05 13:41:57 +00003810 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003811 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003812
Paul Elliott068fe072023-01-16 13:59:15 +00003813 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3814 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3815 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003816
Paul Elliott0290a762023-02-09 14:30:24 +00003817 /* We only need to store the same length of hash as the private key size
3818 * here, it would be truncated by the internal implementation anyway. */
3819 required_hash_length = (hash_length < operation->coordinate_bytes ?
3820 hash_length : operation->coordinate_bytes);
3821
Paul Elliottba70ad42023-02-15 18:23:53 +00003822 if (required_hash_length > sizeof(operation->hash)) {
3823 /* Shouldn't happen, but better safe than sorry. */
3824 return PSA_ERROR_CORRUPTION_DETECTED;
3825 }
3826
Paul Elliott0290a762023-02-09 14:30:24 +00003827 memcpy(operation->hash, hash, required_hash_length);
3828 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003829
3830 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003831
3832#else
Paul Elliott068fe072023-01-16 13:59:15 +00003833 (void) operation;
3834 (void) key_buffer;
3835 (void) key_buffer_size;
3836 (void) alg;
3837 (void) hash;
3838 (void) hash_length;
3839 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003840 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003841
Paul Elliott068fe072023-01-16 13:59:15 +00003842 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003843#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3844 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3845 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003846}
3847
3848psa_status_t mbedtls_psa_sign_hash_complete(
3849 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3850 uint8_t *signature, size_t signature_size,
3851 size_t *signature_length)
3852{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003853#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3854 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3855 defined(MBEDTLS_ECP_RESTARTABLE)
3856
Paul Elliotta1c94092023-02-15 16:38:04 +00003857 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003858 mbedtls_mpi r;
3859 mbedtls_mpi s;
3860
Paul Elliott46845252023-02-03 14:59:11 +00003861 mbedtls_mpi_init(&r);
3862 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003863
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003864 /* Ensure max_ops is set to the current value (or default). */
3865 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3866
Paul Elliotta1c94092023-02-15 16:38:04 +00003867 if (signature_size < 2 * operation->coordinate_bytes) {
3868 status = PSA_ERROR_BUFFER_TOO_SMALL;
3869 goto exit;
3870 }
3871
Paul Elliott588f8ed2022-12-02 18:10:26 +00003872 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003873
Paul Elliott588f8ed2022-12-02 18:10:26 +00003874#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3875 status = mbedtls_to_psa_error(
3876 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003877 &r,
3878 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003879 &operation->ctx->d,
3880 operation->hash,
3881 operation->hash_length,
3882 operation->md_alg,
3883 mbedtls_psa_get_random,
3884 MBEDTLS_PSA_RANDOM_STATE,
3885 &operation->restart_ctx));
3886#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003887 status = PSA_ERROR_NOT_SUPPORTED;
3888 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003889#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3890 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003891 status = mbedtls_to_psa_error(
3892 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003893 &r,
3894 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003895 &operation->ctx->d,
3896 operation->hash,
3897 operation->hash_length,
3898 mbedtls_psa_get_random,
3899 MBEDTLS_PSA_RANDOM_STATE,
3900 mbedtls_psa_get_random,
3901 MBEDTLS_PSA_RANDOM_STATE,
3902 &operation->restart_ctx));
3903 }
3904
Paul Elliottf8e5b562023-02-19 18:43:45 +00003905 /* Hide the fact that the restart context only holds a delta of number of
3906 * ops done during the last operation, not an absolute value. */
3907 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3908
Paul Elliott724bd252023-02-08 12:35:08 +00003909 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003910 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003911 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003912 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003913 operation->coordinate_bytes)
3914 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003915
3916 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003917 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003918 }
3919
3920 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003921 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003922 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003923 operation->coordinate_bytes,
3924 operation->coordinate_bytes)
3925 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003926
3927 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003928 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003929 }
3930
Paul Elliott1bc59df2023-02-05 13:41:57 +00003931 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003932
Paul Elliott724bd252023-02-08 12:35:08 +00003933 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003934 }
Paul Elliott724bd252023-02-08 12:35:08 +00003935
3936exit:
3937
3938 mbedtls_mpi_free(&r);
3939 mbedtls_mpi_free(&s);
3940 return status;
3941
Paul Elliott588f8ed2022-12-02 18:10:26 +00003942 #else
3943
3944 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003945 (void) signature;
3946 (void) signature_size;
3947 (void) signature_length;
3948
3949 return PSA_ERROR_NOT_SUPPORTED;
3950
3951#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3952 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3953 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3954}
3955
3956psa_status_t mbedtls_psa_sign_hash_abort(
3957 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3958{
3959
3960#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3961 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3962 defined(MBEDTLS_ECP_RESTARTABLE)
3963
3964 if (operation->ctx) {
3965 mbedtls_ecdsa_free(operation->ctx);
3966 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003967 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003968 }
3969
3970 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3971
Paul Elliott93d9ca82023-02-15 18:14:21 +00003972 operation->num_ops = 0;
3973
Paul Elliott588f8ed2022-12-02 18:10:26 +00003974 return PSA_SUCCESS;
3975
3976#else
3977
3978 (void) operation;
3979
3980 return PSA_ERROR_NOT_SUPPORTED;
3981
3982#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3983 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3984 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3985}
3986
3987psa_status_t mbedtls_psa_verify_hash_start(
3988 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3989 const psa_key_attributes_t *attributes,
3990 const uint8_t *key_buffer, size_t key_buffer_size,
3991 psa_algorithm_t alg,
3992 const uint8_t *hash, size_t hash_length,
3993 const uint8_t *signature, size_t signature_length)
3994{
3995 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003996 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003997 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003998
Paul Elliott068fe072023-01-16 13:59:15 +00003999 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
4000 return PSA_ERROR_NOT_SUPPORTED;
4001 }
4002
4003 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00004004 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00004005 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004006
4007#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00004008 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4009 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00004010
Paul Elliotta1c94092023-02-15 16:38:04 +00004011 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
4012 mbedtls_mpi_init(&operation->r);
4013 mbedtls_mpi_init(&operation->s);
4014
Paul Elliott93d9ca82023-02-15 18:14:21 +00004015 /* Ensure num_ops is zero'ed in case of context re-use. */
4016 operation->num_ops = 0;
4017
Paul Elliott068fe072023-01-16 13:59:15 +00004018 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
4019 attributes->core.bits,
4020 key_buffer,
4021 key_buffer_size,
4022 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004023
Paul Elliott068fe072023-01-16 13:59:15 +00004024 if (status != PSA_SUCCESS) {
4025 return status;
4026 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004027
Paul Elliottc569fc22023-02-10 13:02:54 +00004028 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004029
Paul Elliott1bc59df2023-02-05 13:41:57 +00004030 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00004031 return PSA_ERROR_INVALID_SIGNATURE;
4032 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004033
Paul Elliott068fe072023-01-16 13:59:15 +00004034 status = mbedtls_to_psa_error(
4035 mbedtls_mpi_read_binary(&operation->r,
4036 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00004037 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00004038
Paul Elliott068fe072023-01-16 13:59:15 +00004039 if (status != PSA_SUCCESS) {
4040 return status;
4041 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004042
Paul Elliott068fe072023-01-16 13:59:15 +00004043 status = mbedtls_to_psa_error(
4044 mbedtls_mpi_read_binary(&operation->s,
4045 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00004046 coordinate_bytes,
4047 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00004048
Paul Elliott068fe072023-01-16 13:59:15 +00004049 if (status != PSA_SUCCESS) {
4050 return status;
4051 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004052
Paul Elliott2c9843f2023-02-15 17:32:42 +00004053 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004054
Paul Elliott2c9843f2023-02-15 17:32:42 +00004055 if (status != PSA_SUCCESS) {
4056 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00004057 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004058
Paul Elliott0290a762023-02-09 14:30:24 +00004059 /* We only need to store the same length of hash as the private key size
4060 * here, it would be truncated by the internal implementation anyway. */
4061 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
4062 coordinate_bytes);
4063
Paul Elliottba70ad42023-02-15 18:23:53 +00004064 if (required_hash_length > sizeof(operation->hash)) {
4065 /* Shouldn't happen, but better safe than sorry. */
4066 return PSA_ERROR_CORRUPTION_DETECTED;
4067 }
4068
Paul Elliott0290a762023-02-09 14:30:24 +00004069 memcpy(operation->hash, hash, required_hash_length);
4070 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00004071
4072 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004073#else
Paul Elliott068fe072023-01-16 13:59:15 +00004074 (void) operation;
4075 (void) key_buffer;
4076 (void) key_buffer_size;
4077 (void) alg;
4078 (void) hash;
4079 (void) hash_length;
4080 (void) signature;
4081 (void) signature_length;
4082 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00004083 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00004084 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004085
Paul Elliott068fe072023-01-16 13:59:15 +00004086 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004087#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4088 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4089 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004090}
4091
4092psa_status_t mbedtls_psa_verify_hash_complete(
4093 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4094{
4095
4096#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4097 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4098 defined(MBEDTLS_ECP_RESTARTABLE)
4099
Paul Elliottf8e5b562023-02-19 18:43:45 +00004100 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4101
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004102 /* Ensure max_ops is set to the current value (or default). */
4103 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4104
Paul Elliottf8e5b562023-02-19 18:43:45 +00004105 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004106 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4107 operation->hash,
4108 operation->hash_length,
4109 &operation->ctx->Q,
4110 &operation->r,
4111 &operation->s,
4112 &operation->restart_ctx));
4113
Paul Elliottf8e5b562023-02-19 18:43:45 +00004114 /* Hide the fact that the restart context only holds a delta of number of
4115 * ops done during the last operation, not an absolute value. */
4116 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4117
4118 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004119#else
4120 (void) operation;
4121
4122 return PSA_ERROR_NOT_SUPPORTED;
4123
4124#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4125 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4126 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4127}
4128
4129psa_status_t mbedtls_psa_verify_hash_abort(
4130 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4131{
4132
4133#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4134 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4135 defined(MBEDTLS_ECP_RESTARTABLE)
4136
4137 if (operation->ctx) {
4138 mbedtls_ecdsa_free(operation->ctx);
4139 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004140 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004141 }
4142
4143 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4144
Paul Elliott93d9ca82023-02-15 18:14:21 +00004145 operation->num_ops = 0;
4146
Paul Elliott588f8ed2022-12-02 18:10:26 +00004147 mbedtls_mpi_free(&operation->r);
4148 mbedtls_mpi_free(&operation->s);
4149
4150 return PSA_SUCCESS;
4151
4152#else
4153 (void) operation;
4154
4155 return PSA_ERROR_NOT_SUPPORTED;
4156
4157#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4158 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4159 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4160}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004161
mohammad1603503973b2018-03-12 15:59:30 +02004162/****************************************************************/
4163/* Symmetric cryptography */
4164/****************************************************************/
4165
Gilles Peskine449bd832023-01-11 14:50:10 +01004166static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4167 mbedtls_svc_key_id_t key,
4168 psa_algorithm_t alg,
4169 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004170{
4171 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4172 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004173 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4175 PSA_KEY_USAGE_ENCRYPT :
4176 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02004177
4178 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004179 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004180 status = PSA_ERROR_BAD_STATE;
4181 goto exit;
4182 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004183
Gilles Peskine449bd832023-01-11 14:50:10 +01004184 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004185 status = PSA_ERROR_INVALID_ARGUMENT;
4186 goto exit;
4187 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004188
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4190 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004191 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004193
Ronald Cron49fafa92021-03-10 08:34:23 +01004194 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004195 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004196 * so we only set it (in the driver wrapper) after resources have been
4197 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004198 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004200 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004201 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004202 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 }
4204 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004205
Ronald Crona4af55f2020-12-14 14:36:06 +01004206 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004208 };
4209
Ronald Cronab99ac22020-10-01 16:38:28 +02004210 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004211 if (cipher_operation == MBEDTLS_ENCRYPT) {
4212 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4213 &attributes,
4214 slot->key.data,
4215 slot->key.bytes,
4216 alg);
4217 } else {
4218 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4219 &attributes,
4220 slot->key.data,
4221 slot->key.bytes,
4222 alg);
4223 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004224
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004225exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004226 if (status != PSA_SUCCESS) {
4227 psa_cipher_abort(operation);
4228 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004229
Gilles Peskine449bd832023-01-11 14:50:10 +01004230 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004231
Gilles Peskine449bd832023-01-11 14:50:10 +01004232 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004233}
4234
Gilles Peskine449bd832023-01-11 14:50:10 +01004235psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4236 mbedtls_svc_key_id_t key,
4237 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004238{
Gilles Peskine449bd832023-01-11 14:50:10 +01004239 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004240}
4241
Gilles Peskine449bd832023-01-11 14:50:10 +01004242psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4243 mbedtls_svc_key_id_t key,
4244 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004245{
Gilles Peskine449bd832023-01-11 14:50:10 +01004246 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004247}
4248
Gilles Peskine449bd832023-01-11 14:50:10 +01004249psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4250 uint8_t *iv,
4251 size_t iv_size,
4252 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004253{
Ronald Cron6d051732020-10-01 14:10:20 +02004254 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004255 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4256 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004257
Gilles Peskine449bd832023-01-11 14:50:10 +01004258 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004259 status = PSA_ERROR_BAD_STATE;
4260 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004261 }
4262
Gilles Peskine449bd832023-01-11 14:50:10 +01004263 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004264 status = PSA_ERROR_BAD_STATE;
4265 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004266 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004267
Ronald Cron2fb90522021-07-05 12:31:44 +02004268 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004270 status = PSA_ERROR_BUFFER_TOO_SMALL;
4271 goto exit;
4272 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004273
Gilles Peskine449bd832023-01-11 14:50:10 +01004274 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004275 status = PSA_ERROR_GENERIC_ERROR;
4276 goto exit;
4277 }
4278
Gilles Peskine449bd832023-01-11 14:50:10 +01004279 status = psa_generate_random(local_iv, default_iv_length);
4280 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004281 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004282 }
Ronald Cron5618a392021-03-26 09:52:26 +01004283
Gilles Peskine449bd832023-01-11 14:50:10 +01004284 status = psa_driver_wrapper_cipher_set_iv(operation,
4285 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004286
4287exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004288 if (status == PSA_SUCCESS) {
4289 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004290 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004291 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004292 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004293 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004294 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004295 }
Ronald Cron6d051732020-10-01 14:10:20 +02004296
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004298}
4299
Gilles Peskine449bd832023-01-11 14:50:10 +01004300psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4301 const uint8_t *iv,
4302 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004303{
Ronald Cron6d051732020-10-01 14:10:20 +02004304 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004305
Gilles Peskine449bd832023-01-11 14:50:10 +01004306 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004307 status = PSA_ERROR_BAD_STATE;
4308 goto exit;
4309 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004310
Gilles Peskine449bd832023-01-11 14:50:10 +01004311 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004312 status = PSA_ERROR_BAD_STATE;
4313 goto exit;
4314 }
Ronald Crona0d68172021-03-26 10:15:08 +01004315
Gilles Peskine449bd832023-01-11 14:50:10 +01004316 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004317 status = PSA_ERROR_INVALID_ARGUMENT;
4318 goto exit;
4319 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004320
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 status = psa_driver_wrapper_cipher_set_iv(operation,
4322 iv,
4323 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004324
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004325exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004326 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004327 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 } else {
4329 psa_cipher_abort(operation);
4330 }
4331 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004332}
4333
Gilles Peskine449bd832023-01-11 14:50:10 +01004334psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4335 const uint8_t *input,
4336 size_t input_length,
4337 uint8_t *output,
4338 size_t output_size,
4339 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004340{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004341 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004342
Gilles Peskine449bd832023-01-11 14:50:10 +01004343 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004344 status = PSA_ERROR_BAD_STATE;
4345 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004346 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004347
Gilles Peskine449bd832023-01-11 14:50:10 +01004348 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004349 status = PSA_ERROR_BAD_STATE;
4350 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004351 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004352
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 status = psa_driver_wrapper_cipher_update(operation,
4354 input,
4355 input_length,
4356 output,
4357 output_size,
4358 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004359
4360exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004361 if (status != PSA_SUCCESS) {
4362 psa_cipher_abort(operation);
4363 }
Ronald Cron6d051732020-10-01 14:10:20 +02004364
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004366}
4367
Gilles Peskine449bd832023-01-11 14:50:10 +01004368psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4369 uint8_t *output,
4370 size_t output_size,
4371 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004372{
David Saadab4ecc272019-02-14 13:48:10 +02004373 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004374
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004376 status = PSA_ERROR_BAD_STATE;
4377 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004378 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004379
Gilles Peskine449bd832023-01-11 14:50:10 +01004380 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004381 status = PSA_ERROR_BAD_STATE;
4382 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004383 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004384
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 status = psa_driver_wrapper_cipher_finish(operation,
4386 output,
4387 output_size,
4388 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004389
4390exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004391 if (status == PSA_SUCCESS) {
4392 return psa_cipher_abort(operation);
4393 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004394 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004396
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004398 }
mohammad1603503973b2018-03-12 15:59:30 +02004399}
4400
Gilles Peskine449bd832023-01-11 14:50:10 +01004401psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004402{
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004404 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004405 * in use. It's ok to call abort on such an object, and there's
4406 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004407 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004408 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004409
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004411
Ronald Cron49fafa92021-03-10 08:34:23 +01004412 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004413 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004414 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004415
Gilles Peskine449bd832023-01-11 14:50:10 +01004416 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004417}
4418
Gilles Peskine449bd832023-01-11 14:50:10 +01004419psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4420 psa_algorithm_t alg,
4421 const uint8_t *input,
4422 size_t input_length,
4423 uint8_t *output,
4424 size_t output_size,
4425 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004426{
4427 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004428 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004429 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004430 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4431 size_t default_iv_length = 0;
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_ENCRYPT,
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
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004445 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004447 };
4448
Gilles Peskine449bd832023-01-11 14:50:10 +01004449 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4450 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004451 status = PSA_ERROR_GENERIC_ERROR;
4452 goto exit;
4453 }
4454
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 if (default_iv_length > 0) {
4456 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004457 status = PSA_ERROR_BUFFER_TOO_SMALL;
4458 goto exit;
4459 }
4460
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 status = psa_generate_random(local_iv, default_iv_length);
4462 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004463 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004465 }
4466
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004467 status = psa_driver_wrapper_cipher_encrypt(
4468 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004469 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004470 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004472
4473exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004474 unlock_status = psa_unlock_key_slot(slot);
4475 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004476 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004477 }
Ronald Cron23919522021-07-08 19:00:07 +02004478
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 if (status == PSA_SUCCESS) {
4480 if (default_iv_length > 0) {
4481 memcpy(output, local_iv, default_iv_length);
4482 }
4483 *output_length += default_iv_length;
4484 } else {
4485 *output_length = 0;
4486 }
4487
4488 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004489}
4490
Gilles Peskine449bd832023-01-11 14:50:10 +01004491psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4492 psa_algorithm_t alg,
4493 const uint8_t *input,
4494 size_t input_length,
4495 uint8_t *output,
4496 size_t output_size,
4497 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004498{
4499 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004500 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004501 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004502
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004504 status = PSA_ERROR_INVALID_ARGUMENT;
4505 goto exit;
4506 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004507
Gilles Peskine449bd832023-01-11 14:50:10 +01004508 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4509 PSA_KEY_USAGE_DECRYPT,
4510 alg);
4511 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004512 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004514
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004515 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004517 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004518
Gilles Peskine449bd832023-01-11 14:50:10 +01004519 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4520 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004521 status = PSA_ERROR_INVALID_ARGUMENT;
4522 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004524 status = PSA_ERROR_INVALID_ARGUMENT;
4525 goto exit;
4526 }
4527
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004528 status = psa_driver_wrapper_cipher_decrypt(
4529 &attributes, slot->key.data, slot->key.bytes,
4530 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004532
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004533exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 unlock_status = psa_unlock_key_slot(slot);
4535 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004536 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004537 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004538
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004540 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 }
Ronald Cron23919522021-07-08 19:00:07 +02004542
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004544}
4545
4546
mohammad16035955c982018-04-26 00:53:03 +03004547/****************************************************************/
4548/* AEAD */
4549/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004550
Paul Elliottbaff51c2021-09-28 17:44:45 +01004551/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004552static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004553{
Gilles Peskine449bd832023-01-11 14:50:10 +01004554 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004555}
4556
4557/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004558static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4559 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004560{
Gilles Peskine449bd832023-01-11 14:50:10 +01004561 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004562
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004564#if defined(PSA_WANT_ALG_GCM)
4565 case PSA_ALG_GCM:
4566 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 * arbitrarily large nonces. Please note that we do not generally
4568 * recommend the usage of nonces of greater length than
4569 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4570 * size, which can then lead to collisions if you encrypt a very
4571 * large number of messages.*/
4572 if (nonce_length != 0) {
4573 return PSA_SUCCESS;
4574 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004575 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004576#endif /* PSA_WANT_ALG_GCM */
4577#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004578 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 if (nonce_length >= 7 && nonce_length <= 13) {
4580 return PSA_SUCCESS;
4581 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004582 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004583#endif /* PSA_WANT_ALG_CCM */
4584#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004585 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 if (nonce_length == 12) {
4587 return PSA_SUCCESS;
4588 } else if (nonce_length == 8) {
4589 return PSA_ERROR_NOT_SUPPORTED;
4590 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004591 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004592#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004593 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004594 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004596 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004597
Gilles Peskine449bd832023-01-11 14:50:10 +01004598 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004599}
4600
Gilles Peskine449bd832023-01-11 14:50:10 +01004601static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004602{
Gilles Peskine449bd832023-01-11 14:50:10 +01004603 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4604 return PSA_ERROR_INVALID_ARGUMENT;
4605 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004606
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004608}
4609
Gilles Peskine449bd832023-01-11 14:50:10 +01004610psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4611 psa_algorithm_t alg,
4612 const uint8_t *nonce,
4613 size_t nonce_length,
4614 const uint8_t *additional_data,
4615 size_t additional_data_length,
4616 const uint8_t *plaintext,
4617 size_t plaintext_length,
4618 uint8_t *ciphertext,
4619 size_t ciphertext_size,
4620 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004621{
Ronald Cron215633c2021-03-16 17:15:37 +01004622 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4623 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004624
mohammad1603f08a5502018-06-03 15:05:47 +03004625 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004626
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 status = psa_aead_check_algorithm(alg);
4628 if (status != PSA_SUCCESS) {
4629 return status;
4630 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004631
Ronald Cron9a986162021-03-26 12:40:07 +01004632 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4634 if (status != PSA_SUCCESS) {
4635 return status;
4636 }
mohammad16035955c982018-04-26 00:53:03 +03004637
Ronald Cron215633c2021-03-16 17:15:37 +01004638 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004640 };
mohammad16035955c982018-04-26 00:53:03 +03004641
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 status = psa_aead_check_nonce_length(alg, nonce_length);
4643 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004644 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004645 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004646
Ronald Cronde822812021-03-17 16:08:20 +01004647 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004648 &attributes, slot->key.data, slot->key.bytes,
4649 alg,
4650 nonce, nonce_length,
4651 additional_data, additional_data_length,
4652 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004654
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4656 memset(ciphertext, 0, ciphertext_size);
4657 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004658
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004659exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004661
Gilles Peskine449bd832023-01-11 14:50:10 +01004662 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004663}
4664
Gilles Peskine449bd832023-01-11 14:50:10 +01004665psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4666 psa_algorithm_t alg,
4667 const uint8_t *nonce,
4668 size_t nonce_length,
4669 const uint8_t *additional_data,
4670 size_t additional_data_length,
4671 const uint8_t *ciphertext,
4672 size_t ciphertext_length,
4673 uint8_t *plaintext,
4674 size_t plaintext_size,
4675 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004676{
Ronald Cron215633c2021-03-16 17:15:37 +01004677 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4678 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004679
Gilles Peskineee652a32018-06-01 19:23:52 +02004680 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004681
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 status = psa_aead_check_algorithm(alg);
4683 if (status != PSA_SUCCESS) {
4684 return status;
4685 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004686
Ronald Cron9a986162021-03-26 12:40:07 +01004687 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4689 if (status != PSA_SUCCESS) {
4690 return status;
4691 }
mohammad16035955c982018-04-26 00:53:03 +03004692
Ronald Cron215633c2021-03-16 17:15:37 +01004693 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004694 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004695 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004696
Gilles Peskine449bd832023-01-11 14:50:10 +01004697 status = psa_aead_check_nonce_length(alg, nonce_length);
4698 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004699 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004700 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004701
Ronald Cronde822812021-03-17 16:08:20 +01004702 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004703 &attributes, slot->key.data, slot->key.bytes,
4704 alg,
4705 nonce, nonce_length,
4706 additional_data, additional_data_length,
4707 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004708 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004709
Gilles Peskine449bd832023-01-11 14:50:10 +01004710 if (status != PSA_SUCCESS && plaintext_size != 0) {
4711 memset(plaintext, 0, plaintext_size);
4712 }
mohammad160360a64d02018-06-03 17:20:42 +03004713
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004714exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004716
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 return status;
mohammad16035955c982018-04-26 00:53:03 +03004718}
4719
Gilles Peskine449bd832023-01-11 14:50:10 +01004720static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4721{
4722 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004723
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004725#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004727 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004728 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4729 return PSA_ERROR_INVALID_ARGUMENT;
4730 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004731 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004732#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004733
Przemek Stekiel86679c72022-10-06 17:06:56 +02004734#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004736 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004737 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4738 return PSA_ERROR_INVALID_ARGUMENT;
4739 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004740 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004741#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004742
Przemek Stekiel86679c72022-10-06 17:06:56 +02004743#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004745 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004746 if (tag_len != 16) {
4747 return PSA_ERROR_INVALID_ARGUMENT;
4748 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004749 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004750#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004751
4752 default:
4753 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004755 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004757}
4758
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004759/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004760static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4761 int is_encrypt,
4762 mbedtls_svc_key_id_t key,
4763 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004764{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004765 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4766 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4767 psa_key_slot_t *slot = NULL;
4768 psa_key_usage_t key_usage = 0;
4769
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 status = psa_aead_check_algorithm(alg);
4771 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004772 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004773 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004774
Gilles Peskine449bd832023-01-11 14:50:10 +01004775 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004776 status = PSA_ERROR_BAD_STATE;
4777 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004778 }
4779
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 if (operation->nonce_set || operation->lengths_set ||
4781 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004782 status = PSA_ERROR_BAD_STATE;
4783 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004784 }
4785
Gilles Peskine449bd832023-01-11 14:50:10 +01004786 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004787 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004788 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004789 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004790 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004791
Gilles Peskine449bd832023-01-11 14:50:10 +01004792 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4793 alg);
4794 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004795 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004796 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004797
4798 psa_key_attributes_t attributes = {
4799 .core = slot->attr
4800 };
4801
Gilles Peskine449bd832023-01-11 14:50:10 +01004802 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004803 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004805
Gilles Peskine449bd832023-01-11 14:50:10 +01004806 if (is_encrypt) {
4807 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4808 &attributes,
4809 slot->key.data,
4810 slot->key.bytes,
4811 alg);
4812 } else {
4813 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4814 &attributes,
4815 slot->key.data,
4816 slot->key.bytes,
4817 alg);
4818 }
4819 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004820 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004822
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004824
4825exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004827
Gilles Peskine449bd832023-01-11 14:50:10 +01004828 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004829 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004830 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004831 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 } else {
4833 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004834 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004835
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004837}
4838
Paul Elliott302ff6b2021-04-20 18:10:30 +01004839/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004840psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4841 mbedtls_svc_key_id_t key,
4842 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004843{
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004845}
4846
4847/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004848psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4849 mbedtls_svc_key_id_t key,
4850 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004851{
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004853}
4854
4855/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004856psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4857 uint8_t *nonce,
4858 size_t nonce_size,
4859 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004860{
4861 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004862 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004863 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004864
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004865 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004866
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004868 status = PSA_ERROR_BAD_STATE;
4869 goto exit;
4870 }
4871
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004873 status = PSA_ERROR_BAD_STATE;
4874 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004875 }
4876
Paul Elliotte193ea82021-10-01 13:00:16 +01004877 /* For CCM, this size may not be correct according to the PSA
4878 * specification. The PSA Crypto 1.0.1 specification states:
4879 *
4880 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4881 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4882 *
4883 * However this restriction that L has to be the smallest integer is not
4884 * applied in practice, and it is not implementable here since the
4885 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4887 operation->alg);
4888 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004889 status = PSA_ERROR_BUFFER_TOO_SMALL;
4890 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004891 }
4892
Gilles Peskine449bd832023-01-11 14:50:10 +01004893 status = psa_generate_random(local_nonce, required_nonce_size);
4894 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004895 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004897
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004899
Paul Elliott39dc6b82021-05-11 19:16:09 +01004900exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 if (status == PSA_SUCCESS) {
4902 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004903 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004904 } else {
4905 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004906 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004907
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004909}
4910
4911/* Set the nonce for a multipart authenticated encryption or decryption
4912 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004913psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4914 const uint8_t *nonce,
4915 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004916{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004917 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4918
Gilles Peskine449bd832023-01-11 14:50:10 +01004919 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004920 status = PSA_ERROR_BAD_STATE;
4921 goto exit;
4922 }
4923
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004925 status = PSA_ERROR_BAD_STATE;
4926 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004927 }
4928
Gilles Peskine449bd832023-01-11 14:50:10 +01004929 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4930 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004931 status = PSA_ERROR_INVALID_ARGUMENT;
4932 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004933 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004934
Gilles Peskine449bd832023-01-11 14:50:10 +01004935 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4936 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004937
Paul Elliott39dc6b82021-05-11 19:16:09 +01004938exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004939 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004940 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004941 } else {
4942 psa_aead_abort(operation);
4943 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004944
Gilles Peskine449bd832023-01-11 14:50:10 +01004945 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004946}
4947
4948/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004949psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4950 size_t ad_length,
4951 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004952{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004953 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4954
Gilles Peskine449bd832023-01-11 14:50:10 +01004955 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004956 status = PSA_ERROR_BAD_STATE;
4957 goto exit;
4958 }
4959
Gilles Peskine449bd832023-01-11 14:50:10 +01004960 if (operation->lengths_set || operation->ad_started ||
4961 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004962 status = PSA_ERROR_BAD_STATE;
4963 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004964 }
4965
Gilles Peskine449bd832023-01-11 14:50:10 +01004966 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004967#if defined(PSA_WANT_ALG_GCM)
4968 case PSA_ALG_GCM:
4969 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 * bits. Without the guard this code will generate warnings on 32bit
4971 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004972#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 if (((uint64_t) ad_length) >> 61 != 0 ||
4974 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004975 status = PSA_ERROR_INVALID_ARGUMENT;
4976 goto exit;
4977 }
Paul Elliott325d3742021-09-27 17:56:28 +01004978#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004979 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004980#endif /* PSA_WANT_ALG_GCM */
4981#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004982 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004983 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004984 status = PSA_ERROR_INVALID_ARGUMENT;
4985 goto exit;
4986 }
4987 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004988#endif /* PSA_WANT_ALG_CCM */
4989#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004990 case PSA_ALG_CHACHA20_POLY1305:
4991 /* No length restrictions for ChaChaPoly. */
4992 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004993#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004994 default:
4995 break;
4996 }
Paul Elliott325d3742021-09-27 17:56:28 +01004997
Gilles Peskine449bd832023-01-11 14:50:10 +01004998 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4999 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005000
Paul Elliott39dc6b82021-05-11 19:16:09 +01005001exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005003 operation->ad_remaining = ad_length;
5004 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01005005 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005006 } else {
5007 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01005008 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005009
Gilles Peskine449bd832023-01-11 14:50:10 +01005010 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005011}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01005012
5013/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005014psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
5015 const uint8_t *input,
5016 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005017{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005018 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5019
Gilles Peskine449bd832023-01-11 14:50:10 +01005020 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005021 status = PSA_ERROR_BAD_STATE;
5022 goto exit;
5023 }
5024
Gilles Peskine449bd832023-01-11 14:50:10 +01005025 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005026 status = PSA_ERROR_BAD_STATE;
5027 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005028 }
5029
Gilles Peskine449bd832023-01-11 14:50:10 +01005030 if (operation->lengths_set) {
5031 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005032 status = PSA_ERROR_INVALID_ARGUMENT;
5033 goto exit;
5034 }
5035
5036 operation->ad_remaining -= input_length;
5037 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005038#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005039 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005040 status = PSA_ERROR_BAD_STATE;
5041 goto exit;
5042 }
5043#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005044
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 status = psa_driver_wrapper_aead_update_ad(operation, input,
5046 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005047
Paul Elliott39dc6b82021-05-11 19:16:09 +01005048exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005049 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005050 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 } else {
5052 psa_aead_abort(operation);
5053 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005054
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005056}
5057
5058/* Encrypt or decrypt a message fragment in an active multipart AEAD
5059 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005060psa_status_t psa_aead_update(psa_aead_operation_t *operation,
5061 const uint8_t *input,
5062 size_t input_length,
5063 uint8_t *output,
5064 size_t output_size,
5065 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005066{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005067 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005068
5069 *output_length = 0;
5070
Gilles Peskine449bd832023-01-11 14:50:10 +01005071 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005072 status = PSA_ERROR_BAD_STATE;
5073 goto exit;
5074 }
5075
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005077 status = PSA_ERROR_BAD_STATE;
5078 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005079 }
5080
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005082 /* Additional data length was supplied, but not all the additional
5083 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005085 status = PSA_ERROR_INVALID_ARGUMENT;
5086 goto exit;
5087 }
5088
5089 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005091 status = PSA_ERROR_INVALID_ARGUMENT;
5092 goto exit;
5093 }
5094
5095 operation->body_remaining -= input_length;
5096 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005097#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005098 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005099 status = PSA_ERROR_BAD_STATE;
5100 goto exit;
5101 }
5102#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005103
Gilles Peskine449bd832023-01-11 14:50:10 +01005104 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5105 output, output_size,
5106 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005107
Paul Elliott39dc6b82021-05-11 19:16:09 +01005108exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005110 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 } else {
5112 psa_aead_abort(operation);
5113 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005114
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005116}
5117
Gilles Peskine449bd832023-01-11 14:50:10 +01005118static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005119{
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 if (operation->id == 0 || !operation->nonce_set) {
5121 return PSA_ERROR_BAD_STATE;
5122 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005123
Gilles Peskine449bd832023-01-11 14:50:10 +01005124 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5125 operation->body_remaining != 0)) {
5126 return PSA_ERROR_INVALID_ARGUMENT;
5127 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005128
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005130}
5131
Paul Elliott302ff6b2021-04-20 18:10:30 +01005132/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005133psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5134 uint8_t *ciphertext,
5135 size_t ciphertext_size,
5136 size_t *ciphertext_length,
5137 uint8_t *tag,
5138 size_t tag_size,
5139 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005140{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005141 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5142
Paul Elliott302ff6b2021-04-20 18:10:30 +01005143 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005144 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005145
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 status = psa_aead_final_checks(operation);
5147 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005148 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005150
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005152 status = PSA_ERROR_BAD_STATE;
5153 goto exit;
5154 }
5155
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5157 ciphertext_size,
5158 ciphertext_length,
5159 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005160
Paul Elliott39dc6b82021-05-11 19:16:09 +01005161exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005162
5163
Paul Elliottf47b0952021-05-21 18:02:33 +01005164 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005165 * the zero tag size, make sure the tag is set to something implausible.
5166 * Even if the operation succeeds, make sure we clear the rest of the
5167 * buffer to prevent potential leakage of anything previously placed in
5168 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005169 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005170
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005172
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005174}
5175
5176/* Finish authenticating and decrypting a message in a multipart AEAD
5177 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005178psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5179 uint8_t *plaintext,
5180 size_t plaintext_size,
5181 size_t *plaintext_length,
5182 const uint8_t *tag,
5183 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005184{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005185 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5186
Paul Elliott302ff6b2021-04-20 18:10:30 +01005187 *plaintext_length = 0;
5188
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 status = psa_aead_final_checks(operation);
5190 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005191 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005193
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005195 status = PSA_ERROR_BAD_STATE;
5196 goto exit;
5197 }
5198
Gilles Peskine449bd832023-01-11 14:50:10 +01005199 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5200 plaintext_size,
5201 plaintext_length,
5202 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005203
5204exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005205 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005206
Gilles Peskine449bd832023-01-11 14:50:10 +01005207 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005208}
5209
5210/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005211psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005212{
5213 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5214
Gilles Peskine449bd832023-01-11 14:50:10 +01005215 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005216 /* The object has (apparently) been initialized but it is not (yet)
5217 * in use. It's ok to call abort on such an object, and there's
5218 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005219 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005220 }
5221
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005223
Gilles Peskine449bd832023-01-11 14:50:10 +01005224 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005225
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005227}
5228
Gilles Peskinee59236f2018-01-27 23:32:46 +01005229/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005230/* Generators */
5231/****************************************************************/
5232
Przemek Stekiel69c46792022-06-10 12:59:51 +02005233#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005234 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005235 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
5236 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08005237#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005238#endif /* At least one builtin KDF */
5239
Przemek Stekiel69c46792022-06-10 12:59:51 +02005240#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005241 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5242 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5243static psa_status_t psa_key_derivation_start_hmac(
5244 psa_mac_operation_t *operation,
5245 psa_algorithm_t hash_alg,
5246 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005247 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005248{
5249 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5250 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005251 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5252 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5253 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005254
Steven Cooreman72f736a2021-05-07 14:14:37 +02005255 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005257
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 status = psa_driver_wrapper_mac_sign_setup(operation,
5259 &attributes,
5260 hmac_key, hmac_key_length,
5261 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005262
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 psa_reset_key_attributes(&attributes);
5264 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005265}
5266#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005267
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005268#define HKDF_STATE_INIT 0 /* no input yet */
5269#define HKDF_STATE_STARTED 1 /* got salt */
5270#define HKDF_STATE_KEYED 2 /* got key */
5271#define HKDF_STATE_OUTPUT 3 /* output started */
5272
Gilles Peskinecbe66502019-05-16 16:59:18 +02005273static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005275{
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5277 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5278 } else {
5279 return operation->alg;
5280 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005281}
5282
Gilles Peskine449bd832023-01-11 14:50:10 +01005283psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005284{
5285 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5287 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005288 /* The object has (apparently) been initialized but it is not
5289 * in use. It's ok to call abort on such an object, and there's
5290 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005292#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005293 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5294 mbedtls_free(operation->ctx.hkdf.info);
5295 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5296 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005297#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005298#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5299 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5301 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5302 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5303 if (operation->ctx.tls12_prf.secret != NULL) {
5304 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5305 operation->ctx.tls12_prf.secret_length);
5306 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005307 }
5308
Gilles Peskine449bd832023-01-11 14:50:10 +01005309 if (operation->ctx.tls12_prf.seed != NULL) {
5310 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5311 operation->ctx.tls12_prf.seed_length);
5312 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005313 }
5314
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 if (operation->ctx.tls12_prf.label != NULL) {
5316 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5317 operation->ctx.tls12_prf.label_length);
5318 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005319 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005320#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005321 if (operation->ctx.tls12_prf.other_secret != NULL) {
5322 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5323 operation->ctx.tls12_prf.other_secret_length);
5324 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005325 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005326#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005327 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005328
5329 /* We leave the fields Ai and output_block to be erased safely by the
5330 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005332#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5333 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005334#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5336 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5337 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5338 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005339#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005340 {
5341 status = PSA_ERROR_BAD_STATE;
5342 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 mbedtls_platform_zeroize(operation, sizeof(*operation));
5344 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005345}
5346
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005347psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005349{
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005351 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005353 }
5354
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005355 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005357}
5358
Gilles Peskine449bd832023-01-11 14:50:10 +01005359psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5360 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005361{
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 if (operation->alg == 0) {
5363 return PSA_ERROR_BAD_STATE;
5364 }
5365 if (capacity > operation->capacity) {
5366 return PSA_ERROR_INVALID_ARGUMENT;
5367 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005368 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005370}
5371
Przemek Stekiel69c46792022-06-10 12:59:51 +02005372#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005373/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005374static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5375 psa_algorithm_t kdf_alg,
5376 uint8_t *output,
5377 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005378{
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5380 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005381 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005382 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005383#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005385#else
5386 const uint8_t last_block = 0xff;
5387#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005388
Gilles Peskine449bd832023-01-11 14:50:10 +01005389 if (hkdf->state < HKDF_STATE_KEYED ||
5390 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005391#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005393#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 )) {
5395 return PSA_ERROR_BAD_STATE;
5396 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005397 hkdf->state = HKDF_STATE_OUTPUT;
5398
Gilles Peskine449bd832023-01-11 14:50:10 +01005399 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005400 /* Copy what remains of the current block */
5401 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005403 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 }
5405 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005406 output += n;
5407 output_length -= n;
5408 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005409 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005410 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005411 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005412 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005413 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005414 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005415 * object was corrupted or if this function is called directly
5416 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 if (hkdf->block_number == last_block) {
5418 return PSA_ERROR_BAD_STATE;
5419 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005420
5421 /* We need a new block */
5422 ++hkdf->block_number;
5423 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005424
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5426 hash_alg,
5427 hkdf->prk,
5428 hash_length);
5429 if (status != PSA_SUCCESS) {
5430 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005431 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005432
5433 if (hkdf->block_number != 1) {
5434 status = psa_mac_update(&hkdf->hmac,
5435 hkdf->output_block,
5436 hash_length);
5437 if (status != PSA_SUCCESS) {
5438 return status;
5439 }
5440 }
5441 status = psa_mac_update(&hkdf->hmac,
5442 hkdf->info,
5443 hkdf->info_length);
5444 if (status != PSA_SUCCESS) {
5445 return status;
5446 }
5447 status = psa_mac_update(&hkdf->hmac,
5448 &hkdf->block_number, 1);
5449 if (status != PSA_SUCCESS) {
5450 return status;
5451 }
5452 status = psa_mac_sign_finish(&hkdf->hmac,
5453 hkdf->output_block,
5454 sizeof(hkdf->output_block),
5455 &hmac_output_length);
5456 if (status != PSA_SUCCESS) {
5457 return status;
5458 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005459 }
5460
Gilles Peskine449bd832023-01-11 14:50:10 +01005461 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005462}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005463#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005464
John Durkop07cc04a2020-11-16 22:08:34 -08005465#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5466 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005467static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5468 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005470{
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5472 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005473 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5474 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005475 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005476
Janos Follath7742fee2019-06-17 12:58:10 +01005477 /* We can't be wanting more output after block 0xff, otherwise
5478 * the capacity check in psa_key_derivation_output_bytes() would have
5479 * prevented this call. It could happen only if the operation
5480 * object was corrupted or if this function is called directly
5481 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 if (tls12_prf->block_number == 0xff) {
5483 return PSA_ERROR_CORRUPTION_DETECTED;
5484 }
Janos Follath7742fee2019-06-17 12:58:10 +01005485
5486 /* We need a new block */
5487 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005488 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005489
5490 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5491 *
5492 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5493 *
5494 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5495 * HMAC_hash(secret, A(2) + seed) +
5496 * HMAC_hash(secret, A(3) + seed) + ...
5497 *
5498 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005499 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005500 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005501 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005502 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005503 * is currently extracted as `output_block` and where i is
5504 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005505 */
5506
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 status = psa_key_derivation_start_hmac(&hmac,
5508 hash_alg,
5509 tls12_prf->secret,
5510 tls12_prf->secret_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
5515 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005517 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5518 * the variable seed and in this instance means it in the context of the
5519 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 status = psa_mac_update(&hmac,
5521 tls12_prf->label,
5522 tls12_prf->label_length);
5523 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005524 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 }
5526 status = psa_mac_update(&hmac,
5527 tls12_prf->seed,
5528 tls12_prf->seed_length);
5529 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005530 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 }
5532 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005533 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5535 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005536 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005537 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005538 }
5539
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 status = psa_mac_sign_finish(&hmac,
5541 tls12_prf->Ai, hash_length,
5542 &hmac_output_length);
5543 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005544 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 }
5546 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005547 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005549
5550 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005551 status = psa_key_derivation_start_hmac(&hmac,
5552 hash_alg,
5553 tls12_prf->secret,
5554 tls12_prf->secret_length);
5555 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005556 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 }
5558 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5559 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005560 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005561 }
5562 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5563 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005564 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 }
5566 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5567 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005568 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 }
5570 status = psa_mac_sign_finish(&hmac,
5571 tls12_prf->output_block, hash_length,
5572 &hmac_output_length);
5573 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005574 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005576
Janos Follath7742fee2019-06-17 12:58:10 +01005577
5578cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 cleanup_status = psa_mac_abort(&hmac);
5580 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005581 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005582 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005583
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005585}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005586
Janos Follath844eb0e2019-06-19 12:10:49 +01005587static psa_status_t psa_key_derivation_tls12_prf_read(
5588 psa_tls12_prf_key_derivation_t *tls12_prf,
5589 psa_algorithm_t alg,
5590 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005591 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005592{
Gilles Peskine449bd832023-01-11 14:50:10 +01005593 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5594 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005595 psa_status_t status;
5596 uint8_t offset, length;
5597
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005599 case PSA_TLS12_PRF_STATE_LABEL_SET:
5600 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5601 break;
5602 case PSA_TLS12_PRF_STATE_OUTPUT:
5603 break;
5604 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005605 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005606 }
5607
Gilles Peskine449bd832023-01-11 14:50:10 +01005608 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005609 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 if (tls12_prf->left_in_block == 0) {
5611 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5612 alg);
5613 if (status != PSA_SUCCESS) {
5614 return status;
5615 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005616
5617 continue;
5618 }
5619
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005621 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005622 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005623 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005624 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005625
5626 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005628 output += length;
5629 output_length -= length;
5630 tls12_prf->left_in_block -= length;
5631 }
5632
Gilles Peskine449bd832023-01-11 14:50:10 +01005633 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005634}
John Durkop07cc04a2020-11-16 22:08:34 -08005635#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5636 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005637
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005638#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5639static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5640 psa_tls12_ecjpake_to_pms_t *ecjpake,
5641 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005643{
5644 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005645 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005646
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 if (output_length != 32) {
5648 return PSA_ERROR_INVALID_ARGUMENT;
5649 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005650
Gilles Peskine449bd832023-01-11 14:50:10 +01005651 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5652 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5653 &output_size);
5654 if (status != PSA_SUCCESS) {
5655 return status;
5656 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005657
Gilles Peskine449bd832023-01-11 14:50:10 +01005658 if (output_size != output_length) {
5659 return PSA_ERROR_GENERIC_ERROR;
5660 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005661
Gilles Peskine449bd832023-01-11 14:50:10 +01005662 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005663}
5664#endif
5665
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005666psa_status_t psa_key_derivation_output_bytes(
5667 psa_key_derivation_operation_t *operation,
5668 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005669 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005670{
5671 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005673
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005675 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005677 }
5678
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005680 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005681 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005682 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005683 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005684 goto exit;
5685 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005687 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005688 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005689 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5690 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005691 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005692 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005693 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005694 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005695 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005696
Przemek Stekiel69c46792022-06-10 12:59:51 +02005697#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005698 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5699 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5700 output, output_length);
5701 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005702#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005703#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5704 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005705 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5706 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5707 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5708 kdf_alg, output,
5709 output_length);
5710 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005711#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5712 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005713#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005714 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005715 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5717 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005718#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5719
Gilles Peskineeab56e42018-07-12 17:12:33 +02005720 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005721 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005723 }
5724
5725exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005727 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005728 * This allows us to differentiate between exhausted operations and
5729 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5730 * operations. */
5731 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005733 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005735 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005737}
5738
David Brown7807bf72021-02-09 16:28:23 -07005739#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005740static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005741{
Gilles Peskine449bd832023-01-11 14:50:10 +01005742 if (data_size >= 8) {
5743 mbedtls_des_key_set_parity(data);
5744 }
5745 if (data_size >= 16) {
5746 mbedtls_des_key_set_parity(data + 8);
5747 }
5748 if (data_size >= 24) {
5749 mbedtls_des_key_set_parity(data + 16);
5750 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005751}
David Brown7807bf72021-02-09 16:28:23 -07005752#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005753
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005754/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005755 * ECC keys on a Weierstrass elliptic curve require the generation
5756 * of a private key which is an integer
5757 * in the range [1, N - 1], where N is the boundary of the private key domain:
5758 * N is the prime p for Diffie-Hellman, or the order of the
5759 * curve’s base point for ECC.
5760 *
5761 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5762 * This function generates the private key using the following process:
5763 *
5764 * 1. Draw a byte string of length ceiling(m/8) bytes.
5765 * 2. If m is not a multiple of 8, set the most significant
5766 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5767 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5768 * 4. If k > N - 2, discard the result and return to step 1.
5769 * 5. Output k + 1 as the private key.
5770 *
5771 * This method allows compliance to NIST standards, specifically the methods titled
5772 * Key-Pair Generation by Testing Candidates in the following publications:
5773 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5774 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5775 * Diffie-Hellman keys.
5776 *
5777 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5778 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5779 *
5780 * Note: Function allocates memory for *data buffer, so given *data should be
5781 * always NULL.
5782 */
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005783#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5784 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005785 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5786 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5787 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005788static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005789 psa_key_slot_t *slot,
5790 size_t bits,
5791 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005792 uint8_t **data
5793 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005794{
Valerio Setti52789862023-04-07 12:13:11 +02005795#if defined(MBEDTLS_ECP_LIGHT)
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005796 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005797 mbedtls_mpi k;
5798 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005799 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005800 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005801
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 mbedtls_mpi_init(&k);
5803 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005804
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005805 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005806 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005807 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005809
Gilles Peskine449bd832023-01-11 14:50:10 +01005810 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005811 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005812 goto cleanup;
5813 }
5814
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005815 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005817
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005819
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005820 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005821 /* Let m be the bit size of N. */
5822 size_t m = ecp_group.nbits;
5823
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005825
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005826 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005828
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005829 /* Note: This function is always called with *data == NULL and it
5830 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 *data = mbedtls_calloc(1, m_bytes);
5832 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005833 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005834 goto cleanup;
5835 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005836
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005838 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005839 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005840 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005842
5843 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005844 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005845 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005846 * (8 * ceiling(m/8) - m) bits of the first byte in
5847 * the string to zero.
5848 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005849 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005850 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005851 }
5852
5853 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 * big-endian byte string.
5855 */
5856 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005857
5858 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 * Result of comparison is returned. When it indicates error
5860 * then this function is called again.
5861 */
5862 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005863 }
5864
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005865 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5867 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005868cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005869 if (ret != 0) {
5870 status = mbedtls_to_psa_error(ret);
5871 }
5872 if (status != PSA_SUCCESS) {
5873 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005874 *data = NULL;
5875 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 mbedtls_mpi_free(&k);
5877 mbedtls_mpi_free(&diff_N_2);
5878 return status;
Valerio Setti52789862023-04-07 12:13:11 +02005879#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005880 (void) slot;
5881 (void) bits;
5882 (void) operation;
5883 (void) data;
5884 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti52789862023-04-07 12:13:11 +02005885#endif /* MBEDTLS_ECP_LIGHT */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005886}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005887
5888/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5889 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5890 *
5891 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5892 * draw a 32-byte string and process it as specified in
5893 * Elliptic Curves for Security [RFC7748] §5.
5894 *
5895 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5896 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5897 *
5898 * Note: Function allocates memory for *data buffer, so given *data should be
5899 * always NULL.
5900 */
5901
5902static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5903 size_t bits,
5904 psa_key_derivation_operation_t *operation,
5905 uint8_t **data
5906 )
5907{
5908 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005909 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005910
Gilles Peskine449bd832023-01-11 14:50:10 +01005911 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005912 case 255:
5913 output_length = 32;
5914 break;
5915 case 448:
5916 output_length = 56;
5917 break;
5918 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005919 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005920 break;
5921 }
5922
Gilles Peskine449bd832023-01-11 14:50:10 +01005923 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005924
Gilles Peskine449bd832023-01-11 14:50:10 +01005925 if (*data == NULL) {
5926 return PSA_ERROR_INSUFFICIENT_MEMORY;
5927 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005928
Gilles Peskine449bd832023-01-11 14:50:10 +01005929 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005930
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005932 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005934
Gilles Peskine449bd832023-01-11 14:50:10 +01005935 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005936 case 255:
5937 (*data)[0] &= 248;
5938 (*data)[31] &= 127;
5939 (*data)[31] |= 64;
5940 break;
5941 case 448:
5942 (*data)[0] &= 252;
5943 (*data)[55] |= 128;
5944 break;
5945 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005946 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005947 break;
5948 }
5949
5950 return status;
5951}
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005952#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5953 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005954 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5955 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5956 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005957
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005958static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005959 psa_key_slot_t *slot,
5960 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005961 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005962{
5963 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005964 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305965 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005966 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005967
Gilles Peskine449bd832023-01-11 14:50:10 +01005968 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5969 return PSA_ERROR_INVALID_ARGUMENT;
5970 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005971
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005972#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5973 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005974 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5975 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5976 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5978 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5979 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005980 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5982 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005983 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 }
5985 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005986 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005987 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5988 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005989 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005991 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005992 } else
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005993#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5994 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005995 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5996 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5997 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005998 if (key_type_is_raw_bytes(slot->attr.type)) {
5999 if (bits % 8 != 0) {
6000 return PSA_ERROR_INVALID_ARGUMENT;
6001 }
6002 data = mbedtls_calloc(1, bytes);
6003 if (data == NULL) {
6004 return PSA_ERROR_INSUFFICIENT_MEMORY;
6005 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006006
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 status = psa_key_derivation_output_bytes(operation, data, bytes);
6008 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006009 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006010 }
David Brown12ca5032021-02-11 11:02:00 -07006011#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6013 psa_des_set_key_parity(data, bytes);
6014 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006015#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 } else {
6017 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006018 }
Ronald Crondd04d422020-11-28 15:14:42 +01006019
Ronald Cronbf33c932020-11-28 18:06:53 +01006020 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006021 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006023 };
6024
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6026 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6027 &storage_size);
6028 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306029 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006030 }
Archanad8a83dc2021-06-14 10:04:16 +05306031 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006032 status = psa_allocate_buffer_to_slot(slot, storage_size);
6033 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306034 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006035 }
Archanad8a83dc2021-06-14 10:04:16 +05306036
Gilles Peskine449bd832023-01-11 14:50:10 +01006037 status = psa_driver_wrapper_import_key(&attributes,
6038 data, bytes,
6039 slot->key.data,
6040 slot->key.bytes,
6041 &slot->key.bytes, &bits);
6042 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006043 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006044 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006045
6046exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006047 mbedtls_free(data);
6048 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006049}
6050
Gilles Peskine449bd832023-01-11 14:50:10 +01006051psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6052 psa_key_derivation_operation_t *operation,
6053 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006054{
6055 psa_status_t status;
6056 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006057 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006058
Ronald Cron81709fc2020-11-14 12:10:32 +01006059 *key = MBEDTLS_SVC_KEY_ID_INIT;
6060
Gilles Peskine0f84d622019-09-12 19:03:13 +02006061 /* Reject any attempt to create a zero-length key so that we don't
6062 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 if (psa_get_key_bits(attributes) == 0) {
6064 return PSA_ERROR_INVALID_ARGUMENT;
6065 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006066
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 if (operation->alg == PSA_ALG_NONE) {
6068 return PSA_ERROR_BAD_STATE;
6069 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006070
Gilles Peskine449bd832023-01-11 14:50:10 +01006071 if (!operation->can_output_key) {
6072 return PSA_ERROR_NOT_PERMITTED;
6073 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006074
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6076 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006077#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006079 /* Deriving a key in a secure element is not implemented yet. */
6080 status = PSA_ERROR_NOT_SUPPORTED;
6081 }
6082#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 if (status == PSA_SUCCESS) {
6084 status = psa_generate_derived_key_internal(slot,
6085 attributes->core.bits,
6086 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006087 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 if (status == PSA_SUCCESS) {
6089 status = psa_finish_key_creation(slot, driver, key);
6090 }
6091 if (status != PSA_SUCCESS) {
6092 psa_fail_key_creation(slot, driver);
6093 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006094
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006096}
6097
Gilles Peskineeab56e42018-07-12 17:12:33 +02006098
6099
6100/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006101/* Key derivation */
6102/****************************************************************/
6103
Steven Cooreman932ffb72021-02-15 12:14:32 +01006104#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006105static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006106{
6107#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6109 return 1;
6110 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006111#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006112#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6114 return 1;
6115 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006116#endif
6117#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6119 return 1;
6120 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006121#endif
6122#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6124 return 1;
6125 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006126#endif
6127#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006128 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6129 return 1;
6130 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006131#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006132#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6134 return 1;
6135 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006136#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006137 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006138}
6139
Gilles Peskine449bd832023-01-11 14:50:10 +01006140static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006141{
6142 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 psa_status_t status = psa_hash_setup(&operation, alg);
6144 psa_hash_abort(&operation);
6145 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006146}
6147
Gilles Peskine969c5d62019-01-16 15:53:06 +01006148static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006149 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006151{
Janos Follath5fe19732019-06-20 15:09:30 +01006152 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6153 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006155
Gilles Peskine969c5d62019-01-16 15:53:06 +01006156 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 if (!is_kdf_alg_supported(kdf_alg)) {
6158 return PSA_ERROR_NOT_SUPPORTED;
6159 }
John Durkop07cc04a2020-11-16 22:08:34 -08006160
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006161 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04006162 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6164 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
6165 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6166 if (hash_size == 0) {
6167 return PSA_ERROR_NOT_SUPPORTED;
6168 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006169
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006170 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6171 * we might fail later, which is somewhat unfriendly and potentially
6172 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006173 psa_status_t status = psa_hash_try_support(hash_alg);
6174 if (status != PSA_SUCCESS) {
6175 return status;
6176 }
6177 } else {
6178 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006179 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006180
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6182 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6183 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6184 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006185 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006186#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006187 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6189 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006190 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006192#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6193 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006194 operation->capacity = 255 * hash_size;
6195 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006196}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006197
Gilles Peskine449bd832023-01-11 14:50:10 +01006198static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006199{
6200#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 if (alg == PSA_ALG_ECDH) {
6202 return PSA_SUCCESS;
6203 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006204#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006205#if defined(PSA_WANT_ALG_FFDH)
6206 if (alg == PSA_ALG_FFDH) {
6207 return PSA_SUCCESS;
6208 }
6209#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006210 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006212}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006213
6214static int psa_key_derivation_allows_free_form_secret_input(
6215 psa_algorithm_t kdf_alg)
6216{
6217#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6218 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6219 return 0;
6220 }
6221#endif
6222 (void) kdf_alg;
6223 return 1;
6224}
John Durkop07cc04a2020-11-16 22:08:34 -08006225#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006226
Gilles Peskine449bd832023-01-11 14:50:10 +01006227psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6228 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006229{
6230 psa_status_t status;
6231
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 if (operation->alg != 0) {
6233 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006234 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006235
Gilles Peskine449bd832023-01-11 14:50:10 +01006236 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6237 return PSA_ERROR_INVALID_ARGUMENT;
6238 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6239#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6240 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6241 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6242 status = psa_key_agreement_try_support(ka_alg);
6243 if (status != PSA_SUCCESS) {
6244 return status;
6245 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006246 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6247 return PSA_ERROR_INVALID_ARGUMENT;
6248 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6250#else
6251 return PSA_ERROR_NOT_SUPPORTED;
6252#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6253 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6254#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6255 status = psa_key_derivation_setup_kdf(operation, alg);
6256#else
6257 return PSA_ERROR_NOT_SUPPORTED;
6258#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6259 } else {
6260 return PSA_ERROR_INVALID_ARGUMENT;
6261 }
6262
6263 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006264 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006265 }
6266 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006267}
6268
Przemek Stekiel69c46792022-06-10 12:59:51 +02006269#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006270static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6271 psa_algorithm_t kdf_alg,
6272 psa_key_derivation_step_t step,
6273 const uint8_t *data,
6274 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006275{
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006277 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006279 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006280#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006281 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6282 return PSA_ERROR_INVALID_ARGUMENT;
6283 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006284#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 if (hkdf->state != HKDF_STATE_INIT) {
6286 return PSA_ERROR_BAD_STATE;
6287 } else {
6288 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6289 hash_alg,
6290 data, data_length);
6291 if (status != PSA_SUCCESS) {
6292 return status;
6293 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006294 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006296 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006297 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006298#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006300 /* We shouldn't be in different state as HKDF_EXPAND only allows
6301 * two inputs: SECRET (this case) and INFO which does not modify
6302 * the state. It could happen only if the hkdf
6303 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006304 if (hkdf->state != HKDF_STATE_INIT) {
6305 return PSA_ERROR_BAD_STATE;
6306 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006307
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006308 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006309 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6310 return PSA_ERROR_INVALID_ARGUMENT;
6311 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006312
Gilles Peskine449bd832023-01-11 14:50:10 +01006313 memcpy(hkdf->prk, data, data_length);
6314 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006315#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006316 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006317 /* HKDF: If no salt was provided, use an empty salt.
6318 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006319 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006320#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006321 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6322 return PSA_ERROR_BAD_STATE;
6323 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006324#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6326 hash_alg,
6327 NULL, 0);
6328 if (status != PSA_SUCCESS) {
6329 return status;
6330 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006331 hkdf->state = HKDF_STATE_STARTED;
6332 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 if (hkdf->state != HKDF_STATE_STARTED) {
6334 return PSA_ERROR_BAD_STATE;
6335 }
6336 status = psa_mac_update(&hkdf->hmac,
6337 data, data_length);
6338 if (status != PSA_SUCCESS) {
6339 return status;
6340 }
6341 status = psa_mac_sign_finish(&hkdf->hmac,
6342 hkdf->prk,
6343 sizeof(hkdf->prk),
6344 &data_length);
6345 if (status != PSA_SUCCESS) {
6346 return status;
6347 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006348 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006349
Gilles Peskine2b522db2019-04-12 15:11:49 +02006350 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006351 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006352#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006354 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006355 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006356 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006358#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006359 {
6360 /* Block 0 is empty, and the next block will be
6361 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006363 }
6364
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006366 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006367#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6369 return PSA_ERROR_INVALID_ARGUMENT;
6370 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006371#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006372#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6374 hkdf->state == HKDF_STATE_INIT) {
6375 return PSA_ERROR_BAD_STATE;
6376 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006377#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006378 if (hkdf->state == HKDF_STATE_OUTPUT) {
6379 return PSA_ERROR_BAD_STATE;
6380 }
6381 if (hkdf->info_set) {
6382 return PSA_ERROR_BAD_STATE;
6383 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006384 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 if (data_length != 0) {
6386 hkdf->info = mbedtls_calloc(1, data_length);
6387 if (hkdf->info == NULL) {
6388 return PSA_ERROR_INSUFFICIENT_MEMORY;
6389 }
6390 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006391 }
6392 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006394 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006396 }
6397}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006398#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006399
John Durkop07cc04a2020-11-16 22:08:34 -08006400#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6401 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006402static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6403 const uint8_t *data,
6404 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006405{
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6407 return PSA_ERROR_BAD_STATE;
6408 }
Janos Follathf08e2652019-06-13 09:05:41 +01006409
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 if (data_length != 0) {
6411 prf->seed = mbedtls_calloc(1, data_length);
6412 if (prf->seed == NULL) {
6413 return PSA_ERROR_INSUFFICIENT_MEMORY;
6414 }
Janos Follathf08e2652019-06-13 09:05:41 +01006415
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006417 prf->seed_length = data_length;
6418 }
Janos Follathf08e2652019-06-13 09:05:41 +01006419
Gilles Peskine43f958b2020-12-13 14:55:14 +01006420 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006421
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006423}
6424
Gilles Peskine449bd832023-01-11 14:50:10 +01006425static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6426 const uint8_t *data,
6427 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006428{
Gilles Peskine449bd832023-01-11 14:50:10 +01006429 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6430 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6431 return PSA_ERROR_BAD_STATE;
6432 }
Janos Follath81550542019-06-13 14:26:34 +01006433
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 if (data_length != 0) {
6435 prf->secret = mbedtls_calloc(1, data_length);
6436 if (prf->secret == NULL) {
6437 return PSA_ERROR_INSUFFICIENT_MEMORY;
6438 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006439
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006441 prf->secret_length = data_length;
6442 }
Janos Follath81550542019-06-13 14:26:34 +01006443
Gilles Peskine43f958b2020-12-13 14:55:14 +01006444 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006445
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006447}
6448
Gilles Peskine449bd832023-01-11 14:50:10 +01006449static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6450 const uint8_t *data,
6451 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006452{
Gilles Peskine449bd832023-01-11 14:50:10 +01006453 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6454 return PSA_ERROR_BAD_STATE;
6455 }
Janos Follath63028dd2019-06-13 09:15:47 +01006456
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 if (data_length != 0) {
6458 prf->label = mbedtls_calloc(1, data_length);
6459 if (prf->label == NULL) {
6460 return PSA_ERROR_INSUFFICIENT_MEMORY;
6461 }
Janos Follath63028dd2019-06-13 09:15:47 +01006462
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006464 prf->label_length = data_length;
6465 }
Janos Follath63028dd2019-06-13 09:15:47 +01006466
Gilles Peskine43f958b2020-12-13 14:55:14 +01006467 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006468
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006470}
6471
Gilles Peskine449bd832023-01-11 14:50:10 +01006472static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6473 psa_key_derivation_step_t step,
6474 const uint8_t *data,
6475 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006476{
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006478 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006479 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006480 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006481 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006482 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006484 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006486 }
6487}
John Durkop07cc04a2020-11-16 22:08:34 -08006488#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6489 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6490
6491#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6492static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6493 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006494 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006495 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006496{
6497 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6499 4 + data_length + prf->other_secret_length :
6500 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006501
Gilles Peskine449bd832023-01-11 14:50:10 +01006502 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6503 return PSA_ERROR_INVALID_ARGUMENT;
6504 }
John Durkop07cc04a2020-11-16 22:08:34 -08006505
Gilles Peskine449bd832023-01-11 14:50:10 +01006506 uint8_t *pms = mbedtls_calloc(1, pms_len);
6507 if (pms == NULL) {
6508 return PSA_ERROR_INSUFFICIENT_MEMORY;
6509 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006510 uint8_t *cur = pms;
6511
6512 /* pure-PSK:
6513 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006514 *
6515 * The premaster secret is formed as follows: if the PSK is N octets
6516 * long, concatenate a uint16 with the value N, N zero octets, a second
6517 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006518 *
6519 * mixed-PSK:
6520 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6521 * follows: concatenate a uint16 with the length of the other secret,
6522 * the other secret itself, uint16 with the length of PSK, and the
6523 * PSK itself.
6524 * For details please check:
6525 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6526 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6527 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006528 */
6529
Gilles Peskine449bd832023-01-11 14:50:10 +01006530 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6531 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6532 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6533 if (prf->other_secret_length != 0) {
6534 memcpy(cur, prf->other_secret, prf->other_secret_length);
6535 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006536 cur += prf->other_secret_length;
6537 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006538 } else {
6539 *cur++ = MBEDTLS_BYTE_1(data_length);
6540 *cur++ = MBEDTLS_BYTE_0(data_length);
6541 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006542 cur += data_length;
6543 }
6544
Gilles Peskine449bd832023-01-11 14:50:10 +01006545 *cur++ = MBEDTLS_BYTE_1(data_length);
6546 *cur++ = MBEDTLS_BYTE_0(data_length);
6547 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006548 cur += data_length;
6549
Gilles Peskine449bd832023-01-11 14:50:10 +01006550 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006551
Gilles Peskine449bd832023-01-11 14:50:10 +01006552 mbedtls_platform_zeroize(pms, pms_len);
6553 mbedtls_free(pms);
6554 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006555}
Janos Follath6660f0e2019-06-17 08:44:03 +01006556
Przemek Stekiele47201b2022-04-19 13:53:28 +02006557static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6558 psa_tls12_prf_key_derivation_t *prf,
6559 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006560 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006561{
Gilles Peskine449bd832023-01-11 14:50:10 +01006562 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6563 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006564 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006565
6566 if (data_length != 0) {
6567 prf->other_secret = mbedtls_calloc(1, data_length);
6568 if (prf->other_secret == NULL) {
6569 return PSA_ERROR_INSUFFICIENT_MEMORY;
6570 }
6571
6572 memcpy(prf->other_secret, data, data_length);
6573 prf->other_secret_length = data_length;
6574 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006575 prf->other_secret_length = 0;
6576 }
6577
6578 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6579
Gilles Peskine449bd832023-01-11 14:50:10 +01006580 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006581}
6582
Janos Follath6660f0e2019-06-17 08:44:03 +01006583static psa_status_t psa_tls12_prf_psk_to_ms_input(
6584 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006585 psa_key_derivation_step_t step,
6586 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006587 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006588{
Gilles Peskine449bd832023-01-11 14:50:10 +01006589 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006590 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006591 return psa_tls12_prf_psk_to_ms_set_key(prf,
6592 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006593 break;
6594 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006595 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6596 data,
6597 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006598 break;
6599 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006600 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006601 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006602
Przemek Stekiele47201b2022-04-19 13:53:28 +02006603 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006604}
John Durkop07cc04a2020-11-16 22:08:34 -08006605#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006606
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006607#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6608static psa_status_t psa_tls12_ecjpake_to_pms_input(
6609 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006610 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006611 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006612 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006613{
Gilles Peskine449bd832023-01-11 14:50:10 +01006614 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6615 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6616 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006617 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006618
6619 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006620 if (data[0] != 0x04) {
6621 return PSA_ERROR_INVALID_ARGUMENT;
6622 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006623
6624 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006625 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006626
Gilles Peskine449bd832023-01-11 14:50:10 +01006627 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006628}
6629#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006630/** Check whether the given key type is acceptable for the given
6631 * input step of a key derivation.
6632 *
6633 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6634 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6635 * Both secret and non-secret inputs can alternatively have the type
6636 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6637 * that the input was passed as a buffer rather than via a key object.
6638 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006639static int psa_key_derivation_check_input_type(
6640 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006641 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006642{
Gilles Peskine449bd832023-01-11 14:50:10 +01006643 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006644 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006645 if (key_type == PSA_KEY_TYPE_DERIVE) {
6646 return PSA_SUCCESS;
6647 }
6648 if (key_type == PSA_KEY_TYPE_NONE) {
6649 return PSA_SUCCESS;
6650 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006651 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006652 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006653 if (key_type == PSA_KEY_TYPE_DERIVE) {
6654 return PSA_SUCCESS;
6655 }
6656 if (key_type == PSA_KEY_TYPE_NONE) {
6657 return PSA_SUCCESS;
6658 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006659 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006660 case PSA_KEY_DERIVATION_INPUT_LABEL:
6661 case PSA_KEY_DERIVATION_INPUT_SALT:
6662 case PSA_KEY_DERIVATION_INPUT_INFO:
6663 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006664 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6665 return PSA_SUCCESS;
6666 }
6667 if (key_type == PSA_KEY_TYPE_NONE) {
6668 return PSA_SUCCESS;
6669 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006670 break;
6671 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006672 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006673}
6674
Janos Follathb80a94e2019-06-12 15:54:46 +01006675static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006676 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006677 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006678 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006679 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006680 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006681{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006682 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006683 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006684
Gilles Peskine449bd832023-01-11 14:50:10 +01006685 status = psa_key_derivation_check_input_type(step, key_type);
6686 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006687 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006688 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006689
Przemek Stekiel69c46792022-06-10 12:59:51 +02006690#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6692 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6693 step, data, data_length);
6694 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006695#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006696#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006697 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6698 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6699 step, data, data_length);
6700 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006701#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6702#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006703 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6704 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6705 step, data, data_length);
6706 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006707#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006708#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006709 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006710 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006711 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6712 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006713#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006714 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006715 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006716 (void) data;
6717 (void) data_length;
6718 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006719 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006720 }
6721
Gilles Peskine224b0d62019-09-23 18:13:17 +02006722exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006723 if (status != PSA_SUCCESS) {
6724 psa_key_derivation_abort(operation);
6725 }
6726 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006727}
6728
Janos Follath51f4a0f2019-06-14 11:35:55 +01006729psa_status_t psa_key_derivation_input_bytes(
6730 psa_key_derivation_operation_t *operation,
6731 psa_key_derivation_step_t step,
6732 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006733 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006734{
Gilles Peskine449bd832023-01-11 14:50:10 +01006735 return psa_key_derivation_input_internal(operation, step,
6736 PSA_KEY_TYPE_NONE,
6737 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006738}
6739
Janos Follath51f4a0f2019-06-14 11:35:55 +01006740psa_status_t psa_key_derivation_input_key(
6741 psa_key_derivation_operation_t *operation,
6742 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006743 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006744{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006745 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006746 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006747 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006748
Ronald Cron5c522922020-11-14 16:35:34 +01006749 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006750 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6751 if (status != PSA_SUCCESS) {
6752 psa_key_derivation_abort(operation);
6753 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006754 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006755
6756 /* Passing a key object as a SECRET input unlocks the permission
6757 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006758 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006759 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006760 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006761
Gilles Peskine449bd832023-01-11 14:50:10 +01006762 status = psa_key_derivation_input_internal(operation,
6763 step, slot->attr.type,
6764 slot->key.data,
6765 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006766
Gilles Peskine449bd832023-01-11 14:50:10 +01006767 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006768
Gilles Peskine449bd832023-01-11 14:50:10 +01006769 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006770}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006771
6772
6773
6774/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006775/* Key agreement */
6776/****************************************************************/
6777
Gilles Peskine449bd832023-01-11 14:50:10 +01006778psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6779 const uint8_t *key_buffer,
6780 size_t key_buffer_size,
6781 psa_algorithm_t alg,
6782 const uint8_t *peer_key,
6783 size_t peer_key_length,
6784 uint8_t *shared_secret,
6785 size_t shared_secret_size,
6786 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006787{
Gilles Peskine449bd832023-01-11 14:50:10 +01006788 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006789#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006790 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006791 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6792 key_buffer_size, alg,
6793 peer_key, peer_key_length,
6794 shared_secret,
6795 shared_secret_size,
6796 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006797#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006798
6799#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6800 case PSA_ALG_FFDH:
6801 if (!PSA_KEY_TYPE_IS_DH_KEY_PAIR(psa_get_key_type(attributes))) {
6802 return PSA_ERROR_INVALID_ARGUMENT;
6803 }
6804
6805 return psa_key_agreement_ffdh(peer_key,
6806 peer_key_length,
6807 key_buffer,
6808 key_buffer_size,
6809 shared_secret,
6810 shared_secret_size,
6811 shared_secret_length);
6812#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6813
Gilles Peskine01d718c2018-09-18 12:01:02 +02006814 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006815 (void) attributes;
6816 (void) key_buffer;
6817 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006818 (void) peer_key;
6819 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006820 (void) shared_secret;
6821 (void) shared_secret_size;
6822 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006823 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006824 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006825}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006826
Aditya Deshpande17845b82022-10-13 17:21:01 +01006827/** Internal function for raw key agreement
6828 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006829 * to the driver's implementation if a driver is present.
6830 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006831 * (psa_key_agreement_raw_builtin).
6832 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006833static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6834 psa_key_slot_t *private_key,
6835 const uint8_t *peer_key,
6836 size_t peer_key_length,
6837 uint8_t *shared_secret,
6838 size_t shared_secret_size,
6839 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006840{
Gilles Peskine449bd832023-01-11 14:50:10 +01006841 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6842 return PSA_ERROR_NOT_SUPPORTED;
6843 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006844
6845 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006846 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006847 };
6848
Gilles Peskine449bd832023-01-11 14:50:10 +01006849 return psa_driver_wrapper_key_agreement(&attributes,
6850 private_key->key.data,
6851 private_key->key.bytes, alg,
6852 peer_key, peer_key_length,
6853 shared_secret,
6854 shared_secret_size,
6855 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006856}
6857
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006858/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006859 * to potentially free embedded data structures and wipe confidential data.
6860 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006861static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6862 psa_key_derivation_step_t step,
6863 psa_key_slot_t *private_key,
6864 const uint8_t *peer_key,
6865 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006866{
6867 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006868 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006869 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006870 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006871
6872 /* Step 1: run the secret agreement algorithm to generate the shared
6873 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006874 status = psa_key_agreement_raw_internal(ka_alg,
6875 private_key,
6876 peer_key, peer_key_length,
6877 shared_secret,
6878 sizeof(shared_secret),
6879 &shared_secret_length);
6880 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006881 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006882 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006883
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006884 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006885 * the shared secret. A shared secret is permitted wherever a key
6886 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006887 status = psa_key_derivation_input_internal(operation, step,
6888 PSA_KEY_TYPE_DERIVE,
6889 shared_secret,
6890 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006891exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006892 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6893 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006894}
6895
Gilles Peskine449bd832023-01-11 14:50:10 +01006896psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6897 psa_key_derivation_step_t step,
6898 mbedtls_svc_key_id_t private_key,
6899 const uint8_t *peer_key,
6900 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006901{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006902 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006903 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006904 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006905
Gilles Peskine449bd832023-01-11 14:50:10 +01006906 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6907 return PSA_ERROR_INVALID_ARGUMENT;
6908 }
Ronald Cron5c522922020-11-14 16:35:34 +01006909 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006910 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6911 if (status != PSA_SUCCESS) {
6912 return status;
6913 }
6914 status = psa_key_agreement_internal(operation, step,
6915 slot,
6916 peer_key, peer_key_length);
6917 if (status != PSA_SUCCESS) {
6918 psa_key_derivation_abort(operation);
6919 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006920 /* If a private key has been added as SECRET, we allow the derived
6921 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006922 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006923 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006924 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006925 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006926
Gilles Peskine449bd832023-01-11 14:50:10 +01006927 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006928
Gilles Peskine449bd832023-01-11 14:50:10 +01006929 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006930}
6931
Gilles Peskine449bd832023-01-11 14:50:10 +01006932psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6933 mbedtls_svc_key_id_t private_key,
6934 const uint8_t *peer_key,
6935 size_t peer_key_length,
6936 uint8_t *output,
6937 size_t output_size,
6938 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006939{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006940 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006941 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006942 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006943
Gilles Peskine449bd832023-01-11 14:50:10 +01006944 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006945 status = PSA_ERROR_INVALID_ARGUMENT;
6946 goto exit;
6947 }
Ronald Cron5c522922020-11-14 16:35:34 +01006948 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006949 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6950 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006951 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006953
Gilles Peskine3e561302022-04-14 00:17:15 +02006954 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6955 * for the output size. The PSA specification only guarantees that this
6956 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6957 * but it might be nice to allow smaller buffers if the output fits.
6958 * At the time of writing this comment, with only ECDH implemented,
6959 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6960 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6961 * be exact for it as well. */
6962 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006963 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6964 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006965 status = PSA_ERROR_BUFFER_TOO_SMALL;
6966 goto exit;
6967 }
6968
Gilles Peskine449bd832023-01-11 14:50:10 +01006969 status = psa_key_agreement_raw_internal(alg, slot,
6970 peer_key, peer_key_length,
6971 output, output_size,
6972 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006973
6974exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006975 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006976 /* If an error happens and is not handled properly, the output
6977 * may be used as a key to protect sensitive data. Arrange for such
6978 * a key to be random, which is likely to result in decryption or
6979 * verification errors. This is better than filling the buffer with
6980 * some constant data such as zeros, which would result in the data
6981 * being protected with a reproducible, easily knowable key.
6982 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006983 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006984 *output_length = output_size;
6985 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006986
Gilles Peskine449bd832023-01-11 14:50:10 +01006987 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006988
Gilles Peskine449bd832023-01-11 14:50:10 +01006989 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006990}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006991
6992
Gilles Peskine30524eb2020-11-13 17:02:26 +01006993
Gilles Peskine86a440b2018-11-12 18:39:40 +01006994/****************************************************************/
6995/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006996/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006997
Gilles Peskine30524eb2020-11-13 17:02:26 +01006998/** Initialize the PSA random generator.
6999 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007000static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007001{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007002#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007004#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7005
Gilles Peskine30524eb2020-11-13 17:02:26 +01007006 /* Set default configuration if
7007 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007008 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007009 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007010 }
7011 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007012 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007013 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007014
Gilles Peskine449bd832023-01-11 14:50:10 +01007015 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007016#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7017 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7018 /* The PSA entropy injection feature depends on using NV seed as an entropy
7019 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007020 mbedtls_entropy_add_source(&rng->entropy,
7021 mbedtls_nv_seed_poll, NULL,
7022 MBEDTLS_ENTROPY_BLOCK_SIZE,
7023 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007024#endif
7025
Gilles Peskine449bd832023-01-11 14:50:10 +01007026 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007027#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007028}
7029
7030/** Deinitialize the PSA random generator.
7031 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007032static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007033{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007034#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007035 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007036#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007037 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7038 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007039#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007040}
7041
7042/** Seed the PSA random generator.
7043 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007044static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007045{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007046#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7047 /* Do nothing: the external RNG seeds itself. */
7048 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007049 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007050#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007051 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007052 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7053 drbg_seed, sizeof(drbg_seed) - 1);
7054 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007055#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007056}
7057
Gilles Peskine449bd832023-01-11 14:50:10 +01007058psa_status_t psa_generate_random(uint8_t *output,
7059 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007060{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007061 GUARD_MODULE_INITIALIZED;
7062
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007063#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7064
7065 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007066 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7067 output, output_size,
7068 &output_length);
7069 if (status != PSA_SUCCESS) {
7070 return status;
7071 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007072 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007073 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007074 if (output_length != output_size) {
7075 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7076 }
7077 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007078
7079#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7080
Gilles Peskine449bd832023-01-11 14:50:10 +01007081 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007082 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007083 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7084 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7085 output_size);
7086 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7087 output, request_size);
7088 if (ret != 0) {
7089 return mbedtls_to_psa_error(ret);
7090 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007091 output_size -= request_size;
7092 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007093 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007094 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007095#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007096}
7097
Gilles Peskine8814fc42020-12-14 15:33:44 +01007098/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007099 *
7100 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7101 * `psa_generate_random(...)`. The state parameter is ignored since the
7102 * PSA API doesn't support passing an explicit state.
7103 *
7104 * In the non-external case, psa_generate_random() calls an
7105 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7106 * and semantics as mbedtls_psa_get_random(). As an optimization,
7107 * instead of doing this back-and-forth between the PSA API and the
7108 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7109 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007110 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007111#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7112int mbedtls_psa_get_random(void *p_rng,
7113 unsigned char *output,
7114 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007115{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007116 /* This function takes a pointer to the RNG state because that's what
7117 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7118 * its own state internally and doesn't let the caller access that state.
7119 * So we just ignore the state parameter, and in practice we'll pass
7120 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007121 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007122 psa_status_t status = psa_generate_random(output, output_size);
7123 if (status == PSA_SUCCESS) {
7124 return 0;
7125 } else {
7126 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7127 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007128}
7129#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7130
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007131#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00007132#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007133
Gilles Peskine449bd832023-01-11 14:50:10 +01007134psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7135 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007136{
Gilles Peskine449bd832023-01-11 14:50:10 +01007137 if (global_data.initialized) {
7138 return PSA_ERROR_NOT_PERMITTED;
7139 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007140
Gilles Peskine449bd832023-01-11 14:50:10 +01007141 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7142 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7143 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7144 return PSA_ERROR_INVALID_ARGUMENT;
7145 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007146
Gilles Peskine449bd832023-01-11 14:50:10 +01007147 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007148}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007149#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007150
Ronald Cron3772afe2021-02-08 16:10:05 +01007151/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007152 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007153 * \param type The key type
7154 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007155 *
7156 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007157 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007158 * \retval #PSA_ERROR_INVALID_ARGUMENT
7159 * The size in bits of the key is not valid.
7160 * \retval #PSA_ERROR_NOT_SUPPORTED
7161 * The type and/or the size in bits of the key or the combination of
7162 * the two is not supported.
7163 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007164static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007165 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007166{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007167 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007168
Gilles Peskine449bd832023-01-11 14:50:10 +01007169 if (key_type_is_raw_bytes(type)) {
7170 status = psa_validate_unstructured_key_bit_size(type, bits);
7171 if (status != PSA_SUCCESS) {
7172 return status;
7173 }
7174 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007175#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007176 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7177 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7178 return PSA_ERROR_NOT_SUPPORTED;
7179 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007180
Ronald Cron01b2aba2020-10-05 09:42:02 +02007181 /* Accept only byte-aligned keys, for the same reasons as
7182 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007183 if (bits % 8 != 0) {
7184 return PSA_ERROR_NOT_SUPPORTED;
7185 }
7186 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007187#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007188
Ronald Cron5c4d3862020-12-07 11:07:24 +01007189#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007190 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007191 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007192 return PSA_SUCCESS;
7193 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007194#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007195
7196#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR)
7197 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7198 if (bits != 2048 && bits != 3072 && bits != 4096 &&
7199 bits != 6144 && bits != 8192) {
7200 return PSA_ERROR_NOT_SUPPORTED;
7201 }
7202 } else
7203#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007204 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007205 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007206 }
7207
Gilles Peskine449bd832023-01-11 14:50:10 +01007208 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007209}
7210
Ronald Cron55ed0592020-10-05 10:30:40 +02007211psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007212 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007213 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007214{
7215 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007216 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007217
Gilles Peskine449bd832023-01-11 14:50:10 +01007218 if ((attributes->domain_parameters == NULL) &&
7219 (attributes->domain_parameters_size != 0)) {
7220 return PSA_ERROR_INVALID_ARGUMENT;
7221 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007222
Gilles Peskine449bd832023-01-11 14:50:10 +01007223 if (key_type_is_raw_bytes(type)) {
7224 status = psa_generate_random(key_buffer, key_buffer_size);
7225 if (status != PSA_SUCCESS) {
7226 return status;
7227 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007228
David Brown12ca5032021-02-11 11:02:00 -07007229#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007230 if (type == PSA_KEY_TYPE_DES) {
7231 psa_des_set_key_parity(key_buffer, key_buffer_size);
7232 }
David Brown8107e312021-02-12 08:08:46 -07007233#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007234 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007235
Jaeden Amero424fa932021-05-14 08:34:32 +01007236#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
7237 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01007238 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7239 return mbedtls_psa_rsa_generate_key(attributes,
7240 key_buffer,
7241 key_buffer_size,
7242 key_buffer_length);
7243 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01007244#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
7245 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007246
John Durkop9814fa22020-11-04 12:28:15 -08007247#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007248 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7249 return mbedtls_psa_ecp_generate_key(attributes,
7250 key_buffer,
7251 key_buffer_size,
7252 key_buffer_length);
7253 } else
John Durkop9814fa22020-11-04 12:28:15 -08007254#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007255
7256#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR)
7257 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7258 return mbedtls_psa_ffdh_generate_key(attributes,
7259 key_buffer,
7260 key_buffer_size,
7261 key_buffer_length);
7262 } else
7263#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02007264 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007265 (void) key_buffer_length;
7266 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007267 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007268
Gilles Peskine449bd832023-01-11 14:50:10 +01007269 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007270}
Darryl Green0c6575a2018-11-07 16:05:30 +00007271
Gilles Peskine449bd832023-01-11 14:50:10 +01007272psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7273 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007274{
7275 psa_status_t status;
7276 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007277 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007278 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007279
Ronald Cron81709fc2020-11-14 12:10:32 +01007280 *key = MBEDTLS_SVC_KEY_ID_INIT;
7281
Gilles Peskine0f84d622019-09-12 19:03:13 +02007282 /* Reject any attempt to create a zero-length key so that we don't
7283 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007284 if (psa_get_key_bits(attributes) == 0) {
7285 return PSA_ERROR_INVALID_ARGUMENT;
7286 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007287
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007288 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007289 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7290 return PSA_ERROR_INVALID_ARGUMENT;
7291 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007292
Gilles Peskine449bd832023-01-11 14:50:10 +01007293 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7294 &slot, &driver);
7295 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007296 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007297 }
Gilles Peskine11792082019-08-06 18:36:36 +02007298
Ronald Cron977c2472020-10-13 08:32:21 +02007299 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307300 * storage ( thus not in the case of generating a key in a secure element
7301 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7302 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007303 if (slot->key.data == NULL) {
7304 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7305 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007306 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007307 attributes->core.type, attributes->core.bits);
7308 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007309 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007310 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007311
7312 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007313 attributes->core.type,
7314 attributes->core.bits);
7315 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007316 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007317 attributes, &key_buffer_size);
7318 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007319 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007320 }
Ronald Cron977c2472020-10-13 08:32:21 +02007321 }
Ronald Cron977c2472020-10-13 08:32:21 +02007322
Gilles Peskine449bd832023-01-11 14:50:10 +01007323 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7324 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007325 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007326 }
Ronald Cron977c2472020-10-13 08:32:21 +02007327 }
7328
Gilles Peskine449bd832023-01-11 14:50:10 +01007329 status = psa_driver_wrapper_generate_key(attributes,
7330 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007331
Gilles Peskine449bd832023-01-11 14:50:10 +01007332 if (status != PSA_SUCCESS) {
7333 psa_remove_key_data_from_memory(slot);
7334 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007335
Gilles Peskine11792082019-08-06 18:36:36 +02007336exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007337 if (status == PSA_SUCCESS) {
7338 status = psa_finish_key_creation(slot, driver, key);
7339 }
7340 if (status != PSA_SUCCESS) {
7341 psa_fail_key_creation(slot, driver);
7342 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007343
Gilles Peskine449bd832023-01-11 14:50:10 +01007344 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007345}
7346
Gilles Peskinee59236f2018-01-27 23:32:46 +01007347/****************************************************************/
7348/* Module setup */
7349/****************************************************************/
7350
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007351#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007352psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007353 void (* entropy_init)(mbedtls_entropy_context *ctx),
7354 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007355{
Gilles Peskine449bd832023-01-11 14:50:10 +01007356 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7357 return PSA_ERROR_BAD_STATE;
7358 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007359 global_data.rng.entropy_init = entropy_init;
7360 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007361 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007362}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007363#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007364
Gilles Peskine449bd832023-01-11 14:50:10 +01007365void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007366{
Gilles Peskine449bd832023-01-11 14:50:10 +01007367 psa_wipe_all_key_slots();
7368 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7369 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007370 }
7371 /* Wipe all remaining data, including configuration.
7372 * In particular, this sets all state indicator to the value
7373 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007374 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007375
7376 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007377 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007378}
7379
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007380#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7381/** Recover a transaction that was interrupted by a power failure.
7382 *
7383 * This function is called during initialization, before psa_crypto_init()
7384 * returns. If this function returns a failure status, the initialization
7385 * fails.
7386 */
7387static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007388 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007389{
Gilles Peskine449bd832023-01-11 14:50:10 +01007390 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007391 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7392 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007393 /* TODO - fall through to the failure case until this
7394 * is implemented.
7395 * https://github.com/ARMmbed/mbed-crypto/issues/218
7396 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007397 default:
7398 /* We found an unsupported transaction in the storage.
7399 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007400 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007401 }
7402}
7403#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7404
Gilles Peskine449bd832023-01-11 14:50:10 +01007405psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007406{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007407 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007408
Gilles Peskinec6b69072018-11-20 21:42:52 +01007409 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007410 if (global_data.initialized != 0) {
7411 return PSA_SUCCESS;
7412 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007413
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007414 /* Init drivers */
7415 status = psa_driver_wrapper_init();
7416 if (status != PSA_SUCCESS) {
7417 goto exit;
7418 }
7419 global_data.drivers_initialized = 1;
7420
Gilles Peskine30524eb2020-11-13 17:02:26 +01007421 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007422 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007423 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007424 status = mbedtls_psa_random_seed(&global_data.rng);
7425 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007426 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007427 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007428 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007429
Gilles Peskine449bd832023-01-11 14:50:10 +01007430 status = psa_initialize_key_slots();
7431 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007432 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007433 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007434
Gilles Peskine4b734222019-07-24 15:56:31 +02007435#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007436 status = psa_crypto_load_transaction();
7437 if (status == PSA_SUCCESS) {
7438 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7439 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007440 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007441 }
7442 status = psa_crypto_stop_transaction();
7443 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007444 /* There's no transaction to complete. It's all good. */
7445 status = PSA_SUCCESS;
7446 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007447#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007448
Gilles Peskinec6b69072018-11-20 21:42:52 +01007449 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007450 global_data.initialized = 1;
7451
7452exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007453 if (status != PSA_SUCCESS) {
7454 mbedtls_psa_crypto_free();
7455 }
7456 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007457}
7458
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007459psa_status_t psa_crypto_driver_pake_get_password_len(
7460 const psa_crypto_driver_pake_inputs_t *inputs,
7461 size_t *password_len)
7462{
7463 if (inputs->password_len == 0) {
7464 return PSA_ERROR_BAD_STATE;
7465 }
7466
7467 *password_len = inputs->password_len;
7468
7469 return PSA_SUCCESS;
7470}
7471
7472psa_status_t psa_crypto_driver_pake_get_password(
7473 const psa_crypto_driver_pake_inputs_t *inputs,
7474 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7475{
7476 if (inputs->password_len == 0) {
7477 return PSA_ERROR_BAD_STATE;
7478 }
7479
7480 if (buffer_size < inputs->password_len) {
7481 return PSA_ERROR_BUFFER_TOO_SMALL;
7482 }
7483
7484 memcpy(buffer, inputs->password, inputs->password_len);
7485 *buffer_length = inputs->password_len;
7486
7487 return PSA_SUCCESS;
7488}
7489
7490psa_status_t psa_crypto_driver_pake_get_role(
7491 const psa_crypto_driver_pake_inputs_t *inputs,
7492 psa_pake_role_t *role)
7493{
7494 if (inputs->role == PSA_PAKE_ROLE_NONE) {
7495 return PSA_ERROR_BAD_STATE;
7496 }
7497
7498 *role = inputs->role;
7499
7500 return PSA_SUCCESS;
7501}
7502
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007503psa_status_t psa_crypto_driver_pake_get_user_len(
7504 const psa_crypto_driver_pake_inputs_t *inputs,
7505 size_t *user_len)
7506{
7507 if (inputs->user_len == 0) {
7508 return PSA_ERROR_BAD_STATE;
7509 }
7510
7511 *user_len = inputs->user_len;
7512
7513 return PSA_SUCCESS;
7514}
7515
7516psa_status_t psa_crypto_driver_pake_get_user(
7517 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007518 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007519{
7520 if (inputs->user_len == 0) {
7521 return PSA_ERROR_BAD_STATE;
7522 }
7523
Przemek Stekielc0e62502023-03-14 11:49:36 +01007524 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007525 return PSA_ERROR_BUFFER_TOO_SMALL;
7526 }
7527
Przemek Stekielc0e62502023-03-14 11:49:36 +01007528 memcpy(user_id, inputs->user, inputs->user_len);
7529 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007530
7531 return PSA_SUCCESS;
7532}
7533
7534psa_status_t psa_crypto_driver_pake_get_peer_len(
7535 const psa_crypto_driver_pake_inputs_t *inputs,
7536 size_t *peer_len)
7537{
7538 if (inputs->peer_len == 0) {
7539 return PSA_ERROR_BAD_STATE;
7540 }
7541
7542 *peer_len = inputs->peer_len;
7543
7544 return PSA_SUCCESS;
7545}
7546
7547psa_status_t psa_crypto_driver_pake_get_peer(
7548 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007549 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007550{
7551 if (inputs->peer_len == 0) {
7552 return PSA_ERROR_BAD_STATE;
7553 }
7554
Przemek Stekielc0e62502023-03-14 11:49:36 +01007555 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007556 return PSA_ERROR_BUFFER_TOO_SMALL;
7557 }
7558
Przemek Stekielc0e62502023-03-14 11:49:36 +01007559 memcpy(peer_id, inputs->peer, inputs->peer_len);
7560 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007561
7562 return PSA_SUCCESS;
7563}
7564
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007565psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7566 const psa_crypto_driver_pake_inputs_t *inputs,
7567 psa_pake_cipher_suite_t *cipher_suite)
7568{
7569 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7570 return PSA_ERROR_BAD_STATE;
7571 }
7572
7573 *cipher_suite = inputs->cipher_suite;
7574
7575 return PSA_SUCCESS;
7576}
7577
Neil Armstronga7d08c32022-06-01 18:21:20 +02007578psa_status_t psa_pake_setup(
7579 psa_pake_operation_t *operation,
7580 const psa_pake_cipher_suite_t *cipher_suite)
7581{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007582 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007583
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007584 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007585 status = PSA_ERROR_BAD_STATE;
7586 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007587 }
7588
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007589 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007590 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007591 status = PSA_ERROR_INVALID_ARGUMENT;
7592 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007593 }
7594
Przemek Stekiel51eac532022-12-07 11:04:51 +01007595 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7596
Przemek Stekiele12ed362022-12-21 12:54:46 +01007597 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007598 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7599 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007600 operation->data.inputs.cipher_suite = *cipher_suite;
7601
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007602#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007603 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007604 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007605 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007606
Przemek Stekiele12ed362022-12-21 12:54:46 +01007607 computation_stage->state = PSA_PAKE_STATE_SETUP;
7608 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7609 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7610 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007611 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007612#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007613 {
7614 status = PSA_ERROR_NOT_SUPPORTED;
7615 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007616 }
7617
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007618 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7619
Przemek Stekiel51eac532022-12-07 11:04:51 +01007620 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007621exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007622 psa_pake_abort(operation);
7623 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007624}
7625
7626psa_status_t psa_pake_set_password_key(
7627 psa_pake_operation_t *operation,
7628 mbedtls_svc_key_id_t password)
7629{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007630 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007631 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7632 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007633
Przemek Stekiel51eac532022-12-07 11:04:51 +01007634 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007635 status = PSA_ERROR_BAD_STATE;
7636 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007637 }
7638
Przemek Stekiel6c764412022-11-22 14:05:12 +01007639 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7640 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007641 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007642 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007643 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007644 }
7645
Przemek Stekiel6c764412022-11-22 14:05:12 +01007646 psa_key_attributes_t attributes = {
7647 .core = slot->attr
7648 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007649
Przemek Stekiel51eac532022-12-07 11:04:51 +01007650 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007651
Przemek Stekiel51eac532022-12-07 11:04:51 +01007652 if (type != PSA_KEY_TYPE_PASSWORD &&
7653 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7654 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007655 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007656 }
7657
Przemek Stekiel51eac532022-12-07 11:04:51 +01007658 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7659 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007660 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007661 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007662 }
7663
7664 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7665 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007666 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007667exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007668 if (status != PSA_SUCCESS) {
7669 psa_pake_abort(operation);
7670 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007671 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007672 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007673}
7674
7675psa_status_t psa_pake_set_user(
7676 psa_pake_operation_t *operation,
7677 const uint8_t *user_id,
7678 size_t user_id_len)
7679{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007680 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007681
7682 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007683 status = PSA_ERROR_BAD_STATE;
7684 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007685 }
7686
Przemek Stekiel51eac532022-12-07 11:04:51 +01007687 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007688 status = PSA_ERROR_INVALID_ARGUMENT;
7689 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007690 }
7691
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007692 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007693 status = PSA_ERROR_BAD_STATE;
7694 goto exit;
7695 }
7696
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007697 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007698 if ((user_id_len != sizeof(jpake_server_id) ||
7699 memcmp(user_id, jpake_server_id, user_id_len) != 0) &&
7700 (user_id_len != sizeof(jpake_client_id) ||
7701 memcmp(user_id, jpake_client_id, user_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007702 status = PSA_ERROR_NOT_SUPPORTED;
7703 goto exit;
7704 }
7705
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007706 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7707 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007708 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7709 goto exit;
7710 }
7711
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007712 memcpy(operation->data.inputs.user, user_id, user_id_len);
7713 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007714
7715 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007716exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007717 psa_pake_abort(operation);
7718 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007719}
7720
7721psa_status_t psa_pake_set_peer(
7722 psa_pake_operation_t *operation,
7723 const uint8_t *peer_id,
7724 size_t peer_id_len)
7725{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007726 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007727
7728 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007729 status = PSA_ERROR_BAD_STATE;
7730 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007731 }
7732
Przemek Stekiel51eac532022-12-07 11:04:51 +01007733 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007734 status = PSA_ERROR_INVALID_ARGUMENT;
7735 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007736 }
7737
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007738 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007739 status = PSA_ERROR_BAD_STATE;
7740 goto exit;
7741 }
7742
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007743 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007744 if ((peer_id_len != sizeof(jpake_server_id) ||
7745 memcmp(peer_id, jpake_server_id, peer_id_len) != 0) &&
7746 (peer_id_len != sizeof(jpake_client_id) ||
7747 memcmp(peer_id, jpake_client_id, peer_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007748 status = PSA_ERROR_NOT_SUPPORTED;
7749 goto exit;
7750 }
7751
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007752 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7753 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007754 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7755 goto exit;
7756 }
7757
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007758 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7759 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007760
7761 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007762exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007763 psa_pake_abort(operation);
7764 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007765}
7766
7767psa_status_t psa_pake_set_role(
7768 psa_pake_operation_t *operation,
7769 psa_pake_role_t role)
7770{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007771 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007772
Przemek Stekiel51eac532022-12-07 11:04:51 +01007773 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007774 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007775 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007776 }
7777
Przemek Stekiel09104b82023-03-06 14:11:51 +01007778 switch (operation->alg) {
7779#if defined(PSA_WANT_ALG_JPAKE)
7780 case PSA_ALG_JPAKE:
7781 if (role == PSA_PAKE_ROLE_NONE) {
7782 return PSA_SUCCESS;
7783 }
7784 status = PSA_ERROR_INVALID_ARGUMENT;
7785 break;
7786#endif
7787 default:
7788 (void) role;
7789 status = PSA_ERROR_NOT_SUPPORTED;
7790 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007791 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007792exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007793 psa_pake_abort(operation);
7794 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007795}
7796
Przemek Stekielb09c4872023-01-17 12:05:38 +01007797/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007798#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007799static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007800 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007801{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007802 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007803 case PSA_PAKE_OUTPUT_X1_X2:
7804 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007805 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007806 case PSA_PAKE_X1_STEP_KEY_SHARE:
7807 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007808 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7809 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007810 case PSA_PAKE_X1_STEP_ZK_PROOF:
7811 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007812 case PSA_PAKE_X2_STEP_KEY_SHARE:
7813 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007814 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7815 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007816 case PSA_PAKE_X2_STEP_ZK_PROOF:
7817 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007818 default:
7819 return PSA_JPAKE_STEP_INVALID;
7820 }
7821 break;
7822 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007823 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007824 case PSA_PAKE_X1_STEP_KEY_SHARE:
7825 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007826 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7827 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007828 case PSA_PAKE_X1_STEP_ZK_PROOF:
7829 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007830 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007831 return PSA_JPAKE_STEP_INVALID;
7832 }
7833 break;
7834 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007835 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007836 case PSA_PAKE_X1_STEP_KEY_SHARE:
7837 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007838 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7839 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007840 case PSA_PAKE_X1_STEP_ZK_PROOF:
7841 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007842 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007843 return PSA_JPAKE_STEP_INVALID;
7844 }
7845 break;
7846 default:
7847 return PSA_JPAKE_STEP_INVALID;
7848 }
7849 return PSA_JPAKE_STEP_INVALID;
7850}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007851#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007852
Przemek Stekiel2797d372022-12-22 11:19:22 +01007853static psa_status_t psa_pake_complete_inputs(
7854 psa_pake_operation_t *operation)
7855{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007856 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007857 /* Create copy of the inputs on stack as inputs share memory
7858 with the driver context which will be setup by the driver. */
7859 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007860
Przemek Stekielfde11282023-03-13 16:06:09 +01007861 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007862 return PSA_ERROR_BAD_STATE;
7863 }
7864
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007865 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007866 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7867 return PSA_ERROR_BAD_STATE;
7868 }
7869 if (memcmp(inputs.user, jpake_client_id, inputs.user_len) == 0 &&
7870 memcmp(inputs.peer, jpake_server_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007871 inputs.role = PSA_PAKE_ROLE_CLIENT;
7872 } else
Przemek Stekielfde11282023-03-13 16:06:09 +01007873 if (memcmp(inputs.user, jpake_server_id, inputs.user_len) == 0 &&
7874 memcmp(inputs.peer, jpake_client_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007875 inputs.role = PSA_PAKE_ROLE_SERVER;
7876 }
7877
7878 if (inputs.role != PSA_PAKE_ROLE_CLIENT &&
7879 inputs.role != PSA_PAKE_ROLE_SERVER) {
7880 return PSA_ERROR_NOT_SUPPORTED;
7881 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007882 }
7883
Przemek Stekiel18620a32023-01-17 16:34:52 +01007884 /* Clear driver context */
7885 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7886
7887 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007888
7889 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007890 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7891 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007892
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007893 /* User and peer are translated to role. */
7894 mbedtls_free(inputs.user);
7895 mbedtls_free(inputs.peer);
7896
Przemek Stekiel2797d372022-12-22 11:19:22 +01007897 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007898#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007899 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007900 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007901 psa_jpake_computation_stage_t *computation_stage =
7902 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007903 computation_stage->state = PSA_PAKE_STATE_READY;
7904 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7905 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7906 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007907 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007908#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007909 {
7910 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007911 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007912 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007913 return status;
7914}
7915
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007916#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007917static psa_status_t psa_jpake_output_prologue(
7918 psa_pake_operation_t *operation,
7919 psa_pake_step_t step)
7920{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007921 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7922 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7923 step != PSA_PAKE_STEP_ZK_PROOF) {
7924 return PSA_ERROR_INVALID_ARGUMENT;
7925 }
7926
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007927 psa_jpake_computation_stage_t *computation_stage =
7928 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007929
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007930 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7931 return PSA_ERROR_BAD_STATE;
7932 }
7933
7934 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7935 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
7936 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
7937 return PSA_ERROR_BAD_STATE;
7938 }
7939
7940 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7941 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007942 return PSA_ERROR_BAD_STATE;
7943 }
7944
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007945 switch (computation_stage->output_step) {
7946 case PSA_PAKE_STEP_X1_X2:
7947 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007948 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007949 case PSA_PAKE_STEP_X2S:
7950 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007951 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007952 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007953 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007954 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007955
7956 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7957 }
7958
7959 /* Check if step matches current sequence */
7960 switch (computation_stage->sequence) {
7961 case PSA_PAKE_X1_STEP_KEY_SHARE:
7962 case PSA_PAKE_X2_STEP_KEY_SHARE:
7963 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7964 return PSA_ERROR_BAD_STATE;
7965 }
7966 break;
7967
7968 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7969 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7970 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7971 return PSA_ERROR_BAD_STATE;
7972 }
7973 break;
7974
7975 case PSA_PAKE_X1_STEP_ZK_PROOF:
7976 case PSA_PAKE_X2_STEP_ZK_PROOF:
7977 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7978 return PSA_ERROR_BAD_STATE;
7979 }
7980 break;
7981
7982 default:
7983 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007984 }
7985
7986 return PSA_SUCCESS;
7987}
7988
7989static psa_status_t psa_jpake_output_epilogue(
7990 psa_pake_operation_t *operation)
7991{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007992 psa_jpake_computation_stage_t *computation_stage =
7993 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007994
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007995 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007996 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007997 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007998 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007999 computation_stage->state = PSA_PAKE_STATE_READY;
8000 computation_stage->output_step++;
8001 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
8002 } else {
8003 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008004 }
8005
8006 return PSA_SUCCESS;
8007}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008008#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008009
Neil Armstronga7d08c32022-06-01 18:21:20 +02008010psa_status_t psa_pake_output(
8011 psa_pake_operation_t *operation,
8012 psa_pake_step_t step,
8013 uint8_t *output,
8014 size_t output_size,
8015 size_t *output_length)
8016{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008017 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008018 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008019 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008020
8021 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008022 status = psa_pake_complete_inputs(operation);
8023 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008024 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008025 }
8026 }
8027
8028 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008029 status = PSA_ERROR_BAD_STATE;
8030 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008031 }
8032
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008033 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008034 status = PSA_ERROR_INVALID_ARGUMENT;
8035 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008036 }
8037
Przemek Stekiele12ed362022-12-21 12:54:46 +01008038 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008039#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008040 case PSA_ALG_JPAKE:
8041 status = psa_jpake_output_prologue(operation, step);
8042 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008043 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008044 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008045 driver_step = convert_jpake_computation_stage_to_driver_step(
8046 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008047 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008048#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008049 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008050 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008051 status = PSA_ERROR_NOT_SUPPORTED;
8052 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008053 }
8054
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008055 status = psa_driver_wrapper_pake_output(operation, driver_step,
8056 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008057
8058 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008059 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008060 }
8061
8062 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008063#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008064 case PSA_ALG_JPAKE:
8065 status = psa_jpake_output_epilogue(operation);
8066 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008067 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008068 }
8069 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008070#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008071 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008072 status = PSA_ERROR_NOT_SUPPORTED;
8073 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008074 }
8075
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008076 return PSA_SUCCESS;
8077exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008078 psa_pake_abort(operation);
8079 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008080}
8081
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008082#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008083static psa_status_t psa_jpake_input_prologue(
8084 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008085 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008086{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008087 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8088 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8089 step != PSA_PAKE_STEP_ZK_PROOF) {
8090 return PSA_ERROR_INVALID_ARGUMENT;
8091 }
8092
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008093 psa_jpake_computation_stage_t *computation_stage =
8094 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008095
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008096 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
8097 return PSA_ERROR_BAD_STATE;
8098 }
8099
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008100 if (computation_stage->state != PSA_PAKE_STATE_READY &&
8101 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
8102 computation_stage->state != PSA_PAKE_INPUT_X4S) {
8103 return PSA_ERROR_BAD_STATE;
8104 }
8105
8106 if (computation_stage->state == PSA_PAKE_STATE_READY) {
8107 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01008108 return PSA_ERROR_BAD_STATE;
8109 }
8110
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008111 switch (computation_stage->input_step) {
8112 case PSA_PAKE_STEP_X1_X2:
8113 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008114 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008115 case PSA_PAKE_STEP_X2S:
8116 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008117 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008118 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01008119 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008120 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008121
8122 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
8123 }
8124
8125 /* Check if step matches current sequence */
8126 switch (computation_stage->sequence) {
8127 case PSA_PAKE_X1_STEP_KEY_SHARE:
8128 case PSA_PAKE_X2_STEP_KEY_SHARE:
8129 if (step != PSA_PAKE_STEP_KEY_SHARE) {
8130 return PSA_ERROR_BAD_STATE;
8131 }
8132 break;
8133
8134 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
8135 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
8136 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
8137 return PSA_ERROR_BAD_STATE;
8138 }
8139 break;
8140
8141 case PSA_PAKE_X1_STEP_ZK_PROOF:
8142 case PSA_PAKE_X2_STEP_ZK_PROOF:
8143 if (step != PSA_PAKE_STEP_ZK_PROOF) {
8144 return PSA_ERROR_BAD_STATE;
8145 }
8146 break;
8147
8148 default:
8149 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008150 }
8151
8152 return PSA_SUCCESS;
8153}
8154
Przemek Stekiele12ed362022-12-21 12:54:46 +01008155static psa_status_t psa_jpake_input_epilogue(
8156 psa_pake_operation_t *operation)
8157{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008158 psa_jpake_computation_stage_t *computation_stage =
8159 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008160
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008161 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01008162 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008163 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01008164 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008165 computation_stage->state = PSA_PAKE_STATE_READY;
8166 computation_stage->input_step++;
8167 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
8168 } else {
8169 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008170 }
8171
8172 return PSA_SUCCESS;
8173}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008174#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008175
Neil Armstronga7d08c32022-06-01 18:21:20 +02008176psa_status_t psa_pake_input(
8177 psa_pake_operation_t *operation,
8178 psa_pake_step_t step,
8179 const uint8_t *input,
8180 size_t input_length)
8181{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008182 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008183 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008184 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8185 operation->primitive,
8186 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008187
8188 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008189 status = psa_pake_complete_inputs(operation);
8190 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008191 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008192 }
8193 }
8194
8195 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008196 status = PSA_ERROR_BAD_STATE;
8197 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008198 }
8199
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008200 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008201 status = PSA_ERROR_INVALID_ARGUMENT;
8202 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008203 }
8204
Przemek Stekiele12ed362022-12-21 12:54:46 +01008205 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008206#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008207 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008208 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008209 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008210 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008211 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008212 driver_step = convert_jpake_computation_stage_to_driver_step(
8213 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008214 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008215#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008216 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008217 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008218 status = PSA_ERROR_NOT_SUPPORTED;
8219 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008220 }
8221
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008222 status = psa_driver_wrapper_pake_input(operation, driver_step,
8223 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008224
8225 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008226 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008227 }
8228
8229 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008230#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008231 case PSA_ALG_JPAKE:
8232 status = psa_jpake_input_epilogue(operation);
8233 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008234 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008235 }
8236 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008237#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008238 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008239 status = PSA_ERROR_NOT_SUPPORTED;
8240 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008241 }
8242
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008243 return PSA_SUCCESS;
8244exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008245 psa_pake_abort(operation);
8246 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008247}
8248
8249psa_status_t psa_pake_get_implicit_key(
8250 psa_pake_operation_t *operation,
8251 psa_key_derivation_operation_t *output)
8252{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008253 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008254 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008255 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008256 size_t shared_key_len = 0;
8257
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008258 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008259 status = PSA_ERROR_BAD_STATE;
8260 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008261 }
8262
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008263#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008264 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008265 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008266 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008267 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
8268 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008269 status = PSA_ERROR_BAD_STATE;
8270 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008271 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008272 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008273#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008274 {
8275 status = PSA_ERROR_NOT_SUPPORTED;
8276 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008277 }
8278
Przemek Stekiel0c781802022-11-29 14:53:13 +01008279 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8280 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008281 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008282 &shared_key_len);
8283
8284 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008285 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008286 }
8287
8288 status = psa_key_derivation_input_bytes(output,
8289 PSA_KEY_DERIVATION_INPUT_SECRET,
8290 shared_key,
8291 shared_key_len);
8292
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008293 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008294exit:
8295 abort_status = psa_pake_abort(operation);
8296 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008297}
8298
8299psa_status_t psa_pake_abort(
8300 psa_pake_operation_t *operation)
8301{
Przemek Stekield69dca92023-01-31 19:59:20 +01008302 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008303
Przemek Stekield69dca92023-01-31 19:59:20 +01008304 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008305 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008306 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008307
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008308 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8309 if (operation->data.inputs.password != NULL) {
8310 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008311 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008312 mbedtls_free(operation->data.inputs.password);
8313 }
8314 if (operation->data.inputs.user != NULL) {
8315 mbedtls_free(operation->data.inputs.user);
8316 }
8317 if (operation->data.inputs.peer != NULL) {
8318 mbedtls_free(operation->data.inputs.peer);
8319 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008320 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008321 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008322
Przemek Stekield69dca92023-01-31 19:59:20 +01008323 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008324}
Neil Armstronga7d08c32022-06-01 18:21:20 +02008325
Gilles Peskinee59236f2018-01-27 23:32:46 +01008326#endif /* MBEDTLS_PSA_CRYPTO_C */