blob: 3c3e80d6a3947f16c077501264dc241766790f9f [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"
Przemek Stekiel359f4622022-12-05 14:11:55 +010038#include "psa_crypto_ffdh.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010039#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010040#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010041#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010042#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020043#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020044#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020045#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010046#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010047/* Include internal declarations that are useful for implementing persistently
48 * stored keys. */
49#include "psa_crypto_storage.h"
50
Gilles Peskineb2b64d32020-12-14 16:43:58 +010051#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010052
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#include <stdlib.h>
54#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010055#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010056
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010057#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020058#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000059#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020060#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020062#include "mbedtls/chacha20.h"
63#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/cipher.h"
65#include "mbedtls/ccm.h"
66#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020068#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010069#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010070#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/error.h"
72#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010074#include "mbedtls/md.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000075#include "md_wrap.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010076#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000077#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010078#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000079#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010081#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010082#include "mbedtls/sha1.h"
83#include "mbedtls/sha256.h"
84#include "mbedtls/sha512.h"
Paul Elliott588f8ed2022-12-02 18:10:26 +000085#include "hash_info.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
Ronald Cron7285cda2020-11-26 14:46:37 +01001410psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001411 const psa_key_attributes_t *attributes,
1412 const uint8_t *key_buffer,
1413 size_t key_buffer_size,
1414 uint8_t *data,
1415 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001417{
Ronald Crond18b5f82020-11-25 16:46:05 +01001418 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001419
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001420 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
1421 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1422 PSA_KEY_TYPE_IS_DH(type))) {
1423 /* Exporting public -> public */
1424 return psa_export_key_buffer_internal(
1425 key_buffer, key_buffer_size,
1426 data, data_size, data_length);
1427 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001428#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1430 return mbedtls_psa_rsa_export_public_key(attributes,
1431 key_buffer,
1432 key_buffer_size,
1433 data,
1434 data_size,
1435 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001436#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001437 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001439#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1440 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001441 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
John Durkop6ba40d12020-11-10 08:50:04 -08001442#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1444 return mbedtls_psa_ecp_export_public_key(attributes,
1445 key_buffer,
1446 key_buffer_size,
1447 data,
1448 data_size,
1449 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001450#else
1451 /* We don't know how to convert a private ECC key to public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001453#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1454 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001455 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001456#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) || \
1457 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY)
Przemek Stekiel359f4622022-12-05 14:11:55 +01001458 return mbedtls_psa_export_ffdh_public_key(attributes,
1459 key_buffer,
1460 key_buffer_size,
1461 data, data_size,
1462 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001463#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001464 return PSA_ERROR_NOT_SUPPORTED;
1465#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) ||
1466 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001469 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001470}
Ronald Crond18b5f82020-11-25 16:46:05 +01001471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1473 uint8_t *data,
1474 size_t data_size,
1475 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001476{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001477 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001478 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001479 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001480
Ronald Crone907e552021-01-18 13:22:38 +01001481 /* Reject a zero-length output buffer now, since this can never be a
1482 * valid key representation. This way we know that data must be a valid
1483 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 if (data_size == 0) {
1485 return PSA_ERROR_BUFFER_TOO_SMALL;
1486 }
Ronald Crone907e552021-01-18 13:22:38 +01001487
Darryl Greendd8fb772018-11-07 16:00:44 +00001488 /* Set the key to empty now, so that even when there are errors, we always
1489 * set data_length to a value between 0 and data_size. On error, setting
1490 * the key to empty is a good choice because an empty key representation is
1491 * unlikely to be accepted anywhere. */
1492 *data_length = 0;
1493
1494 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1496 if (status != PSA_SUCCESS) {
1497 return status;
1498 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1501 status = PSA_ERROR_INVALID_ARGUMENT;
1502 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001503 }
1504
Ronald Crond18b5f82020-11-25 16:46:05 +01001505 psa_key_attributes_t attributes = {
1506 .core = slot->attr
1507 };
Ronald Cron67227982020-11-26 15:16:05 +01001508 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001509 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001511
1512exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001514
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001516}
1517
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001518MBEDTLS_STATIC_ASSERT(
1519 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1520 "One or more key attribute flag is listed as both external-only and dual-use")
1521MBEDTLS_STATIC_ASSERT(
1522 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1523 "One or more key attribute flag is listed as both internal-only and dual-use")
1524MBEDTLS_STATIC_ASSERT(
1525 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1526 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001527
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001528/** Validate that a key policy is internally well-formed.
1529 *
1530 * This function only rejects invalid policies. It does not validate the
1531 * consistency of the policy with respect to other attributes of the key
1532 * such as the key type.
1533 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001534static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001535{
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1537 PSA_KEY_USAGE_COPY |
1538 PSA_KEY_USAGE_ENCRYPT |
1539 PSA_KEY_USAGE_DECRYPT |
1540 PSA_KEY_USAGE_SIGN_MESSAGE |
1541 PSA_KEY_USAGE_VERIFY_MESSAGE |
1542 PSA_KEY_USAGE_SIGN_HASH |
1543 PSA_KEY_USAGE_VERIFY_HASH |
1544 PSA_KEY_USAGE_VERIFY_DERIVATION |
1545 PSA_KEY_USAGE_DERIVE)) != 0) {
1546 return PSA_ERROR_INVALID_ARGUMENT;
1547 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001548
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001550}
1551
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001552/** Validate the internal consistency of key attributes.
1553 *
1554 * This function only rejects invalid attribute values. If does not
1555 * validate the consistency of the attributes with any key data that may
1556 * be involved in the creation of the key.
1557 *
1558 * Call this function early in the key creation process.
1559 *
1560 * \param[in] attributes Key attributes for the new key.
1561 * \param[out] p_drv On any return, the driver for the key, if any.
1562 * NULL for a transparent key.
1563 *
1564 */
1565static psa_status_t psa_validate_key_attributes(
1566 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001568{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001569 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1571 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001572
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 status = psa_validate_key_location(lifetime, p_drv);
1574 if (status != PSA_SUCCESS) {
1575 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001576 }
1577
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 status = psa_validate_key_persistence(lifetime);
1579 if (status != PSA_SUCCESS) {
1580 return status;
1581 }
1582
1583 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1584 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1585 return PSA_ERROR_INVALID_ARGUMENT;
1586 }
1587 } else {
1588 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1589 return PSA_ERROR_INVALID_ARGUMENT;
1590 }
1591 }
1592
1593 status = psa_validate_key_policy(&attributes->core.policy);
1594 if (status != PSA_SUCCESS) {
1595 return status;
1596 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001597
1598 /* Refuse to create overly large keys.
1599 * Note that this doesn't trigger on import if the attributes don't
1600 * explicitly specify a size (so psa_get_key_bits returns 0), so
1601 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1603 return PSA_ERROR_NOT_SUPPORTED;
1604 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001605
Gilles Peskine91e8c332019-08-02 19:19:39 +02001606 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1608 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1609 return PSA_ERROR_INVALID_ARGUMENT;
1610 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001611
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001613}
1614
Gilles Peskine4747d192019-04-17 15:05:45 +02001615/** Prepare a key slot to receive key material.
1616 *
1617 * This function allocates a key slot and sets its metadata.
1618 *
1619 * If this function fails, call psa_fail_key_creation().
1620 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001621 * This function is intended to be used as follows:
1622 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001623 * it with the specified attributes, and in case of a volatile key assign it
1624 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001625 * -# Populate the slot with the key material.
1626 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1627 * In case of failure at any step, stop the sequence and call
1628 * psa_fail_key_creation().
1629 *
Ronald Cron5c522922020-11-14 16:35:34 +01001630 * On success, the key slot is locked. It is the responsibility of the caller
1631 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001632 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001633 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001634 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001635 * \param[out] p_slot On success, a pointer to the prepared slot.
1636 * \param[out] p_drv On any return, the driver for the key, if any.
1637 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001638 *
1639 * \retval #PSA_SUCCESS
1640 * The key slot is ready to receive key material.
1641 * \return If this function fails, the key slot is an invalid state.
1642 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001643 */
1644static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001645 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001646 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001647 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001649{
1650 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001651 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001652 psa_key_slot_t *slot;
1653
Gilles Peskinedf179142019-07-15 22:02:14 +02001654 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001655 *p_drv = NULL;
1656
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 status = psa_validate_key_attributes(attributes, p_drv);
1658 if (status != PSA_SUCCESS) {
1659 return status;
1660 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1663 if (status != PSA_SUCCESS) {
1664 return status;
1665 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001666 slot = *p_slot;
1667
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001668 /* We're storing the declared bit-size of the key. It's up to each
1669 * creation mechanism to verify that this information is correct.
1670 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001671 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001672 * is optional (import, copy). In case of a volatile key, assign it the
1673 * volatile key identifier associated to the slot returned to contain its
1674 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001675
1676 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001678#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1679 slot->attr.id = volatile_key_id;
1680#else
1681 slot->attr.id.key_id = volatile_key_id;
1682#endif
1683 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001684
Gilles Peskine91e8c332019-08-02 19:19:39 +02001685 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001686 * external-only flags, query `attributes`. Thanks to the check
1687 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001688 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001689 * may have set. */
1690 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001691
Gilles Peskinecbaff462019-07-12 23:46:04 +02001692#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001693 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001694 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001695 * create the key file in internal storage, create the
1696 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001697 * persistent data. This is done by starting a transaction that will
1698 * encompass these three actions.
1699 * For registering a volatile key, we just need to find an appropriate
1700 * slot number inside the SE. Since the key is designated volatile, creating
1701 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001702 /* The first thing to do is to find a slot number for the new key.
1703 * We save the slot number in persistent storage as part of the
1704 * transaction data. It will be needed to recover if the power
1705 * fails during the key creation process, to clean up on the secure
1706 * element side after restarting. Obtaining a slot number from the
1707 * secure element driver updates its persistent state, but we do not yet
1708 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001709 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001711 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1713 &slot_number);
1714 if (status != PSA_SUCCESS) {
1715 return status;
1716 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001717
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1719 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001720 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001721 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001722 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 status = psa_crypto_save_transaction();
1724 if (status != PSA_SUCCESS) {
1725 (void) psa_crypto_stop_transaction();
1726 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001727 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001728 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001729
1730 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001732 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001733
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001735 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001737 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001738#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1739
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001741}
Gilles Peskine4747d192019-04-17 15:05:45 +02001742
1743/** Finalize the creation of a key once its key material has been set.
1744 *
1745 * This entails writing the key to persistent storage.
1746 *
1747 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001748 * See the documentation of psa_start_key_creation() for the intended use
1749 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001750 *
Ronald Cron5c522922020-11-14 16:35:34 +01001751 * If the finalization succeeds, the function unlocks the key slot (it was
1752 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1753 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001754 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001755 * \param[in,out] slot Pointer to the slot with key material.
1756 * \param[in] driver The secure element driver for the key,
1757 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001758 * \param[out] key On success, identifier of the key. Note that the
1759 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001760 *
1761 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001762 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001763 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1764 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1765 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1766 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1767 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1768 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001769 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001770 * \return If this function fails, the key slot is an invalid state.
1771 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001772 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001773static psa_status_t psa_finish_key_creation(
1774 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001775 psa_se_drv_table_entry_t *driver,
1776 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001777{
1778 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001779 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001780 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001781
1782#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001784#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001786 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001787 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001789
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001790 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1791 sizeof(data.slot_number),
1792 "Slot number size does not match psa_se_key_data_storage_t");
1793
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1795 status = psa_save_persistent_key(&slot->attr,
1796 (uint8_t *) &data,
1797 sizeof(data));
1798 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001799#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1800 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001801 /* Key material is saved in export representation in the slot, so
1802 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 status = psa_save_persistent_key(&slot->attr,
1804 slot->key.data,
1805 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001806 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001807 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001808#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1809
Gilles Peskinecbaff462019-07-12 23:46:04 +02001810#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001811 /* Finish the transaction for a key creation. This does not
1812 * happen when registering an existing key. Detect this case
1813 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001814 * creation of a persistent key in a secure element requires a transaction,
1815 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 if (driver != NULL &&
1817 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1818 status = psa_save_se_persistent_data(driver);
1819 if (status != PSA_SUCCESS) {
1820 psa_destroy_persistent_key(slot->attr.id);
1821 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001822 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001824 }
1825#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1826
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001828 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 status = psa_unlock_key_slot(slot);
1830 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001831 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001833 }
Ronald Cron50972942020-11-14 11:28:25 +01001834
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001836}
1837
1838/** Abort the creation of a key.
1839 *
1840 * You may call this function after calling psa_start_key_creation(),
1841 * or after psa_finish_key_creation() fails. In other circumstances, this
1842 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001843 * See the documentation of psa_start_key_creation() for the intended use
1844 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001845 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001846 * \param[in,out] slot Pointer to the slot with key material.
1847 * \param[in] driver The secure element driver for the key,
1848 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001849 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001850static void psa_fail_key_creation(psa_key_slot_t *slot,
1851 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001852{
Gilles Peskine011e4282019-06-26 18:34:38 +02001853 (void) driver;
1854
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001856 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001858
1859#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001860 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001861 * element, and the failure happened later (when saving metadata
1862 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001863 * element.
1864 * https://github.com/ARMmbed/mbed-crypto/issues/217
1865 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001866
Gilles Peskined7729582019-08-05 15:55:54 +02001867 /* Abort the ongoing transaction if any (there may not be one if
1868 * the creation process failed before starting one, or if the
1869 * key creation is a registration of a key in a secure element).
1870 * Earlier functions must already have done what it takes to undo any
1871 * partial creation. All that's left is to update the transaction data
1872 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001874#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1875
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001877}
1878
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001879/** Validate optional attributes during key creation.
1880 *
1881 * Some key attributes are optional during key creation. If they are
1882 * specified in the attributes structure, check that they are consistent
1883 * with the data in the slot.
1884 *
1885 * This function should be called near the end of key creation, after
1886 * the slot in memory is fully populated but before saving persistent data.
1887 */
1888static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001889 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001891{
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 if (attributes->core.type != 0) {
1893 if (attributes->core.type != slot->attr.type) {
1894 return PSA_ERROR_INVALID_ARGUMENT;
1895 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001896 }
1897
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001899#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1901 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001902 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001903 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001904 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001905
Ronald Cron90857082020-11-25 14:58:33 +01001906 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 slot->attr.type,
1908 slot->key.data,
1909 slot->key.bytes,
1910 &rsa);
1911 if (status != PSA_SUCCESS) {
1912 return status;
1913 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 mbedtls_mpi_init(&actual);
1916 mbedtls_mpi_init(&required);
1917 ret = mbedtls_rsa_export(rsa,
1918 NULL, NULL, NULL, NULL, &actual);
1919 mbedtls_rsa_free(rsa);
1920 mbedtls_free(rsa);
1921 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001922 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 }
1924 ret = mbedtls_mpi_read_binary(&required,
1925 attributes->domain_parameters,
1926 attributes->domain_parameters_size);
1927 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001928 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 }
1930 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001931 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 }
1933rsa_exit:
1934 mbedtls_mpi_free(&actual);
1935 mbedtls_mpi_free(&required);
1936 if (ret != 0) {
1937 return mbedtls_to_psa_error(ret);
1938 }
1939 } else
John Durkop9814fa22020-11-04 12:28:15 -08001940#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1941 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001942 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001944 }
1945 }
1946
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 if (attributes->core.bits != 0) {
1948 if (attributes->core.bits != slot->attr.bits) {
1949 return PSA_ERROR_INVALID_ARGUMENT;
1950 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001951 }
1952
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001954}
1955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1957 const uint8_t *data,
1958 size_t data_length,
1959 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001960{
1961 psa_status_t status;
1962 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001963 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001964 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301965 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001966
Ronald Cron81709fc2020-11-14 12:10:32 +01001967 *key = MBEDTLS_SVC_KEY_ID_INIT;
1968
Gilles Peskine0f84d622019-09-12 19:03:13 +02001969 /* Reject zero-length symmetric keys (including raw data key objects).
1970 * This also rejects any key which might be encoded as an empty string,
1971 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 if (data_length == 0) {
1973 return PSA_ERROR_INVALID_ARGUMENT;
1974 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001975
Archana449608b2021-09-08 15:36:05 +05301976 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 if (data_length > SIZE_MAX / 8) {
1978 return PSA_ERROR_NOT_SUPPORTED;
1979 }
Archana6ed4bda2021-08-04 10:47:15 +05301980
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1982 &slot, &driver);
1983 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001984 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001986
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001987 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301988 * storage ( thus not in the case of importing a key in a secure element
1989 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1990 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 if (slot->key.data == NULL) {
1992 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301993 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 attributes, data, data_length, &storage_size);
1995 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301996 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 }
Archanad8a83dc2021-06-14 10:04:16 +05301998 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 status = psa_allocate_buffer_to_slot(slot, storage_size);
2000 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002001 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002003 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002004
2005 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 status = psa_driver_wrapper_import_key(attributes,
2007 data, data_length,
2008 slot->key.data,
2009 slot->key.bytes,
2010 &slot->key.bytes, &bits);
2011 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002012 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002014
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002016 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002018 status = PSA_ERROR_INVALID_ARGUMENT;
2019 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002020 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002021
Archana6ed4bda2021-08-04 10:47:15 +05302022 /* Enforce a size limit, and in particular ensure that the bit
2023 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302025 status = PSA_ERROR_NOT_SUPPORTED;
2026 goto exit;
2027 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 status = psa_validate_optional_attributes(slot, attributes);
2029 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002030 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002034exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 if (status != PSA_SUCCESS) {
2036 psa_fail_key_creation(slot, driver);
2037 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002040}
2041
Gilles Peskined7729582019-08-05 15:55:54 +02002042#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2043psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002045{
2046 psa_status_t status;
2047 psa_key_slot_t *slot = NULL;
2048 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002049 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002050
2051 /* Leaving attributes unspecified is not currently supported.
2052 * It could make sense to query the key type and size from the
2053 * secure element, but not all secure elements support this
2054 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2056 return PSA_ERROR_NOT_SUPPORTED;
2057 }
2058 if (psa_get_key_bits(attributes) == 0) {
2059 return PSA_ERROR_NOT_SUPPORTED;
2060 }
Gilles Peskined7729582019-08-05 15:55:54 +02002061
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2063 &slot, &driver);
2064 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002065 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 }
Gilles Peskined7729582019-08-05 15:55:54 +02002067
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002069
2070exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 if (status != PSA_SUCCESS) {
2072 psa_fail_key_creation(slot, driver);
2073 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002074
Gilles Peskined7729582019-08-05 15:55:54 +02002075 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 psa_close_key(key);
2077 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002078}
2079#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2080
Gilles Peskine449bd832023-01-11 14:50:10 +01002081psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2082 const psa_key_attributes_t *specified_attributes,
2083 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002084{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002085 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002086 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002087 psa_key_slot_t *source_slot = NULL;
2088 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002089 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002090 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302091 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002092
Ronald Cron81709fc2020-11-14 12:10:32 +01002093 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2094
Archana8a180362021-07-05 02:18:48 +05302095 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2097 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002098 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002100
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 status = psa_validate_optional_attributes(source_slot,
2102 specified_attributes);
2103 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002104 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002106
Archana9d17bf42021-09-10 06:22:44 +05302107 /* The target key type and number of bits have been validated by
2108 * psa_validate_optional_attributes() to be either equal to zero or
2109 * equal to the ones of the source key. So it is safe to inherit
2110 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302111 * */
Archana9d17bf42021-09-10 06:22:44 +05302112 actual_attributes.core.bits = source_slot->attr.bits;
2113 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302114
2115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 status = psa_restrict_key_policy(source_slot->attr.type,
2117 &actual_attributes.core.policy,
2118 &source_slot->attr.policy);
2119 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002120 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2124 &target_slot, &driver);
2125 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002126 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 }
2128 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2129 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302130 /*
Archana9d17bf42021-09-10 06:22:44 +05302131 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302132 * the source key would need to be exported as plaintext and re-imported
2133 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302134 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302135 * appropriate API invocations from the application, if needed.
2136 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002137 status = PSA_ERROR_NOT_SUPPORTED;
2138 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002139 }
Archana8a180362021-07-05 02:18:48 +05302140 /*
2141 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302142 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302143 * - For opaque keys this translates to an invocation of the drivers'
2144 * copy_key entry point through the dispatch layer.
2145 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2147 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2148 &storage_size);
2149 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302150 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 }
Archana374fe5b2021-09-08 15:50:28 +05302152
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2154 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302155 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 }
Archana374fe5b2021-09-08 15:50:28 +05302157
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 status = psa_driver_wrapper_copy_key(&actual_attributes,
2159 source_slot->key.data,
2160 source_slot->key.bytes,
2161 target_slot->key.data,
2162 target_slot->key.bytes,
2163 &target_slot->key.bytes);
2164 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302165 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 }
2167 } else {
2168 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302169 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 source_slot->key.bytes);
2171 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302172 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 }
Archana8a180362021-07-05 02:18:48 +05302174 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002176exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 if (status != PSA_SUCCESS) {
2178 psa_fail_key_creation(target_slot, driver);
2179 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002182
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002184}
2185
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002186
2187
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002188/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002189/* Message digests */
2190/****************************************************************/
2191
Gilles Peskine449bd832023-01-11 14:50:10 +01002192psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002193{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002194 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 if (operation->id == 0) {
2196 return PSA_SUCCESS;
2197 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002198
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002200 operation->id = 0;
2201
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002203}
2204
Gilles Peskine449bd832023-01-11 14:50:10 +01002205psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2206 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002207{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002208 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002209
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002210 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002212 status = PSA_ERROR_BAD_STATE;
2213 goto exit;
2214 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002217 status = PSA_ERROR_INVALID_ARGUMENT;
2218 goto exit;
2219 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002220
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002221 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2222 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002226
2227exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 if (status != PSA_SUCCESS) {
2229 psa_hash_abort(operation);
2230 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002231
2232 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002233}
2234
Gilles Peskine449bd832023-01-11 14:50:10 +01002235psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2236 const uint8_t *input,
2237 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002238{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002239 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002242 status = PSA_ERROR_BAD_STATE;
2243 goto exit;
2244 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002245
Steven Cooremanc8288352021-03-04 14:02:19 +01002246 /* Don't require hash implementations to behave correctly on a
2247 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 if (input_length == 0) {
2249 return PSA_SUCCESS;
2250 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002251
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002253
2254exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 if (status != PSA_SUCCESS) {
2256 psa_hash_abort(operation);
2257 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002258
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002260}
2261
Gilles Peskine449bd832023-01-11 14:50:10 +01002262psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2263 uint8_t *hash,
2264 size_t hash_size,
2265 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002266{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002267 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 if (operation->id == 0) {
2269 return PSA_ERROR_BAD_STATE;
2270 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002271
Steven Cooreman1e582352021-02-18 17:24:37 +01002272 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 operation, hash, hash_size, hash_length);
2274 psa_hash_abort(operation);
2275 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002276}
2277
Gilles Peskine449bd832023-01-11 14:50:10 +01002278psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2279 const uint8_t *hash,
2280 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002281{
Ronald Cron69a63422021-10-18 09:47:58 +02002282 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002283 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002284 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 operation,
2286 actual_hash, sizeof(actual_hash),
2287 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002290 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002292
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002294 status = PSA_ERROR_INVALID_SIGNATURE;
2295 goto exit;
2296 }
2297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002299 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002301
2302exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2304 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002305 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002307
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002309}
2310
Gilles Peskine449bd832023-01-11 14:50:10 +01002311psa_status_t psa_hash_compute(psa_algorithm_t alg,
2312 const uint8_t *input, size_t input_length,
2313 uint8_t *hash, size_t hash_size,
2314 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002315{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002316 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 if (!PSA_ALG_IS_HASH(alg)) {
2318 return PSA_ERROR_INVALID_ARGUMENT;
2319 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002320
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2322 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002323}
2324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325psa_status_t psa_hash_compare(psa_algorithm_t alg,
2326 const uint8_t *input, size_t input_length,
2327 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002328{
Ronald Cron69a63422021-10-18 09:47:58 +02002329 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002330 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 if (!PSA_ALG_IS_HASH(alg)) {
2333 return PSA_ERROR_INVALID_ARGUMENT;
2334 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002335
Steven Cooreman1e582352021-02-18 17:24:37 +01002336 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 alg, input, input_length,
2338 actual_hash, sizeof(actual_hash),
2339 &actual_hash_length);
2340 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002341 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 }
2343 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002344 status = PSA_ERROR_INVALID_SIGNATURE;
2345 goto exit;
2346 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002348 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002350
2351exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2353 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002354}
2355
Gilles Peskine449bd832023-01-11 14:50:10 +01002356psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2357 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002358{
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 if (source_operation->id == 0 ||
2360 target_operation->id != 0) {
2361 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002362 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2365 target_operation);
2366 if (status != PSA_SUCCESS) {
2367 psa_hash_abort(target_operation);
2368 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002369
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002371}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002372
2373
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002374/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002375/* MAC */
2376/****************************************************************/
2377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002379{
Steven Cooremane6804192021-03-19 18:28:56 +01002380 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 if (operation->id == 0) {
2382 return PSA_SUCCESS;
2383 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002384
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002386 operation->mac_size = 0;
2387 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002388 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002391}
2392
Ronald Cron2dff3b22021-06-17 16:33:22 +02002393static psa_status_t psa_mac_finalize_alg_and_key_validation(
2394 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002395 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002397{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002398 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 psa_key_type_t key_type = psa_get_key_type(attributes);
2400 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002401
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 if (!PSA_ALG_IS_MAC(alg)) {
2403 return PSA_ERROR_INVALID_ARGUMENT;
2404 }
Steven Cooremane6804192021-03-19 18:28:56 +01002405
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002406 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 status = psa_mac_key_can_do(alg, key_type);
2408 if (status != PSA_SUCCESS) {
2409 return status;
2410 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002411
Steven Cooremand1a68f12021-05-10 11:13:41 +02002412 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002416 /* A very short MAC is too short for security since it can be
2417 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2418 * so we make this our minimum, even though 32 bits is still
2419 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002421 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002422
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2424 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002425 /* It's impossible to "truncate" to a larger length than the full length
2426 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002428 }
2429
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002431 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2432 * that is disabled in the compile-time configuration. The result can
2433 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2434 * configuration into account. In this case, force a return of
2435 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2436 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2437 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2438 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2439 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002441 }
2442
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002444}
2445
Gilles Peskine449bd832023-01-11 14:50:10 +01002446static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2447 mbedtls_svc_key_id_t key,
2448 psa_algorithm_t alg,
2449 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002450{
2451 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2452 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002453 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002454
2455 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002457 status = PSA_ERROR_BAD_STATE;
2458 goto exit;
2459 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002460
2461 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 key,
2463 &slot,
2464 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2465 alg);
2466 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002467 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002469
2470 psa_key_attributes_t attributes = {
2471 .core = slot->attr
2472 };
2473
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2475 &operation->mac_size);
2476 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002477 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002479
2480 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002481 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 if (is_sign) {
2483 status = psa_driver_wrapper_mac_sign_setup(operation,
2484 &attributes,
2485 slot->key.data,
2486 slot->key.bytes,
2487 alg);
2488 } else {
2489 status = psa_driver_wrapper_mac_verify_setup(operation,
2490 &attributes,
2491 slot->key.data,
2492 slot->key.bytes,
2493 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002494 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002495
Gilles Peskinefbfac682018-07-08 20:51:54 +02002496exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 if (status != PSA_SUCCESS) {
2498 psa_mac_abort(operation);
2499 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002500
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002504}
2505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2507 mbedtls_svc_key_id_t key,
2508 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002509{
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002511}
2512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2514 mbedtls_svc_key_id_t key,
2515 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002516{
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002518}
2519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2521 const uint8_t *input,
2522 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002523{
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 if (operation->id == 0) {
2525 return PSA_ERROR_BAD_STATE;
2526 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002527
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002528 /* Don't require hash implementations to behave correctly on a
2529 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 if (input_length == 0) {
2531 return PSA_SUCCESS;
2532 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002533
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2535 input, input_length);
2536 if (status != PSA_SUCCESS) {
2537 psa_mac_abort(operation);
2538 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002541}
2542
Gilles Peskine449bd832023-01-11 14:50:10 +01002543psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2544 uint8_t *mac,
2545 size_t mac_size,
2546 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002547{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002548 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2549 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002550
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002552 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002553 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002554 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002555
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002557 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002558 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002559 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002560
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002561 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2562 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002564 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002565 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002566 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002569 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002570 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002571 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002572
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 status = psa_driver_wrapper_mac_sign_finish(operation,
2574 mac, operation->mac_size,
2575 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002576
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002577exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002578 /* In case of success, set the potential excess room in the output buffer
2579 * to an invalid value, to avoid potentially leaking a longer MAC.
2580 * In case of error, set the output length and content to a safe default,
2581 * such that in case the caller misses an error check, the output would be
2582 * an unachievable MAC.
2583 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002585 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002586 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002587 }
mohammad16036df908f2018-04-02 08:34:15 -07002588
Paul Elliottdc42ca82023-02-24 18:11:59 +00002589 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002590
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002592
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002594}
2595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2597 const uint8_t *mac,
2598 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002599{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002600 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2601 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002604 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002605 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002606 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002607
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002609 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002610 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002611 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002612
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002614 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002615 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002616 }
2617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 status = psa_driver_wrapper_mac_verify_finish(operation,
2619 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002620
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002621exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002623
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002625}
2626
Gilles Peskine449bd832023-01-11 14:50:10 +01002627static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2628 psa_algorithm_t alg,
2629 const uint8_t *input,
2630 size_t input_length,
2631 uint8_t *mac,
2632 size_t mac_size,
2633 size_t *mac_length,
2634 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002635{
2636 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002637 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2638 psa_key_slot_t *slot;
2639 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002640
Ronald Cron51131b52021-06-17 17:17:20 +02002641 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 key,
2643 &slot,
2644 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2645 alg);
2646 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002647 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002649
Ronald Cron51131b52021-06-17 17:17:20 +02002650 psa_key_attributes_t attributes = {
2651 .core = slot->attr
2652 };
2653
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2655 &operation_mac_size);
2656 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002657 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002661 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002662 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002663 }
2664
2665 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 &attributes,
2667 slot->key.data, slot->key.bytes,
2668 alg,
2669 input, input_length,
2670 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002671
2672exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002673 /* In case of success, set the potential excess room in the output buffer
2674 * to an invalid value, to avoid potentially leaking a longer MAC.
2675 * In case of error, set the output length and content to a safe default,
2676 * such that in case the caller misses an error check, the output would be
2677 * an unachievable MAC.
2678 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002680 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002681 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002682 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002683
2684 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002685
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002687
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002689}
2690
Gilles Peskine449bd832023-01-11 14:50:10 +01002691psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002692 psa_algorithm_t alg,
2693 const uint8_t *input,
2694 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 uint8_t *mac,
2696 size_t mac_size,
2697 size_t *mac_length)
2698{
2699 return psa_mac_compute_internal(key, alg,
2700 input, input_length,
2701 mac, mac_size, mac_length, 1);
2702}
2703
2704psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2705 psa_algorithm_t alg,
2706 const uint8_t *input,
2707 size_t input_length,
2708 const uint8_t *mac,
2709 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002710{
2711 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002712 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2713 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002714
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 status = psa_mac_compute_internal(key, alg,
2716 input, input_length,
2717 actual_mac, sizeof(actual_mac),
2718 &actual_mac_length, 0);
2719 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002720 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002722
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002724 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002725 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002726 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002727 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002728 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002729 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002730 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002731
2732exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002736}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002737
Gilles Peskinee59236f2018-01-27 23:32:46 +01002738/****************************************************************/
2739/* Asymmetric cryptography */
2740/****************************************************************/
2741
Gilles Peskine449bd832023-01-11 14:50:10 +01002742static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2743 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002744{
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 if (input_is_message) {
2746 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2747 return PSA_ERROR_INVALID_ARGUMENT;
2748 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002749
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2751 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2752 return PSA_ERROR_INVALID_ARGUMENT;
2753 }
2754 }
2755 } else {
2756 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2757 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002758 }
2759 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002760
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002762}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002763
Gilles Peskine449bd832023-01-11 14:50:10 +01002764static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2765 int input_is_message,
2766 psa_algorithm_t alg,
2767 const uint8_t *input,
2768 size_t input_length,
2769 uint8_t *signature,
2770 size_t signature_size,
2771 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002772{
2773 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2774 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2775 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002776
2777 *signature_length = 0;
2778
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 status = psa_sign_verify_check_alg(input_is_message, alg);
2780 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002781 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002783
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002784 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002785 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002786 * buffer can in principle be empty since it doesn't actually have
2787 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 if (signature_size == 0) {
2789 return PSA_ERROR_BUFFER_TOO_SMALL;
2790 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002791
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002792 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 key, &slot,
2794 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2795 PSA_KEY_USAGE_SIGN_HASH,
2796 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002797
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002799 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002801
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002803 status = PSA_ERROR_INVALID_ARGUMENT;
2804 goto exit;
2805 }
2806
2807 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002809 };
2810
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002812 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002813 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002814 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 signature, signature_size, signature_length);
2816 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002817
2818 status = psa_driver_wrapper_sign_hash(
2819 &attributes, slot->key.data, slot->key.bytes,
2820 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002822 }
2823
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002824
2825exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002826 psa_wipe_tag_output_buffer(signature, status, signature_size,
2827 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002828
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002830
Gilles Peskine449bd832023-01-11 14:50:10 +01002831 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002832}
2833
Gilles Peskine449bd832023-01-11 14:50:10 +01002834static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2835 int input_is_message,
2836 psa_algorithm_t alg,
2837 const uint8_t *input,
2838 size_t input_length,
2839 const uint8_t *signature,
2840 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002841{
2842 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2843 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2844 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002845
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 status = psa_sign_verify_check_alg(input_is_message, alg);
2847 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002848 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002850
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002851 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 key, &slot,
2853 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2854 PSA_KEY_USAGE_VERIFY_HASH,
2855 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002856
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 if (status != PSA_SUCCESS) {
2858 return status;
2859 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002860
2861 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002863 };
2864
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002866 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002867 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002868 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 signature, signature_length);
2870 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002871 status = psa_driver_wrapper_verify_hash(
2872 &attributes, slot->key.data, slot->key.bytes,
2873 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002875 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002876
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002878
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002880
2881}
2882
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002883psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002884 const psa_key_attributes_t *attributes,
2885 const uint8_t *key_buffer,
2886 size_t key_buffer_size,
2887 psa_algorithm_t alg,
2888 const uint8_t *input,
2889 size_t input_length,
2890 uint8_t *signature,
2891 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002892 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002893{
2894 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002895
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002897 size_t hash_length;
2898 uint8_t hash[PSA_HASH_MAX_SIZE];
2899
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002900 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 PSA_ALG_SIGN_GET_HASH(alg),
2902 input, input_length,
2903 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002904
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002906 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002908
2909 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 attributes, key_buffer, key_buffer_size,
2911 alg, hash, hash_length,
2912 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002913 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002914
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002916}
2917
Gilles Peskine449bd832023-01-11 14:50:10 +01002918psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2919 psa_algorithm_t alg,
2920 const uint8_t *input,
2921 size_t input_length,
2922 uint8_t *signature,
2923 size_t signature_size,
2924 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002925{
2926 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002927 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002928 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002929}
2930
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002931psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002932 const psa_key_attributes_t *attributes,
2933 const uint8_t *key_buffer,
2934 size_t key_buffer_size,
2935 psa_algorithm_t alg,
2936 const uint8_t *input,
2937 size_t input_length,
2938 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002940{
2941 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002942
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002944 size_t hash_length;
2945 uint8_t hash[PSA_HASH_MAX_SIZE];
2946
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002947 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 PSA_ALG_SIGN_GET_HASH(alg),
2949 input, input_length,
2950 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002951
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002953 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002955
2956 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 attributes, key_buffer, key_buffer_size,
2958 alg, hash, hash_length,
2959 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002960 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002961
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002963}
2964
Gilles Peskine449bd832023-01-11 14:50:10 +01002965psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2966 psa_algorithm_t alg,
2967 const uint8_t *input,
2968 size_t input_length,
2969 const uint8_t *signature,
2970 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002971{
2972 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002973 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002975}
2976
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002977psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002978 const psa_key_attributes_t *attributes,
2979 const uint8_t *key_buffer, size_t key_buffer_size,
2980 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002982{
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2984 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2985 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002986#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2988 return mbedtls_psa_rsa_sign_hash(
2989 attributes,
2990 key_buffer, key_buffer_size,
2991 alg, hash, hash_length,
2992 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002993#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2994 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 } else {
2996 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002997 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2999 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003000#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3002 return mbedtls_psa_ecdsa_sign_hash(
3003 attributes,
3004 key_buffer, key_buffer_size,
3005 alg, hash, hash_length,
3006 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003007#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3008 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 } else {
3010 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003011 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003012 }
Ronald Cron4501c982021-03-19 20:09:32 +01003013
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 (void) key_buffer;
3015 (void) key_buffer_size;
3016 (void) hash;
3017 (void) hash_length;
3018 (void) signature;
3019 (void) signature_size;
3020 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003021
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003023}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3026 psa_algorithm_t alg,
3027 const uint8_t *hash,
3028 size_t hash_length,
3029 uint8_t *signature,
3030 size_t signature_size,
3031 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003032{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003033 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003034 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003036}
3037
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003038psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003039 const psa_key_attributes_t *attributes,
3040 const uint8_t *key_buffer, size_t key_buffer_size,
3041 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003042 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003043{
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3045 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3046 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003047#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3049 return mbedtls_psa_rsa_verify_hash(
3050 attributes,
3051 key_buffer, key_buffer_size,
3052 alg, hash, hash_length,
3053 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003054#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3055 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 } else {
3057 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003058 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3060 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003061#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3063 return mbedtls_psa_ecdsa_verify_hash(
3064 attributes,
3065 key_buffer, key_buffer_size,
3066 alg, hash, hash_length,
3067 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003068#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3069 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 } else {
3071 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003072 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003073 }
Ronald Cron4501c982021-03-19 20:09:32 +01003074
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 (void) key_buffer;
3076 (void) key_buffer_size;
3077 (void) hash;
3078 (void) hash_length;
3079 (void) signature;
3080 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003081
Gilles Peskine449bd832023-01-11 14:50:10 +01003082 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003083}
3084
Gilles Peskine449bd832023-01-11 14:50:10 +01003085psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3086 psa_algorithm_t alg,
3087 const uint8_t *hash,
3088 size_t hash_length,
3089 const uint8_t *signature,
3090 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003091{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003092 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003093 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003095}
3096
Gilles Peskine449bd832023-01-11 14:50:10 +01003097psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3098 psa_algorithm_t alg,
3099 const uint8_t *input,
3100 size_t input_length,
3101 const uint8_t *salt,
3102 size_t salt_length,
3103 uint8_t *output,
3104 size_t output_size,
3105 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003106{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003107 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003108 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003109 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003110
Darryl Green5cc689a2018-07-24 15:34:10 +01003111 (void) input;
3112 (void) input_length;
3113 (void) salt;
3114 (void) output;
3115 (void) output_size;
3116
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003117 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003118
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3120 return PSA_ERROR_INVALID_ARGUMENT;
3121 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003122
Ronald Cron5c522922020-11-14 16:35:34 +01003123 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3125 if (status != PSA_SUCCESS) {
3126 return status;
3127 }
3128 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3129 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003130 status = PSA_ERROR_INVALID_ARGUMENT;
3131 goto exit;
3132 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003133
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003134 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003136 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003137
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003138 status = psa_driver_wrapper_asymmetric_encrypt(
3139 &attributes, slot->key.data, slot->key.bytes,
3140 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003142exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003144
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003146}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003147
Gilles Peskine449bd832023-01-11 14:50:10 +01003148psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3149 psa_algorithm_t alg,
3150 const uint8_t *input,
3151 size_t input_length,
3152 const uint8_t *salt,
3153 size_t salt_length,
3154 uint8_t *output,
3155 size_t output_size,
3156 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003157{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003158 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003159 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003160 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003161
Darryl Green5cc689a2018-07-24 15:34:10 +01003162 (void) input;
3163 (void) input_length;
3164 (void) salt;
3165 (void) output;
3166 (void) output_size;
3167
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003168 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003169
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3171 return PSA_ERROR_INVALID_ARGUMENT;
3172 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003173
Ronald Cron5c522922020-11-14 16:35:34 +01003174 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3176 if (status != PSA_SUCCESS) {
3177 return status;
3178 }
3179 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003180 status = PSA_ERROR_INVALID_ARGUMENT;
3181 goto exit;
3182 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003183
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003184 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003185 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003186 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003187
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003188 status = psa_driver_wrapper_asymmetric_decrypt(
3189 &attributes, slot->key.data, slot->key.bytes,
3190 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003192
3193exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003195
Gilles Peskine449bd832023-01-11 14:50:10 +01003196 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003197}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003198
Paul Elliott9fe12f62022-11-30 19:16:02 +00003199/****************************************************************/
3200/* Asymmetric interruptible cryptography */
3201/****************************************************************/
3202
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003203static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3204
Paul Elliott9fe12f62022-11-30 19:16:02 +00003205void psa_interruptible_set_max_ops(uint32_t max_ops)
3206{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003207 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003208}
3209
3210uint32_t psa_interruptible_get_max_ops(void)
3211{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003212 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003213}
3214
Paul Elliott9fe12f62022-11-30 19:16:02 +00003215uint32_t psa_sign_hash_get_num_ops(
3216 const psa_sign_hash_interruptible_operation_t *operation)
3217{
Paul Elliott296ede92022-12-15 17:00:30 +00003218 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003219}
3220
3221uint32_t psa_verify_hash_get_num_ops(
3222 const psa_verify_hash_interruptible_operation_t *operation)
3223{
Paul Elliott296ede92022-12-15 17:00:30 +00003224 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003225}
3226
Paul Elliott59ad9452022-12-18 15:09:02 +00003227static psa_status_t psa_sign_hash_abort_internal(
3228 psa_sign_hash_interruptible_operation_t *operation)
3229{
3230 if (operation->id == 0) {
3231 /* The object has (apparently) been initialized but it is not (yet)
3232 * in use. It's ok to call abort on such an object, and there's
3233 * nothing to do. */
3234 return PSA_SUCCESS;
3235 }
3236
3237 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3238
3239 status = psa_driver_wrapper_sign_hash_abort(operation);
3240
3241 operation->id = 0;
3242
Paul Elliotte6145dc2023-02-07 12:51:21 +00003243 /* Do not clear either the error_occurred or num_ops elements here as they
3244 * only want to be cleared by the application calling abort, not by abort
3245 * being called at completion of an operation. */
3246
Paul Elliott59ad9452022-12-18 15:09:02 +00003247 return status;
3248}
3249
Paul Elliott9fe12f62022-11-30 19:16:02 +00003250psa_status_t psa_sign_hash_start(
3251 psa_sign_hash_interruptible_operation_t *operation,
3252 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3253 const uint8_t *hash, size_t hash_length)
3254{
3255 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3256 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3257 psa_key_slot_t *slot;
3258
Paul Elliottc9774412023-02-06 15:14:07 +00003259 /* Check that start has not been previously called, or operation has not
3260 * previously errored. */
3261 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003262 return PSA_ERROR_BAD_STATE;
3263 }
3264
Paul Elliott9fe12f62022-11-30 19:16:02 +00003265 status = psa_sign_verify_check_alg(0, alg);
3266 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003267 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003268 return status;
3269 }
3270
3271 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3272 PSA_KEY_USAGE_SIGN_HASH,
3273 alg);
3274
3275 if (status != PSA_SUCCESS) {
3276 goto exit;
3277 }
3278
3279 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3280 status = PSA_ERROR_INVALID_ARGUMENT;
3281 goto exit;
3282 }
3283
3284 psa_key_attributes_t attributes = {
3285 .core = slot->attr
3286 };
3287
Paul Elliott296ede92022-12-15 17:00:30 +00003288 /* Ensure ops count gets reset, in case of operation re-use. */
3289 operation->num_ops = 0;
3290
Paul Elliott9fe12f62022-11-30 19:16:02 +00003291 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3292 slot->key.data,
3293 slot->key.bytes, alg,
3294 hash, hash_length);
3295exit:
3296
3297 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003298 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003299 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003300 }
3301
3302 unlock_status = psa_unlock_key_slot(slot);
3303
Paul Elliottc9774412023-02-06 15:14:07 +00003304 if (unlock_status != PSA_SUCCESS) {
3305 operation->error_occurred = 1;
3306 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003307
Paul Elliottc9774412023-02-06 15:14:07 +00003308 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003309}
3310
3311
3312psa_status_t psa_sign_hash_complete(
3313 psa_sign_hash_interruptible_operation_t *operation,
3314 uint8_t *signature, size_t signature_size,
3315 size_t *signature_length)
3316{
3317 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3318
3319 *signature_length = 0;
3320
Paul Elliottc9774412023-02-06 15:14:07 +00003321 /* Check that start has been called first, and that operation has not
3322 * previously errored. */
3323 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003324 status = PSA_ERROR_BAD_STATE;
3325 goto exit;
3326 }
3327
Paul Elliott096abc42023-02-03 18:33:23 +00003328 /* Immediately reject a zero-length signature buffer. This guarantees that
3329 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003330 if (signature_size == 0) {
3331 status = PSA_ERROR_BUFFER_TOO_SMALL;
3332 goto exit;
3333 }
3334
3335 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3336 signature_size,
3337 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003338
Paul Elliott296ede92022-12-15 17:00:30 +00003339 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003340 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003341
Paul Elliottebe225c2023-02-10 14:32:53 +00003342exit:
3343
Paul Elliott59200a22023-02-21 15:07:40 +00003344 psa_wipe_tag_output_buffer(signature, status, signature_size,
3345 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003346
Paul Elliott53bb3122023-02-10 14:22:22 +00003347 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003348 if (status != PSA_SUCCESS) {
3349 operation->error_occurred = 1;
3350 }
3351
Paul Elliott59ad9452022-12-18 15:09:02 +00003352 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003353 }
3354
3355 return status;
3356}
3357
3358psa_status_t psa_sign_hash_abort(
3359 psa_sign_hash_interruptible_operation_t *operation)
3360{
Paul Elliott59ad9452022-12-18 15:09:02 +00003361 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3362
3363 status = psa_sign_hash_abort_internal(operation);
3364
3365 /* We clear the number of ops done here, so that it is not cleared when
3366 * the operation fails or succeeds, only on manual abort. */
3367 operation->num_ops = 0;
3368
Paul Elliottc9774412023-02-06 15:14:07 +00003369 /* Likewise, failure state. */
3370 operation->error_occurred = 0;
3371
Paul Elliott59ad9452022-12-18 15:09:02 +00003372 return status;
3373}
3374
3375static psa_status_t psa_verify_hash_abort_internal(
3376 psa_verify_hash_interruptible_operation_t *operation)
3377{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003378 if (operation->id == 0) {
3379 /* The object has (apparently) been initialized but it is not (yet)
3380 * in use. It's ok to call abort on such an object, and there's
3381 * nothing to do. */
3382 return PSA_SUCCESS;
3383 }
3384
Paul Elliott59ad9452022-12-18 15:09:02 +00003385 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3386
3387 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003388
3389 operation->id = 0;
3390
Paul Elliotte6145dc2023-02-07 12:51:21 +00003391 /* Do not clear either the error_occurred or num_ops elements here as they
3392 * only want to be cleared by the application calling abort, not by abort
3393 * being called at completion of an operation. */
3394
Paul Elliott59ad9452022-12-18 15:09:02 +00003395 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003396}
3397
3398psa_status_t psa_verify_hash_start(
3399 psa_verify_hash_interruptible_operation_t *operation,
3400 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3401 const uint8_t *hash, size_t hash_length,
3402 const uint8_t *signature, size_t signature_length)
3403{
3404 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3405 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3406 psa_key_slot_t *slot;
3407
Paul Elliottc9774412023-02-06 15:14:07 +00003408 /* Check that start has not been previously called, or operation has not
3409 * previously errored. */
3410 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003411 return PSA_ERROR_BAD_STATE;
3412 }
3413
3414 status = psa_sign_verify_check_alg(0, alg);
3415 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003416 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003417 return status;
3418 }
3419
3420 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3421 PSA_KEY_USAGE_VERIFY_HASH,
3422 alg);
3423
3424 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003425 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003426 return status;
3427 }
3428
3429 psa_key_attributes_t attributes = {
3430 .core = slot->attr
3431 };
3432
Paul Elliott296ede92022-12-15 17:00:30 +00003433 /* Ensure ops count gets reset, in case of operation re-use. */
3434 operation->num_ops = 0;
3435
Paul Elliott9fe12f62022-11-30 19:16:02 +00003436 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3437 slot->key.data,
3438 slot->key.bytes,
3439 alg, hash, hash_length,
3440 signature, signature_length);
3441
3442 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003443 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003444 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003445 }
3446
3447 unlock_status = psa_unlock_key_slot(slot);
3448
Paul Elliottc9774412023-02-06 15:14:07 +00003449 if (unlock_status != PSA_SUCCESS) {
3450 operation->error_occurred = 1;
3451 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003452
Paul Elliottc9774412023-02-06 15:14:07 +00003453 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003454}
3455
3456psa_status_t psa_verify_hash_complete(
3457 psa_verify_hash_interruptible_operation_t *operation)
3458{
3459 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3460
Paul Elliottc9774412023-02-06 15:14:07 +00003461 /* Check that start has been called first, and that operation has not
3462 * previously errored. */
3463 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003464 status = PSA_ERROR_BAD_STATE;
3465 goto exit;
3466 }
3467
3468 status = psa_driver_wrapper_verify_hash_complete(operation);
3469
Paul Elliott296ede92022-12-15 17:00:30 +00003470 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003471 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003472 operation);
3473
Paul Elliottebe225c2023-02-10 14:32:53 +00003474exit:
3475
Paul Elliott9fe12f62022-11-30 19:16:02 +00003476 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003477 if (status != PSA_SUCCESS) {
3478 operation->error_occurred = 1;
3479 }
3480
Paul Elliott59ad9452022-12-18 15:09:02 +00003481 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003482 }
3483
3484 return status;
3485}
3486
3487psa_status_t psa_verify_hash_abort(
3488 psa_verify_hash_interruptible_operation_t *operation)
3489{
Paul Elliott59ad9452022-12-18 15:09:02 +00003490 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003491
Paul Elliott59ad9452022-12-18 15:09:02 +00003492 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003493
Paul Elliott59ad9452022-12-18 15:09:02 +00003494 /* We clear the number of ops done here, so that it is not cleared when
3495 * the operation fails or succeeds, only on manual abort. */
3496 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003497
Paul Elliottc9774412023-02-06 15:14:07 +00003498 /* Likewise, failure state. */
3499 operation->error_occurred = 0;
3500
Paul Elliott59ad9452022-12-18 15:09:02 +00003501 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003502}
3503
Paul Elliott588f8ed2022-12-02 18:10:26 +00003504/****************************************************************/
3505/* Asymmetric interruptible cryptography internal */
3506/* implementations */
3507/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003508
Paul Elliott588f8ed2022-12-02 18:10:26 +00003509void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3510{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003511
Paul Elliott588f8ed2022-12-02 18:10:26 +00003512#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3513 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3514 defined(MBEDTLS_ECP_RESTARTABLE)
3515
3516 /* Internal implementation uses zero to indicate infinite number max ops,
3517 * therefore avoid this value, and set to minimum possible. */
3518 if (max_ops == 0) {
3519 max_ops = 1;
3520 }
3521
Paul Elliott588f8ed2022-12-02 18:10:26 +00003522 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003523#else
3524 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003525#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3526 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3527 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3528}
3529
Paul Elliott588f8ed2022-12-02 18:10:26 +00003530uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003531 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003532{
3533#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3534 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3535 defined(MBEDTLS_ECP_RESTARTABLE)
3536
Paul Elliott93d9ca82023-02-15 18:14:21 +00003537 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003538#else
3539 (void) operation;
3540 return 0;
3541#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3542 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3543 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3544}
3545
3546uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003547 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003548{
3549 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3550 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3551 defined(MBEDTLS_ECP_RESTARTABLE)
3552
Paul Elliott93d9ca82023-02-15 18:14:21 +00003553 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003554#else
3555 (void) operation;
3556 return 0;
3557#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3558 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3559 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3560}
3561
3562psa_status_t mbedtls_psa_sign_hash_start(
3563 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3564 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3565 size_t key_buffer_size, psa_algorithm_t alg,
3566 const uint8_t *hash, size_t hash_length)
3567{
3568 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003569 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003570
Paul Elliott068fe072023-01-16 13:59:15 +00003571 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3572 return PSA_ERROR_NOT_SUPPORTED;
3573 }
3574
3575 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003576 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003577 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003578
3579#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003580 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3581 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003582
Paul Elliotta1c94092023-02-15 16:38:04 +00003583 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3584
Paul Elliott93d9ca82023-02-15 18:14:21 +00003585 /* Ensure num_ops is zero'ed in case of context re-use. */
3586 operation->num_ops = 0;
3587
Paul Elliott068fe072023-01-16 13:59:15 +00003588 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3589 attributes->core.bits,
3590 key_buffer,
3591 key_buffer_size,
3592 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003593
Paul Elliott068fe072023-01-16 13:59:15 +00003594 if (status != PSA_SUCCESS) {
3595 return status;
3596 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003597
Paul Elliott1bc59df2023-02-05 13:41:57 +00003598 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003599 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003600
Paul Elliott068fe072023-01-16 13:59:15 +00003601 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3602 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3603 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003604
Paul Elliott0290a762023-02-09 14:30:24 +00003605 /* We only need to store the same length of hash as the private key size
3606 * here, it would be truncated by the internal implementation anyway. */
3607 required_hash_length = (hash_length < operation->coordinate_bytes ?
3608 hash_length : operation->coordinate_bytes);
3609
Paul Elliottba70ad42023-02-15 18:23:53 +00003610 if (required_hash_length > sizeof(operation->hash)) {
3611 /* Shouldn't happen, but better safe than sorry. */
3612 return PSA_ERROR_CORRUPTION_DETECTED;
3613 }
3614
Paul Elliott0290a762023-02-09 14:30:24 +00003615 memcpy(operation->hash, hash, required_hash_length);
3616 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003617
3618 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003619
3620#else
Paul Elliott068fe072023-01-16 13:59:15 +00003621 (void) operation;
3622 (void) key_buffer;
3623 (void) key_buffer_size;
3624 (void) alg;
3625 (void) hash;
3626 (void) hash_length;
3627 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003628 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003629
Paul Elliott068fe072023-01-16 13:59:15 +00003630 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003631#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3632 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3633 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003634}
3635
3636psa_status_t mbedtls_psa_sign_hash_complete(
3637 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3638 uint8_t *signature, size_t signature_size,
3639 size_t *signature_length)
3640{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003641#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3642 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3643 defined(MBEDTLS_ECP_RESTARTABLE)
3644
Paul Elliotta1c94092023-02-15 16:38:04 +00003645 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003646 mbedtls_mpi r;
3647 mbedtls_mpi s;
3648
Paul Elliott46845252023-02-03 14:59:11 +00003649 mbedtls_mpi_init(&r);
3650 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003651
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003652 /* Ensure max_ops is set to the current value (or default). */
3653 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3654
Paul Elliotta1c94092023-02-15 16:38:04 +00003655 if (signature_size < 2 * operation->coordinate_bytes) {
3656 status = PSA_ERROR_BUFFER_TOO_SMALL;
3657 goto exit;
3658 }
3659
Paul Elliott588f8ed2022-12-02 18:10:26 +00003660 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003661
Paul Elliott588f8ed2022-12-02 18:10:26 +00003662#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3663 status = mbedtls_to_psa_error(
3664 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003665 &r,
3666 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003667 &operation->ctx->d,
3668 operation->hash,
3669 operation->hash_length,
3670 operation->md_alg,
3671 mbedtls_psa_get_random,
3672 MBEDTLS_PSA_RANDOM_STATE,
3673 &operation->restart_ctx));
3674#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003675 status = PSA_ERROR_NOT_SUPPORTED;
3676 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003677#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3678 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003679 status = mbedtls_to_psa_error(
3680 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003681 &r,
3682 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003683 &operation->ctx->d,
3684 operation->hash,
3685 operation->hash_length,
3686 mbedtls_psa_get_random,
3687 MBEDTLS_PSA_RANDOM_STATE,
3688 mbedtls_psa_get_random,
3689 MBEDTLS_PSA_RANDOM_STATE,
3690 &operation->restart_ctx));
3691 }
3692
Paul Elliottf8e5b562023-02-19 18:43:45 +00003693 /* Hide the fact that the restart context only holds a delta of number of
3694 * ops done during the last operation, not an absolute value. */
3695 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3696
Paul Elliott724bd252023-02-08 12:35:08 +00003697 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003698 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003699 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003700 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003701 operation->coordinate_bytes)
3702 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003703
3704 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003705 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003706 }
3707
3708 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003709 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003710 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003711 operation->coordinate_bytes,
3712 operation->coordinate_bytes)
3713 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003714
3715 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003716 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003717 }
3718
Paul Elliott1bc59df2023-02-05 13:41:57 +00003719 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003720
Paul Elliott724bd252023-02-08 12:35:08 +00003721 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003722 }
Paul Elliott724bd252023-02-08 12:35:08 +00003723
3724exit:
3725
3726 mbedtls_mpi_free(&r);
3727 mbedtls_mpi_free(&s);
3728 return status;
3729
Paul Elliott588f8ed2022-12-02 18:10:26 +00003730 #else
3731
3732 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003733 (void) signature;
3734 (void) signature_size;
3735 (void) signature_length;
3736
3737 return PSA_ERROR_NOT_SUPPORTED;
3738
3739#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3740 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3741 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3742}
3743
3744psa_status_t mbedtls_psa_sign_hash_abort(
3745 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3746{
3747
3748#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3749 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3750 defined(MBEDTLS_ECP_RESTARTABLE)
3751
3752 if (operation->ctx) {
3753 mbedtls_ecdsa_free(operation->ctx);
3754 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003755 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003756 }
3757
3758 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3759
Paul Elliott93d9ca82023-02-15 18:14:21 +00003760 operation->num_ops = 0;
3761
Paul Elliott588f8ed2022-12-02 18:10:26 +00003762 return PSA_SUCCESS;
3763
3764#else
3765
3766 (void) operation;
3767
3768 return PSA_ERROR_NOT_SUPPORTED;
3769
3770#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3771 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3772 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3773}
3774
3775psa_status_t mbedtls_psa_verify_hash_start(
3776 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3777 const psa_key_attributes_t *attributes,
3778 const uint8_t *key_buffer, size_t key_buffer_size,
3779 psa_algorithm_t alg,
3780 const uint8_t *hash, size_t hash_length,
3781 const uint8_t *signature, size_t signature_length)
3782{
3783 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003784 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003785 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003786
Paul Elliott068fe072023-01-16 13:59:15 +00003787 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3788 return PSA_ERROR_NOT_SUPPORTED;
3789 }
3790
3791 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003792 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003793 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003794
3795#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003796 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3797 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003798
Paul Elliotta1c94092023-02-15 16:38:04 +00003799 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3800 mbedtls_mpi_init(&operation->r);
3801 mbedtls_mpi_init(&operation->s);
3802
Paul Elliott93d9ca82023-02-15 18:14:21 +00003803 /* Ensure num_ops is zero'ed in case of context re-use. */
3804 operation->num_ops = 0;
3805
Paul Elliott068fe072023-01-16 13:59:15 +00003806 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3807 attributes->core.bits,
3808 key_buffer,
3809 key_buffer_size,
3810 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003811
Paul Elliott068fe072023-01-16 13:59:15 +00003812 if (status != PSA_SUCCESS) {
3813 return status;
3814 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003815
Paul Elliottc569fc22023-02-10 13:02:54 +00003816 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003817
Paul Elliott1bc59df2023-02-05 13:41:57 +00003818 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003819 return PSA_ERROR_INVALID_SIGNATURE;
3820 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003821
Paul Elliott068fe072023-01-16 13:59:15 +00003822 status = mbedtls_to_psa_error(
3823 mbedtls_mpi_read_binary(&operation->r,
3824 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003825 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003826
Paul Elliott068fe072023-01-16 13:59:15 +00003827 if (status != PSA_SUCCESS) {
3828 return status;
3829 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003830
Paul Elliott068fe072023-01-16 13:59:15 +00003831 status = mbedtls_to_psa_error(
3832 mbedtls_mpi_read_binary(&operation->s,
3833 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003834 coordinate_bytes,
3835 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003836
Paul Elliott068fe072023-01-16 13:59:15 +00003837 if (status != PSA_SUCCESS) {
3838 return status;
3839 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003840
Paul Elliott2c9843f2023-02-15 17:32:42 +00003841 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003842
Paul Elliott2c9843f2023-02-15 17:32:42 +00003843 if (status != PSA_SUCCESS) {
3844 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003845 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003846
Paul Elliott0290a762023-02-09 14:30:24 +00003847 /* We only need to store the same length of hash as the private key size
3848 * here, it would be truncated by the internal implementation anyway. */
3849 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3850 coordinate_bytes);
3851
Paul Elliottba70ad42023-02-15 18:23:53 +00003852 if (required_hash_length > sizeof(operation->hash)) {
3853 /* Shouldn't happen, but better safe than sorry. */
3854 return PSA_ERROR_CORRUPTION_DETECTED;
3855 }
3856
Paul Elliott0290a762023-02-09 14:30:24 +00003857 memcpy(operation->hash, hash, required_hash_length);
3858 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003859
3860 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003861#else
Paul Elliott068fe072023-01-16 13:59:15 +00003862 (void) operation;
3863 (void) key_buffer;
3864 (void) key_buffer_size;
3865 (void) alg;
3866 (void) hash;
3867 (void) hash_length;
3868 (void) signature;
3869 (void) signature_length;
3870 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003871 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003872 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003873
Paul Elliott068fe072023-01-16 13:59:15 +00003874 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003875#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3876 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3877 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003878}
3879
3880psa_status_t mbedtls_psa_verify_hash_complete(
3881 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3882{
3883
3884#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3885 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3886 defined(MBEDTLS_ECP_RESTARTABLE)
3887
Paul Elliottf8e5b562023-02-19 18:43:45 +00003888 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3889
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003890 /* Ensure max_ops is set to the current value (or default). */
3891 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3892
Paul Elliottf8e5b562023-02-19 18:43:45 +00003893 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003894 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3895 operation->hash,
3896 operation->hash_length,
3897 &operation->ctx->Q,
3898 &operation->r,
3899 &operation->s,
3900 &operation->restart_ctx));
3901
Paul Elliottf8e5b562023-02-19 18:43:45 +00003902 /* Hide the fact that the restart context only holds a delta of number of
3903 * ops done during the last operation, not an absolute value. */
3904 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3905
3906 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003907#else
3908 (void) operation;
3909
3910 return PSA_ERROR_NOT_SUPPORTED;
3911
3912#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3913 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3914 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3915}
3916
3917psa_status_t mbedtls_psa_verify_hash_abort(
3918 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3919{
3920
3921#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3922 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3923 defined(MBEDTLS_ECP_RESTARTABLE)
3924
3925 if (operation->ctx) {
3926 mbedtls_ecdsa_free(operation->ctx);
3927 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003928 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003929 }
3930
3931 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3932
Paul Elliott93d9ca82023-02-15 18:14:21 +00003933 operation->num_ops = 0;
3934
Paul Elliott588f8ed2022-12-02 18:10:26 +00003935 mbedtls_mpi_free(&operation->r);
3936 mbedtls_mpi_free(&operation->s);
3937
3938 return PSA_SUCCESS;
3939
3940#else
3941 (void) operation;
3942
3943 return PSA_ERROR_NOT_SUPPORTED;
3944
3945#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3946 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3947 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3948}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003949
mohammad1603503973b2018-03-12 15:59:30 +02003950/****************************************************************/
3951/* Symmetric cryptography */
3952/****************************************************************/
3953
Gilles Peskine449bd832023-01-11 14:50:10 +01003954static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3955 mbedtls_svc_key_id_t key,
3956 psa_algorithm_t alg,
3957 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003958{
3959 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3960 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003961 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003962 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3963 PSA_KEY_USAGE_ENCRYPT :
3964 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003965
3966 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003967 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003968 status = PSA_ERROR_BAD_STATE;
3969 goto exit;
3970 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003971
Gilles Peskine449bd832023-01-11 14:50:10 +01003972 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003973 status = PSA_ERROR_INVALID_ARGUMENT;
3974 goto exit;
3975 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003976
Gilles Peskine449bd832023-01-11 14:50:10 +01003977 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3978 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003979 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003980 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003981
Ronald Cron49fafa92021-03-10 08:34:23 +01003982 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003983 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003984 * so we only set it (in the driver wrapper) after resources have been
3985 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003986 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003987 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003988 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003989 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003990 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003991 }
3992 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003993
Ronald Crona4af55f2020-12-14 14:36:06 +01003994 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003995 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003996 };
3997
Ronald Cronab99ac22020-10-01 16:38:28 +02003998 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003999 if (cipher_operation == MBEDTLS_ENCRYPT) {
4000 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4001 &attributes,
4002 slot->key.data,
4003 slot->key.bytes,
4004 alg);
4005 } else {
4006 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4007 &attributes,
4008 slot->key.data,
4009 slot->key.bytes,
4010 alg);
4011 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004012
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004013exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004014 if (status != PSA_SUCCESS) {
4015 psa_cipher_abort(operation);
4016 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004017
Gilles Peskine449bd832023-01-11 14:50:10 +01004018 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004019
Gilles Peskine449bd832023-01-11 14:50:10 +01004020 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004021}
4022
Gilles Peskine449bd832023-01-11 14:50:10 +01004023psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4024 mbedtls_svc_key_id_t key,
4025 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004026{
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004028}
4029
Gilles Peskine449bd832023-01-11 14:50:10 +01004030psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4031 mbedtls_svc_key_id_t key,
4032 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004033{
Gilles Peskine449bd832023-01-11 14:50:10 +01004034 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004035}
4036
Gilles Peskine449bd832023-01-11 14:50:10 +01004037psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4038 uint8_t *iv,
4039 size_t iv_size,
4040 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004041{
Ronald Cron6d051732020-10-01 14:10:20 +02004042 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004043 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4044 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004045
Gilles Peskine449bd832023-01-11 14:50:10 +01004046 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004047 status = PSA_ERROR_BAD_STATE;
4048 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004049 }
4050
Gilles Peskine449bd832023-01-11 14:50:10 +01004051 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004052 status = PSA_ERROR_BAD_STATE;
4053 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004054 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004055
Ronald Cron2fb90522021-07-05 12:31:44 +02004056 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004058 status = PSA_ERROR_BUFFER_TOO_SMALL;
4059 goto exit;
4060 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004061
Gilles Peskine449bd832023-01-11 14:50:10 +01004062 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004063 status = PSA_ERROR_GENERIC_ERROR;
4064 goto exit;
4065 }
4066
Gilles Peskine449bd832023-01-11 14:50:10 +01004067 status = psa_generate_random(local_iv, default_iv_length);
4068 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004069 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004070 }
Ronald Cron5618a392021-03-26 09:52:26 +01004071
Gilles Peskine449bd832023-01-11 14:50:10 +01004072 status = psa_driver_wrapper_cipher_set_iv(operation,
4073 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004074
4075exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 if (status == PSA_SUCCESS) {
4077 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004078 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004079 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004080 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004081 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004082 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004083 }
Ronald Cron6d051732020-10-01 14:10:20 +02004084
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004086}
4087
Gilles Peskine449bd832023-01-11 14:50:10 +01004088psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4089 const uint8_t *iv,
4090 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004091{
Ronald Cron6d051732020-10-01 14:10:20 +02004092 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004093
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004095 status = PSA_ERROR_BAD_STATE;
4096 goto exit;
4097 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004098
Gilles Peskine449bd832023-01-11 14:50:10 +01004099 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004100 status = PSA_ERROR_BAD_STATE;
4101 goto exit;
4102 }
Ronald Crona0d68172021-03-26 10:15:08 +01004103
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004105 status = PSA_ERROR_INVALID_ARGUMENT;
4106 goto exit;
4107 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004108
Gilles Peskine449bd832023-01-11 14:50:10 +01004109 status = psa_driver_wrapper_cipher_set_iv(operation,
4110 iv,
4111 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004112
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004113exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004115 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004116 } else {
4117 psa_cipher_abort(operation);
4118 }
4119 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004120}
4121
Gilles Peskine449bd832023-01-11 14:50:10 +01004122psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4123 const uint8_t *input,
4124 size_t input_length,
4125 uint8_t *output,
4126 size_t output_size,
4127 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004128{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004129 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004130
Gilles Peskine449bd832023-01-11 14:50:10 +01004131 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004132 status = PSA_ERROR_BAD_STATE;
4133 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004134 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004135
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004137 status = PSA_ERROR_BAD_STATE;
4138 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004139 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004140
Gilles Peskine449bd832023-01-11 14:50:10 +01004141 status = psa_driver_wrapper_cipher_update(operation,
4142 input,
4143 input_length,
4144 output,
4145 output_size,
4146 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004147
4148exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004149 if (status != PSA_SUCCESS) {
4150 psa_cipher_abort(operation);
4151 }
Ronald Cron6d051732020-10-01 14:10:20 +02004152
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004154}
4155
Gilles Peskine449bd832023-01-11 14:50:10 +01004156psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4157 uint8_t *output,
4158 size_t output_size,
4159 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004160{
David Saadab4ecc272019-02-14 13:48:10 +02004161 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004162
Gilles Peskine449bd832023-01-11 14:50:10 +01004163 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004164 status = PSA_ERROR_BAD_STATE;
4165 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004166 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004167
Gilles Peskine449bd832023-01-11 14:50:10 +01004168 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004169 status = PSA_ERROR_BAD_STATE;
4170 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004171 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004172
Gilles Peskine449bd832023-01-11 14:50:10 +01004173 status = psa_driver_wrapper_cipher_finish(operation,
4174 output,
4175 output_size,
4176 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004177
4178exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004179 if (status == PSA_SUCCESS) {
4180 return psa_cipher_abort(operation);
4181 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004182 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004184
Gilles Peskine449bd832023-01-11 14:50:10 +01004185 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004186 }
mohammad1603503973b2018-03-12 15:59:30 +02004187}
4188
Gilles Peskine449bd832023-01-11 14:50:10 +01004189psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004190{
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004192 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004193 * in use. It's ok to call abort on such an object, and there's
4194 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004195 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004196 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004197
Gilles Peskine449bd832023-01-11 14:50:10 +01004198 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004199
Ronald Cron49fafa92021-03-10 08:34:23 +01004200 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004201 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004202 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004203
Gilles Peskine449bd832023-01-11 14:50:10 +01004204 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004205}
4206
Gilles Peskine449bd832023-01-11 14:50:10 +01004207psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4208 psa_algorithm_t alg,
4209 const uint8_t *input,
4210 size_t input_length,
4211 uint8_t *output,
4212 size_t output_size,
4213 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004214{
4215 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004216 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004217 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004218 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4219 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004220
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004222 status = PSA_ERROR_INVALID_ARGUMENT;
4223 goto exit;
4224 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004225
Gilles Peskine449bd832023-01-11 14:50:10 +01004226 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4227 PSA_KEY_USAGE_ENCRYPT,
4228 alg);
4229 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004230 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004232
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004233 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004235 };
4236
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4238 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004239 status = PSA_ERROR_GENERIC_ERROR;
4240 goto exit;
4241 }
4242
Gilles Peskine449bd832023-01-11 14:50:10 +01004243 if (default_iv_length > 0) {
4244 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004245 status = PSA_ERROR_BUFFER_TOO_SMALL;
4246 goto exit;
4247 }
4248
Gilles Peskine449bd832023-01-11 14:50:10 +01004249 status = psa_generate_random(local_iv, default_iv_length);
4250 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004251 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004252 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004253 }
4254
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004255 status = psa_driver_wrapper_cipher_encrypt(
4256 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004257 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004258 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004259 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004260
4261exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004262 unlock_status = psa_unlock_key_slot(slot);
4263 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004264 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004265 }
Ronald Cron23919522021-07-08 19:00:07 +02004266
Gilles Peskine449bd832023-01-11 14:50:10 +01004267 if (status == PSA_SUCCESS) {
4268 if (default_iv_length > 0) {
4269 memcpy(output, local_iv, default_iv_length);
4270 }
4271 *output_length += default_iv_length;
4272 } else {
4273 *output_length = 0;
4274 }
4275
4276 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004277}
4278
Gilles Peskine449bd832023-01-11 14:50:10 +01004279psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4280 psa_algorithm_t alg,
4281 const uint8_t *input,
4282 size_t input_length,
4283 uint8_t *output,
4284 size_t output_size,
4285 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004286{
4287 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004288 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004289 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004290
Gilles Peskine449bd832023-01-11 14:50:10 +01004291 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004292 status = PSA_ERROR_INVALID_ARGUMENT;
4293 goto exit;
4294 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004295
Gilles Peskine449bd832023-01-11 14:50:10 +01004296 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4297 PSA_KEY_USAGE_DECRYPT,
4298 alg);
4299 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004300 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004301 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004302
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004303 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004304 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004305 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004306
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4308 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004309 status = PSA_ERROR_INVALID_ARGUMENT;
4310 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004311 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004312 status = PSA_ERROR_INVALID_ARGUMENT;
4313 goto exit;
4314 }
4315
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004316 status = psa_driver_wrapper_cipher_decrypt(
4317 &attributes, slot->key.data, slot->key.bytes,
4318 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004319 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004320
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004321exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 unlock_status = psa_unlock_key_slot(slot);
4323 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004324 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004325 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004326
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004328 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004329 }
Ronald Cron23919522021-07-08 19:00:07 +02004330
Gilles Peskine449bd832023-01-11 14:50:10 +01004331 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004332}
4333
4334
mohammad16035955c982018-04-26 00:53:03 +03004335/****************************************************************/
4336/* AEAD */
4337/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004338
Paul Elliottbaff51c2021-09-28 17:44:45 +01004339/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004340static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004341{
Gilles Peskine449bd832023-01-11 14:50:10 +01004342 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004343}
4344
4345/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004346static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4347 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004348{
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004350
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004352#if defined(PSA_WANT_ALG_GCM)
4353 case PSA_ALG_GCM:
4354 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004355 * arbitrarily large nonces. Please note that we do not generally
4356 * recommend the usage of nonces of greater length than
4357 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4358 * size, which can then lead to collisions if you encrypt a very
4359 * large number of messages.*/
4360 if (nonce_length != 0) {
4361 return PSA_SUCCESS;
4362 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004363 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004364#endif /* PSA_WANT_ALG_GCM */
4365#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004366 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 if (nonce_length >= 7 && nonce_length <= 13) {
4368 return PSA_SUCCESS;
4369 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004370 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004371#endif /* PSA_WANT_ALG_CCM */
4372#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004373 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 if (nonce_length == 12) {
4375 return PSA_SUCCESS;
4376 } else if (nonce_length == 8) {
4377 return PSA_ERROR_NOT_SUPPORTED;
4378 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004379 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004380#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004381 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004382 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004383 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004384 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004385
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004387}
4388
Gilles Peskine449bd832023-01-11 14:50:10 +01004389static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004390{
Gilles Peskine449bd832023-01-11 14:50:10 +01004391 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4392 return PSA_ERROR_INVALID_ARGUMENT;
4393 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004394
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004396}
4397
Gilles Peskine449bd832023-01-11 14:50:10 +01004398psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4399 psa_algorithm_t alg,
4400 const uint8_t *nonce,
4401 size_t nonce_length,
4402 const uint8_t *additional_data,
4403 size_t additional_data_length,
4404 const uint8_t *plaintext,
4405 size_t plaintext_length,
4406 uint8_t *ciphertext,
4407 size_t ciphertext_size,
4408 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004409{
Ronald Cron215633c2021-03-16 17:15:37 +01004410 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4411 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004412
mohammad1603f08a5502018-06-03 15:05:47 +03004413 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004414
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 status = psa_aead_check_algorithm(alg);
4416 if (status != PSA_SUCCESS) {
4417 return status;
4418 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004419
Ronald Cron9a986162021-03-26 12:40:07 +01004420 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004421 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4422 if (status != PSA_SUCCESS) {
4423 return status;
4424 }
mohammad16035955c982018-04-26 00:53:03 +03004425
Ronald Cron215633c2021-03-16 17:15:37 +01004426 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004428 };
mohammad16035955c982018-04-26 00:53:03 +03004429
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 status = psa_aead_check_nonce_length(alg, nonce_length);
4431 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004432 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004434
Ronald Cronde822812021-03-17 16:08:20 +01004435 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004436 &attributes, slot->key.data, slot->key.bytes,
4437 alg,
4438 nonce, nonce_length,
4439 additional_data, additional_data_length,
4440 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004442
Gilles Peskine449bd832023-01-11 14:50:10 +01004443 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4444 memset(ciphertext, 0, ciphertext_size);
4445 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004446
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004447exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004449
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004451}
4452
Gilles Peskine449bd832023-01-11 14:50:10 +01004453psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4454 psa_algorithm_t alg,
4455 const uint8_t *nonce,
4456 size_t nonce_length,
4457 const uint8_t *additional_data,
4458 size_t additional_data_length,
4459 const uint8_t *ciphertext,
4460 size_t ciphertext_length,
4461 uint8_t *plaintext,
4462 size_t plaintext_size,
4463 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004464{
Ronald Cron215633c2021-03-16 17:15:37 +01004465 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4466 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004467
Gilles Peskineee652a32018-06-01 19:23:52 +02004468 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004469
Gilles Peskine449bd832023-01-11 14:50:10 +01004470 status = psa_aead_check_algorithm(alg);
4471 if (status != PSA_SUCCESS) {
4472 return status;
4473 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004474
Ronald Cron9a986162021-03-26 12:40:07 +01004475 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004476 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4477 if (status != PSA_SUCCESS) {
4478 return status;
4479 }
mohammad16035955c982018-04-26 00:53:03 +03004480
Ronald Cron215633c2021-03-16 17:15:37 +01004481 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004482 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004483 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004484
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 status = psa_aead_check_nonce_length(alg, nonce_length);
4486 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004487 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004489
Ronald Cronde822812021-03-17 16:08:20 +01004490 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004491 &attributes, slot->key.data, slot->key.bytes,
4492 alg,
4493 nonce, nonce_length,
4494 additional_data, additional_data_length,
4495 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004496 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004497
Gilles Peskine449bd832023-01-11 14:50:10 +01004498 if (status != PSA_SUCCESS && plaintext_size != 0) {
4499 memset(plaintext, 0, plaintext_size);
4500 }
mohammad160360a64d02018-06-03 17:20:42 +03004501
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004502exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004504
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 return status;
mohammad16035955c982018-04-26 00:53:03 +03004506}
4507
Gilles Peskine449bd832023-01-11 14:50:10 +01004508static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4509{
4510 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004511
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004513#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004515 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4517 return PSA_ERROR_INVALID_ARGUMENT;
4518 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004519 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004520#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004521
Przemek Stekiel86679c72022-10-06 17:06:56 +02004522#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004524 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004525 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4526 return PSA_ERROR_INVALID_ARGUMENT;
4527 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004528 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004529#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004530
Przemek Stekiel86679c72022-10-06 17:06:56 +02004531#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004533 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 if (tag_len != 16) {
4535 return PSA_ERROR_INVALID_ARGUMENT;
4536 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004537 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004538#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004539
4540 default:
4541 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004543 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004544 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004545}
4546
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004547/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004548static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4549 int is_encrypt,
4550 mbedtls_svc_key_id_t key,
4551 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004552{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004553 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4554 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4555 psa_key_slot_t *slot = NULL;
4556 psa_key_usage_t key_usage = 0;
4557
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 status = psa_aead_check_algorithm(alg);
4559 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004560 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004561 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004562
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004564 status = PSA_ERROR_BAD_STATE;
4565 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004566 }
4567
Gilles Peskine449bd832023-01-11 14:50:10 +01004568 if (operation->nonce_set || operation->lengths_set ||
4569 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004570 status = PSA_ERROR_BAD_STATE;
4571 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004572 }
4573
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004575 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004577 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004578 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004579
Gilles Peskine449bd832023-01-11 14:50:10 +01004580 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4581 alg);
4582 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004583 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004585
4586 psa_key_attributes_t attributes = {
4587 .core = slot->attr
4588 };
4589
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004591 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004593
Gilles Peskine449bd832023-01-11 14:50:10 +01004594 if (is_encrypt) {
4595 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4596 &attributes,
4597 slot->key.data,
4598 slot->key.bytes,
4599 alg);
4600 } else {
4601 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4602 &attributes,
4603 slot->key.data,
4604 slot->key.bytes,
4605 alg);
4606 }
4607 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004608 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004609 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004610
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004612
4613exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004614 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004615
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004617 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004619 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 } else {
4621 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004622 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004623
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004625}
4626
Paul Elliott302ff6b2021-04-20 18:10:30 +01004627/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004628psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4629 mbedtls_svc_key_id_t key,
4630 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004631{
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004633}
4634
4635/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004636psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4637 mbedtls_svc_key_id_t key,
4638 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004639{
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004641}
4642
4643/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004644psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4645 uint8_t *nonce,
4646 size_t nonce_size,
4647 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004648{
4649 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004650 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004651 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004652
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004653 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004654
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004656 status = PSA_ERROR_BAD_STATE;
4657 goto exit;
4658 }
4659
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004661 status = PSA_ERROR_BAD_STATE;
4662 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004663 }
4664
Paul Elliotte193ea82021-10-01 13:00:16 +01004665 /* For CCM, this size may not be correct according to the PSA
4666 * specification. The PSA Crypto 1.0.1 specification states:
4667 *
4668 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4669 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4670 *
4671 * However this restriction that L has to be the smallest integer is not
4672 * applied in practice, and it is not implementable here since the
4673 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004674 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4675 operation->alg);
4676 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004677 status = PSA_ERROR_BUFFER_TOO_SMALL;
4678 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004679 }
4680
Gilles Peskine449bd832023-01-11 14:50:10 +01004681 status = psa_generate_random(local_nonce, required_nonce_size);
4682 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004683 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004685
Gilles Peskine449bd832023-01-11 14:50:10 +01004686 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004687
Paul Elliott39dc6b82021-05-11 19:16:09 +01004688exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 if (status == PSA_SUCCESS) {
4690 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004691 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 } else {
4693 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004694 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004695
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004697}
4698
4699/* Set the nonce for a multipart authenticated encryption or decryption
4700 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004701psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4702 const uint8_t *nonce,
4703 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004704{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004705 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4706
Gilles Peskine449bd832023-01-11 14:50:10 +01004707 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004708 status = PSA_ERROR_BAD_STATE;
4709 goto exit;
4710 }
4711
Gilles Peskine449bd832023-01-11 14:50:10 +01004712 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004713 status = PSA_ERROR_BAD_STATE;
4714 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004715 }
4716
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4718 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004719 status = PSA_ERROR_INVALID_ARGUMENT;
4720 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004721 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004722
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4724 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004725
Paul Elliott39dc6b82021-05-11 19:16:09 +01004726exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004728 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 } else {
4730 psa_aead_abort(operation);
4731 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004732
Gilles Peskine449bd832023-01-11 14:50:10 +01004733 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004734}
4735
4736/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004737psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4738 size_t ad_length,
4739 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004740{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004741 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4742
Gilles Peskine449bd832023-01-11 14:50:10 +01004743 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004744 status = PSA_ERROR_BAD_STATE;
4745 goto exit;
4746 }
4747
Gilles Peskine449bd832023-01-11 14:50:10 +01004748 if (operation->lengths_set || operation->ad_started ||
4749 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004750 status = PSA_ERROR_BAD_STATE;
4751 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004752 }
4753
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004755#if defined(PSA_WANT_ALG_GCM)
4756 case PSA_ALG_GCM:
4757 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 * bits. Without the guard this code will generate warnings on 32bit
4759 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004760#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 if (((uint64_t) ad_length) >> 61 != 0 ||
4762 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004763 status = PSA_ERROR_INVALID_ARGUMENT;
4764 goto exit;
4765 }
Paul Elliott325d3742021-09-27 17:56:28 +01004766#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004767 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004768#endif /* PSA_WANT_ALG_GCM */
4769#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004770 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004772 status = PSA_ERROR_INVALID_ARGUMENT;
4773 goto exit;
4774 }
4775 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004776#endif /* PSA_WANT_ALG_CCM */
4777#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004778 case PSA_ALG_CHACHA20_POLY1305:
4779 /* No length restrictions for ChaChaPoly. */
4780 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004781#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004782 default:
4783 break;
4784 }
Paul Elliott325d3742021-09-27 17:56:28 +01004785
Gilles Peskine449bd832023-01-11 14:50:10 +01004786 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4787 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004788
Paul Elliott39dc6b82021-05-11 19:16:09 +01004789exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004790 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004791 operation->ad_remaining = ad_length;
4792 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004793 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004794 } else {
4795 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004796 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004797
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004799}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004800
4801/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004802psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4803 const uint8_t *input,
4804 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004805{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004806 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4807
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004809 status = PSA_ERROR_BAD_STATE;
4810 goto exit;
4811 }
4812
Gilles Peskine449bd832023-01-11 14:50:10 +01004813 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004814 status = PSA_ERROR_BAD_STATE;
4815 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004816 }
4817
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 if (operation->lengths_set) {
4819 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004820 status = PSA_ERROR_INVALID_ARGUMENT;
4821 goto exit;
4822 }
4823
4824 operation->ad_remaining -= input_length;
4825 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004826#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004828 status = PSA_ERROR_BAD_STATE;
4829 goto exit;
4830 }
4831#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004832
Gilles Peskine449bd832023-01-11 14:50:10 +01004833 status = psa_driver_wrapper_aead_update_ad(operation, input,
4834 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004835
Paul Elliott39dc6b82021-05-11 19:16:09 +01004836exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004837 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004838 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 } else {
4840 psa_aead_abort(operation);
4841 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004842
Gilles Peskine449bd832023-01-11 14:50:10 +01004843 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004844}
4845
4846/* Encrypt or decrypt a message fragment in an active multipart AEAD
4847 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004848psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4849 const uint8_t *input,
4850 size_t input_length,
4851 uint8_t *output,
4852 size_t output_size,
4853 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004854{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004855 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004856
4857 *output_length = 0;
4858
Gilles Peskine449bd832023-01-11 14:50:10 +01004859 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004860 status = PSA_ERROR_BAD_STATE;
4861 goto exit;
4862 }
4863
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004865 status = PSA_ERROR_BAD_STATE;
4866 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004867 }
4868
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004870 /* Additional data length was supplied, but not all the additional
4871 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004873 status = PSA_ERROR_INVALID_ARGUMENT;
4874 goto exit;
4875 }
4876
4877 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004878 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004879 status = PSA_ERROR_INVALID_ARGUMENT;
4880 goto exit;
4881 }
4882
4883 operation->body_remaining -= input_length;
4884 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004885#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004887 status = PSA_ERROR_BAD_STATE;
4888 goto exit;
4889 }
4890#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004891
Gilles Peskine449bd832023-01-11 14:50:10 +01004892 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4893 output, output_size,
4894 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004895
Paul Elliott39dc6b82021-05-11 19:16:09 +01004896exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004897 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004898 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004899 } else {
4900 psa_aead_abort(operation);
4901 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004902
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004904}
4905
Gilles Peskine449bd832023-01-11 14:50:10 +01004906static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004907{
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 if (operation->id == 0 || !operation->nonce_set) {
4909 return PSA_ERROR_BAD_STATE;
4910 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004911
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4913 operation->body_remaining != 0)) {
4914 return PSA_ERROR_INVALID_ARGUMENT;
4915 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004916
Gilles Peskine449bd832023-01-11 14:50:10 +01004917 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004918}
4919
Paul Elliott302ff6b2021-04-20 18:10:30 +01004920/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004921psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4922 uint8_t *ciphertext,
4923 size_t ciphertext_size,
4924 size_t *ciphertext_length,
4925 uint8_t *tag,
4926 size_t tag_size,
4927 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004928{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004929 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4930
Paul Elliott302ff6b2021-04-20 18:10:30 +01004931 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004932 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004933
Gilles Peskine449bd832023-01-11 14:50:10 +01004934 status = psa_aead_final_checks(operation);
4935 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004936 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004937 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004938
Gilles Peskine449bd832023-01-11 14:50:10 +01004939 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004940 status = PSA_ERROR_BAD_STATE;
4941 goto exit;
4942 }
4943
Gilles Peskine449bd832023-01-11 14:50:10 +01004944 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4945 ciphertext_size,
4946 ciphertext_length,
4947 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004948
Paul Elliott39dc6b82021-05-11 19:16:09 +01004949exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004950
4951
Paul Elliottf47b0952021-05-21 18:02:33 +01004952 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004953 * the zero tag size, make sure the tag is set to something implausible.
4954 * Even if the operation succeeds, make sure we clear the rest of the
4955 * buffer to prevent potential leakage of anything previously placed in
4956 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004957 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004958
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004960
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004962}
4963
4964/* Finish authenticating and decrypting a message in a multipart AEAD
4965 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004966psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4967 uint8_t *plaintext,
4968 size_t plaintext_size,
4969 size_t *plaintext_length,
4970 const uint8_t *tag,
4971 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004972{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004973 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4974
Paul Elliott302ff6b2021-04-20 18:10:30 +01004975 *plaintext_length = 0;
4976
Gilles Peskine449bd832023-01-11 14:50:10 +01004977 status = psa_aead_final_checks(operation);
4978 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004979 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004980 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004981
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004983 status = PSA_ERROR_BAD_STATE;
4984 goto exit;
4985 }
4986
Gilles Peskine449bd832023-01-11 14:50:10 +01004987 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4988 plaintext_size,
4989 plaintext_length,
4990 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004991
4992exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004993 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004994
Gilles Peskine449bd832023-01-11 14:50:10 +01004995 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004996}
4997
4998/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004999psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005000{
5001 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5002
Gilles Peskine449bd832023-01-11 14:50:10 +01005003 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005004 /* The object has (apparently) been initialized but it is not (yet)
5005 * in use. It's ok to call abort on such an object, and there's
5006 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005007 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005008 }
5009
Gilles Peskine449bd832023-01-11 14:50:10 +01005010 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005011
Gilles Peskine449bd832023-01-11 14:50:10 +01005012 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005013
Gilles Peskine449bd832023-01-11 14:50:10 +01005014 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005015}
5016
Gilles Peskinee59236f2018-01-27 23:32:46 +01005017/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005018/* Generators */
5019/****************************************************************/
5020
Przemek Stekiel69c46792022-06-10 12:59:51 +02005021#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005022 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005023 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
5024 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08005025#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005026#endif /* At least one builtin KDF */
5027
Przemek Stekiel69c46792022-06-10 12:59:51 +02005028#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005029 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5030 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5031static psa_status_t psa_key_derivation_start_hmac(
5032 psa_mac_operation_t *operation,
5033 psa_algorithm_t hash_alg,
5034 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005036{
5037 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5038 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005039 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5040 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5041 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005042
Steven Cooreman72f736a2021-05-07 14:14:37 +02005043 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005044 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005045
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 status = psa_driver_wrapper_mac_sign_setup(operation,
5047 &attributes,
5048 hmac_key, hmac_key_length,
5049 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005050
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 psa_reset_key_attributes(&attributes);
5052 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005053}
5054#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005055
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005056#define HKDF_STATE_INIT 0 /* no input yet */
5057#define HKDF_STATE_STARTED 1 /* got salt */
5058#define HKDF_STATE_KEYED 2 /* got key */
5059#define HKDF_STATE_OUTPUT 3 /* output started */
5060
Gilles Peskinecbe66502019-05-16 16:59:18 +02005061static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005062 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005063{
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5065 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5066 } else {
5067 return operation->alg;
5068 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005069}
5070
Gilles Peskine449bd832023-01-11 14:50:10 +01005071psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005072{
5073 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5075 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005076 /* The object has (apparently) been initialized but it is not
5077 * in use. It's ok to call abort on such an object, and there's
5078 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005079 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005080#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5082 mbedtls_free(operation->ctx.hkdf.info);
5083 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5084 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005085#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005086#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5087 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5089 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5090 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5091 if (operation->ctx.tls12_prf.secret != NULL) {
5092 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5093 operation->ctx.tls12_prf.secret_length);
5094 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005095 }
5096
Gilles Peskine449bd832023-01-11 14:50:10 +01005097 if (operation->ctx.tls12_prf.seed != NULL) {
5098 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5099 operation->ctx.tls12_prf.seed_length);
5100 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005101 }
5102
Gilles Peskine449bd832023-01-11 14:50:10 +01005103 if (operation->ctx.tls12_prf.label != NULL) {
5104 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5105 operation->ctx.tls12_prf.label_length);
5106 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005107 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005108#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 if (operation->ctx.tls12_prf.other_secret != NULL) {
5110 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5111 operation->ctx.tls12_prf.other_secret_length);
5112 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005113 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005114#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005115 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005116
5117 /* We leave the fields Ai and output_block to be erased safely by the
5118 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005120#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5121 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005122#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005123 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5124 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5125 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5126 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005127#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005128 {
5129 status = PSA_ERROR_BAD_STATE;
5130 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 mbedtls_platform_zeroize(operation, sizeof(*operation));
5132 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005133}
5134
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005135psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005137{
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005139 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005140 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005141 }
5142
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005143 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005145}
5146
Gilles Peskine449bd832023-01-11 14:50:10 +01005147psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5148 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005149{
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 if (operation->alg == 0) {
5151 return PSA_ERROR_BAD_STATE;
5152 }
5153 if (capacity > operation->capacity) {
5154 return PSA_ERROR_INVALID_ARGUMENT;
5155 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005156 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005158}
5159
Przemek Stekiel69c46792022-06-10 12:59:51 +02005160#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005161/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005162static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5163 psa_algorithm_t kdf_alg,
5164 uint8_t *output,
5165 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005166{
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5168 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005169 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005170 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005171#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005173#else
5174 const uint8_t last_block = 0xff;
5175#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005176
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 if (hkdf->state < HKDF_STATE_KEYED ||
5178 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005179#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005181#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005182 )) {
5183 return PSA_ERROR_BAD_STATE;
5184 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005185 hkdf->state = HKDF_STATE_OUTPUT;
5186
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005188 /* Copy what remains of the current block */
5189 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005190 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005191 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 }
5193 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005194 output += n;
5195 output_length -= n;
5196 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005197 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005198 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005199 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005200 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005201 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005202 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005203 * object was corrupted or if this function is called directly
5204 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005205 if (hkdf->block_number == last_block) {
5206 return PSA_ERROR_BAD_STATE;
5207 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005208
5209 /* We need a new block */
5210 ++hkdf->block_number;
5211 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005212
Gilles Peskine449bd832023-01-11 14:50:10 +01005213 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5214 hash_alg,
5215 hkdf->prk,
5216 hash_length);
5217 if (status != PSA_SUCCESS) {
5218 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005219 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005220
5221 if (hkdf->block_number != 1) {
5222 status = psa_mac_update(&hkdf->hmac,
5223 hkdf->output_block,
5224 hash_length);
5225 if (status != PSA_SUCCESS) {
5226 return status;
5227 }
5228 }
5229 status = psa_mac_update(&hkdf->hmac,
5230 hkdf->info,
5231 hkdf->info_length);
5232 if (status != PSA_SUCCESS) {
5233 return status;
5234 }
5235 status = psa_mac_update(&hkdf->hmac,
5236 &hkdf->block_number, 1);
5237 if (status != PSA_SUCCESS) {
5238 return status;
5239 }
5240 status = psa_mac_sign_finish(&hkdf->hmac,
5241 hkdf->output_block,
5242 sizeof(hkdf->output_block),
5243 &hmac_output_length);
5244 if (status != PSA_SUCCESS) {
5245 return status;
5246 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005247 }
5248
Gilles Peskine449bd832023-01-11 14:50:10 +01005249 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005250}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005251#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005252
John Durkop07cc04a2020-11-16 22:08:34 -08005253#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5254 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005255static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5256 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005258{
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5260 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005261 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5262 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005263 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005264
Janos Follath7742fee2019-06-17 12:58:10 +01005265 /* We can't be wanting more output after block 0xff, otherwise
5266 * the capacity check in psa_key_derivation_output_bytes() would have
5267 * prevented this call. It could happen only if the operation
5268 * object was corrupted or if this function is called directly
5269 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 if (tls12_prf->block_number == 0xff) {
5271 return PSA_ERROR_CORRUPTION_DETECTED;
5272 }
Janos Follath7742fee2019-06-17 12:58:10 +01005273
5274 /* We need a new block */
5275 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005276 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005277
5278 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5279 *
5280 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5281 *
5282 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5283 * HMAC_hash(secret, A(2) + seed) +
5284 * HMAC_hash(secret, A(3) + seed) + ...
5285 *
5286 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005287 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005288 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005289 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005290 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005291 * is currently extracted as `output_block` and where i is
5292 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005293 */
5294
Gilles Peskine449bd832023-01-11 14:50:10 +01005295 status = psa_key_derivation_start_hmac(&hmac,
5296 hash_alg,
5297 tls12_prf->secret,
5298 tls12_prf->secret_length);
5299 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005300 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005302
5303 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005305 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5306 * the variable seed and in this instance means it in the context of the
5307 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 status = psa_mac_update(&hmac,
5309 tls12_prf->label,
5310 tls12_prf->label_length);
5311 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005312 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 }
5314 status = psa_mac_update(&hmac,
5315 tls12_prf->seed,
5316 tls12_prf->seed_length);
5317 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005318 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 }
5320 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005321 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5323 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005324 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005326 }
5327
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 status = psa_mac_sign_finish(&hmac,
5329 tls12_prf->Ai, hash_length,
5330 &hmac_output_length);
5331 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005332 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005333 }
5334 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005335 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005337
5338 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 status = psa_key_derivation_start_hmac(&hmac,
5340 hash_alg,
5341 tls12_prf->secret,
5342 tls12_prf->secret_length);
5343 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005344 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 }
5346 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5347 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005348 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005349 }
5350 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5351 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005352 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 }
5354 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5355 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005356 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005357 }
5358 status = psa_mac_sign_finish(&hmac,
5359 tls12_prf->output_block, hash_length,
5360 &hmac_output_length);
5361 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005362 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005364
Janos Follath7742fee2019-06-17 12:58:10 +01005365
5366cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 cleanup_status = psa_mac_abort(&hmac);
5368 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005369 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005370 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005371
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005373}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005374
Janos Follath844eb0e2019-06-19 12:10:49 +01005375static psa_status_t psa_key_derivation_tls12_prf_read(
5376 psa_tls12_prf_key_derivation_t *tls12_prf,
5377 psa_algorithm_t alg,
5378 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005380{
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5382 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005383 psa_status_t status;
5384 uint8_t offset, length;
5385
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005387 case PSA_TLS12_PRF_STATE_LABEL_SET:
5388 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5389 break;
5390 case PSA_TLS12_PRF_STATE_OUTPUT:
5391 break;
5392 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005394 }
5395
Gilles Peskine449bd832023-01-11 14:50:10 +01005396 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005397 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005398 if (tls12_prf->left_in_block == 0) {
5399 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5400 alg);
5401 if (status != PSA_SUCCESS) {
5402 return status;
5403 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005404
5405 continue;
5406 }
5407
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005409 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005410 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005411 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005413
5414 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005416 output += length;
5417 output_length -= length;
5418 tls12_prf->left_in_block -= length;
5419 }
5420
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005422}
John Durkop07cc04a2020-11-16 22:08:34 -08005423#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5424 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005425
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005426#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5427static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5428 psa_tls12_ecjpake_to_pms_t *ecjpake,
5429 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005430 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005431{
5432 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005433 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005434
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 if (output_length != 32) {
5436 return PSA_ERROR_INVALID_ARGUMENT;
5437 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005438
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5440 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5441 &output_size);
5442 if (status != PSA_SUCCESS) {
5443 return status;
5444 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005445
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 if (output_size != output_length) {
5447 return PSA_ERROR_GENERIC_ERROR;
5448 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005449
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005451}
5452#endif
5453
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005454psa_status_t psa_key_derivation_output_bytes(
5455 psa_key_derivation_operation_t *operation,
5456 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005458{
5459 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005461
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005463 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005465 }
5466
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005468 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005469 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005470 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005471 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005472 goto exit;
5473 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005475 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005476 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005477 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5478 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005479 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005480 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005482 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005483 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005484
Przemek Stekiel69c46792022-06-10 12:59:51 +02005485#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5487 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5488 output, output_length);
5489 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005490#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005491#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5492 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5494 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5495 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5496 kdf_alg, output,
5497 output_length);
5498 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005499#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5500 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005501#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005503 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5505 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005506#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5507
Gilles Peskineeab56e42018-07-12 17:12:33 +02005508 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005509 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005511 }
5512
5513exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005514 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005515 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005516 * This allows us to differentiate between exhausted operations and
5517 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5518 * operations. */
5519 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005521 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005523 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005525}
5526
David Brown7807bf72021-02-09 16:28:23 -07005527#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005528static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005529{
Gilles Peskine449bd832023-01-11 14:50:10 +01005530 if (data_size >= 8) {
5531 mbedtls_des_key_set_parity(data);
5532 }
5533 if (data_size >= 16) {
5534 mbedtls_des_key_set_parity(data + 8);
5535 }
5536 if (data_size >= 24) {
5537 mbedtls_des_key_set_parity(data + 16);
5538 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005539}
David Brown7807bf72021-02-09 16:28:23 -07005540#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005541
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005542/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 * ECC keys on a Weierstrass elliptic curve require the generation
5544 * of a private key which is an integer
5545 * in the range [1, N - 1], where N is the boundary of the private key domain:
5546 * N is the prime p for Diffie-Hellman, or the order of the
5547 * curve’s base point for ECC.
5548 *
5549 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5550 * This function generates the private key using the following process:
5551 *
5552 * 1. Draw a byte string of length ceiling(m/8) bytes.
5553 * 2. If m is not a multiple of 8, set the most significant
5554 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5555 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5556 * 4. If k > N - 2, discard the result and return to step 1.
5557 * 5. Output k + 1 as the private key.
5558 *
5559 * This method allows compliance to NIST standards, specifically the methods titled
5560 * Key-Pair Generation by Testing Candidates in the following publications:
5561 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5562 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5563 * Diffie-Hellman keys.
5564 *
5565 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5566 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5567 *
5568 * Note: Function allocates memory for *data buffer, so given *data should be
5569 * always NULL.
5570 */
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005571#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5572 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005573 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5574 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5575 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005576static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005577 psa_key_slot_t *slot,
5578 size_t bits,
5579 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005580 uint8_t **data
5581 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005582{
Valerio Setti52789862023-04-07 12:13:11 +02005583#if defined(MBEDTLS_ECP_LIGHT)
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005584 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005585 mbedtls_mpi k;
5586 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005587 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005588 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005589
Gilles Peskine449bd832023-01-11 14:50:10 +01005590 mbedtls_mpi_init(&k);
5591 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005592
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005593 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005594 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005595 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005597
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005599 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005600 goto cleanup;
5601 }
5602
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005603 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005605
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005607
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005608 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005609 /* Let m be the bit size of N. */
5610 size_t m = ecp_group.nbits;
5611
Gilles Peskine449bd832023-01-11 14:50:10 +01005612 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005613
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005614 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005616
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005617 /* Note: This function is always called with *data == NULL and it
5618 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 *data = mbedtls_calloc(1, m_bytes);
5620 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005621 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005622 goto cleanup;
5623 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005624
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005626 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005628 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005630
5631 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005633 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005634 * (8 * ceiling(m/8) - m) bits of the first byte in
5635 * the string to zero.
5636 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005637 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005638 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005639 }
5640
5641 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 * big-endian byte string.
5643 */
5644 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005645
5646 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 * Result of comparison is returned. When it indicates error
5648 * then this function is called again.
5649 */
5650 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005651 }
5652
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005653 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005654 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5655 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005656cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005657 if (ret != 0) {
5658 status = mbedtls_to_psa_error(ret);
5659 }
5660 if (status != PSA_SUCCESS) {
5661 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005662 *data = NULL;
5663 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005664 mbedtls_mpi_free(&k);
5665 mbedtls_mpi_free(&diff_N_2);
5666 return status;
Valerio Setti52789862023-04-07 12:13:11 +02005667#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005668 (void) slot;
5669 (void) bits;
5670 (void) operation;
5671 (void) data;
5672 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti52789862023-04-07 12:13:11 +02005673#endif /* MBEDTLS_ECP_LIGHT */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005674}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005675
5676/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5677 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5678 *
5679 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5680 * draw a 32-byte string and process it as specified in
5681 * Elliptic Curves for Security [RFC7748] §5.
5682 *
5683 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5684 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5685 *
5686 * Note: Function allocates memory for *data buffer, so given *data should be
5687 * always NULL.
5688 */
5689
5690static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5691 size_t bits,
5692 psa_key_derivation_operation_t *operation,
5693 uint8_t **data
5694 )
5695{
5696 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005697 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005698
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005700 case 255:
5701 output_length = 32;
5702 break;
5703 case 448:
5704 output_length = 56;
5705 break;
5706 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005708 break;
5709 }
5710
Gilles Peskine449bd832023-01-11 14:50:10 +01005711 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005712
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 if (*data == NULL) {
5714 return PSA_ERROR_INSUFFICIENT_MEMORY;
5715 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005716
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005718
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005720 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005722
Gilles Peskine449bd832023-01-11 14:50:10 +01005723 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005724 case 255:
5725 (*data)[0] &= 248;
5726 (*data)[31] &= 127;
5727 (*data)[31] |= 64;
5728 break;
5729 case 448:
5730 (*data)[0] &= 252;
5731 (*data)[55] |= 128;
5732 break;
5733 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005735 break;
5736 }
5737
5738 return status;
5739}
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005740#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5741 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005742 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5743 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5744 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005745
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005746static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005747 psa_key_slot_t *slot,
5748 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005750{
5751 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305753 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005754 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005755
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5757 return PSA_ERROR_INVALID_ARGUMENT;
5758 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005759
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005760#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5761 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005762 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5763 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5764 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5766 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5767 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005768 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5770 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005771 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005772 }
5773 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005774 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5776 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005777 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005779 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005780 } else
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005781#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5782 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005783 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5784 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5785 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 if (key_type_is_raw_bytes(slot->attr.type)) {
5787 if (bits % 8 != 0) {
5788 return PSA_ERROR_INVALID_ARGUMENT;
5789 }
5790 data = mbedtls_calloc(1, bytes);
5791 if (data == NULL) {
5792 return PSA_ERROR_INSUFFICIENT_MEMORY;
5793 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005794
Gilles Peskine449bd832023-01-11 14:50:10 +01005795 status = psa_key_derivation_output_bytes(operation, data, bytes);
5796 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005797 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 }
David Brown12ca5032021-02-11 11:02:00 -07005799#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5801 psa_des_set_key_parity(data, bytes);
5802 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005803#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005804 } else {
5805 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005806 }
Ronald Crondd04d422020-11-28 15:14:42 +01005807
Ronald Cronbf33c932020-11-28 18:06:53 +01005808 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005809 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005810 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005811 };
5812
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5814 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5815 &storage_size);
5816 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305817 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 }
Archanad8a83dc2021-06-14 10:04:16 +05305819 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 status = psa_allocate_buffer_to_slot(slot, storage_size);
5821 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305822 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005823 }
Archanad8a83dc2021-06-14 10:04:16 +05305824
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 status = psa_driver_wrapper_import_key(&attributes,
5826 data, bytes,
5827 slot->key.data,
5828 slot->key.bytes,
5829 &slot->key.bytes, &bits);
5830 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005831 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005833
5834exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005835 mbedtls_free(data);
5836 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005837}
5838
Gilles Peskine449bd832023-01-11 14:50:10 +01005839psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5840 psa_key_derivation_operation_t *operation,
5841 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005842{
5843 psa_status_t status;
5844 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005845 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005846
Ronald Cron81709fc2020-11-14 12:10:32 +01005847 *key = MBEDTLS_SVC_KEY_ID_INIT;
5848
Gilles Peskine0f84d622019-09-12 19:03:13 +02005849 /* Reject any attempt to create a zero-length key so that we don't
5850 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005851 if (psa_get_key_bits(attributes) == 0) {
5852 return PSA_ERROR_INVALID_ARGUMENT;
5853 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005854
Gilles Peskine449bd832023-01-11 14:50:10 +01005855 if (operation->alg == PSA_ALG_NONE) {
5856 return PSA_ERROR_BAD_STATE;
5857 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005858
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 if (!operation->can_output_key) {
5860 return PSA_ERROR_NOT_PERMITTED;
5861 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005862
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5864 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005865#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005867 /* Deriving a key in a secure element is not implemented yet. */
5868 status = PSA_ERROR_NOT_SUPPORTED;
5869 }
5870#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 if (status == PSA_SUCCESS) {
5872 status = psa_generate_derived_key_internal(slot,
5873 attributes->core.bits,
5874 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005875 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 if (status == PSA_SUCCESS) {
5877 status = psa_finish_key_creation(slot, driver, key);
5878 }
5879 if (status != PSA_SUCCESS) {
5880 psa_fail_key_creation(slot, driver);
5881 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005882
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005884}
5885
Gilles Peskineeab56e42018-07-12 17:12:33 +02005886
5887
5888/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005889/* Key derivation */
5890/****************************************************************/
5891
Steven Cooreman932ffb72021-02-15 12:14:32 +01005892#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005893static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005894{
5895#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5897 return 1;
5898 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005899#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005900#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005901 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5902 return 1;
5903 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005904#endif
5905#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5907 return 1;
5908 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005909#endif
5910#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005911 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5912 return 1;
5913 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005914#endif
5915#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5917 return 1;
5918 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005919#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005920#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5922 return 1;
5923 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005924#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005925 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005926}
5927
Gilles Peskine449bd832023-01-11 14:50:10 +01005928static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005929{
5930 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 psa_status_t status = psa_hash_setup(&operation, alg);
5932 psa_hash_abort(&operation);
5933 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005934}
5935
Gilles Peskine969c5d62019-01-16 15:53:06 +01005936static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005937 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005939{
Janos Follath5fe19732019-06-20 15:09:30 +01005940 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5941 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005942 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005943
Gilles Peskine969c5d62019-01-16 15:53:06 +01005944 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005945 if (!is_kdf_alg_supported(kdf_alg)) {
5946 return PSA_ERROR_NOT_SUPPORTED;
5947 }
John Durkop07cc04a2020-11-16 22:08:34 -08005948
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005949 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005950 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5952 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5953 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5954 if (hash_size == 0) {
5955 return PSA_ERROR_NOT_SUPPORTED;
5956 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005957
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005958 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5959 * we might fail later, which is somewhat unfriendly and potentially
5960 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005961 psa_status_t status = psa_hash_try_support(hash_alg);
5962 if (status != PSA_SUCCESS) {
5963 return status;
5964 }
5965 } else {
5966 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005967 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005968
Gilles Peskine449bd832023-01-11 14:50:10 +01005969 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5970 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
5971 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
5972 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005973 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005974#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005975 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005976 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
5977 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005978 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005980#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5981 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005982 operation->capacity = 255 * hash_size;
5983 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005984}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005985
Gilles Peskine449bd832023-01-11 14:50:10 +01005986static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005987{
5988#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 if (alg == PSA_ALG_ECDH) {
5990 return PSA_SUCCESS;
5991 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005992#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01005993#if defined(PSA_WANT_ALG_FFDH)
5994 if (alg == PSA_ALG_FFDH) {
5995 return PSA_SUCCESS;
5996 }
5997#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005998 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005999 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006000}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006001
6002static int psa_key_derivation_allows_free_form_secret_input(
6003 psa_algorithm_t kdf_alg)
6004{
6005#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6006 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6007 return 0;
6008 }
6009#endif
6010 (void) kdf_alg;
6011 return 1;
6012}
John Durkop07cc04a2020-11-16 22:08:34 -08006013#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006014
Gilles Peskine449bd832023-01-11 14:50:10 +01006015psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6016 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006017{
6018 psa_status_t status;
6019
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 if (operation->alg != 0) {
6021 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006022 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006023
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6025 return PSA_ERROR_INVALID_ARGUMENT;
6026 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6027#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6028 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6029 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6030 status = psa_key_agreement_try_support(ka_alg);
6031 if (status != PSA_SUCCESS) {
6032 return status;
6033 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006034 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6035 return PSA_ERROR_INVALID_ARGUMENT;
6036 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006037 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6038#else
6039 return PSA_ERROR_NOT_SUPPORTED;
6040#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6041 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6042#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6043 status = psa_key_derivation_setup_kdf(operation, alg);
6044#else
6045 return PSA_ERROR_NOT_SUPPORTED;
6046#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6047 } else {
6048 return PSA_ERROR_INVALID_ARGUMENT;
6049 }
6050
6051 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006052 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 }
6054 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006055}
6056
Przemek Stekiel69c46792022-06-10 12:59:51 +02006057#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006058static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6059 psa_algorithm_t kdf_alg,
6060 psa_key_derivation_step_t step,
6061 const uint8_t *data,
6062 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006063{
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006065 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006067 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006068#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6070 return PSA_ERROR_INVALID_ARGUMENT;
6071 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006072#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 if (hkdf->state != HKDF_STATE_INIT) {
6074 return PSA_ERROR_BAD_STATE;
6075 } else {
6076 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6077 hash_alg,
6078 data, data_length);
6079 if (status != PSA_SUCCESS) {
6080 return status;
6081 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006082 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006084 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006085 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006086#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006087 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006088 /* We shouldn't be in different state as HKDF_EXPAND only allows
6089 * two inputs: SECRET (this case) and INFO which does not modify
6090 * the state. It could happen only if the hkdf
6091 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 if (hkdf->state != HKDF_STATE_INIT) {
6093 return PSA_ERROR_BAD_STATE;
6094 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006095
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006096 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6098 return PSA_ERROR_INVALID_ARGUMENT;
6099 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006100
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 memcpy(hkdf->prk, data, data_length);
6102 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006103#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006104 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006105 /* HKDF: If no salt was provided, use an empty salt.
6106 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006108#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6110 return PSA_ERROR_BAD_STATE;
6111 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006112#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6114 hash_alg,
6115 NULL, 0);
6116 if (status != PSA_SUCCESS) {
6117 return status;
6118 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006119 hkdf->state = HKDF_STATE_STARTED;
6120 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 if (hkdf->state != HKDF_STATE_STARTED) {
6122 return PSA_ERROR_BAD_STATE;
6123 }
6124 status = psa_mac_update(&hkdf->hmac,
6125 data, data_length);
6126 if (status != PSA_SUCCESS) {
6127 return status;
6128 }
6129 status = psa_mac_sign_finish(&hkdf->hmac,
6130 hkdf->prk,
6131 sizeof(hkdf->prk),
6132 &data_length);
6133 if (status != PSA_SUCCESS) {
6134 return status;
6135 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006136 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006137
Gilles Peskine2b522db2019-04-12 15:11:49 +02006138 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006139 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006140#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006142 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006144 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006146#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006147 {
6148 /* Block 0 is empty, and the next block will be
6149 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006151 }
6152
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006154 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006155#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6157 return PSA_ERROR_INVALID_ARGUMENT;
6158 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006159#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006160#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6162 hkdf->state == HKDF_STATE_INIT) {
6163 return PSA_ERROR_BAD_STATE;
6164 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006165#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 if (hkdf->state == HKDF_STATE_OUTPUT) {
6167 return PSA_ERROR_BAD_STATE;
6168 }
6169 if (hkdf->info_set) {
6170 return PSA_ERROR_BAD_STATE;
6171 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006172 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006173 if (data_length != 0) {
6174 hkdf->info = mbedtls_calloc(1, data_length);
6175 if (hkdf->info == NULL) {
6176 return PSA_ERROR_INSUFFICIENT_MEMORY;
6177 }
6178 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006179 }
6180 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006182 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006183 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006184 }
6185}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006186#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006187
John Durkop07cc04a2020-11-16 22:08:34 -08006188#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6189 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006190static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6191 const uint8_t *data,
6192 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006193{
Gilles Peskine449bd832023-01-11 14:50:10 +01006194 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6195 return PSA_ERROR_BAD_STATE;
6196 }
Janos Follathf08e2652019-06-13 09:05:41 +01006197
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 if (data_length != 0) {
6199 prf->seed = mbedtls_calloc(1, data_length);
6200 if (prf->seed == NULL) {
6201 return PSA_ERROR_INSUFFICIENT_MEMORY;
6202 }
Janos Follathf08e2652019-06-13 09:05:41 +01006203
Gilles Peskine449bd832023-01-11 14:50:10 +01006204 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006205 prf->seed_length = data_length;
6206 }
Janos Follathf08e2652019-06-13 09:05:41 +01006207
Gilles Peskine43f958b2020-12-13 14:55:14 +01006208 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006209
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006211}
6212
Gilles Peskine449bd832023-01-11 14:50:10 +01006213static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6214 const uint8_t *data,
6215 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006216{
Gilles Peskine449bd832023-01-11 14:50:10 +01006217 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6218 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6219 return PSA_ERROR_BAD_STATE;
6220 }
Janos Follath81550542019-06-13 14:26:34 +01006221
Gilles Peskine449bd832023-01-11 14:50:10 +01006222 if (data_length != 0) {
6223 prf->secret = mbedtls_calloc(1, data_length);
6224 if (prf->secret == NULL) {
6225 return PSA_ERROR_INSUFFICIENT_MEMORY;
6226 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006227
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006229 prf->secret_length = data_length;
6230 }
Janos Follath81550542019-06-13 14:26:34 +01006231
Gilles Peskine43f958b2020-12-13 14:55:14 +01006232 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006233
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006235}
6236
Gilles Peskine449bd832023-01-11 14:50:10 +01006237static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6238 const uint8_t *data,
6239 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006240{
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6242 return PSA_ERROR_BAD_STATE;
6243 }
Janos Follath63028dd2019-06-13 09:15:47 +01006244
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 if (data_length != 0) {
6246 prf->label = mbedtls_calloc(1, data_length);
6247 if (prf->label == NULL) {
6248 return PSA_ERROR_INSUFFICIENT_MEMORY;
6249 }
Janos Follath63028dd2019-06-13 09:15:47 +01006250
Gilles Peskine449bd832023-01-11 14:50:10 +01006251 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006252 prf->label_length = data_length;
6253 }
Janos Follath63028dd2019-06-13 09:15:47 +01006254
Gilles Peskine43f958b2020-12-13 14:55:14 +01006255 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006256
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006258}
6259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6261 psa_key_derivation_step_t step,
6262 const uint8_t *data,
6263 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006264{
Gilles Peskine449bd832023-01-11 14:50:10 +01006265 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006266 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006268 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006270 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006271 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006272 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006274 }
6275}
John Durkop07cc04a2020-11-16 22:08:34 -08006276#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6277 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6278
6279#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6280static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6281 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006282 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006283 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006284{
6285 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6287 4 + data_length + prf->other_secret_length :
6288 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006289
Gilles Peskine449bd832023-01-11 14:50:10 +01006290 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6291 return PSA_ERROR_INVALID_ARGUMENT;
6292 }
John Durkop07cc04a2020-11-16 22:08:34 -08006293
Gilles Peskine449bd832023-01-11 14:50:10 +01006294 uint8_t *pms = mbedtls_calloc(1, pms_len);
6295 if (pms == NULL) {
6296 return PSA_ERROR_INSUFFICIENT_MEMORY;
6297 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006298 uint8_t *cur = pms;
6299
6300 /* pure-PSK:
6301 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006302 *
6303 * The premaster secret is formed as follows: if the PSK is N octets
6304 * long, concatenate a uint16 with the value N, N zero octets, a second
6305 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006306 *
6307 * mixed-PSK:
6308 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6309 * follows: concatenate a uint16 with the length of the other secret,
6310 * the other secret itself, uint16 with the length of PSK, and the
6311 * PSK itself.
6312 * For details please check:
6313 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6314 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6315 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006316 */
6317
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6319 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6320 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6321 if (prf->other_secret_length != 0) {
6322 memcpy(cur, prf->other_secret, prf->other_secret_length);
6323 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006324 cur += prf->other_secret_length;
6325 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006326 } else {
6327 *cur++ = MBEDTLS_BYTE_1(data_length);
6328 *cur++ = MBEDTLS_BYTE_0(data_length);
6329 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006330 cur += data_length;
6331 }
6332
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 *cur++ = MBEDTLS_BYTE_1(data_length);
6334 *cur++ = MBEDTLS_BYTE_0(data_length);
6335 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006336 cur += data_length;
6337
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006339
Gilles Peskine449bd832023-01-11 14:50:10 +01006340 mbedtls_platform_zeroize(pms, pms_len);
6341 mbedtls_free(pms);
6342 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006343}
Janos Follath6660f0e2019-06-17 08:44:03 +01006344
Przemek Stekiele47201b2022-04-19 13:53:28 +02006345static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6346 psa_tls12_prf_key_derivation_t *prf,
6347 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006348 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006349{
Gilles Peskine449bd832023-01-11 14:50:10 +01006350 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6351 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006352 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006353
6354 if (data_length != 0) {
6355 prf->other_secret = mbedtls_calloc(1, data_length);
6356 if (prf->other_secret == NULL) {
6357 return PSA_ERROR_INSUFFICIENT_MEMORY;
6358 }
6359
6360 memcpy(prf->other_secret, data, data_length);
6361 prf->other_secret_length = data_length;
6362 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006363 prf->other_secret_length = 0;
6364 }
6365
6366 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6367
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006369}
6370
Janos Follath6660f0e2019-06-17 08:44:03 +01006371static psa_status_t psa_tls12_prf_psk_to_ms_input(
6372 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006373 psa_key_derivation_step_t step,
6374 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006376{
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006378 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 return psa_tls12_prf_psk_to_ms_set_key(prf,
6380 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006381 break;
6382 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6384 data,
6385 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006386 break;
6387 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006389 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006390
Przemek Stekiele47201b2022-04-19 13:53:28 +02006391 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006392}
John Durkop07cc04a2020-11-16 22:08:34 -08006393#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006394
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006395#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6396static psa_status_t psa_tls12_ecjpake_to_pms_input(
6397 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006398 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006399 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006400 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006401{
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6403 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6404 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006405 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006406
6407 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 if (data[0] != 0x04) {
6409 return PSA_ERROR_INVALID_ARGUMENT;
6410 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006411
6412 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006414
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006416}
6417#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006418/** Check whether the given key type is acceptable for the given
6419 * input step of a key derivation.
6420 *
6421 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6422 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6423 * Both secret and non-secret inputs can alternatively have the type
6424 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6425 * that the input was passed as a buffer rather than via a key object.
6426 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006427static int psa_key_derivation_check_input_type(
6428 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006429 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006430{
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006432 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 if (key_type == PSA_KEY_TYPE_DERIVE) {
6434 return PSA_SUCCESS;
6435 }
6436 if (key_type == PSA_KEY_TYPE_NONE) {
6437 return PSA_SUCCESS;
6438 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006439 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006440 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 if (key_type == PSA_KEY_TYPE_DERIVE) {
6442 return PSA_SUCCESS;
6443 }
6444 if (key_type == PSA_KEY_TYPE_NONE) {
6445 return PSA_SUCCESS;
6446 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006447 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006448 case PSA_KEY_DERIVATION_INPUT_LABEL:
6449 case PSA_KEY_DERIVATION_INPUT_SALT:
6450 case PSA_KEY_DERIVATION_INPUT_INFO:
6451 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6453 return PSA_SUCCESS;
6454 }
6455 if (key_type == PSA_KEY_TYPE_NONE) {
6456 return PSA_SUCCESS;
6457 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006458 break;
6459 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006460 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006461}
6462
Janos Follathb80a94e2019-06-12 15:54:46 +01006463static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006464 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006465 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006466 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006467 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006468 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006469{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006470 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006472
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 status = psa_key_derivation_check_input_type(step, key_type);
6474 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006475 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006476 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006477
Przemek Stekiel69c46792022-06-10 12:59:51 +02006478#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006479 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6480 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6481 step, data, data_length);
6482 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006483#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006484#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6486 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6487 step, data, data_length);
6488 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006489#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6490#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6492 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6493 step, data, data_length);
6494 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006495#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006496#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006498 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006499 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6500 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006501#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006502 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006503 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006504 (void) data;
6505 (void) data_length;
6506 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006507 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006508 }
6509
Gilles Peskine224b0d62019-09-23 18:13:17 +02006510exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006511 if (status != PSA_SUCCESS) {
6512 psa_key_derivation_abort(operation);
6513 }
6514 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006515}
6516
Janos Follath51f4a0f2019-06-14 11:35:55 +01006517psa_status_t psa_key_derivation_input_bytes(
6518 psa_key_derivation_operation_t *operation,
6519 psa_key_derivation_step_t step,
6520 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006522{
Gilles Peskine449bd832023-01-11 14:50:10 +01006523 return psa_key_derivation_input_internal(operation, step,
6524 PSA_KEY_TYPE_NONE,
6525 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006526}
6527
Janos Follath51f4a0f2019-06-14 11:35:55 +01006528psa_status_t psa_key_derivation_input_key(
6529 psa_key_derivation_operation_t *operation,
6530 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006531 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006532{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006533 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006534 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006535 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006536
Ronald Cron5c522922020-11-14 16:35:34 +01006537 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006538 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6539 if (status != PSA_SUCCESS) {
6540 psa_key_derivation_abort(operation);
6541 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006542 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006543
6544 /* Passing a key object as a SECRET input unlocks the permission
6545 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006546 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006547 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006548 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006549
Gilles Peskine449bd832023-01-11 14:50:10 +01006550 status = psa_key_derivation_input_internal(operation,
6551 step, slot->attr.type,
6552 slot->key.data,
6553 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006554
Gilles Peskine449bd832023-01-11 14:50:10 +01006555 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006556
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006558}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006559
6560
6561
6562/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006563/* Key agreement */
6564/****************************************************************/
6565
Gilles Peskine449bd832023-01-11 14:50:10 +01006566psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6567 const uint8_t *key_buffer,
6568 size_t key_buffer_size,
6569 psa_algorithm_t alg,
6570 const uint8_t *peer_key,
6571 size_t peer_key_length,
6572 uint8_t *shared_secret,
6573 size_t shared_secret_size,
6574 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006575{
Gilles Peskine449bd832023-01-11 14:50:10 +01006576 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006577#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006578 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006579 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6580 key_buffer_size, alg,
6581 peer_key, peer_key_length,
6582 shared_secret,
6583 shared_secret_size,
6584 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006585#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006586
6587#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6588 case PSA_ALG_FFDH:
Przemek Stekiel359f4622022-12-05 14:11:55 +01006589 return mbedtls_psa_key_agreement_ffdh(attributes,
6590 peer_key,
6591 peer_key_length,
6592 key_buffer,
6593 key_buffer_size,
6594 shared_secret,
6595 shared_secret_size,
6596 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006597#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6598
Gilles Peskine01d718c2018-09-18 12:01:02 +02006599 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006600 (void) attributes;
6601 (void) key_buffer;
6602 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006603 (void) peer_key;
6604 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006605 (void) shared_secret;
6606 (void) shared_secret_size;
6607 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006608 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006609 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006610}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006611
Aditya Deshpande17845b82022-10-13 17:21:01 +01006612/** Internal function for raw key agreement
6613 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006614 * to the driver's implementation if a driver is present.
6615 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006616 * (psa_key_agreement_raw_builtin).
6617 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006618static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6619 psa_key_slot_t *private_key,
6620 const uint8_t *peer_key,
6621 size_t peer_key_length,
6622 uint8_t *shared_secret,
6623 size_t shared_secret_size,
6624 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006625{
Gilles Peskine449bd832023-01-11 14:50:10 +01006626 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6627 return PSA_ERROR_NOT_SUPPORTED;
6628 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006629
6630 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006631 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006632 };
6633
Gilles Peskine449bd832023-01-11 14:50:10 +01006634 return psa_driver_wrapper_key_agreement(&attributes,
6635 private_key->key.data,
6636 private_key->key.bytes, alg,
6637 peer_key, peer_key_length,
6638 shared_secret,
6639 shared_secret_size,
6640 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006641}
6642
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006643/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006644 * to potentially free embedded data structures and wipe confidential data.
6645 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006646static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6647 psa_key_derivation_step_t step,
6648 psa_key_slot_t *private_key,
6649 const uint8_t *peer_key,
6650 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006651{
6652 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006653 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006654 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006655 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006656
6657 /* Step 1: run the secret agreement algorithm to generate the shared
6658 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006659 status = psa_key_agreement_raw_internal(ka_alg,
6660 private_key,
6661 peer_key, peer_key_length,
6662 shared_secret,
6663 sizeof(shared_secret),
6664 &shared_secret_length);
6665 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006666 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006667 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006668
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006669 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006670 * the shared secret. A shared secret is permitted wherever a key
6671 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006672 status = psa_key_derivation_input_internal(operation, step,
6673 PSA_KEY_TYPE_DERIVE,
6674 shared_secret,
6675 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006676exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006677 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6678 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006679}
6680
Gilles Peskine449bd832023-01-11 14:50:10 +01006681psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6682 psa_key_derivation_step_t step,
6683 mbedtls_svc_key_id_t private_key,
6684 const uint8_t *peer_key,
6685 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006686{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006687 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006688 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006689 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006690
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6692 return PSA_ERROR_INVALID_ARGUMENT;
6693 }
Ronald Cron5c522922020-11-14 16:35:34 +01006694 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006695 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6696 if (status != PSA_SUCCESS) {
6697 return status;
6698 }
6699 status = psa_key_agreement_internal(operation, step,
6700 slot,
6701 peer_key, peer_key_length);
6702 if (status != PSA_SUCCESS) {
6703 psa_key_derivation_abort(operation);
6704 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006705 /* If a private key has been added as SECRET, we allow the derived
6706 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006707 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006708 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006709 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006710 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006711
Gilles Peskine449bd832023-01-11 14:50:10 +01006712 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006713
Gilles Peskine449bd832023-01-11 14:50:10 +01006714 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006715}
6716
Gilles Peskine449bd832023-01-11 14:50:10 +01006717psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6718 mbedtls_svc_key_id_t private_key,
6719 const uint8_t *peer_key,
6720 size_t peer_key_length,
6721 uint8_t *output,
6722 size_t output_size,
6723 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006724{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006725 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006726 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006727 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006728
Gilles Peskine449bd832023-01-11 14:50:10 +01006729 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006730 status = PSA_ERROR_INVALID_ARGUMENT;
6731 goto exit;
6732 }
Ronald Cron5c522922020-11-14 16:35:34 +01006733 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006734 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6735 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006736 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006737 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006738
Gilles Peskine3e561302022-04-14 00:17:15 +02006739 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6740 * for the output size. The PSA specification only guarantees that this
6741 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6742 * but it might be nice to allow smaller buffers if the output fits.
6743 * At the time of writing this comment, with only ECDH implemented,
6744 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6745 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6746 * be exact for it as well. */
6747 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006748 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6749 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006750 status = PSA_ERROR_BUFFER_TOO_SMALL;
6751 goto exit;
6752 }
6753
Gilles Peskine449bd832023-01-11 14:50:10 +01006754 status = psa_key_agreement_raw_internal(alg, slot,
6755 peer_key, peer_key_length,
6756 output, output_size,
6757 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006758
6759exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006760 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006761 /* If an error happens and is not handled properly, the output
6762 * may be used as a key to protect sensitive data. Arrange for such
6763 * a key to be random, which is likely to result in decryption or
6764 * verification errors. This is better than filling the buffer with
6765 * some constant data such as zeros, which would result in the data
6766 * being protected with a reproducible, easily knowable key.
6767 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006768 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006769 *output_length = output_size;
6770 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006771
Gilles Peskine449bd832023-01-11 14:50:10 +01006772 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006773
Gilles Peskine449bd832023-01-11 14:50:10 +01006774 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006775}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006776
6777
Gilles Peskine30524eb2020-11-13 17:02:26 +01006778
Gilles Peskine86a440b2018-11-12 18:39:40 +01006779/****************************************************************/
6780/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006781/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006782
Gilles Peskine30524eb2020-11-13 17:02:26 +01006783/** Initialize the PSA random generator.
6784 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006785static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006786{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006787#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006788 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006789#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6790
Gilles Peskine30524eb2020-11-13 17:02:26 +01006791 /* Set default configuration if
6792 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006793 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006794 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006795 }
6796 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006797 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006798 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006799
Gilles Peskine449bd832023-01-11 14:50:10 +01006800 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006801#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6802 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6803 /* The PSA entropy injection feature depends on using NV seed as an entropy
6804 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006805 mbedtls_entropy_add_source(&rng->entropy,
6806 mbedtls_nv_seed_poll, NULL,
6807 MBEDTLS_ENTROPY_BLOCK_SIZE,
6808 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006809#endif
6810
Gilles Peskine449bd832023-01-11 14:50:10 +01006811 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006812#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006813}
6814
6815/** Deinitialize the PSA random generator.
6816 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006817static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006818{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006819#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006820 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006821#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01006822 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
6823 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006824#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006825}
6826
6827/** Seed the PSA random generator.
6828 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006829static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006830{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006831#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6832 /* Do nothing: the external RNG seeds itself. */
6833 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006834 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006835#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006836 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01006837 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
6838 drbg_seed, sizeof(drbg_seed) - 1);
6839 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006840#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006841}
6842
Gilles Peskine449bd832023-01-11 14:50:10 +01006843psa_status_t psa_generate_random(uint8_t *output,
6844 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006845{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006846 GUARD_MODULE_INITIALIZED;
6847
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006848#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6849
6850 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6852 output, output_size,
6853 &output_length);
6854 if (status != PSA_SUCCESS) {
6855 return status;
6856 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006857 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006858 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006859 if (output_length != output_size) {
6860 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6861 }
6862 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006863
6864#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6865
Gilles Peskine449bd832023-01-11 14:50:10 +01006866 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006867 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6869 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6870 output_size);
6871 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6872 output, request_size);
6873 if (ret != 0) {
6874 return mbedtls_to_psa_error(ret);
6875 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006876 output_size -= request_size;
6877 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006878 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006880#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006881}
6882
Gilles Peskine8814fc42020-12-14 15:33:44 +01006883/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006884 *
6885 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6886 * `psa_generate_random(...)`. The state parameter is ignored since the
6887 * PSA API doesn't support passing an explicit state.
6888 *
6889 * In the non-external case, psa_generate_random() calls an
6890 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6891 * and semantics as mbedtls_psa_get_random(). As an optimization,
6892 * instead of doing this back-and-forth between the PSA API and the
6893 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6894 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006895 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006896#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6897int mbedtls_psa_get_random(void *p_rng,
6898 unsigned char *output,
6899 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01006900{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006901 /* This function takes a pointer to the RNG state because that's what
6902 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6903 * its own state internally and doesn't let the caller access that state.
6904 * So we just ignore the state parameter, and in practice we'll pass
6905 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006906 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006907 psa_status_t status = psa_generate_random(output, output_size);
6908 if (status == PSA_SUCCESS) {
6909 return 0;
6910 } else {
6911 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
6912 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01006913}
6914#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6915
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006916#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006917#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006918
Gilles Peskine449bd832023-01-11 14:50:10 +01006919psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
6920 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006921{
Gilles Peskine449bd832023-01-11 14:50:10 +01006922 if (global_data.initialized) {
6923 return PSA_ERROR_NOT_PERMITTED;
6924 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006925
Gilles Peskine449bd832023-01-11 14:50:10 +01006926 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
6927 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
6928 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
6929 return PSA_ERROR_INVALID_ARGUMENT;
6930 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006931
Gilles Peskine449bd832023-01-11 14:50:10 +01006932 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006933}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006934#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006935
Ronald Cron3772afe2021-02-08 16:10:05 +01006936/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006937 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006938 * \param type The key type
6939 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006940 *
6941 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006942 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006943 * \retval #PSA_ERROR_INVALID_ARGUMENT
6944 * The size in bits of the key is not valid.
6945 * \retval #PSA_ERROR_NOT_SUPPORTED
6946 * The type and/or the size in bits of the key or the combination of
6947 * the two is not supported.
6948 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006949static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006950 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006951{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006952 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006953
Gilles Peskine449bd832023-01-11 14:50:10 +01006954 if (key_type_is_raw_bytes(type)) {
6955 status = psa_validate_unstructured_key_bit_size(type, bits);
6956 if (status != PSA_SUCCESS) {
6957 return status;
6958 }
6959 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006960#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006961 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6962 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
6963 return PSA_ERROR_NOT_SUPPORTED;
6964 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006965
Ronald Cron01b2aba2020-10-05 09:42:02 +02006966 /* Accept only byte-aligned keys, for the same reasons as
6967 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006968 if (bits % 8 != 0) {
6969 return PSA_ERROR_NOT_SUPPORTED;
6970 }
6971 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006972#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006973
Ronald Cron5c4d3862020-12-07 11:07:24 +01006974#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006975 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01006976 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006977 return PSA_SUCCESS;
6978 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006979#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01006980
6981#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR)
6982 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6983 if (bits != 2048 && bits != 3072 && bits != 4096 &&
6984 bits != 6144 && bits != 8192) {
6985 return PSA_ERROR_NOT_SUPPORTED;
6986 }
6987 } else
6988#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006989 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006990 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006991 }
6992
Gilles Peskine449bd832023-01-11 14:50:10 +01006993 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006994}
6995
Ronald Cron55ed0592020-10-05 10:30:40 +02006996psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006997 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01006998 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006999{
7000 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007001 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007002
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 if ((attributes->domain_parameters == NULL) &&
7004 (attributes->domain_parameters_size != 0)) {
7005 return PSA_ERROR_INVALID_ARGUMENT;
7006 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007007
Gilles Peskine449bd832023-01-11 14:50:10 +01007008 if (key_type_is_raw_bytes(type)) {
7009 status = psa_generate_random(key_buffer, key_buffer_size);
7010 if (status != PSA_SUCCESS) {
7011 return status;
7012 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007013
David Brown12ca5032021-02-11 11:02:00 -07007014#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007015 if (type == PSA_KEY_TYPE_DES) {
7016 psa_des_set_key_parity(key_buffer, key_buffer_size);
7017 }
David Brown8107e312021-02-12 08:08:46 -07007018#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007019 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007020
Jaeden Amero424fa932021-05-14 08:34:32 +01007021#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
7022 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01007023 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7024 return mbedtls_psa_rsa_generate_key(attributes,
7025 key_buffer,
7026 key_buffer_size,
7027 key_buffer_length);
7028 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01007029#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
7030 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007031
John Durkop9814fa22020-11-04 12:28:15 -08007032#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007033 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7034 return mbedtls_psa_ecp_generate_key(attributes,
7035 key_buffer,
7036 key_buffer_size,
7037 key_buffer_length);
7038 } else
John Durkop9814fa22020-11-04 12:28:15 -08007039#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007040
7041#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR)
7042 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7043 return mbedtls_psa_ffdh_generate_key(attributes,
7044 key_buffer,
7045 key_buffer_size,
7046 key_buffer_length);
7047 } else
7048#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02007049 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007050 (void) key_buffer_length;
7051 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007052 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007053
Gilles Peskine449bd832023-01-11 14:50:10 +01007054 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007055}
Darryl Green0c6575a2018-11-07 16:05:30 +00007056
Gilles Peskine449bd832023-01-11 14:50:10 +01007057psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7058 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007059{
7060 psa_status_t status;
7061 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007062 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007063 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007064
Ronald Cron81709fc2020-11-14 12:10:32 +01007065 *key = MBEDTLS_SVC_KEY_ID_INIT;
7066
Gilles Peskine0f84d622019-09-12 19:03:13 +02007067 /* Reject any attempt to create a zero-length key so that we don't
7068 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007069 if (psa_get_key_bits(attributes) == 0) {
7070 return PSA_ERROR_INVALID_ARGUMENT;
7071 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007072
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007073 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007074 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7075 return PSA_ERROR_INVALID_ARGUMENT;
7076 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007077
Gilles Peskine449bd832023-01-11 14:50:10 +01007078 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7079 &slot, &driver);
7080 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007081 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007082 }
Gilles Peskine11792082019-08-06 18:36:36 +02007083
Ronald Cron977c2472020-10-13 08:32:21 +02007084 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307085 * storage ( thus not in the case of generating a key in a secure element
7086 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7087 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007088 if (slot->key.data == NULL) {
7089 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7090 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007091 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007092 attributes->core.type, attributes->core.bits);
7093 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007094 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007095 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007096
7097 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 attributes->core.type,
7099 attributes->core.bits);
7100 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007101 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007102 attributes, &key_buffer_size);
7103 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007104 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007105 }
Ronald Cron977c2472020-10-13 08:32:21 +02007106 }
Ronald Cron977c2472020-10-13 08:32:21 +02007107
Gilles Peskine449bd832023-01-11 14:50:10 +01007108 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7109 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007110 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007111 }
Ronald Cron977c2472020-10-13 08:32:21 +02007112 }
7113
Gilles Peskine449bd832023-01-11 14:50:10 +01007114 status = psa_driver_wrapper_generate_key(attributes,
7115 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007116
Gilles Peskine449bd832023-01-11 14:50:10 +01007117 if (status != PSA_SUCCESS) {
7118 psa_remove_key_data_from_memory(slot);
7119 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007120
Gilles Peskine11792082019-08-06 18:36:36 +02007121exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007122 if (status == PSA_SUCCESS) {
7123 status = psa_finish_key_creation(slot, driver, key);
7124 }
7125 if (status != PSA_SUCCESS) {
7126 psa_fail_key_creation(slot, driver);
7127 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007128
Gilles Peskine449bd832023-01-11 14:50:10 +01007129 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007130}
7131
Gilles Peskinee59236f2018-01-27 23:32:46 +01007132/****************************************************************/
7133/* Module setup */
7134/****************************************************************/
7135
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007136#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007137psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007138 void (* entropy_init)(mbedtls_entropy_context *ctx),
7139 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007140{
Gilles Peskine449bd832023-01-11 14:50:10 +01007141 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7142 return PSA_ERROR_BAD_STATE;
7143 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007144 global_data.rng.entropy_init = entropy_init;
7145 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007146 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007147}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007148#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007149
Gilles Peskine449bd832023-01-11 14:50:10 +01007150void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007151{
Gilles Peskine449bd832023-01-11 14:50:10 +01007152 psa_wipe_all_key_slots();
7153 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7154 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007155 }
7156 /* Wipe all remaining data, including configuration.
7157 * In particular, this sets all state indicator to the value
7158 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007159 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007160
7161 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007162 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007163}
7164
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007165#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7166/** Recover a transaction that was interrupted by a power failure.
7167 *
7168 * This function is called during initialization, before psa_crypto_init()
7169 * returns. If this function returns a failure status, the initialization
7170 * fails.
7171 */
7172static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007173 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007174{
Gilles Peskine449bd832023-01-11 14:50:10 +01007175 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007176 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7177 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007178 /* TODO - fall through to the failure case until this
7179 * is implemented.
7180 * https://github.com/ARMmbed/mbed-crypto/issues/218
7181 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007182 default:
7183 /* We found an unsupported transaction in the storage.
7184 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007185 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007186 }
7187}
7188#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7189
Gilles Peskine449bd832023-01-11 14:50:10 +01007190psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007191{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007192 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007193
Gilles Peskinec6b69072018-11-20 21:42:52 +01007194 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007195 if (global_data.initialized != 0) {
7196 return PSA_SUCCESS;
7197 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007198
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007199 /* Init drivers */
7200 status = psa_driver_wrapper_init();
7201 if (status != PSA_SUCCESS) {
7202 goto exit;
7203 }
7204 global_data.drivers_initialized = 1;
7205
Gilles Peskine30524eb2020-11-13 17:02:26 +01007206 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007207 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007208 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007209 status = mbedtls_psa_random_seed(&global_data.rng);
7210 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007211 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007212 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007213 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007214
Gilles Peskine449bd832023-01-11 14:50:10 +01007215 status = psa_initialize_key_slots();
7216 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007217 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007218 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007219
Gilles Peskine4b734222019-07-24 15:56:31 +02007220#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 status = psa_crypto_load_transaction();
7222 if (status == PSA_SUCCESS) {
7223 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7224 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007225 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007226 }
7227 status = psa_crypto_stop_transaction();
7228 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007229 /* There's no transaction to complete. It's all good. */
7230 status = PSA_SUCCESS;
7231 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007232#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007233
Gilles Peskinec6b69072018-11-20 21:42:52 +01007234 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007235 global_data.initialized = 1;
7236
7237exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007238 if (status != PSA_SUCCESS) {
7239 mbedtls_psa_crypto_free();
7240 }
7241 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007242}
7243
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007244psa_status_t psa_crypto_driver_pake_get_password_len(
7245 const psa_crypto_driver_pake_inputs_t *inputs,
7246 size_t *password_len)
7247{
7248 if (inputs->password_len == 0) {
7249 return PSA_ERROR_BAD_STATE;
7250 }
7251
7252 *password_len = inputs->password_len;
7253
7254 return PSA_SUCCESS;
7255}
7256
7257psa_status_t psa_crypto_driver_pake_get_password(
7258 const psa_crypto_driver_pake_inputs_t *inputs,
7259 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7260{
7261 if (inputs->password_len == 0) {
7262 return PSA_ERROR_BAD_STATE;
7263 }
7264
7265 if (buffer_size < inputs->password_len) {
7266 return PSA_ERROR_BUFFER_TOO_SMALL;
7267 }
7268
7269 memcpy(buffer, inputs->password, inputs->password_len);
7270 *buffer_length = inputs->password_len;
7271
7272 return PSA_SUCCESS;
7273}
7274
7275psa_status_t psa_crypto_driver_pake_get_role(
7276 const psa_crypto_driver_pake_inputs_t *inputs,
7277 psa_pake_role_t *role)
7278{
7279 if (inputs->role == PSA_PAKE_ROLE_NONE) {
7280 return PSA_ERROR_BAD_STATE;
7281 }
7282
7283 *role = inputs->role;
7284
7285 return PSA_SUCCESS;
7286}
7287
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007288psa_status_t psa_crypto_driver_pake_get_user_len(
7289 const psa_crypto_driver_pake_inputs_t *inputs,
7290 size_t *user_len)
7291{
7292 if (inputs->user_len == 0) {
7293 return PSA_ERROR_BAD_STATE;
7294 }
7295
7296 *user_len = inputs->user_len;
7297
7298 return PSA_SUCCESS;
7299}
7300
7301psa_status_t psa_crypto_driver_pake_get_user(
7302 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007303 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007304{
7305 if (inputs->user_len == 0) {
7306 return PSA_ERROR_BAD_STATE;
7307 }
7308
Przemek Stekielc0e62502023-03-14 11:49:36 +01007309 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007310 return PSA_ERROR_BUFFER_TOO_SMALL;
7311 }
7312
Przemek Stekielc0e62502023-03-14 11:49:36 +01007313 memcpy(user_id, inputs->user, inputs->user_len);
7314 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007315
7316 return PSA_SUCCESS;
7317}
7318
7319psa_status_t psa_crypto_driver_pake_get_peer_len(
7320 const psa_crypto_driver_pake_inputs_t *inputs,
7321 size_t *peer_len)
7322{
7323 if (inputs->peer_len == 0) {
7324 return PSA_ERROR_BAD_STATE;
7325 }
7326
7327 *peer_len = inputs->peer_len;
7328
7329 return PSA_SUCCESS;
7330}
7331
7332psa_status_t psa_crypto_driver_pake_get_peer(
7333 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007334 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007335{
7336 if (inputs->peer_len == 0) {
7337 return PSA_ERROR_BAD_STATE;
7338 }
7339
Przemek Stekielc0e62502023-03-14 11:49:36 +01007340 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007341 return PSA_ERROR_BUFFER_TOO_SMALL;
7342 }
7343
Przemek Stekielc0e62502023-03-14 11:49:36 +01007344 memcpy(peer_id, inputs->peer, inputs->peer_len);
7345 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007346
7347 return PSA_SUCCESS;
7348}
7349
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007350psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7351 const psa_crypto_driver_pake_inputs_t *inputs,
7352 psa_pake_cipher_suite_t *cipher_suite)
7353{
7354 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7355 return PSA_ERROR_BAD_STATE;
7356 }
7357
7358 *cipher_suite = inputs->cipher_suite;
7359
7360 return PSA_SUCCESS;
7361}
7362
Neil Armstronga7d08c32022-06-01 18:21:20 +02007363psa_status_t psa_pake_setup(
7364 psa_pake_operation_t *operation,
7365 const psa_pake_cipher_suite_t *cipher_suite)
7366{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007367 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007368
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007369 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007370 status = PSA_ERROR_BAD_STATE;
7371 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007372 }
7373
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007374 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007375 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007376 status = PSA_ERROR_INVALID_ARGUMENT;
7377 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007378 }
7379
Przemek Stekiel51eac532022-12-07 11:04:51 +01007380 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7381
Przemek Stekiele12ed362022-12-21 12:54:46 +01007382 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007383 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7384 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007385 operation->data.inputs.cipher_suite = *cipher_suite;
7386
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007387#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007388 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007389 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007390 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007391
Przemek Stekiele12ed362022-12-21 12:54:46 +01007392 computation_stage->state = PSA_PAKE_STATE_SETUP;
7393 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7394 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7395 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007396 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007397#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007398 {
7399 status = PSA_ERROR_NOT_SUPPORTED;
7400 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007401 }
7402
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007403 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7404
Przemek Stekiel51eac532022-12-07 11:04:51 +01007405 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007406exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007407 psa_pake_abort(operation);
7408 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007409}
7410
7411psa_status_t psa_pake_set_password_key(
7412 psa_pake_operation_t *operation,
7413 mbedtls_svc_key_id_t password)
7414{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007415 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007416 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7417 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007418
Przemek Stekiel51eac532022-12-07 11:04:51 +01007419 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007420 status = PSA_ERROR_BAD_STATE;
7421 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007422 }
7423
Przemek Stekiel6c764412022-11-22 14:05:12 +01007424 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7425 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007426 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007427 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007428 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007429 }
7430
Przemek Stekiel6c764412022-11-22 14:05:12 +01007431 psa_key_attributes_t attributes = {
7432 .core = slot->attr
7433 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007434
Przemek Stekiel51eac532022-12-07 11:04:51 +01007435 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007436
Przemek Stekiel51eac532022-12-07 11:04:51 +01007437 if (type != PSA_KEY_TYPE_PASSWORD &&
7438 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7439 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007440 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007441 }
7442
Przemek Stekiel51eac532022-12-07 11:04:51 +01007443 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7444 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007445 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007446 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007447 }
7448
7449 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7450 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007451 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007452exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007453 if (status != PSA_SUCCESS) {
7454 psa_pake_abort(operation);
7455 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007456 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007457 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007458}
7459
7460psa_status_t psa_pake_set_user(
7461 psa_pake_operation_t *operation,
7462 const uint8_t *user_id,
7463 size_t user_id_len)
7464{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007465 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007466
7467 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007468 status = PSA_ERROR_BAD_STATE;
7469 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007470 }
7471
Przemek Stekiel51eac532022-12-07 11:04:51 +01007472 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007473 status = PSA_ERROR_INVALID_ARGUMENT;
7474 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007475 }
7476
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007477 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007478 status = PSA_ERROR_BAD_STATE;
7479 goto exit;
7480 }
7481
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007482 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007483 if ((user_id_len != sizeof(jpake_server_id) ||
7484 memcmp(user_id, jpake_server_id, user_id_len) != 0) &&
7485 (user_id_len != sizeof(jpake_client_id) ||
7486 memcmp(user_id, jpake_client_id, user_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007487 status = PSA_ERROR_NOT_SUPPORTED;
7488 goto exit;
7489 }
7490
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007491 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7492 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007493 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7494 goto exit;
7495 }
7496
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007497 memcpy(operation->data.inputs.user, user_id, user_id_len);
7498 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007499
7500 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007501exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007502 psa_pake_abort(operation);
7503 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007504}
7505
7506psa_status_t psa_pake_set_peer(
7507 psa_pake_operation_t *operation,
7508 const uint8_t *peer_id,
7509 size_t peer_id_len)
7510{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007511 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007512
7513 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007514 status = PSA_ERROR_BAD_STATE;
7515 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007516 }
7517
Przemek Stekiel51eac532022-12-07 11:04:51 +01007518 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007519 status = PSA_ERROR_INVALID_ARGUMENT;
7520 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007521 }
7522
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007523 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007524 status = PSA_ERROR_BAD_STATE;
7525 goto exit;
7526 }
7527
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007528 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007529 if ((peer_id_len != sizeof(jpake_server_id) ||
7530 memcmp(peer_id, jpake_server_id, peer_id_len) != 0) &&
7531 (peer_id_len != sizeof(jpake_client_id) ||
7532 memcmp(peer_id, jpake_client_id, peer_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007533 status = PSA_ERROR_NOT_SUPPORTED;
7534 goto exit;
7535 }
7536
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007537 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7538 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007539 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7540 goto exit;
7541 }
7542
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007543 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7544 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007545
7546 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007547exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007548 psa_pake_abort(operation);
7549 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007550}
7551
7552psa_status_t psa_pake_set_role(
7553 psa_pake_operation_t *operation,
7554 psa_pake_role_t role)
7555{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007556 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007557
Przemek Stekiel51eac532022-12-07 11:04:51 +01007558 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007559 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007560 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007561 }
7562
Przemek Stekiel09104b82023-03-06 14:11:51 +01007563 switch (operation->alg) {
7564#if defined(PSA_WANT_ALG_JPAKE)
7565 case PSA_ALG_JPAKE:
7566 if (role == PSA_PAKE_ROLE_NONE) {
7567 return PSA_SUCCESS;
7568 }
7569 status = PSA_ERROR_INVALID_ARGUMENT;
7570 break;
7571#endif
7572 default:
7573 (void) role;
7574 status = PSA_ERROR_NOT_SUPPORTED;
7575 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007576 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007577exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007578 psa_pake_abort(operation);
7579 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007580}
7581
Przemek Stekielb09c4872023-01-17 12:05:38 +01007582/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007583#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007584static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007585 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007586{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007587 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007588 case PSA_PAKE_OUTPUT_X1_X2:
7589 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007590 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007591 case PSA_PAKE_X1_STEP_KEY_SHARE:
7592 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007593 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7594 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007595 case PSA_PAKE_X1_STEP_ZK_PROOF:
7596 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007597 case PSA_PAKE_X2_STEP_KEY_SHARE:
7598 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007599 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7600 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007601 case PSA_PAKE_X2_STEP_ZK_PROOF:
7602 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007603 default:
7604 return PSA_JPAKE_STEP_INVALID;
7605 }
7606 break;
7607 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007608 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007609 case PSA_PAKE_X1_STEP_KEY_SHARE:
7610 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007611 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7612 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007613 case PSA_PAKE_X1_STEP_ZK_PROOF:
7614 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007615 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007616 return PSA_JPAKE_STEP_INVALID;
7617 }
7618 break;
7619 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007620 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007621 case PSA_PAKE_X1_STEP_KEY_SHARE:
7622 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007623 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7624 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007625 case PSA_PAKE_X1_STEP_ZK_PROOF:
7626 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007627 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007628 return PSA_JPAKE_STEP_INVALID;
7629 }
7630 break;
7631 default:
7632 return PSA_JPAKE_STEP_INVALID;
7633 }
7634 return PSA_JPAKE_STEP_INVALID;
7635}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007636#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007637
Przemek Stekiel2797d372022-12-22 11:19:22 +01007638static psa_status_t psa_pake_complete_inputs(
7639 psa_pake_operation_t *operation)
7640{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007641 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007642 /* Create copy of the inputs on stack as inputs share memory
7643 with the driver context which will be setup by the driver. */
7644 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007645
Przemek Stekielfde11282023-03-13 16:06:09 +01007646 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007647 return PSA_ERROR_BAD_STATE;
7648 }
7649
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007650 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007651 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7652 return PSA_ERROR_BAD_STATE;
7653 }
7654 if (memcmp(inputs.user, jpake_client_id, inputs.user_len) == 0 &&
7655 memcmp(inputs.peer, jpake_server_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007656 inputs.role = PSA_PAKE_ROLE_CLIENT;
7657 } else
Przemek Stekielfde11282023-03-13 16:06:09 +01007658 if (memcmp(inputs.user, jpake_server_id, inputs.user_len) == 0 &&
7659 memcmp(inputs.peer, jpake_client_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007660 inputs.role = PSA_PAKE_ROLE_SERVER;
7661 }
7662
7663 if (inputs.role != PSA_PAKE_ROLE_CLIENT &&
7664 inputs.role != PSA_PAKE_ROLE_SERVER) {
7665 return PSA_ERROR_NOT_SUPPORTED;
7666 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007667 }
7668
Przemek Stekiel18620a32023-01-17 16:34:52 +01007669 /* Clear driver context */
7670 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7671
7672 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007673
7674 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007675 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7676 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007677
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007678 /* User and peer are translated to role. */
7679 mbedtls_free(inputs.user);
7680 mbedtls_free(inputs.peer);
7681
Przemek Stekiel2797d372022-12-22 11:19:22 +01007682 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007683#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007684 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007685 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007686 psa_jpake_computation_stage_t *computation_stage =
7687 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007688 computation_stage->state = PSA_PAKE_STATE_READY;
7689 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7690 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7691 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007692 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007693#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007694 {
7695 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007696 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007697 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007698 return status;
7699}
7700
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007701#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007702static psa_status_t psa_jpake_output_prologue(
7703 psa_pake_operation_t *operation,
7704 psa_pake_step_t step)
7705{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007706 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7707 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7708 step != PSA_PAKE_STEP_ZK_PROOF) {
7709 return PSA_ERROR_INVALID_ARGUMENT;
7710 }
7711
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007712 psa_jpake_computation_stage_t *computation_stage =
7713 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007714
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007715 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7716 return PSA_ERROR_BAD_STATE;
7717 }
7718
7719 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7720 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
7721 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
7722 return PSA_ERROR_BAD_STATE;
7723 }
7724
7725 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7726 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007727 return PSA_ERROR_BAD_STATE;
7728 }
7729
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007730 switch (computation_stage->output_step) {
7731 case PSA_PAKE_STEP_X1_X2:
7732 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007733 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007734 case PSA_PAKE_STEP_X2S:
7735 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007736 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007737 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007738 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007739 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007740
7741 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7742 }
7743
7744 /* Check if step matches current sequence */
7745 switch (computation_stage->sequence) {
7746 case PSA_PAKE_X1_STEP_KEY_SHARE:
7747 case PSA_PAKE_X2_STEP_KEY_SHARE:
7748 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7749 return PSA_ERROR_BAD_STATE;
7750 }
7751 break;
7752
7753 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7754 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7755 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7756 return PSA_ERROR_BAD_STATE;
7757 }
7758 break;
7759
7760 case PSA_PAKE_X1_STEP_ZK_PROOF:
7761 case PSA_PAKE_X2_STEP_ZK_PROOF:
7762 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7763 return PSA_ERROR_BAD_STATE;
7764 }
7765 break;
7766
7767 default:
7768 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007769 }
7770
7771 return PSA_SUCCESS;
7772}
7773
7774static psa_status_t psa_jpake_output_epilogue(
7775 psa_pake_operation_t *operation)
7776{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007777 psa_jpake_computation_stage_t *computation_stage =
7778 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007779
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007780 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007781 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007782 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007783 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007784 computation_stage->state = PSA_PAKE_STATE_READY;
7785 computation_stage->output_step++;
7786 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7787 } else {
7788 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007789 }
7790
7791 return PSA_SUCCESS;
7792}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007793#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007794
Neil Armstronga7d08c32022-06-01 18:21:20 +02007795psa_status_t psa_pake_output(
7796 psa_pake_operation_t *operation,
7797 psa_pake_step_t step,
7798 uint8_t *output,
7799 size_t output_size,
7800 size_t *output_length)
7801{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007802 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007803 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007804 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007805
7806 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007807 status = psa_pake_complete_inputs(operation);
7808 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007809 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007810 }
7811 }
7812
7813 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007814 status = PSA_ERROR_BAD_STATE;
7815 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007816 }
7817
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007818 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007819 status = PSA_ERROR_INVALID_ARGUMENT;
7820 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007821 }
7822
Przemek Stekiele12ed362022-12-21 12:54:46 +01007823 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007824#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007825 case PSA_ALG_JPAKE:
7826 status = psa_jpake_output_prologue(operation, step);
7827 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007828 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007829 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007830 driver_step = convert_jpake_computation_stage_to_driver_step(
7831 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007832 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007833#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007834 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007835 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007836 status = PSA_ERROR_NOT_SUPPORTED;
7837 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007838 }
7839
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007840 status = psa_driver_wrapper_pake_output(operation, driver_step,
7841 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007842
7843 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007844 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007845 }
7846
7847 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007848#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007849 case PSA_ALG_JPAKE:
7850 status = psa_jpake_output_epilogue(operation);
7851 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007852 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007853 }
7854 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007855#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007856 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007857 status = PSA_ERROR_NOT_SUPPORTED;
7858 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007859 }
7860
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007861 return PSA_SUCCESS;
7862exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007863 psa_pake_abort(operation);
7864 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007865}
7866
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007867#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007868static psa_status_t psa_jpake_input_prologue(
7869 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007870 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007871{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007872 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7873 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7874 step != PSA_PAKE_STEP_ZK_PROOF) {
7875 return PSA_ERROR_INVALID_ARGUMENT;
7876 }
7877
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007878 psa_jpake_computation_stage_t *computation_stage =
7879 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007880
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007881 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7882 return PSA_ERROR_BAD_STATE;
7883 }
7884
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007885 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7886 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
7887 computation_stage->state != PSA_PAKE_INPUT_X4S) {
7888 return PSA_ERROR_BAD_STATE;
7889 }
7890
7891 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7892 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007893 return PSA_ERROR_BAD_STATE;
7894 }
7895
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007896 switch (computation_stage->input_step) {
7897 case PSA_PAKE_STEP_X1_X2:
7898 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007899 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007900 case PSA_PAKE_STEP_X2S:
7901 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007902 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007903 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007904 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007905 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007906
7907 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7908 }
7909
7910 /* Check if step matches current sequence */
7911 switch (computation_stage->sequence) {
7912 case PSA_PAKE_X1_STEP_KEY_SHARE:
7913 case PSA_PAKE_X2_STEP_KEY_SHARE:
7914 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7915 return PSA_ERROR_BAD_STATE;
7916 }
7917 break;
7918
7919 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7920 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7921 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7922 return PSA_ERROR_BAD_STATE;
7923 }
7924 break;
7925
7926 case PSA_PAKE_X1_STEP_ZK_PROOF:
7927 case PSA_PAKE_X2_STEP_ZK_PROOF:
7928 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7929 return PSA_ERROR_BAD_STATE;
7930 }
7931 break;
7932
7933 default:
7934 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007935 }
7936
7937 return PSA_SUCCESS;
7938}
7939
Przemek Stekiele12ed362022-12-21 12:54:46 +01007940static psa_status_t psa_jpake_input_epilogue(
7941 psa_pake_operation_t *operation)
7942{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007943 psa_jpake_computation_stage_t *computation_stage =
7944 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007945
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007946 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007947 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007948 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007949 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007950 computation_stage->state = PSA_PAKE_STATE_READY;
7951 computation_stage->input_step++;
7952 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7953 } else {
7954 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007955 }
7956
7957 return PSA_SUCCESS;
7958}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007959#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007960
Neil Armstronga7d08c32022-06-01 18:21:20 +02007961psa_status_t psa_pake_input(
7962 psa_pake_operation_t *operation,
7963 psa_pake_step_t step,
7964 const uint8_t *input,
7965 size_t input_length)
7966{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007967 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007968 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01007969 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
7970 operation->primitive,
7971 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007972
7973 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007974 status = psa_pake_complete_inputs(operation);
7975 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007976 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007977 }
7978 }
7979
7980 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007981 status = PSA_ERROR_BAD_STATE;
7982 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007983 }
7984
Przemek Stekiel256c75d2023-03-23 14:09:34 +01007985 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007986 status = PSA_ERROR_INVALID_ARGUMENT;
7987 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007988 }
7989
Przemek Stekiele12ed362022-12-21 12:54:46 +01007990 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007991#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007992 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007993 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007994 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007995 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007996 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007997 driver_step = convert_jpake_computation_stage_to_driver_step(
7998 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007999 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008000#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008001 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008002 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008003 status = PSA_ERROR_NOT_SUPPORTED;
8004 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008005 }
8006
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008007 status = psa_driver_wrapper_pake_input(operation, driver_step,
8008 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008009
8010 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008011 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008012 }
8013
8014 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008015#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008016 case PSA_ALG_JPAKE:
8017 status = psa_jpake_input_epilogue(operation);
8018 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008019 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008020 }
8021 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008022#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008023 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008024 status = PSA_ERROR_NOT_SUPPORTED;
8025 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008026 }
8027
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008028 return PSA_SUCCESS;
8029exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008030 psa_pake_abort(operation);
8031 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008032}
8033
8034psa_status_t psa_pake_get_implicit_key(
8035 psa_pake_operation_t *operation,
8036 psa_key_derivation_operation_t *output)
8037{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008038 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008039 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008040 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008041 size_t shared_key_len = 0;
8042
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008043 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008044 status = PSA_ERROR_BAD_STATE;
8045 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008046 }
8047
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008048#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008049 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008050 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008051 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008052 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
8053 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008054 status = PSA_ERROR_BAD_STATE;
8055 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008056 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008057 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008058#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008059 {
8060 status = PSA_ERROR_NOT_SUPPORTED;
8061 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008062 }
8063
Przemek Stekiel0c781802022-11-29 14:53:13 +01008064 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8065 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008066 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008067 &shared_key_len);
8068
8069 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008070 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008071 }
8072
8073 status = psa_key_derivation_input_bytes(output,
8074 PSA_KEY_DERIVATION_INPUT_SECRET,
8075 shared_key,
8076 shared_key_len);
8077
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008078 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008079exit:
8080 abort_status = psa_pake_abort(operation);
8081 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008082}
8083
8084psa_status_t psa_pake_abort(
8085 psa_pake_operation_t *operation)
8086{
Przemek Stekield69dca92023-01-31 19:59:20 +01008087 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008088
Przemek Stekield69dca92023-01-31 19:59:20 +01008089 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008090 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008091 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008092
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008093 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8094 if (operation->data.inputs.password != NULL) {
8095 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008096 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008097 mbedtls_free(operation->data.inputs.password);
8098 }
8099 if (operation->data.inputs.user != NULL) {
8100 mbedtls_free(operation->data.inputs.user);
8101 }
8102 if (operation->data.inputs.peer != NULL) {
8103 mbedtls_free(operation->data.inputs.peer);
8104 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008105 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008106 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008107
Przemek Stekield69dca92023-01-31 19:59:20 +01008108 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008109}
Neil Armstronga7d08c32022-06-01 18:21:20 +02008110
Gilles Peskinee59236f2018-01-27 23:32:46 +01008111#endif /* MBEDTLS_PSA_CRYPTO_C */