blob: c7d1b766c1706c5ba84ed9c996b42e8a14920830 [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 Stekiel472b3f32022-12-01 14:38:49 +01001420 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1421 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001423 /* Exporting public -> public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 return psa_export_key_buffer_internal(
1425 key_buffer, key_buffer_size,
1426 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001427 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001430#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1432 return mbedtls_psa_rsa_export_public_key(attributes,
1433 key_buffer,
1434 key_buffer_size,
1435 data,
1436 data_size,
1437 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001438#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001439 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001441#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1442 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001443 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
John Durkop6ba40d12020-11-10 08:50:04 -08001444#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1446 return mbedtls_psa_ecp_export_public_key(attributes,
1447 key_buffer,
1448 key_buffer_size,
1449 data,
1450 data_size,
1451 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001452#else
1453 /* We don't know how to convert a private ECC key to public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001455#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1456 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001457 }
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001458#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) || \
1459 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY)
Przemek Stekielc8298162022-12-05 15:11:49 +01001460 else if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekiel359f4622022-12-05 14:11:55 +01001461 return mbedtls_psa_export_ffdh_public_key(attributes,
1462 key_buffer,
1463 key_buffer_size,
1464 data, data_size,
1465 data_length);
Przemek Stekielc8298162022-12-05 15:11:49 +01001466 } else {
1467 return PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001468 }
1469#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001470 return PSA_ERROR_NOT_SUPPORTED;
1471#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) ||
1472 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001474 /* This shouldn't happen in the reference implementation, but
1475 it is valid for a special-purpose implementation to omit
1476 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001478 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001479}
Ronald Crond18b5f82020-11-25 16:46:05 +01001480
Gilles Peskine449bd832023-01-11 14:50:10 +01001481psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1482 uint8_t *data,
1483 size_t data_size,
1484 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001485{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001486 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001487 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001488 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001489
Ronald Crone907e552021-01-18 13:22:38 +01001490 /* Reject a zero-length output buffer now, since this can never be a
1491 * valid key representation. This way we know that data must be a valid
1492 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 if (data_size == 0) {
1494 return PSA_ERROR_BUFFER_TOO_SMALL;
1495 }
Ronald Crone907e552021-01-18 13:22:38 +01001496
Darryl Greendd8fb772018-11-07 16:00:44 +00001497 /* Set the key to empty now, so that even when there are errors, we always
1498 * set data_length to a value between 0 and data_size. On error, setting
1499 * the key to empty is a good choice because an empty key representation is
1500 * unlikely to be accepted anywhere. */
1501 *data_length = 0;
1502
1503 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1505 if (status != PSA_SUCCESS) {
1506 return status;
1507 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1510 status = PSA_ERROR_INVALID_ARGUMENT;
1511 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001512 }
1513
Ronald Crond18b5f82020-11-25 16:46:05 +01001514 psa_key_attributes_t attributes = {
1515 .core = slot->attr
1516 };
Ronald Cron67227982020-11-26 15:16:05 +01001517 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001518 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001520
1521exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001525}
1526
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001527MBEDTLS_STATIC_ASSERT(
1528 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1529 "One or more key attribute flag is listed as both external-only and dual-use")
1530MBEDTLS_STATIC_ASSERT(
1531 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1532 "One or more key attribute flag is listed as both internal-only and dual-use")
1533MBEDTLS_STATIC_ASSERT(
1534 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1535 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001536
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001537/** Validate that a key policy is internally well-formed.
1538 *
1539 * This function only rejects invalid policies. It does not validate the
1540 * consistency of the policy with respect to other attributes of the key
1541 * such as the key type.
1542 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001543static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001544{
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1546 PSA_KEY_USAGE_COPY |
1547 PSA_KEY_USAGE_ENCRYPT |
1548 PSA_KEY_USAGE_DECRYPT |
1549 PSA_KEY_USAGE_SIGN_MESSAGE |
1550 PSA_KEY_USAGE_VERIFY_MESSAGE |
1551 PSA_KEY_USAGE_SIGN_HASH |
1552 PSA_KEY_USAGE_VERIFY_HASH |
1553 PSA_KEY_USAGE_VERIFY_DERIVATION |
1554 PSA_KEY_USAGE_DERIVE)) != 0) {
1555 return PSA_ERROR_INVALID_ARGUMENT;
1556 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001559}
1560
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001561/** Validate the internal consistency of key attributes.
1562 *
1563 * This function only rejects invalid attribute values. If does not
1564 * validate the consistency of the attributes with any key data that may
1565 * be involved in the creation of the key.
1566 *
1567 * Call this function early in the key creation process.
1568 *
1569 * \param[in] attributes Key attributes for the new key.
1570 * \param[out] p_drv On any return, the driver for the key, if any.
1571 * NULL for a transparent key.
1572 *
1573 */
1574static psa_status_t psa_validate_key_attributes(
1575 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001577{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001578 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1580 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001581
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 status = psa_validate_key_location(lifetime, p_drv);
1583 if (status != PSA_SUCCESS) {
1584 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001585 }
1586
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 status = psa_validate_key_persistence(lifetime);
1588 if (status != PSA_SUCCESS) {
1589 return status;
1590 }
1591
1592 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1593 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1594 return PSA_ERROR_INVALID_ARGUMENT;
1595 }
1596 } else {
1597 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1598 return PSA_ERROR_INVALID_ARGUMENT;
1599 }
1600 }
1601
1602 status = psa_validate_key_policy(&attributes->core.policy);
1603 if (status != PSA_SUCCESS) {
1604 return status;
1605 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001606
1607 /* Refuse to create overly large keys.
1608 * Note that this doesn't trigger on import if the attributes don't
1609 * explicitly specify a size (so psa_get_key_bits returns 0), so
1610 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1612 return PSA_ERROR_NOT_SUPPORTED;
1613 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001614
Gilles Peskine91e8c332019-08-02 19:19:39 +02001615 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1617 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1618 return PSA_ERROR_INVALID_ARGUMENT;
1619 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001620
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001622}
1623
Gilles Peskine4747d192019-04-17 15:05:45 +02001624/** Prepare a key slot to receive key material.
1625 *
1626 * This function allocates a key slot and sets its metadata.
1627 *
1628 * If this function fails, call psa_fail_key_creation().
1629 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001630 * This function is intended to be used as follows:
1631 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001632 * it with the specified attributes, and in case of a volatile key assign it
1633 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001634 * -# Populate the slot with the key material.
1635 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1636 * In case of failure at any step, stop the sequence and call
1637 * psa_fail_key_creation().
1638 *
Ronald Cron5c522922020-11-14 16:35:34 +01001639 * On success, the key slot is locked. It is the responsibility of the caller
1640 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001641 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001642 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001643 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001644 * \param[out] p_slot On success, a pointer to the prepared slot.
1645 * \param[out] p_drv On any return, the driver for the key, if any.
1646 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001647 *
1648 * \retval #PSA_SUCCESS
1649 * The key slot is ready to receive key material.
1650 * \return If this function fails, the key slot is an invalid state.
1651 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001652 */
1653static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001654 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001655 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001656 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001658{
1659 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001660 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001661 psa_key_slot_t *slot;
1662
Gilles Peskinedf179142019-07-15 22:02:14 +02001663 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001664 *p_drv = NULL;
1665
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 status = psa_validate_key_attributes(attributes, p_drv);
1667 if (status != PSA_SUCCESS) {
1668 return status;
1669 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001670
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1672 if (status != PSA_SUCCESS) {
1673 return status;
1674 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001675 slot = *p_slot;
1676
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001677 /* We're storing the declared bit-size of the key. It's up to each
1678 * creation mechanism to verify that this information is correct.
1679 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001680 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001681 * is optional (import, copy). In case of a volatile key, assign it the
1682 * volatile key identifier associated to the slot returned to contain its
1683 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001684
1685 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001687#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1688 slot->attr.id = volatile_key_id;
1689#else
1690 slot->attr.id.key_id = volatile_key_id;
1691#endif
1692 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001693
Gilles Peskine91e8c332019-08-02 19:19:39 +02001694 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001695 * external-only flags, query `attributes`. Thanks to the check
1696 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001697 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001698 * may have set. */
1699 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001700
Gilles Peskinecbaff462019-07-12 23:46:04 +02001701#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001702 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001703 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001704 * create the key file in internal storage, create the
1705 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001706 * persistent data. This is done by starting a transaction that will
1707 * encompass these three actions.
1708 * For registering a volatile key, we just need to find an appropriate
1709 * slot number inside the SE. Since the key is designated volatile, creating
1710 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001711 /* The first thing to do is to find a slot number for the new key.
1712 * We save the slot number in persistent storage as part of the
1713 * transaction data. It will be needed to recover if the power
1714 * fails during the key creation process, to clean up on the secure
1715 * element side after restarting. Obtaining a slot number from the
1716 * secure element driver updates its persistent state, but we do not yet
1717 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001718 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001720 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1722 &slot_number);
1723 if (status != PSA_SUCCESS) {
1724 return status;
1725 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001726
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1728 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001729 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001730 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001731 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 status = psa_crypto_save_transaction();
1733 if (status != PSA_SUCCESS) {
1734 (void) psa_crypto_stop_transaction();
1735 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001736 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001737 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001738
1739 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001741 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001742
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001744 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001746 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001747#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1748
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001750}
Gilles Peskine4747d192019-04-17 15:05:45 +02001751
1752/** Finalize the creation of a key once its key material has been set.
1753 *
1754 * This entails writing the key to persistent storage.
1755 *
1756 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001757 * See the documentation of psa_start_key_creation() for the intended use
1758 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001759 *
Ronald Cron5c522922020-11-14 16:35:34 +01001760 * If the finalization succeeds, the function unlocks the key slot (it was
1761 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1762 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001763 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001764 * \param[in,out] slot Pointer to the slot with key material.
1765 * \param[in] driver The secure element driver for the key,
1766 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001767 * \param[out] key On success, identifier of the key. Note that the
1768 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001769 *
1770 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001771 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001772 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1773 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1774 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1775 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1776 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1777 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001778 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001779 * \return If this function fails, the key slot is an invalid state.
1780 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001781 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001782static psa_status_t psa_finish_key_creation(
1783 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001784 psa_se_drv_table_entry_t *driver,
1785 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001786{
1787 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001788 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001789 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001790
1791#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001793#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001795 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001796 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001798
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001799 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1800 sizeof(data.slot_number),
1801 "Slot number size does not match psa_se_key_data_storage_t");
1802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1804 status = psa_save_persistent_key(&slot->attr,
1805 (uint8_t *) &data,
1806 sizeof(data));
1807 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001808#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1809 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001810 /* Key material is saved in export representation in the slot, so
1811 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 status = psa_save_persistent_key(&slot->attr,
1813 slot->key.data,
1814 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001815 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001816 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001817#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1818
Gilles Peskinecbaff462019-07-12 23:46:04 +02001819#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001820 /* Finish the transaction for a key creation. This does not
1821 * happen when registering an existing key. Detect this case
1822 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001823 * creation of a persistent key in a secure element requires a transaction,
1824 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 if (driver != NULL &&
1826 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1827 status = psa_save_se_persistent_data(driver);
1828 if (status != PSA_SUCCESS) {
1829 psa_destroy_persistent_key(slot->attr.id);
1830 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001831 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001833 }
1834#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001837 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 status = psa_unlock_key_slot(slot);
1839 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001840 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001842 }
Ronald Cron50972942020-11-14 11:28:25 +01001843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001845}
1846
1847/** Abort the creation of a key.
1848 *
1849 * You may call this function after calling psa_start_key_creation(),
1850 * or after psa_finish_key_creation() fails. In other circumstances, this
1851 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001852 * See the documentation of psa_start_key_creation() for the intended use
1853 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001854 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001855 * \param[in,out] slot Pointer to the slot with key material.
1856 * \param[in] driver The secure element driver for the key,
1857 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001858 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001859static void psa_fail_key_creation(psa_key_slot_t *slot,
1860 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001861{
Gilles Peskine011e4282019-06-26 18:34:38 +02001862 (void) driver;
1863
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001865 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001867
1868#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001869 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001870 * element, and the failure happened later (when saving metadata
1871 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001872 * element.
1873 * https://github.com/ARMmbed/mbed-crypto/issues/217
1874 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001875
Gilles Peskined7729582019-08-05 15:55:54 +02001876 /* Abort the ongoing transaction if any (there may not be one if
1877 * the creation process failed before starting one, or if the
1878 * key creation is a registration of a key in a secure element).
1879 * Earlier functions must already have done what it takes to undo any
1880 * partial creation. All that's left is to update the transaction data
1881 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001883#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001886}
1887
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001888/** Validate optional attributes during key creation.
1889 *
1890 * Some key attributes are optional during key creation. If they are
1891 * specified in the attributes structure, check that they are consistent
1892 * with the data in the slot.
1893 *
1894 * This function should be called near the end of key creation, after
1895 * the slot in memory is fully populated but before saving persistent data.
1896 */
1897static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001898 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001900{
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 if (attributes->core.type != 0) {
1902 if (attributes->core.type != slot->attr.type) {
1903 return PSA_ERROR_INVALID_ARGUMENT;
1904 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001905 }
1906
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001908#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1910 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001911 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001912 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001913 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001914
Ronald Cron90857082020-11-25 14:58:33 +01001915 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 slot->attr.type,
1917 slot->key.data,
1918 slot->key.bytes,
1919 &rsa);
1920 if (status != PSA_SUCCESS) {
1921 return status;
1922 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001923
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 mbedtls_mpi_init(&actual);
1925 mbedtls_mpi_init(&required);
1926 ret = mbedtls_rsa_export(rsa,
1927 NULL, NULL, NULL, NULL, &actual);
1928 mbedtls_rsa_free(rsa);
1929 mbedtls_free(rsa);
1930 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001931 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 }
1933 ret = mbedtls_mpi_read_binary(&required,
1934 attributes->domain_parameters,
1935 attributes->domain_parameters_size);
1936 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001937 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 }
1939 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001940 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 }
1942rsa_exit:
1943 mbedtls_mpi_free(&actual);
1944 mbedtls_mpi_free(&required);
1945 if (ret != 0) {
1946 return mbedtls_to_psa_error(ret);
1947 }
1948 } else
John Durkop9814fa22020-11-04 12:28:15 -08001949#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1950 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001951 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001953 }
1954 }
1955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 if (attributes->core.bits != 0) {
1957 if (attributes->core.bits != slot->attr.bits) {
1958 return PSA_ERROR_INVALID_ARGUMENT;
1959 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001960 }
1961
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001963}
1964
Gilles Peskine449bd832023-01-11 14:50:10 +01001965psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1966 const uint8_t *data,
1967 size_t data_length,
1968 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001969{
1970 psa_status_t status;
1971 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001972 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001973 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301974 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001975
Ronald Cron81709fc2020-11-14 12:10:32 +01001976 *key = MBEDTLS_SVC_KEY_ID_INIT;
1977
Gilles Peskine0f84d622019-09-12 19:03:13 +02001978 /* Reject zero-length symmetric keys (including raw data key objects).
1979 * This also rejects any key which might be encoded as an empty string,
1980 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 if (data_length == 0) {
1982 return PSA_ERROR_INVALID_ARGUMENT;
1983 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001984
Archana449608b2021-09-08 15:36:05 +05301985 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 if (data_length > SIZE_MAX / 8) {
1987 return PSA_ERROR_NOT_SUPPORTED;
1988 }
Archana6ed4bda2021-08-04 10:47:15 +05301989
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1991 &slot, &driver);
1992 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001993 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001995
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001996 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301997 * storage ( thus not in the case of importing a key in a secure element
1998 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1999 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 if (slot->key.data == NULL) {
2001 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302002 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 attributes, data, data_length, &storage_size);
2004 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302005 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 }
Archanad8a83dc2021-06-14 10:04:16 +05302007 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 status = psa_allocate_buffer_to_slot(slot, storage_size);
2009 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002010 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002012 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002013
2014 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 status = psa_driver_wrapper_import_key(attributes,
2016 data, data_length,
2017 slot->key.data,
2018 slot->key.bytes,
2019 &slot->key.bytes, &bits);
2020 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002021 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002025 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002027 status = PSA_ERROR_INVALID_ARGUMENT;
2028 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002029 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002030
Archana6ed4bda2021-08-04 10:47:15 +05302031 /* Enforce a size limit, and in particular ensure that the bit
2032 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302034 status = PSA_ERROR_NOT_SUPPORTED;
2035 goto exit;
2036 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 status = psa_validate_optional_attributes(slot, attributes);
2038 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002039 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002043exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 if (status != PSA_SUCCESS) {
2045 psa_fail_key_creation(slot, driver);
2046 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002047
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002049}
2050
Gilles Peskined7729582019-08-05 15:55:54 +02002051#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2052psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002054{
2055 psa_status_t status;
2056 psa_key_slot_t *slot = NULL;
2057 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002058 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002059
2060 /* Leaving attributes unspecified is not currently supported.
2061 * It could make sense to query the key type and size from the
2062 * secure element, but not all secure elements support this
2063 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2065 return PSA_ERROR_NOT_SUPPORTED;
2066 }
2067 if (psa_get_key_bits(attributes) == 0) {
2068 return PSA_ERROR_NOT_SUPPORTED;
2069 }
Gilles Peskined7729582019-08-05 15:55:54 +02002070
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2072 &slot, &driver);
2073 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002074 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 }
Gilles Peskined7729582019-08-05 15:55:54 +02002076
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002078
2079exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 if (status != PSA_SUCCESS) {
2081 psa_fail_key_creation(slot, driver);
2082 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002083
Gilles Peskined7729582019-08-05 15:55:54 +02002084 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 psa_close_key(key);
2086 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002087}
2088#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2089
Gilles Peskine449bd832023-01-11 14:50:10 +01002090psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2091 const psa_key_attributes_t *specified_attributes,
2092 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002093{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002094 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002095 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002096 psa_key_slot_t *source_slot = NULL;
2097 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002098 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002099 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302100 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002101
Ronald Cron81709fc2020-11-14 12:10:32 +01002102 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2103
Archana8a180362021-07-05 02:18:48 +05302104 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2106 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002107 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 status = psa_validate_optional_attributes(source_slot,
2111 specified_attributes);
2112 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002113 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002115
Archana9d17bf42021-09-10 06:22:44 +05302116 /* The target key type and number of bits have been validated by
2117 * psa_validate_optional_attributes() to be either equal to zero or
2118 * equal to the ones of the source key. So it is safe to inherit
2119 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302120 * */
Archana9d17bf42021-09-10 06:22:44 +05302121 actual_attributes.core.bits = source_slot->attr.bits;
2122 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302123
2124
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 status = psa_restrict_key_policy(source_slot->attr.type,
2126 &actual_attributes.core.policy,
2127 &source_slot->attr.policy);
2128 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002129 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002131
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2133 &target_slot, &driver);
2134 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002135 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 }
2137 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2138 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302139 /*
Archana9d17bf42021-09-10 06:22:44 +05302140 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302141 * the source key would need to be exported as plaintext and re-imported
2142 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302143 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302144 * appropriate API invocations from the application, if needed.
2145 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002146 status = PSA_ERROR_NOT_SUPPORTED;
2147 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002148 }
Archana8a180362021-07-05 02:18:48 +05302149 /*
2150 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302151 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302152 * - For opaque keys this translates to an invocation of the drivers'
2153 * copy_key entry point through the dispatch layer.
2154 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2156 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2157 &storage_size);
2158 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302159 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 }
Archana374fe5b2021-09-08 15:50:28 +05302161
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2163 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302164 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 }
Archana374fe5b2021-09-08 15:50:28 +05302166
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 status = psa_driver_wrapper_copy_key(&actual_attributes,
2168 source_slot->key.data,
2169 source_slot->key.bytes,
2170 target_slot->key.data,
2171 target_slot->key.bytes,
2172 &target_slot->key.bytes);
2173 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302174 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 }
2176 } else {
2177 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302178 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 source_slot->key.bytes);
2180 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302181 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 }
Archana8a180362021-07-05 02:18:48 +05302183 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002185exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 if (status != PSA_SUCCESS) {
2187 psa_fail_key_creation(target_slot, driver);
2188 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002189
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002191
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002193}
2194
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002195
2196
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002197/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002198/* Message digests */
2199/****************************************************************/
2200
Gilles Peskine449bd832023-01-11 14:50:10 +01002201psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002202{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002203 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 if (operation->id == 0) {
2205 return PSA_SUCCESS;
2206 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002209 operation->id = 0;
2210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002212}
2213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2215 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002216{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002217 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002218
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002219 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002221 status = PSA_ERROR_BAD_STATE;
2222 goto exit;
2223 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002226 status = PSA_ERROR_INVALID_ARGUMENT;
2227 goto exit;
2228 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002229
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002230 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2231 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002235
2236exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 if (status != PSA_SUCCESS) {
2238 psa_hash_abort(operation);
2239 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002240
2241 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002242}
2243
Gilles Peskine449bd832023-01-11 14:50:10 +01002244psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2245 const uint8_t *input,
2246 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002247{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002248 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002251 status = PSA_ERROR_BAD_STATE;
2252 goto exit;
2253 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002254
Steven Cooremanc8288352021-03-04 14:02:19 +01002255 /* Don't require hash implementations to behave correctly on a
2256 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 if (input_length == 0) {
2258 return PSA_SUCCESS;
2259 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002260
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002262
2263exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 if (status != PSA_SUCCESS) {
2265 psa_hash_abort(operation);
2266 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002267
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002269}
2270
Gilles Peskine449bd832023-01-11 14:50:10 +01002271psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2272 uint8_t *hash,
2273 size_t hash_size,
2274 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002275{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002276 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 if (operation->id == 0) {
2278 return PSA_ERROR_BAD_STATE;
2279 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002280
Steven Cooreman1e582352021-02-18 17:24:37 +01002281 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 operation, hash, hash_size, hash_length);
2283 psa_hash_abort(operation);
2284 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002285}
2286
Gilles Peskine449bd832023-01-11 14:50:10 +01002287psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2288 const uint8_t *hash,
2289 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002290{
Ronald Cron69a63422021-10-18 09:47:58 +02002291 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002292 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002293 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 operation,
2295 actual_hash, sizeof(actual_hash),
2296 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002299 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002301
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002303 status = PSA_ERROR_INVALID_SIGNATURE;
2304 goto exit;
2305 }
2306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002308 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002310
2311exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2313 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002314 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002316
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002318}
2319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320psa_status_t psa_hash_compute(psa_algorithm_t alg,
2321 const uint8_t *input, size_t input_length,
2322 uint8_t *hash, size_t hash_size,
2323 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002324{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002325 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 if (!PSA_ALG_IS_HASH(alg)) {
2327 return PSA_ERROR_INVALID_ARGUMENT;
2328 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2331 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002332}
2333
Gilles Peskine449bd832023-01-11 14:50:10 +01002334psa_status_t psa_hash_compare(psa_algorithm_t alg,
2335 const uint8_t *input, size_t input_length,
2336 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002337{
Ronald Cron69a63422021-10-18 09:47:58 +02002338 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002339 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 if (!PSA_ALG_IS_HASH(alg)) {
2342 return PSA_ERROR_INVALID_ARGUMENT;
2343 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002344
Steven Cooreman1e582352021-02-18 17:24:37 +01002345 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 alg, input, input_length,
2347 actual_hash, sizeof(actual_hash),
2348 &actual_hash_length);
2349 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002350 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 }
2352 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002353 status = PSA_ERROR_INVALID_SIGNATURE;
2354 goto exit;
2355 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002357 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002359
2360exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2362 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002363}
2364
Gilles Peskine449bd832023-01-11 14:50:10 +01002365psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2366 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002367{
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 if (source_operation->id == 0 ||
2369 target_operation->id != 0) {
2370 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002371 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002372
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2374 target_operation);
2375 if (status != PSA_SUCCESS) {
2376 psa_hash_abort(target_operation);
2377 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002378
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002380}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002381
2382
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002383/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002384/* MAC */
2385/****************************************************************/
2386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002388{
Steven Cooremane6804192021-03-19 18:28:56 +01002389 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 if (operation->id == 0) {
2391 return PSA_SUCCESS;
2392 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002393
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002395 operation->mac_size = 0;
2396 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002397 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002398
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002400}
2401
Ronald Cron2dff3b22021-06-17 16:33:22 +02002402static psa_status_t psa_mac_finalize_alg_and_key_validation(
2403 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002404 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002406{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002407 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 psa_key_type_t key_type = psa_get_key_type(attributes);
2409 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 if (!PSA_ALG_IS_MAC(alg)) {
2412 return PSA_ERROR_INVALID_ARGUMENT;
2413 }
Steven Cooremane6804192021-03-19 18:28:56 +01002414
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002415 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 status = psa_mac_key_can_do(alg, key_type);
2417 if (status != PSA_SUCCESS) {
2418 return status;
2419 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002420
Steven Cooremand1a68f12021-05-10 11:13:41 +02002421 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002425 /* A very short MAC is too short for security since it can be
2426 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2427 * so we make this our minimum, even though 32 bits is still
2428 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002430 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002431
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2433 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002434 /* It's impossible to "truncate" to a larger length than the full length
2435 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002437 }
2438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002440 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2441 * that is disabled in the compile-time configuration. The result can
2442 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2443 * configuration into account. In this case, force a return of
2444 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2445 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2446 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2447 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2448 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002450 }
2451
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002453}
2454
Gilles Peskine449bd832023-01-11 14:50:10 +01002455static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2456 mbedtls_svc_key_id_t key,
2457 psa_algorithm_t alg,
2458 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002459{
2460 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2461 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002462 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002463
2464 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002466 status = PSA_ERROR_BAD_STATE;
2467 goto exit;
2468 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002469
2470 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 key,
2472 &slot,
2473 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2474 alg);
2475 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002476 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002478
2479 psa_key_attributes_t attributes = {
2480 .core = slot->attr
2481 };
2482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2484 &operation->mac_size);
2485 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002486 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002488
2489 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002490 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 if (is_sign) {
2492 status = psa_driver_wrapper_mac_sign_setup(operation,
2493 &attributes,
2494 slot->key.data,
2495 slot->key.bytes,
2496 alg);
2497 } else {
2498 status = psa_driver_wrapper_mac_verify_setup(operation,
2499 &attributes,
2500 slot->key.data,
2501 slot->key.bytes,
2502 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002503 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002504
Gilles Peskinefbfac682018-07-08 20:51:54 +02002505exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 if (status != PSA_SUCCESS) {
2507 psa_mac_abort(operation);
2508 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002513}
2514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2516 mbedtls_svc_key_id_t key,
2517 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002518{
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002520}
2521
Gilles Peskine449bd832023-01-11 14:50:10 +01002522psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2523 mbedtls_svc_key_id_t key,
2524 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002525{
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002527}
2528
Gilles Peskine449bd832023-01-11 14:50:10 +01002529psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2530 const uint8_t *input,
2531 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002532{
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 if (operation->id == 0) {
2534 return PSA_ERROR_BAD_STATE;
2535 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002536
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002537 /* Don't require hash implementations to behave correctly on a
2538 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 if (input_length == 0) {
2540 return PSA_SUCCESS;
2541 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002542
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2544 input, input_length);
2545 if (status != PSA_SUCCESS) {
2546 psa_mac_abort(operation);
2547 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002548
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002550}
2551
Gilles Peskine449bd832023-01-11 14:50:10 +01002552psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2553 uint8_t *mac,
2554 size_t mac_size,
2555 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002556{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002557 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2558 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002561 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002562 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002563 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002566 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002567 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002568 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002569
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002570 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2571 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002573 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002574 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002575 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002576
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002578 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002579 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002580 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 status = psa_driver_wrapper_mac_sign_finish(operation,
2583 mac, operation->mac_size,
2584 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002585
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002586exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002587 /* In case of success, set the potential excess room in the output buffer
2588 * to an invalid value, to avoid potentially leaking a longer MAC.
2589 * In case of error, set the output length and content to a safe default,
2590 * such that in case the caller misses an error check, the output would be
2591 * an unachievable MAC.
2592 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002594 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002595 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002596 }
mohammad16036df908f2018-04-02 08:34:15 -07002597
Paul Elliottdc42ca82023-02-24 18:11:59 +00002598 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002599
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002603}
2604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2606 const uint8_t *mac,
2607 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002608{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002609 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2610 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002611
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002613 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002614 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002615 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002616
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002618 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002619 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002620 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002621
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002623 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002624 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002625 }
2626
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 status = psa_driver_wrapper_mac_verify_finish(operation,
2628 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002629
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002630exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002632
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002634}
2635
Gilles Peskine449bd832023-01-11 14:50:10 +01002636static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2637 psa_algorithm_t alg,
2638 const uint8_t *input,
2639 size_t input_length,
2640 uint8_t *mac,
2641 size_t mac_size,
2642 size_t *mac_length,
2643 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002644{
2645 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002646 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2647 psa_key_slot_t *slot;
2648 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002649
Ronald Cron51131b52021-06-17 17:17:20 +02002650 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 key,
2652 &slot,
2653 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2654 alg);
2655 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002656 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002658
Ronald Cron51131b52021-06-17 17:17:20 +02002659 psa_key_attributes_t attributes = {
2660 .core = slot->attr
2661 };
2662
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2664 &operation_mac_size);
2665 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002666 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002670 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002671 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002672 }
2673
2674 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 &attributes,
2676 slot->key.data, slot->key.bytes,
2677 alg,
2678 input, input_length,
2679 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002680
2681exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002682 /* In case of success, set the potential excess room in the output buffer
2683 * to an invalid value, to avoid potentially leaking a longer MAC.
2684 * In case of error, set the output length and content to a safe default,
2685 * such that in case the caller misses an error check, the output would be
2686 * an unachievable MAC.
2687 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002689 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002690 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002691 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002692
2693 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002694
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002696
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002698}
2699
Gilles Peskine449bd832023-01-11 14:50:10 +01002700psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002701 psa_algorithm_t alg,
2702 const uint8_t *input,
2703 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 uint8_t *mac,
2705 size_t mac_size,
2706 size_t *mac_length)
2707{
2708 return psa_mac_compute_internal(key, alg,
2709 input, input_length,
2710 mac, mac_size, mac_length, 1);
2711}
2712
2713psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2714 psa_algorithm_t alg,
2715 const uint8_t *input,
2716 size_t input_length,
2717 const uint8_t *mac,
2718 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002719{
2720 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002721 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2722 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 status = psa_mac_compute_internal(key, alg,
2725 input, input_length,
2726 actual_mac, sizeof(actual_mac),
2727 &actual_mac_length, 0);
2728 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002729 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002731
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002733 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002734 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002735 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002737 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002738 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002739 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002740
2741exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002743
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002745}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002746
Gilles Peskinee59236f2018-01-27 23:32:46 +01002747/****************************************************************/
2748/* Asymmetric cryptography */
2749/****************************************************************/
2750
Gilles Peskine449bd832023-01-11 14:50:10 +01002751static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2752 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002753{
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 if (input_is_message) {
2755 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2756 return PSA_ERROR_INVALID_ARGUMENT;
2757 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002758
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2760 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2761 return PSA_ERROR_INVALID_ARGUMENT;
2762 }
2763 }
2764 } else {
2765 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2766 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002767 }
2768 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002769
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002771}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002772
Gilles Peskine449bd832023-01-11 14:50:10 +01002773static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2774 int input_is_message,
2775 psa_algorithm_t alg,
2776 const uint8_t *input,
2777 size_t input_length,
2778 uint8_t *signature,
2779 size_t signature_size,
2780 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002781{
2782 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2783 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2784 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002785
2786 *signature_length = 0;
2787
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 status = psa_sign_verify_check_alg(input_is_message, alg);
2789 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002790 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002792
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002793 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002794 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002795 * buffer can in principle be empty since it doesn't actually have
2796 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002797 if (signature_size == 0) {
2798 return PSA_ERROR_BUFFER_TOO_SMALL;
2799 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002800
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002801 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 key, &slot,
2803 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2804 PSA_KEY_USAGE_SIGN_HASH,
2805 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002806
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002808 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002810
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002812 status = PSA_ERROR_INVALID_ARGUMENT;
2813 goto exit;
2814 }
2815
2816 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002818 };
2819
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002821 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002822 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002823 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 signature, signature_size, signature_length);
2825 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002826
2827 status = psa_driver_wrapper_sign_hash(
2828 &attributes, slot->key.data, slot->key.bytes,
2829 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002831 }
2832
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002833
2834exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002835 psa_wipe_tag_output_buffer(signature, status, signature_size,
2836 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002837
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002839
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002841}
2842
Gilles Peskine449bd832023-01-11 14:50:10 +01002843static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2844 int input_is_message,
2845 psa_algorithm_t alg,
2846 const uint8_t *input,
2847 size_t input_length,
2848 const uint8_t *signature,
2849 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002850{
2851 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2852 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2853 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002854
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 status = psa_sign_verify_check_alg(input_is_message, alg);
2856 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002857 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002859
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002860 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 key, &slot,
2862 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2863 PSA_KEY_USAGE_VERIFY_HASH,
2864 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002865
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 if (status != PSA_SUCCESS) {
2867 return status;
2868 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002869
2870 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002872 };
2873
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002875 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002876 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002877 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 signature, signature_length);
2879 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002880 status = psa_driver_wrapper_verify_hash(
2881 &attributes, slot->key.data, slot->key.bytes,
2882 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002884 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002885
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002887
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002889
2890}
2891
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002892psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002893 const psa_key_attributes_t *attributes,
2894 const uint8_t *key_buffer,
2895 size_t key_buffer_size,
2896 psa_algorithm_t alg,
2897 const uint8_t *input,
2898 size_t input_length,
2899 uint8_t *signature,
2900 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002902{
2903 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002904
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002906 size_t hash_length;
2907 uint8_t hash[PSA_HASH_MAX_SIZE];
2908
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002909 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 PSA_ALG_SIGN_GET_HASH(alg),
2911 input, input_length,
2912 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002913
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002915 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002917
2918 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 attributes, key_buffer, key_buffer_size,
2920 alg, hash, hash_length,
2921 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002922 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002923
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002925}
2926
Gilles Peskine449bd832023-01-11 14:50:10 +01002927psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2928 psa_algorithm_t alg,
2929 const uint8_t *input,
2930 size_t input_length,
2931 uint8_t *signature,
2932 size_t signature_size,
2933 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002934{
2935 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002936 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002938}
2939
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002940psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002941 const psa_key_attributes_t *attributes,
2942 const uint8_t *key_buffer,
2943 size_t key_buffer_size,
2944 psa_algorithm_t alg,
2945 const uint8_t *input,
2946 size_t input_length,
2947 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002949{
2950 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002951
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002953 size_t hash_length;
2954 uint8_t hash[PSA_HASH_MAX_SIZE];
2955
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002956 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 PSA_ALG_SIGN_GET_HASH(alg),
2958 input, input_length,
2959 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002960
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002962 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002964
2965 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 attributes, key_buffer, key_buffer_size,
2967 alg, hash, hash_length,
2968 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002969 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002970
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002972}
2973
Gilles Peskine449bd832023-01-11 14:50:10 +01002974psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2975 psa_algorithm_t alg,
2976 const uint8_t *input,
2977 size_t input_length,
2978 const uint8_t *signature,
2979 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002980{
2981 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002982 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002984}
2985
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002986psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002987 const psa_key_attributes_t *attributes,
2988 const uint8_t *key_buffer, size_t key_buffer_size,
2989 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002991{
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2993 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2994 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002995#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2997 return mbedtls_psa_rsa_sign_hash(
2998 attributes,
2999 key_buffer, key_buffer_size,
3000 alg, hash, hash_length,
3001 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003002#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3003 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 } else {
3005 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003006 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3008 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003009#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3011 return mbedtls_psa_ecdsa_sign_hash(
3012 attributes,
3013 key_buffer, key_buffer_size,
3014 alg, hash, hash_length,
3015 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003016#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3017 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 } else {
3019 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003020 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003021 }
Ronald Cron4501c982021-03-19 20:09:32 +01003022
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 (void) key_buffer;
3024 (void) key_buffer_size;
3025 (void) hash;
3026 (void) hash_length;
3027 (void) signature;
3028 (void) signature_size;
3029 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003030
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003032}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003033
Gilles Peskine449bd832023-01-11 14:50:10 +01003034psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3035 psa_algorithm_t alg,
3036 const uint8_t *hash,
3037 size_t hash_length,
3038 uint8_t *signature,
3039 size_t signature_size,
3040 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003041{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003042 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003043 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003045}
3046
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003047psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003048 const psa_key_attributes_t *attributes,
3049 const uint8_t *key_buffer, size_t key_buffer_size,
3050 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003052{
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3054 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3055 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003056#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3058 return mbedtls_psa_rsa_verify_hash(
3059 attributes,
3060 key_buffer, key_buffer_size,
3061 alg, hash, hash_length,
3062 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003063#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3064 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 } else {
3066 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003067 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3069 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003070#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3072 return mbedtls_psa_ecdsa_verify_hash(
3073 attributes,
3074 key_buffer, key_buffer_size,
3075 alg, hash, hash_length,
3076 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003077#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3078 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 } else {
3080 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003081 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003082 }
Ronald Cron4501c982021-03-19 20:09:32 +01003083
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 (void) key_buffer;
3085 (void) key_buffer_size;
3086 (void) hash;
3087 (void) hash_length;
3088 (void) signature;
3089 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003090
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003092}
3093
Gilles Peskine449bd832023-01-11 14:50:10 +01003094psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3095 psa_algorithm_t alg,
3096 const uint8_t *hash,
3097 size_t hash_length,
3098 const uint8_t *signature,
3099 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003100{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003101 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003102 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003104}
3105
Gilles Peskine449bd832023-01-11 14:50:10 +01003106psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3107 psa_algorithm_t alg,
3108 const uint8_t *input,
3109 size_t input_length,
3110 const uint8_t *salt,
3111 size_t salt_length,
3112 uint8_t *output,
3113 size_t output_size,
3114 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003115{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003116 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003117 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003118 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003119
Darryl Green5cc689a2018-07-24 15:34:10 +01003120 (void) input;
3121 (void) input_length;
3122 (void) salt;
3123 (void) output;
3124 (void) output_size;
3125
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003126 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003127
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3129 return PSA_ERROR_INVALID_ARGUMENT;
3130 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003131
Ronald Cron5c522922020-11-14 16:35:34 +01003132 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3134 if (status != PSA_SUCCESS) {
3135 return status;
3136 }
3137 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3138 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003139 status = PSA_ERROR_INVALID_ARGUMENT;
3140 goto exit;
3141 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003142
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003143 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003145 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003146
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003147 status = psa_driver_wrapper_asymmetric_encrypt(
3148 &attributes, slot->key.data, slot->key.bytes,
3149 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003150 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003151exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003153
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003155}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003156
Gilles Peskine449bd832023-01-11 14:50:10 +01003157psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3158 psa_algorithm_t alg,
3159 const uint8_t *input,
3160 size_t input_length,
3161 const uint8_t *salt,
3162 size_t salt_length,
3163 uint8_t *output,
3164 size_t output_size,
3165 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003166{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003167 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003168 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003169 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003170
Darryl Green5cc689a2018-07-24 15:34:10 +01003171 (void) input;
3172 (void) input_length;
3173 (void) salt;
3174 (void) output;
3175 (void) output_size;
3176
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003177 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003178
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3180 return PSA_ERROR_INVALID_ARGUMENT;
3181 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003182
Ronald Cron5c522922020-11-14 16:35:34 +01003183 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3185 if (status != PSA_SUCCESS) {
3186 return status;
3187 }
3188 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003189 status = PSA_ERROR_INVALID_ARGUMENT;
3190 goto exit;
3191 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003192
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003193 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003195 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003196
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003197 status = psa_driver_wrapper_asymmetric_decrypt(
3198 &attributes, slot->key.data, slot->key.bytes,
3199 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003200 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003201
3202exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003204
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003206}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003207
Paul Elliott9fe12f62022-11-30 19:16:02 +00003208/****************************************************************/
3209/* Asymmetric interruptible cryptography */
3210/****************************************************************/
3211
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003212static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3213
Paul Elliott9fe12f62022-11-30 19:16:02 +00003214void psa_interruptible_set_max_ops(uint32_t max_ops)
3215{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003216 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003217}
3218
3219uint32_t psa_interruptible_get_max_ops(void)
3220{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003221 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003222}
3223
Paul Elliott9fe12f62022-11-30 19:16:02 +00003224uint32_t psa_sign_hash_get_num_ops(
3225 const psa_sign_hash_interruptible_operation_t *operation)
3226{
Paul Elliott296ede92022-12-15 17:00:30 +00003227 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003228}
3229
3230uint32_t psa_verify_hash_get_num_ops(
3231 const psa_verify_hash_interruptible_operation_t *operation)
3232{
Paul Elliott296ede92022-12-15 17:00:30 +00003233 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003234}
3235
Paul Elliott59ad9452022-12-18 15:09:02 +00003236static psa_status_t psa_sign_hash_abort_internal(
3237 psa_sign_hash_interruptible_operation_t *operation)
3238{
3239 if (operation->id == 0) {
3240 /* The object has (apparently) been initialized but it is not (yet)
3241 * in use. It's ok to call abort on such an object, and there's
3242 * nothing to do. */
3243 return PSA_SUCCESS;
3244 }
3245
3246 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3247
3248 status = psa_driver_wrapper_sign_hash_abort(operation);
3249
3250 operation->id = 0;
3251
Paul Elliotte6145dc2023-02-07 12:51:21 +00003252 /* Do not clear either the error_occurred or num_ops elements here as they
3253 * only want to be cleared by the application calling abort, not by abort
3254 * being called at completion of an operation. */
3255
Paul Elliott59ad9452022-12-18 15:09:02 +00003256 return status;
3257}
3258
Paul Elliott9fe12f62022-11-30 19:16:02 +00003259psa_status_t psa_sign_hash_start(
3260 psa_sign_hash_interruptible_operation_t *operation,
3261 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3262 const uint8_t *hash, size_t hash_length)
3263{
3264 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3265 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3266 psa_key_slot_t *slot;
3267
Paul Elliottc9774412023-02-06 15:14:07 +00003268 /* Check that start has not been previously called, or operation has not
3269 * previously errored. */
3270 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003271 return PSA_ERROR_BAD_STATE;
3272 }
3273
Paul Elliott9fe12f62022-11-30 19:16:02 +00003274 status = psa_sign_verify_check_alg(0, alg);
3275 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003276 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003277 return status;
3278 }
3279
3280 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3281 PSA_KEY_USAGE_SIGN_HASH,
3282 alg);
3283
3284 if (status != PSA_SUCCESS) {
3285 goto exit;
3286 }
3287
3288 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3289 status = PSA_ERROR_INVALID_ARGUMENT;
3290 goto exit;
3291 }
3292
3293 psa_key_attributes_t attributes = {
3294 .core = slot->attr
3295 };
3296
Paul Elliott296ede92022-12-15 17:00:30 +00003297 /* Ensure ops count gets reset, in case of operation re-use. */
3298 operation->num_ops = 0;
3299
Paul Elliott9fe12f62022-11-30 19:16:02 +00003300 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3301 slot->key.data,
3302 slot->key.bytes, alg,
3303 hash, hash_length);
3304exit:
3305
3306 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003307 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003308 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003309 }
3310
3311 unlock_status = psa_unlock_key_slot(slot);
3312
Paul Elliottc9774412023-02-06 15:14:07 +00003313 if (unlock_status != PSA_SUCCESS) {
3314 operation->error_occurred = 1;
3315 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003316
Paul Elliottc9774412023-02-06 15:14:07 +00003317 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003318}
3319
3320
3321psa_status_t psa_sign_hash_complete(
3322 psa_sign_hash_interruptible_operation_t *operation,
3323 uint8_t *signature, size_t signature_size,
3324 size_t *signature_length)
3325{
3326 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3327
3328 *signature_length = 0;
3329
Paul Elliottc9774412023-02-06 15:14:07 +00003330 /* Check that start has been called first, and that operation has not
3331 * previously errored. */
3332 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003333 status = PSA_ERROR_BAD_STATE;
3334 goto exit;
3335 }
3336
Paul Elliott096abc42023-02-03 18:33:23 +00003337 /* Immediately reject a zero-length signature buffer. This guarantees that
3338 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003339 if (signature_size == 0) {
3340 status = PSA_ERROR_BUFFER_TOO_SMALL;
3341 goto exit;
3342 }
3343
3344 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3345 signature_size,
3346 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003347
Paul Elliott296ede92022-12-15 17:00:30 +00003348 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003349 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003350
Paul Elliottebe225c2023-02-10 14:32:53 +00003351exit:
3352
Paul Elliott59200a22023-02-21 15:07:40 +00003353 psa_wipe_tag_output_buffer(signature, status, signature_size,
3354 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003355
Paul Elliott53bb3122023-02-10 14:22:22 +00003356 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003357 if (status != PSA_SUCCESS) {
3358 operation->error_occurred = 1;
3359 }
3360
Paul Elliott59ad9452022-12-18 15:09:02 +00003361 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003362 }
3363
3364 return status;
3365}
3366
3367psa_status_t psa_sign_hash_abort(
3368 psa_sign_hash_interruptible_operation_t *operation)
3369{
Paul Elliott59ad9452022-12-18 15:09:02 +00003370 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3371
3372 status = psa_sign_hash_abort_internal(operation);
3373
3374 /* We clear the number of ops done here, so that it is not cleared when
3375 * the operation fails or succeeds, only on manual abort. */
3376 operation->num_ops = 0;
3377
Paul Elliottc9774412023-02-06 15:14:07 +00003378 /* Likewise, failure state. */
3379 operation->error_occurred = 0;
3380
Paul Elliott59ad9452022-12-18 15:09:02 +00003381 return status;
3382}
3383
3384static psa_status_t psa_verify_hash_abort_internal(
3385 psa_verify_hash_interruptible_operation_t *operation)
3386{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003387 if (operation->id == 0) {
3388 /* The object has (apparently) been initialized but it is not (yet)
3389 * in use. It's ok to call abort on such an object, and there's
3390 * nothing to do. */
3391 return PSA_SUCCESS;
3392 }
3393
Paul Elliott59ad9452022-12-18 15:09:02 +00003394 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3395
3396 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003397
3398 operation->id = 0;
3399
Paul Elliotte6145dc2023-02-07 12:51:21 +00003400 /* Do not clear either the error_occurred or num_ops elements here as they
3401 * only want to be cleared by the application calling abort, not by abort
3402 * being called at completion of an operation. */
3403
Paul Elliott59ad9452022-12-18 15:09:02 +00003404 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003405}
3406
3407psa_status_t psa_verify_hash_start(
3408 psa_verify_hash_interruptible_operation_t *operation,
3409 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3410 const uint8_t *hash, size_t hash_length,
3411 const uint8_t *signature, size_t signature_length)
3412{
3413 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3414 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3415 psa_key_slot_t *slot;
3416
Paul Elliottc9774412023-02-06 15:14:07 +00003417 /* Check that start has not been previously called, or operation has not
3418 * previously errored. */
3419 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003420 return PSA_ERROR_BAD_STATE;
3421 }
3422
3423 status = psa_sign_verify_check_alg(0, alg);
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 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3430 PSA_KEY_USAGE_VERIFY_HASH,
3431 alg);
3432
3433 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003434 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003435 return status;
3436 }
3437
3438 psa_key_attributes_t attributes = {
3439 .core = slot->attr
3440 };
3441
Paul Elliott296ede92022-12-15 17:00:30 +00003442 /* Ensure ops count gets reset, in case of operation re-use. */
3443 operation->num_ops = 0;
3444
Paul Elliott9fe12f62022-11-30 19:16:02 +00003445 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3446 slot->key.data,
3447 slot->key.bytes,
3448 alg, hash, hash_length,
3449 signature, signature_length);
3450
3451 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003452 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003453 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003454 }
3455
3456 unlock_status = psa_unlock_key_slot(slot);
3457
Paul Elliottc9774412023-02-06 15:14:07 +00003458 if (unlock_status != PSA_SUCCESS) {
3459 operation->error_occurred = 1;
3460 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003461
Paul Elliottc9774412023-02-06 15:14:07 +00003462 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003463}
3464
3465psa_status_t psa_verify_hash_complete(
3466 psa_verify_hash_interruptible_operation_t *operation)
3467{
3468 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3469
Paul Elliottc9774412023-02-06 15:14:07 +00003470 /* Check that start has been called first, and that operation has not
3471 * previously errored. */
3472 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003473 status = PSA_ERROR_BAD_STATE;
3474 goto exit;
3475 }
3476
3477 status = psa_driver_wrapper_verify_hash_complete(operation);
3478
Paul Elliott296ede92022-12-15 17:00:30 +00003479 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003480 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003481 operation);
3482
Paul Elliottebe225c2023-02-10 14:32:53 +00003483exit:
3484
Paul Elliott9fe12f62022-11-30 19:16:02 +00003485 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003486 if (status != PSA_SUCCESS) {
3487 operation->error_occurred = 1;
3488 }
3489
Paul Elliott59ad9452022-12-18 15:09:02 +00003490 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003491 }
3492
3493 return status;
3494}
3495
3496psa_status_t psa_verify_hash_abort(
3497 psa_verify_hash_interruptible_operation_t *operation)
3498{
Paul Elliott59ad9452022-12-18 15:09:02 +00003499 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003500
Paul Elliott59ad9452022-12-18 15:09:02 +00003501 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003502
Paul Elliott59ad9452022-12-18 15:09:02 +00003503 /* We clear the number of ops done here, so that it is not cleared when
3504 * the operation fails or succeeds, only on manual abort. */
3505 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003506
Paul Elliottc9774412023-02-06 15:14:07 +00003507 /* Likewise, failure state. */
3508 operation->error_occurred = 0;
3509
Paul Elliott59ad9452022-12-18 15:09:02 +00003510 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003511}
3512
Paul Elliott588f8ed2022-12-02 18:10:26 +00003513/****************************************************************/
3514/* Asymmetric interruptible cryptography internal */
3515/* implementations */
3516/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003517
Paul Elliott588f8ed2022-12-02 18:10:26 +00003518void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3519{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003520
Paul Elliott588f8ed2022-12-02 18:10:26 +00003521#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3522 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3523 defined(MBEDTLS_ECP_RESTARTABLE)
3524
3525 /* Internal implementation uses zero to indicate infinite number max ops,
3526 * therefore avoid this value, and set to minimum possible. */
3527 if (max_ops == 0) {
3528 max_ops = 1;
3529 }
3530
Paul Elliott588f8ed2022-12-02 18:10:26 +00003531 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003532#else
3533 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003534#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3535 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3536 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3537}
3538
Paul Elliott588f8ed2022-12-02 18:10:26 +00003539uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003540 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003541{
3542#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3543 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3544 defined(MBEDTLS_ECP_RESTARTABLE)
3545
Paul Elliott93d9ca82023-02-15 18:14:21 +00003546 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003547#else
3548 (void) operation;
3549 return 0;
3550#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3551 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3552 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3553}
3554
3555uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003556 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003557{
3558 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3559 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3560 defined(MBEDTLS_ECP_RESTARTABLE)
3561
Paul Elliott93d9ca82023-02-15 18:14:21 +00003562 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003563#else
3564 (void) operation;
3565 return 0;
3566#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3567 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3568 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3569}
3570
3571psa_status_t mbedtls_psa_sign_hash_start(
3572 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3573 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3574 size_t key_buffer_size, psa_algorithm_t alg,
3575 const uint8_t *hash, size_t hash_length)
3576{
3577 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003578 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003579
Paul Elliott068fe072023-01-16 13:59:15 +00003580 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3581 return PSA_ERROR_NOT_SUPPORTED;
3582 }
3583
3584 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003585 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003586 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003587
3588#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003589 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3590 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003591
Paul Elliotta1c94092023-02-15 16:38:04 +00003592 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3593
Paul Elliott93d9ca82023-02-15 18:14:21 +00003594 /* Ensure num_ops is zero'ed in case of context re-use. */
3595 operation->num_ops = 0;
3596
Paul Elliott068fe072023-01-16 13:59:15 +00003597 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3598 attributes->core.bits,
3599 key_buffer,
3600 key_buffer_size,
3601 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003602
Paul Elliott068fe072023-01-16 13:59:15 +00003603 if (status != PSA_SUCCESS) {
3604 return status;
3605 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003606
Paul Elliott1bc59df2023-02-05 13:41:57 +00003607 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003608 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003609
Paul Elliott068fe072023-01-16 13:59:15 +00003610 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3611 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3612 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003613
Paul Elliott0290a762023-02-09 14:30:24 +00003614 /* We only need to store the same length of hash as the private key size
3615 * here, it would be truncated by the internal implementation anyway. */
3616 required_hash_length = (hash_length < operation->coordinate_bytes ?
3617 hash_length : operation->coordinate_bytes);
3618
Paul Elliottba70ad42023-02-15 18:23:53 +00003619 if (required_hash_length > sizeof(operation->hash)) {
3620 /* Shouldn't happen, but better safe than sorry. */
3621 return PSA_ERROR_CORRUPTION_DETECTED;
3622 }
3623
Paul Elliott0290a762023-02-09 14:30:24 +00003624 memcpy(operation->hash, hash, required_hash_length);
3625 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003626
3627 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003628
3629#else
Paul Elliott068fe072023-01-16 13:59:15 +00003630 (void) operation;
3631 (void) key_buffer;
3632 (void) key_buffer_size;
3633 (void) alg;
3634 (void) hash;
3635 (void) hash_length;
3636 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003637 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003638
Paul Elliott068fe072023-01-16 13:59:15 +00003639 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003640#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3641 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3642 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003643}
3644
3645psa_status_t mbedtls_psa_sign_hash_complete(
3646 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3647 uint8_t *signature, size_t signature_size,
3648 size_t *signature_length)
3649{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003650#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3651 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3652 defined(MBEDTLS_ECP_RESTARTABLE)
3653
Paul Elliotta1c94092023-02-15 16:38:04 +00003654 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003655 mbedtls_mpi r;
3656 mbedtls_mpi s;
3657
Paul Elliott46845252023-02-03 14:59:11 +00003658 mbedtls_mpi_init(&r);
3659 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003660
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003661 /* Ensure max_ops is set to the current value (or default). */
3662 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3663
Paul Elliotta1c94092023-02-15 16:38:04 +00003664 if (signature_size < 2 * operation->coordinate_bytes) {
3665 status = PSA_ERROR_BUFFER_TOO_SMALL;
3666 goto exit;
3667 }
3668
Paul Elliott588f8ed2022-12-02 18:10:26 +00003669 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003670
Paul Elliott588f8ed2022-12-02 18:10:26 +00003671#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3672 status = mbedtls_to_psa_error(
3673 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003674 &r,
3675 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003676 &operation->ctx->d,
3677 operation->hash,
3678 operation->hash_length,
3679 operation->md_alg,
3680 mbedtls_psa_get_random,
3681 MBEDTLS_PSA_RANDOM_STATE,
3682 &operation->restart_ctx));
3683#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003684 status = PSA_ERROR_NOT_SUPPORTED;
3685 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003686#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3687 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003688 status = mbedtls_to_psa_error(
3689 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003690 &r,
3691 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003692 &operation->ctx->d,
3693 operation->hash,
3694 operation->hash_length,
3695 mbedtls_psa_get_random,
3696 MBEDTLS_PSA_RANDOM_STATE,
3697 mbedtls_psa_get_random,
3698 MBEDTLS_PSA_RANDOM_STATE,
3699 &operation->restart_ctx));
3700 }
3701
Paul Elliottf8e5b562023-02-19 18:43:45 +00003702 /* Hide the fact that the restart context only holds a delta of number of
3703 * ops done during the last operation, not an absolute value. */
3704 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3705
Paul Elliott724bd252023-02-08 12:35:08 +00003706 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003707 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003708 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003709 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003710 operation->coordinate_bytes)
3711 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003712
3713 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003714 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003715 }
3716
3717 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003718 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003719 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003720 operation->coordinate_bytes,
3721 operation->coordinate_bytes)
3722 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003723
3724 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003725 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003726 }
3727
Paul Elliott1bc59df2023-02-05 13:41:57 +00003728 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003729
Paul Elliott724bd252023-02-08 12:35:08 +00003730 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003731 }
Paul Elliott724bd252023-02-08 12:35:08 +00003732
3733exit:
3734
3735 mbedtls_mpi_free(&r);
3736 mbedtls_mpi_free(&s);
3737 return status;
3738
Paul Elliott588f8ed2022-12-02 18:10:26 +00003739 #else
3740
3741 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003742 (void) signature;
3743 (void) signature_size;
3744 (void) signature_length;
3745
3746 return PSA_ERROR_NOT_SUPPORTED;
3747
3748#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3749 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3750 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3751}
3752
3753psa_status_t mbedtls_psa_sign_hash_abort(
3754 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3755{
3756
3757#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3758 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3759 defined(MBEDTLS_ECP_RESTARTABLE)
3760
3761 if (operation->ctx) {
3762 mbedtls_ecdsa_free(operation->ctx);
3763 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003764 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003765 }
3766
3767 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3768
Paul Elliott93d9ca82023-02-15 18:14:21 +00003769 operation->num_ops = 0;
3770
Paul Elliott588f8ed2022-12-02 18:10:26 +00003771 return PSA_SUCCESS;
3772
3773#else
3774
3775 (void) operation;
3776
3777 return PSA_ERROR_NOT_SUPPORTED;
3778
3779#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3780 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3781 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3782}
3783
3784psa_status_t mbedtls_psa_verify_hash_start(
3785 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3786 const psa_key_attributes_t *attributes,
3787 const uint8_t *key_buffer, size_t key_buffer_size,
3788 psa_algorithm_t alg,
3789 const uint8_t *hash, size_t hash_length,
3790 const uint8_t *signature, size_t signature_length)
3791{
3792 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003793 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003794 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003795
Paul Elliott068fe072023-01-16 13:59:15 +00003796 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3797 return PSA_ERROR_NOT_SUPPORTED;
3798 }
3799
3800 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003801 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003802 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003803
3804#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003805 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3806 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003807
Paul Elliotta1c94092023-02-15 16:38:04 +00003808 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3809 mbedtls_mpi_init(&operation->r);
3810 mbedtls_mpi_init(&operation->s);
3811
Paul Elliott93d9ca82023-02-15 18:14:21 +00003812 /* Ensure num_ops is zero'ed in case of context re-use. */
3813 operation->num_ops = 0;
3814
Paul Elliott068fe072023-01-16 13:59:15 +00003815 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3816 attributes->core.bits,
3817 key_buffer,
3818 key_buffer_size,
3819 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003820
Paul Elliott068fe072023-01-16 13:59:15 +00003821 if (status != PSA_SUCCESS) {
3822 return status;
3823 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003824
Paul Elliottc569fc22023-02-10 13:02:54 +00003825 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003826
Paul Elliott1bc59df2023-02-05 13:41:57 +00003827 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003828 return PSA_ERROR_INVALID_SIGNATURE;
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->r,
3833 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003834 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003835
Paul Elliott068fe072023-01-16 13:59:15 +00003836 if (status != PSA_SUCCESS) {
3837 return status;
3838 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003839
Paul Elliott068fe072023-01-16 13:59:15 +00003840 status = mbedtls_to_psa_error(
3841 mbedtls_mpi_read_binary(&operation->s,
3842 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003843 coordinate_bytes,
3844 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003845
Paul Elliott068fe072023-01-16 13:59:15 +00003846 if (status != PSA_SUCCESS) {
3847 return status;
3848 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003849
Paul Elliott2c9843f2023-02-15 17:32:42 +00003850 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003851
Paul Elliott2c9843f2023-02-15 17:32:42 +00003852 if (status != PSA_SUCCESS) {
3853 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003854 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003855
Paul Elliott0290a762023-02-09 14:30:24 +00003856 /* We only need to store the same length of hash as the private key size
3857 * here, it would be truncated by the internal implementation anyway. */
3858 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3859 coordinate_bytes);
3860
Paul Elliottba70ad42023-02-15 18:23:53 +00003861 if (required_hash_length > sizeof(operation->hash)) {
3862 /* Shouldn't happen, but better safe than sorry. */
3863 return PSA_ERROR_CORRUPTION_DETECTED;
3864 }
3865
Paul Elliott0290a762023-02-09 14:30:24 +00003866 memcpy(operation->hash, hash, required_hash_length);
3867 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003868
3869 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003870#else
Paul Elliott068fe072023-01-16 13:59:15 +00003871 (void) operation;
3872 (void) key_buffer;
3873 (void) key_buffer_size;
3874 (void) alg;
3875 (void) hash;
3876 (void) hash_length;
3877 (void) signature;
3878 (void) signature_length;
3879 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003880 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003881 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003882
Paul Elliott068fe072023-01-16 13:59:15 +00003883 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003884#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3885 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3886 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003887}
3888
3889psa_status_t mbedtls_psa_verify_hash_complete(
3890 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3891{
3892
3893#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3894 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3895 defined(MBEDTLS_ECP_RESTARTABLE)
3896
Paul Elliottf8e5b562023-02-19 18:43:45 +00003897 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3898
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003899 /* Ensure max_ops is set to the current value (or default). */
3900 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3901
Paul Elliottf8e5b562023-02-19 18:43:45 +00003902 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003903 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3904 operation->hash,
3905 operation->hash_length,
3906 &operation->ctx->Q,
3907 &operation->r,
3908 &operation->s,
3909 &operation->restart_ctx));
3910
Paul Elliottf8e5b562023-02-19 18:43:45 +00003911 /* Hide the fact that the restart context only holds a delta of number of
3912 * ops done during the last operation, not an absolute value. */
3913 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3914
3915 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003916#else
3917 (void) operation;
3918
3919 return PSA_ERROR_NOT_SUPPORTED;
3920
3921#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3922 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3923 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3924}
3925
3926psa_status_t mbedtls_psa_verify_hash_abort(
3927 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3928{
3929
3930#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3931 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3932 defined(MBEDTLS_ECP_RESTARTABLE)
3933
3934 if (operation->ctx) {
3935 mbedtls_ecdsa_free(operation->ctx);
3936 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003937 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003938 }
3939
3940 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3941
Paul Elliott93d9ca82023-02-15 18:14:21 +00003942 operation->num_ops = 0;
3943
Paul Elliott588f8ed2022-12-02 18:10:26 +00003944 mbedtls_mpi_free(&operation->r);
3945 mbedtls_mpi_free(&operation->s);
3946
3947 return PSA_SUCCESS;
3948
3949#else
3950 (void) operation;
3951
3952 return PSA_ERROR_NOT_SUPPORTED;
3953
3954#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3955 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3956 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3957}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003958
mohammad1603503973b2018-03-12 15:59:30 +02003959/****************************************************************/
3960/* Symmetric cryptography */
3961/****************************************************************/
3962
Gilles Peskine449bd832023-01-11 14:50:10 +01003963static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3964 mbedtls_svc_key_id_t key,
3965 psa_algorithm_t alg,
3966 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003967{
3968 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3969 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003970 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003971 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3972 PSA_KEY_USAGE_ENCRYPT :
3973 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003974
3975 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003976 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003977 status = PSA_ERROR_BAD_STATE;
3978 goto exit;
3979 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003980
Gilles Peskine449bd832023-01-11 14:50:10 +01003981 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003982 status = PSA_ERROR_INVALID_ARGUMENT;
3983 goto exit;
3984 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003985
Gilles Peskine449bd832023-01-11 14:50:10 +01003986 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3987 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003988 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003989 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003990
Ronald Cron49fafa92021-03-10 08:34:23 +01003991 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003992 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003993 * so we only set it (in the driver wrapper) after resources have been
3994 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003995 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003996 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003997 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003998 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003999 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004000 }
4001 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004002
Ronald Crona4af55f2020-12-14 14:36:06 +01004003 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004004 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004005 };
4006
Ronald Cronab99ac22020-10-01 16:38:28 +02004007 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004008 if (cipher_operation == MBEDTLS_ENCRYPT) {
4009 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4010 &attributes,
4011 slot->key.data,
4012 slot->key.bytes,
4013 alg);
4014 } else {
4015 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4016 &attributes,
4017 slot->key.data,
4018 slot->key.bytes,
4019 alg);
4020 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004021
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004022exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004023 if (status != PSA_SUCCESS) {
4024 psa_cipher_abort(operation);
4025 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004026
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004028
Gilles Peskine449bd832023-01-11 14:50:10 +01004029 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004030}
4031
Gilles Peskine449bd832023-01-11 14:50:10 +01004032psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4033 mbedtls_svc_key_id_t key,
4034 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004035{
Gilles Peskine449bd832023-01-11 14:50:10 +01004036 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004037}
4038
Gilles Peskine449bd832023-01-11 14:50:10 +01004039psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4040 mbedtls_svc_key_id_t key,
4041 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004042{
Gilles Peskine449bd832023-01-11 14:50:10 +01004043 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004044}
4045
Gilles Peskine449bd832023-01-11 14:50:10 +01004046psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4047 uint8_t *iv,
4048 size_t iv_size,
4049 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004050{
Ronald Cron6d051732020-10-01 14:10:20 +02004051 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004052 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4053 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004054
Gilles Peskine449bd832023-01-11 14:50:10 +01004055 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004056 status = PSA_ERROR_BAD_STATE;
4057 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004058 }
4059
Gilles Peskine449bd832023-01-11 14:50:10 +01004060 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004061 status = PSA_ERROR_BAD_STATE;
4062 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004063 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004064
Ronald Cron2fb90522021-07-05 12:31:44 +02004065 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004066 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004067 status = PSA_ERROR_BUFFER_TOO_SMALL;
4068 goto exit;
4069 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004070
Gilles Peskine449bd832023-01-11 14:50:10 +01004071 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004072 status = PSA_ERROR_GENERIC_ERROR;
4073 goto exit;
4074 }
4075
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 status = psa_generate_random(local_iv, default_iv_length);
4077 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004078 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004079 }
Ronald Cron5618a392021-03-26 09:52:26 +01004080
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 status = psa_driver_wrapper_cipher_set_iv(operation,
4082 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004083
4084exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 if (status == PSA_SUCCESS) {
4086 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004087 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004088 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004089 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004090 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004092 }
Ronald Cron6d051732020-10-01 14:10:20 +02004093
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004095}
4096
Gilles Peskine449bd832023-01-11 14:50:10 +01004097psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4098 const uint8_t *iv,
4099 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004100{
Ronald Cron6d051732020-10-01 14:10:20 +02004101 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004102
Gilles Peskine449bd832023-01-11 14:50:10 +01004103 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004104 status = PSA_ERROR_BAD_STATE;
4105 goto exit;
4106 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004107
Gilles Peskine449bd832023-01-11 14:50:10 +01004108 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004109 status = PSA_ERROR_BAD_STATE;
4110 goto exit;
4111 }
Ronald Crona0d68172021-03-26 10:15:08 +01004112
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004114 status = PSA_ERROR_INVALID_ARGUMENT;
4115 goto exit;
4116 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004117
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 status = psa_driver_wrapper_cipher_set_iv(operation,
4119 iv,
4120 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004121
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004122exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004124 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004125 } else {
4126 psa_cipher_abort(operation);
4127 }
4128 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004129}
4130
Gilles Peskine449bd832023-01-11 14:50:10 +01004131psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4132 const uint8_t *input,
4133 size_t input_length,
4134 uint8_t *output,
4135 size_t output_size,
4136 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004137{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004138 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004139
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004141 status = PSA_ERROR_BAD_STATE;
4142 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004143 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004144
Gilles Peskine449bd832023-01-11 14:50:10 +01004145 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004146 status = PSA_ERROR_BAD_STATE;
4147 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004148 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004149
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 status = psa_driver_wrapper_cipher_update(operation,
4151 input,
4152 input_length,
4153 output,
4154 output_size,
4155 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004156
4157exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004158 if (status != PSA_SUCCESS) {
4159 psa_cipher_abort(operation);
4160 }
Ronald Cron6d051732020-10-01 14:10:20 +02004161
Gilles Peskine449bd832023-01-11 14:50:10 +01004162 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004163}
4164
Gilles Peskine449bd832023-01-11 14:50:10 +01004165psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4166 uint8_t *output,
4167 size_t output_size,
4168 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004169{
David Saadab4ecc272019-02-14 13:48:10 +02004170 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004171
Gilles Peskine449bd832023-01-11 14:50:10 +01004172 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004173 status = PSA_ERROR_BAD_STATE;
4174 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004175 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004176
Gilles Peskine449bd832023-01-11 14:50:10 +01004177 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004178 status = PSA_ERROR_BAD_STATE;
4179 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004180 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004181
Gilles Peskine449bd832023-01-11 14:50:10 +01004182 status = psa_driver_wrapper_cipher_finish(operation,
4183 output,
4184 output_size,
4185 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004186
4187exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004188 if (status == PSA_SUCCESS) {
4189 return psa_cipher_abort(operation);
4190 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004191 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004193
Gilles Peskine449bd832023-01-11 14:50:10 +01004194 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004195 }
mohammad1603503973b2018-03-12 15:59:30 +02004196}
4197
Gilles Peskine449bd832023-01-11 14:50:10 +01004198psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004199{
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004201 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004202 * in use. It's ok to call abort on such an object, and there's
4203 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004204 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004205 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004206
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004208
Ronald Cron49fafa92021-03-10 08:34:23 +01004209 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004210 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004211 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004212
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004214}
4215
Gilles Peskine449bd832023-01-11 14:50:10 +01004216psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4217 psa_algorithm_t alg,
4218 const uint8_t *input,
4219 size_t input_length,
4220 uint8_t *output,
4221 size_t output_size,
4222 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004223{
4224 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004225 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004226 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004227 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4228 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004229
Gilles Peskine449bd832023-01-11 14:50:10 +01004230 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004231 status = PSA_ERROR_INVALID_ARGUMENT;
4232 goto exit;
4233 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004234
Gilles Peskine449bd832023-01-11 14:50:10 +01004235 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4236 PSA_KEY_USAGE_ENCRYPT,
4237 alg);
4238 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004239 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004240 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004241
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004242 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004243 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004244 };
4245
Gilles Peskine449bd832023-01-11 14:50:10 +01004246 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4247 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004248 status = PSA_ERROR_GENERIC_ERROR;
4249 goto exit;
4250 }
4251
Gilles Peskine449bd832023-01-11 14:50:10 +01004252 if (default_iv_length > 0) {
4253 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004254 status = PSA_ERROR_BUFFER_TOO_SMALL;
4255 goto exit;
4256 }
4257
Gilles Peskine449bd832023-01-11 14:50:10 +01004258 status = psa_generate_random(local_iv, default_iv_length);
4259 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004260 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004261 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004262 }
4263
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004264 status = psa_driver_wrapper_cipher_encrypt(
4265 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004266 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004267 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004268 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004269
4270exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004271 unlock_status = psa_unlock_key_slot(slot);
4272 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004273 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004274 }
Ronald Cron23919522021-07-08 19:00:07 +02004275
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 if (status == PSA_SUCCESS) {
4277 if (default_iv_length > 0) {
4278 memcpy(output, local_iv, default_iv_length);
4279 }
4280 *output_length += default_iv_length;
4281 } else {
4282 *output_length = 0;
4283 }
4284
4285 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004286}
4287
Gilles Peskine449bd832023-01-11 14:50:10 +01004288psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4289 psa_algorithm_t alg,
4290 const uint8_t *input,
4291 size_t input_length,
4292 uint8_t *output,
4293 size_t output_size,
4294 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004295{
4296 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004297 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004298 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004299
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004301 status = PSA_ERROR_INVALID_ARGUMENT;
4302 goto exit;
4303 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004304
Gilles Peskine449bd832023-01-11 14:50:10 +01004305 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4306 PSA_KEY_USAGE_DECRYPT,
4307 alg);
4308 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004309 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004310 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004311
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004312 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004313 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004314 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004315
Gilles Peskine449bd832023-01-11 14:50:10 +01004316 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4317 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004318 status = PSA_ERROR_INVALID_ARGUMENT;
4319 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004321 status = PSA_ERROR_INVALID_ARGUMENT;
4322 goto exit;
4323 }
4324
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004325 status = psa_driver_wrapper_cipher_decrypt(
4326 &attributes, slot->key.data, slot->key.bytes,
4327 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004329
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004330exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004331 unlock_status = psa_unlock_key_slot(slot);
4332 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004333 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004334 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004335
Gilles Peskine449bd832023-01-11 14:50:10 +01004336 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004337 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004338 }
Ronald Cron23919522021-07-08 19:00:07 +02004339
Gilles Peskine449bd832023-01-11 14:50:10 +01004340 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004341}
4342
4343
mohammad16035955c982018-04-26 00:53:03 +03004344/****************************************************************/
4345/* AEAD */
4346/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004347
Paul Elliottbaff51c2021-09-28 17:44:45 +01004348/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004349static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004350{
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004352}
4353
4354/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004355static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4356 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004357{
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004359
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004361#if defined(PSA_WANT_ALG_GCM)
4362 case PSA_ALG_GCM:
4363 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004364 * arbitrarily large nonces. Please note that we do not generally
4365 * recommend the usage of nonces of greater length than
4366 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4367 * size, which can then lead to collisions if you encrypt a very
4368 * large number of messages.*/
4369 if (nonce_length != 0) {
4370 return PSA_SUCCESS;
4371 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004372 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004373#endif /* PSA_WANT_ALG_GCM */
4374#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004375 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 if (nonce_length >= 7 && nonce_length <= 13) {
4377 return PSA_SUCCESS;
4378 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004379 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004380#endif /* PSA_WANT_ALG_CCM */
4381#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004382 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004383 if (nonce_length == 12) {
4384 return PSA_SUCCESS;
4385 } else if (nonce_length == 8) {
4386 return PSA_ERROR_NOT_SUPPORTED;
4387 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004388 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004389#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004390 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004391 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004393 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004394
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004396}
4397
Gilles Peskine449bd832023-01-11 14:50:10 +01004398static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004399{
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4401 return PSA_ERROR_INVALID_ARGUMENT;
4402 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004403
Gilles Peskine449bd832023-01-11 14:50:10 +01004404 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004405}
4406
Gilles Peskine449bd832023-01-11 14:50:10 +01004407psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4408 psa_algorithm_t alg,
4409 const uint8_t *nonce,
4410 size_t nonce_length,
4411 const uint8_t *additional_data,
4412 size_t additional_data_length,
4413 const uint8_t *plaintext,
4414 size_t plaintext_length,
4415 uint8_t *ciphertext,
4416 size_t ciphertext_size,
4417 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004418{
Ronald Cron215633c2021-03-16 17:15:37 +01004419 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4420 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004421
mohammad1603f08a5502018-06-03 15:05:47 +03004422 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004423
Gilles Peskine449bd832023-01-11 14:50:10 +01004424 status = psa_aead_check_algorithm(alg);
4425 if (status != PSA_SUCCESS) {
4426 return status;
4427 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004428
Ronald Cron9a986162021-03-26 12:40:07 +01004429 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4431 if (status != PSA_SUCCESS) {
4432 return status;
4433 }
mohammad16035955c982018-04-26 00:53:03 +03004434
Ronald Cron215633c2021-03-16 17:15:37 +01004435 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004436 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004437 };
mohammad16035955c982018-04-26 00:53:03 +03004438
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 status = psa_aead_check_nonce_length(alg, nonce_length);
4440 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004441 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004443
Ronald Cronde822812021-03-17 16:08:20 +01004444 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004445 &attributes, slot->key.data, slot->key.bytes,
4446 alg,
4447 nonce, nonce_length,
4448 additional_data, additional_data_length,
4449 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004451
Gilles Peskine449bd832023-01-11 14:50:10 +01004452 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4453 memset(ciphertext, 0, ciphertext_size);
4454 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004455
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004456exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004458
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004460}
4461
Gilles Peskine449bd832023-01-11 14:50:10 +01004462psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4463 psa_algorithm_t alg,
4464 const uint8_t *nonce,
4465 size_t nonce_length,
4466 const uint8_t *additional_data,
4467 size_t additional_data_length,
4468 const uint8_t *ciphertext,
4469 size_t ciphertext_length,
4470 uint8_t *plaintext,
4471 size_t plaintext_size,
4472 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004473{
Ronald Cron215633c2021-03-16 17:15:37 +01004474 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4475 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004476
Gilles Peskineee652a32018-06-01 19:23:52 +02004477 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004478
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 status = psa_aead_check_algorithm(alg);
4480 if (status != PSA_SUCCESS) {
4481 return status;
4482 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004483
Ronald Cron9a986162021-03-26 12:40:07 +01004484 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4486 if (status != PSA_SUCCESS) {
4487 return status;
4488 }
mohammad16035955c982018-04-26 00:53:03 +03004489
Ronald Cron215633c2021-03-16 17:15:37 +01004490 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004492 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004493
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 status = psa_aead_check_nonce_length(alg, nonce_length);
4495 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004496 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004498
Ronald Cronde822812021-03-17 16:08:20 +01004499 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004500 &attributes, slot->key.data, slot->key.bytes,
4501 alg,
4502 nonce, nonce_length,
4503 additional_data, additional_data_length,
4504 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004506
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 if (status != PSA_SUCCESS && plaintext_size != 0) {
4508 memset(plaintext, 0, plaintext_size);
4509 }
mohammad160360a64d02018-06-03 17:20:42 +03004510
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004511exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004513
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 return status;
mohammad16035955c982018-04-26 00:53:03 +03004515}
4516
Gilles Peskine449bd832023-01-11 14:50:10 +01004517static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4518{
4519 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004520
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004522#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004524 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004525 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
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_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004530
Przemek Stekiel86679c72022-10-06 17:06:56 +02004531#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004533 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || 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_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004539
Przemek Stekiel86679c72022-10-06 17:06:56 +02004540#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004542 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 if (tag_len != 16) {
4544 return PSA_ERROR_INVALID_ARGUMENT;
4545 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004546 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004547#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004548
4549 default:
4550 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004551 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004552 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004554}
4555
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004556/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004557static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4558 int is_encrypt,
4559 mbedtls_svc_key_id_t key,
4560 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004561{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004562 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4563 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4564 psa_key_slot_t *slot = NULL;
4565 psa_key_usage_t key_usage = 0;
4566
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 status = psa_aead_check_algorithm(alg);
4568 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004569 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004571
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004573 status = PSA_ERROR_BAD_STATE;
4574 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004575 }
4576
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 if (operation->nonce_set || operation->lengths_set ||
4578 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004579 status = PSA_ERROR_BAD_STATE;
4580 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004581 }
4582
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004584 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004586 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004588
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4590 alg);
4591 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004592 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004594
4595 psa_key_attributes_t attributes = {
4596 .core = slot->attr
4597 };
4598
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004600 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004602
Gilles Peskine449bd832023-01-11 14:50:10 +01004603 if (is_encrypt) {
4604 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4605 &attributes,
4606 slot->key.data,
4607 slot->key.bytes,
4608 alg);
4609 } else {
4610 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4611 &attributes,
4612 slot->key.data,
4613 slot->key.bytes,
4614 alg);
4615 }
4616 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004617 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004619
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004621
4622exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004624
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004626 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004628 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 } else {
4630 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004631 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004632
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004634}
4635
Paul Elliott302ff6b2021-04-20 18:10:30 +01004636/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004637psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4638 mbedtls_svc_key_id_t key,
4639 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004640{
Gilles Peskine449bd832023-01-11 14:50:10 +01004641 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004642}
4643
4644/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004645psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4646 mbedtls_svc_key_id_t key,
4647 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004648{
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004650}
4651
4652/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004653psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4654 uint8_t *nonce,
4655 size_t nonce_size,
4656 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004657{
4658 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004659 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004660 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004661
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004662 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004663
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004665 status = PSA_ERROR_BAD_STATE;
4666 goto exit;
4667 }
4668
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004670 status = PSA_ERROR_BAD_STATE;
4671 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004672 }
4673
Paul Elliotte193ea82021-10-01 13:00:16 +01004674 /* For CCM, this size may not be correct according to the PSA
4675 * specification. The PSA Crypto 1.0.1 specification states:
4676 *
4677 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4678 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4679 *
4680 * However this restriction that L has to be the smallest integer is not
4681 * applied in practice, and it is not implementable here since the
4682 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004683 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4684 operation->alg);
4685 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004686 status = PSA_ERROR_BUFFER_TOO_SMALL;
4687 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004688 }
4689
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 status = psa_generate_random(local_nonce, required_nonce_size);
4691 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004692 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004694
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004696
Paul Elliott39dc6b82021-05-11 19:16:09 +01004697exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 if (status == PSA_SUCCESS) {
4699 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004700 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 } else {
4702 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004703 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004704
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004706}
4707
4708/* Set the nonce for a multipart authenticated encryption or decryption
4709 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004710psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4711 const uint8_t *nonce,
4712 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004713{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004714 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4715
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004717 status = PSA_ERROR_BAD_STATE;
4718 goto exit;
4719 }
4720
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004722 status = PSA_ERROR_BAD_STATE;
4723 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004724 }
4725
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4727 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004728 status = PSA_ERROR_INVALID_ARGUMENT;
4729 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004730 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004731
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4733 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004734
Paul Elliott39dc6b82021-05-11 19:16:09 +01004735exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004737 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 } else {
4739 psa_aead_abort(operation);
4740 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004741
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004743}
4744
4745/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004746psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4747 size_t ad_length,
4748 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004749{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004750 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4751
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004753 status = PSA_ERROR_BAD_STATE;
4754 goto exit;
4755 }
4756
Gilles Peskine449bd832023-01-11 14:50:10 +01004757 if (operation->lengths_set || operation->ad_started ||
4758 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004759 status = PSA_ERROR_BAD_STATE;
4760 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004761 }
4762
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004764#if defined(PSA_WANT_ALG_GCM)
4765 case PSA_ALG_GCM:
4766 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 * bits. Without the guard this code will generate warnings on 32bit
4768 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004769#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 if (((uint64_t) ad_length) >> 61 != 0 ||
4771 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004772 status = PSA_ERROR_INVALID_ARGUMENT;
4773 goto exit;
4774 }
Paul Elliott325d3742021-09-27 17:56:28 +01004775#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004776 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004777#endif /* PSA_WANT_ALG_GCM */
4778#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004779 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004781 status = PSA_ERROR_INVALID_ARGUMENT;
4782 goto exit;
4783 }
4784 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004785#endif /* PSA_WANT_ALG_CCM */
4786#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004787 case PSA_ALG_CHACHA20_POLY1305:
4788 /* No length restrictions for ChaChaPoly. */
4789 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004790#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004791 default:
4792 break;
4793 }
Paul Elliott325d3742021-09-27 17:56:28 +01004794
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4796 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004797
Paul Elliott39dc6b82021-05-11 19:16:09 +01004798exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004800 operation->ad_remaining = ad_length;
4801 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004802 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 } else {
4804 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004805 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004806
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004808}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004809
4810/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004811psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4812 const uint8_t *input,
4813 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004814{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004815 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4816
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004818 status = PSA_ERROR_BAD_STATE;
4819 goto exit;
4820 }
4821
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004823 status = PSA_ERROR_BAD_STATE;
4824 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004825 }
4826
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 if (operation->lengths_set) {
4828 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004829 status = PSA_ERROR_INVALID_ARGUMENT;
4830 goto exit;
4831 }
4832
4833 operation->ad_remaining -= input_length;
4834 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004835#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004837 status = PSA_ERROR_BAD_STATE;
4838 goto exit;
4839 }
4840#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004841
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 status = psa_driver_wrapper_aead_update_ad(operation, input,
4843 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004844
Paul Elliott39dc6b82021-05-11 19:16:09 +01004845exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004846 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004847 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004848 } else {
4849 psa_aead_abort(operation);
4850 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004851
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004853}
4854
4855/* Encrypt or decrypt a message fragment in an active multipart AEAD
4856 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004857psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4858 const uint8_t *input,
4859 size_t input_length,
4860 uint8_t *output,
4861 size_t output_size,
4862 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004863{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004864 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004865
4866 *output_length = 0;
4867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004869 status = PSA_ERROR_BAD_STATE;
4870 goto exit;
4871 }
4872
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004874 status = PSA_ERROR_BAD_STATE;
4875 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004876 }
4877
Gilles Peskine449bd832023-01-11 14:50:10 +01004878 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004879 /* Additional data length was supplied, but not all the additional
4880 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004882 status = PSA_ERROR_INVALID_ARGUMENT;
4883 goto exit;
4884 }
4885
4886 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004888 status = PSA_ERROR_INVALID_ARGUMENT;
4889 goto exit;
4890 }
4891
4892 operation->body_remaining -= input_length;
4893 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004894#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004896 status = PSA_ERROR_BAD_STATE;
4897 goto exit;
4898 }
4899#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004900
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4902 output, output_size,
4903 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004904
Paul Elliott39dc6b82021-05-11 19:16:09 +01004905exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004906 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004907 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 } else {
4909 psa_aead_abort(operation);
4910 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004911
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004913}
4914
Gilles Peskine449bd832023-01-11 14:50:10 +01004915static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004916{
Gilles Peskine449bd832023-01-11 14:50:10 +01004917 if (operation->id == 0 || !operation->nonce_set) {
4918 return PSA_ERROR_BAD_STATE;
4919 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004920
Gilles Peskine449bd832023-01-11 14:50:10 +01004921 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4922 operation->body_remaining != 0)) {
4923 return PSA_ERROR_INVALID_ARGUMENT;
4924 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004925
Gilles Peskine449bd832023-01-11 14:50:10 +01004926 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004927}
4928
Paul Elliott302ff6b2021-04-20 18:10:30 +01004929/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004930psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4931 uint8_t *ciphertext,
4932 size_t ciphertext_size,
4933 size_t *ciphertext_length,
4934 uint8_t *tag,
4935 size_t tag_size,
4936 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004937{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004938 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4939
Paul Elliott302ff6b2021-04-20 18:10:30 +01004940 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004941 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004942
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 status = psa_aead_final_checks(operation);
4944 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004945 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004946 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004947
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004949 status = PSA_ERROR_BAD_STATE;
4950 goto exit;
4951 }
4952
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4954 ciphertext_size,
4955 ciphertext_length,
4956 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004957
Paul Elliott39dc6b82021-05-11 19:16:09 +01004958exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004959
4960
Paul Elliottf47b0952021-05-21 18:02:33 +01004961 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004962 * the zero tag size, make sure the tag is set to something implausible.
4963 * Even if the operation succeeds, make sure we clear the rest of the
4964 * buffer to prevent potential leakage of anything previously placed in
4965 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004966 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004967
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004969
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004971}
4972
4973/* Finish authenticating and decrypting a message in a multipart AEAD
4974 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004975psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4976 uint8_t *plaintext,
4977 size_t plaintext_size,
4978 size_t *plaintext_length,
4979 const uint8_t *tag,
4980 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004981{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004982 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4983
Paul Elliott302ff6b2021-04-20 18:10:30 +01004984 *plaintext_length = 0;
4985
Gilles Peskine449bd832023-01-11 14:50:10 +01004986 status = psa_aead_final_checks(operation);
4987 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004988 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004989 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004990
Gilles Peskine449bd832023-01-11 14:50:10 +01004991 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004992 status = PSA_ERROR_BAD_STATE;
4993 goto exit;
4994 }
4995
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4997 plaintext_size,
4998 plaintext_length,
4999 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005000
5001exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005003
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005005}
5006
5007/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005008psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005009{
5010 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5011
Gilles Peskine449bd832023-01-11 14:50:10 +01005012 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005013 /* The object has (apparently) been initialized but it is not (yet)
5014 * in use. It's ok to call abort on such an object, and there's
5015 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005016 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005017 }
5018
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005020
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005022
Gilles Peskine449bd832023-01-11 14:50:10 +01005023 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005024}
5025
Gilles Peskinee59236f2018-01-27 23:32:46 +01005026/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005027/* Generators */
5028/****************************************************************/
5029
Przemek Stekiel69c46792022-06-10 12:59:51 +02005030#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005031 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005032 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
5033 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08005034#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005035#endif /* At least one builtin KDF */
5036
Przemek Stekiel69c46792022-06-10 12:59:51 +02005037#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005038 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5039 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5040static psa_status_t psa_key_derivation_start_hmac(
5041 psa_mac_operation_t *operation,
5042 psa_algorithm_t hash_alg,
5043 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005044 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005045{
5046 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5047 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5049 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5050 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005051
Steven Cooreman72f736a2021-05-07 14:14:37 +02005052 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005053 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005054
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 status = psa_driver_wrapper_mac_sign_setup(operation,
5056 &attributes,
5057 hmac_key, hmac_key_length,
5058 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005059
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 psa_reset_key_attributes(&attributes);
5061 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005062}
5063#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005064
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005065#define HKDF_STATE_INIT 0 /* no input yet */
5066#define HKDF_STATE_STARTED 1 /* got salt */
5067#define HKDF_STATE_KEYED 2 /* got key */
5068#define HKDF_STATE_OUTPUT 3 /* output started */
5069
Gilles Peskinecbe66502019-05-16 16:59:18 +02005070static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005071 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005072{
Gilles Peskine449bd832023-01-11 14:50:10 +01005073 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5074 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5075 } else {
5076 return operation->alg;
5077 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005078}
5079
Gilles Peskine449bd832023-01-11 14:50:10 +01005080psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005081{
5082 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005083 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5084 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005085 /* The object has (apparently) been initialized but it is not
5086 * in use. It's ok to call abort on such an object, and there's
5087 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005089#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5091 mbedtls_free(operation->ctx.hkdf.info);
5092 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5093 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005094#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005095#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5096 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005097 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5098 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5099 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5100 if (operation->ctx.tls12_prf.secret != NULL) {
5101 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5102 operation->ctx.tls12_prf.secret_length);
5103 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005104 }
5105
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 if (operation->ctx.tls12_prf.seed != NULL) {
5107 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5108 operation->ctx.tls12_prf.seed_length);
5109 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005110 }
5111
Gilles Peskine449bd832023-01-11 14:50:10 +01005112 if (operation->ctx.tls12_prf.label != NULL) {
5113 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5114 operation->ctx.tls12_prf.label_length);
5115 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005116 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005117#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005118 if (operation->ctx.tls12_prf.other_secret != NULL) {
5119 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5120 operation->ctx.tls12_prf.other_secret_length);
5121 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005122 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005123#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005124 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005125
5126 /* We leave the fields Ai and output_block to be erased safely by the
5127 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005128 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005129#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5130 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005131#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005132 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5133 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5134 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5135 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005136#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005137 {
5138 status = PSA_ERROR_BAD_STATE;
5139 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005140 mbedtls_platform_zeroize(operation, sizeof(*operation));
5141 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005142}
5143
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005144psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005145 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005146{
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005148 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005150 }
5151
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005152 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005154}
5155
Gilles Peskine449bd832023-01-11 14:50:10 +01005156psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5157 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005158{
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 if (operation->alg == 0) {
5160 return PSA_ERROR_BAD_STATE;
5161 }
5162 if (capacity > operation->capacity) {
5163 return PSA_ERROR_INVALID_ARGUMENT;
5164 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005165 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005167}
5168
Przemek Stekiel69c46792022-06-10 12:59:51 +02005169#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005170/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005171static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5172 psa_algorithm_t kdf_alg,
5173 uint8_t *output,
5174 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005175{
Gilles Peskine449bd832023-01-11 14:50:10 +01005176 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5177 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005178 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005179 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005180#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005182#else
5183 const uint8_t last_block = 0xff;
5184#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005185
Gilles Peskine449bd832023-01-11 14:50:10 +01005186 if (hkdf->state < HKDF_STATE_KEYED ||
5187 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005188#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005190#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005191 )) {
5192 return PSA_ERROR_BAD_STATE;
5193 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005194 hkdf->state = HKDF_STATE_OUTPUT;
5195
Gilles Peskine449bd832023-01-11 14:50:10 +01005196 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005197 /* Copy what remains of the current block */
5198 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005199 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005200 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005201 }
5202 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005203 output += n;
5204 output_length -= n;
5205 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005207 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005208 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005209 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005210 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005211 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005212 * object was corrupted or if this function is called directly
5213 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 if (hkdf->block_number == last_block) {
5215 return PSA_ERROR_BAD_STATE;
5216 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005217
5218 /* We need a new block */
5219 ++hkdf->block_number;
5220 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005221
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5223 hash_alg,
5224 hkdf->prk,
5225 hash_length);
5226 if (status != PSA_SUCCESS) {
5227 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005228 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005229
5230 if (hkdf->block_number != 1) {
5231 status = psa_mac_update(&hkdf->hmac,
5232 hkdf->output_block,
5233 hash_length);
5234 if (status != PSA_SUCCESS) {
5235 return status;
5236 }
5237 }
5238 status = psa_mac_update(&hkdf->hmac,
5239 hkdf->info,
5240 hkdf->info_length);
5241 if (status != PSA_SUCCESS) {
5242 return status;
5243 }
5244 status = psa_mac_update(&hkdf->hmac,
5245 &hkdf->block_number, 1);
5246 if (status != PSA_SUCCESS) {
5247 return status;
5248 }
5249 status = psa_mac_sign_finish(&hkdf->hmac,
5250 hkdf->output_block,
5251 sizeof(hkdf->output_block),
5252 &hmac_output_length);
5253 if (status != PSA_SUCCESS) {
5254 return status;
5255 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005256 }
5257
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005259}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005260#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005261
John Durkop07cc04a2020-11-16 22:08:34 -08005262#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5263 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005264static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5265 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005267{
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5269 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005270 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5271 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005272 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005273
Janos Follath7742fee2019-06-17 12:58:10 +01005274 /* We can't be wanting more output after block 0xff, otherwise
5275 * the capacity check in psa_key_derivation_output_bytes() would have
5276 * prevented this call. It could happen only if the operation
5277 * object was corrupted or if this function is called directly
5278 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 if (tls12_prf->block_number == 0xff) {
5280 return PSA_ERROR_CORRUPTION_DETECTED;
5281 }
Janos Follath7742fee2019-06-17 12:58:10 +01005282
5283 /* We need a new block */
5284 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005285 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005286
5287 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5288 *
5289 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5290 *
5291 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5292 * HMAC_hash(secret, A(2) + seed) +
5293 * HMAC_hash(secret, A(3) + seed) + ...
5294 *
5295 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005296 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005297 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005298 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005299 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005300 * is currently extracted as `output_block` and where i is
5301 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005302 */
5303
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 status = psa_key_derivation_start_hmac(&hmac,
5305 hash_alg,
5306 tls12_prf->secret,
5307 tls12_prf->secret_length);
5308 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005309 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005311
5312 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005314 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5315 * the variable seed and in this instance means it in the context of the
5316 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 status = psa_mac_update(&hmac,
5318 tls12_prf->label,
5319 tls12_prf->label_length);
5320 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005321 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 }
5323 status = psa_mac_update(&hmac,
5324 tls12_prf->seed,
5325 tls12_prf->seed_length);
5326 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005327 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 }
5329 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005330 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5332 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005333 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005335 }
5336
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 status = psa_mac_sign_finish(&hmac,
5338 tls12_prf->Ai, hash_length,
5339 &hmac_output_length);
5340 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005341 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 }
5343 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005344 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005346
5347 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 status = psa_key_derivation_start_hmac(&hmac,
5349 hash_alg,
5350 tls12_prf->secret,
5351 tls12_prf->secret_length);
5352 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005353 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 }
5355 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5356 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005357 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 }
5359 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5360 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005361 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 }
5363 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5364 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005365 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005366 }
5367 status = psa_mac_sign_finish(&hmac,
5368 tls12_prf->output_block, hash_length,
5369 &hmac_output_length);
5370 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005371 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005373
Janos Follath7742fee2019-06-17 12:58:10 +01005374
5375cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 cleanup_status = psa_mac_abort(&hmac);
5377 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005378 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005380
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005382}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005383
Janos Follath844eb0e2019-06-19 12:10:49 +01005384static psa_status_t psa_key_derivation_tls12_prf_read(
5385 psa_tls12_prf_key_derivation_t *tls12_prf,
5386 psa_algorithm_t alg,
5387 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005389{
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5391 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005392 psa_status_t status;
5393 uint8_t offset, length;
5394
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005396 case PSA_TLS12_PRF_STATE_LABEL_SET:
5397 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5398 break;
5399 case PSA_TLS12_PRF_STATE_OUTPUT:
5400 break;
5401 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005403 }
5404
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005406 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 if (tls12_prf->left_in_block == 0) {
5408 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5409 alg);
5410 if (status != PSA_SUCCESS) {
5411 return status;
5412 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005413
5414 continue;
5415 }
5416
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005418 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005420 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005422
5423 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005424 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005425 output += length;
5426 output_length -= length;
5427 tls12_prf->left_in_block -= length;
5428 }
5429
Gilles Peskine449bd832023-01-11 14:50:10 +01005430 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005431}
John Durkop07cc04a2020-11-16 22:08:34 -08005432#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5433 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005434
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005435#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5436static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5437 psa_tls12_ecjpake_to_pms_t *ecjpake,
5438 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005440{
5441 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005442 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005443
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 if (output_length != 32) {
5445 return PSA_ERROR_INVALID_ARGUMENT;
5446 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005447
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5449 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5450 &output_size);
5451 if (status != PSA_SUCCESS) {
5452 return status;
5453 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005454
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 if (output_size != output_length) {
5456 return PSA_ERROR_GENERIC_ERROR;
5457 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005458
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005460}
5461#endif
5462
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005463psa_status_t psa_key_derivation_output_bytes(
5464 psa_key_derivation_operation_t *operation,
5465 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005467{
5468 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005470
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005472 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005474 }
5475
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005477 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005478 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005479 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005480 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005481 goto exit;
5482 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005484 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005485 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005486 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5487 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005488 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005489 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005490 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005491 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005492 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005493
Przemek Stekiel69c46792022-06-10 12:59:51 +02005494#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5496 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5497 output, output_length);
5498 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005499#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005500#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5501 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5503 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5504 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5505 kdf_alg, output,
5506 output_length);
5507 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005508#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5509 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005510#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005512 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5514 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005515#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5516
Gilles Peskineeab56e42018-07-12 17:12:33 +02005517 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005518 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005520 }
5521
5522exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005523 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005524 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005525 * This allows us to differentiate between exhausted operations and
5526 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5527 * operations. */
5528 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005530 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005532 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005534}
5535
David Brown7807bf72021-02-09 16:28:23 -07005536#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005537static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005538{
Gilles Peskine449bd832023-01-11 14:50:10 +01005539 if (data_size >= 8) {
5540 mbedtls_des_key_set_parity(data);
5541 }
5542 if (data_size >= 16) {
5543 mbedtls_des_key_set_parity(data + 8);
5544 }
5545 if (data_size >= 24) {
5546 mbedtls_des_key_set_parity(data + 16);
5547 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005548}
David Brown7807bf72021-02-09 16:28:23 -07005549#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005550
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005551/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 * ECC keys on a Weierstrass elliptic curve require the generation
5553 * of a private key which is an integer
5554 * in the range [1, N - 1], where N is the boundary of the private key domain:
5555 * N is the prime p for Diffie-Hellman, or the order of the
5556 * curve’s base point for ECC.
5557 *
5558 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5559 * This function generates the private key using the following process:
5560 *
5561 * 1. Draw a byte string of length ceiling(m/8) bytes.
5562 * 2. If m is not a multiple of 8, set the most significant
5563 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5564 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5565 * 4. If k > N - 2, discard the result and return to step 1.
5566 * 5. Output k + 1 as the private key.
5567 *
5568 * This method allows compliance to NIST standards, specifically the methods titled
5569 * Key-Pair Generation by Testing Candidates in the following publications:
5570 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5571 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5572 * Diffie-Hellman keys.
5573 *
5574 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5575 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5576 *
5577 * Note: Function allocates memory for *data buffer, so given *data should be
5578 * always NULL.
5579 */
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005580#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5581 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005582 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5583 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5584 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005585static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005586 psa_key_slot_t *slot,
5587 size_t bits,
5588 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005589 uint8_t **data
5590 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005591{
Valerio Setti52789862023-04-07 12:13:11 +02005592#if defined(MBEDTLS_ECP_LIGHT)
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005593 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005594 mbedtls_mpi k;
5595 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005596 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005597 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005598
Gilles Peskine449bd832023-01-11 14:50:10 +01005599 mbedtls_mpi_init(&k);
5600 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005601
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005602 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005603 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005604 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005605 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005606
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005608 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005609 goto cleanup;
5610 }
5611
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005612 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005614
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005616
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005617 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005618 /* Let m be the bit size of N. */
5619 size_t m = ecp_group.nbits;
5620
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005622
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005623 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005624 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005625
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005626 /* Note: This function is always called with *data == NULL and it
5627 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 *data = mbedtls_calloc(1, m_bytes);
5629 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005630 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005631 goto cleanup;
5632 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005633
Gilles Peskine449bd832023-01-11 14:50:10 +01005634 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005635 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005636 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005637 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005638 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005639
5640 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005641 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005642 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005643 * (8 * ceiling(m/8) - m) bits of the first byte in
5644 * the string to zero.
5645 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005646 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005647 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005648 }
5649
5650 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005651 * big-endian byte string.
5652 */
5653 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005654
5655 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005656 * Result of comparison is returned. When it indicates error
5657 * then this function is called again.
5658 */
5659 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005660 }
5661
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005662 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005663 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5664 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005665cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005666 if (ret != 0) {
5667 status = mbedtls_to_psa_error(ret);
5668 }
5669 if (status != PSA_SUCCESS) {
5670 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005671 *data = NULL;
5672 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005673 mbedtls_mpi_free(&k);
5674 mbedtls_mpi_free(&diff_N_2);
5675 return status;
Valerio Setti52789862023-04-07 12:13:11 +02005676#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005677 (void) slot;
5678 (void) bits;
5679 (void) operation;
5680 (void) data;
5681 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti52789862023-04-07 12:13:11 +02005682#endif /* MBEDTLS_ECP_LIGHT */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005683}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005684
5685/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5686 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5687 *
5688 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5689 * draw a 32-byte string and process it as specified in
5690 * Elliptic Curves for Security [RFC7748] §5.
5691 *
5692 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5693 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5694 *
5695 * Note: Function allocates memory for *data buffer, so given *data should be
5696 * always NULL.
5697 */
5698
5699static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5700 size_t bits,
5701 psa_key_derivation_operation_t *operation,
5702 uint8_t **data
5703 )
5704{
5705 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005706 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005707
Gilles Peskine449bd832023-01-11 14:50:10 +01005708 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005709 case 255:
5710 output_length = 32;
5711 break;
5712 case 448:
5713 output_length = 56;
5714 break;
5715 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005717 break;
5718 }
5719
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005721
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 if (*data == NULL) {
5723 return PSA_ERROR_INSUFFICIENT_MEMORY;
5724 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005725
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005727
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005729 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005730 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005731
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005733 case 255:
5734 (*data)[0] &= 248;
5735 (*data)[31] &= 127;
5736 (*data)[31] |= 64;
5737 break;
5738 case 448:
5739 (*data)[0] &= 252;
5740 (*data)[55] |= 128;
5741 break;
5742 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005744 break;
5745 }
5746
5747 return status;
5748}
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005749#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5750 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005751 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5752 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5753 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005754
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005755static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005756 psa_key_slot_t *slot,
5757 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005759{
5760 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305762 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005763 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005764
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5766 return PSA_ERROR_INVALID_ARGUMENT;
5767 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005768
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005769#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5770 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005771 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5772 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5773 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5775 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5776 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005777 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5779 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005780 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 }
5782 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005783 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005784 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5785 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005786 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005788 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005789 } else
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005790#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5791 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005792 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5793 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5794 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005795 if (key_type_is_raw_bytes(slot->attr.type)) {
5796 if (bits % 8 != 0) {
5797 return PSA_ERROR_INVALID_ARGUMENT;
5798 }
5799 data = mbedtls_calloc(1, bytes);
5800 if (data == NULL) {
5801 return PSA_ERROR_INSUFFICIENT_MEMORY;
5802 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005803
Gilles Peskine449bd832023-01-11 14:50:10 +01005804 status = psa_key_derivation_output_bytes(operation, data, bytes);
5805 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005806 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 }
David Brown12ca5032021-02-11 11:02:00 -07005808#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5810 psa_des_set_key_parity(data, bytes);
5811 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005812#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 } else {
5814 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005815 }
Ronald Crondd04d422020-11-28 15:14:42 +01005816
Ronald Cronbf33c932020-11-28 18:06:53 +01005817 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005818 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005819 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005820 };
5821
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5823 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5824 &storage_size);
5825 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305826 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 }
Archanad8a83dc2021-06-14 10:04:16 +05305828 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005829 status = psa_allocate_buffer_to_slot(slot, storage_size);
5830 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305831 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 }
Archanad8a83dc2021-06-14 10:04:16 +05305833
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 status = psa_driver_wrapper_import_key(&attributes,
5835 data, bytes,
5836 slot->key.data,
5837 slot->key.bytes,
5838 &slot->key.bytes, &bits);
5839 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005840 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005842
5843exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005844 mbedtls_free(data);
5845 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005846}
5847
Gilles Peskine449bd832023-01-11 14:50:10 +01005848psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5849 psa_key_derivation_operation_t *operation,
5850 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005851{
5852 psa_status_t status;
5853 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005854 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005855
Ronald Cron81709fc2020-11-14 12:10:32 +01005856 *key = MBEDTLS_SVC_KEY_ID_INIT;
5857
Gilles Peskine0f84d622019-09-12 19:03:13 +02005858 /* Reject any attempt to create a zero-length key so that we don't
5859 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 if (psa_get_key_bits(attributes) == 0) {
5861 return PSA_ERROR_INVALID_ARGUMENT;
5862 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005863
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 if (operation->alg == PSA_ALG_NONE) {
5865 return PSA_ERROR_BAD_STATE;
5866 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005867
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 if (!operation->can_output_key) {
5869 return PSA_ERROR_NOT_PERMITTED;
5870 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005871
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5873 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005874#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005876 /* Deriving a key in a secure element is not implemented yet. */
5877 status = PSA_ERROR_NOT_SUPPORTED;
5878 }
5879#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 if (status == PSA_SUCCESS) {
5881 status = psa_generate_derived_key_internal(slot,
5882 attributes->core.bits,
5883 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005884 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 if (status == PSA_SUCCESS) {
5886 status = psa_finish_key_creation(slot, driver, key);
5887 }
5888 if (status != PSA_SUCCESS) {
5889 psa_fail_key_creation(slot, driver);
5890 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005891
Gilles Peskine449bd832023-01-11 14:50:10 +01005892 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005893}
5894
Gilles Peskineeab56e42018-07-12 17:12:33 +02005895
5896
5897/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005898/* Key derivation */
5899/****************************************************************/
5900
Steven Cooreman932ffb72021-02-15 12:14:32 +01005901#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005902static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005903{
5904#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5906 return 1;
5907 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005908#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005909#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005910 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5911 return 1;
5912 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005913#endif
5914#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005915 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5916 return 1;
5917 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005918#endif
5919#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5921 return 1;
5922 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005923#endif
5924#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005925 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5926 return 1;
5927 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005928#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005929#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5931 return 1;
5932 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005933#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005935}
5936
Gilles Peskine449bd832023-01-11 14:50:10 +01005937static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005938{
5939 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 psa_status_t status = psa_hash_setup(&operation, alg);
5941 psa_hash_abort(&operation);
5942 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005943}
5944
Gilles Peskine969c5d62019-01-16 15:53:06 +01005945static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005946 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005948{
Janos Follath5fe19732019-06-20 15:09:30 +01005949 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5950 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005952
Gilles Peskine969c5d62019-01-16 15:53:06 +01005953 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 if (!is_kdf_alg_supported(kdf_alg)) {
5955 return PSA_ERROR_NOT_SUPPORTED;
5956 }
John Durkop07cc04a2020-11-16 22:08:34 -08005957
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005958 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005959 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005960 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5961 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5962 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5963 if (hash_size == 0) {
5964 return PSA_ERROR_NOT_SUPPORTED;
5965 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005966
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005967 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5968 * we might fail later, which is somewhat unfriendly and potentially
5969 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005970 psa_status_t status = psa_hash_try_support(hash_alg);
5971 if (status != PSA_SUCCESS) {
5972 return status;
5973 }
5974 } else {
5975 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005976 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005977
Gilles Peskine449bd832023-01-11 14:50:10 +01005978 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5979 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
5980 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
5981 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005982 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005983#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005984 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
5986 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005987 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005989#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5990 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 operation->capacity = 255 * hash_size;
5992 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005993}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005994
Gilles Peskine449bd832023-01-11 14:50:10 +01005995static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005996{
5997#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005998 if (alg == PSA_ALG_ECDH) {
5999 return PSA_SUCCESS;
6000 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006001#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006002#if defined(PSA_WANT_ALG_FFDH)
6003 if (alg == PSA_ALG_FFDH) {
6004 return PSA_SUCCESS;
6005 }
6006#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006007 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006008 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006009}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006010
6011static int psa_key_derivation_allows_free_form_secret_input(
6012 psa_algorithm_t kdf_alg)
6013{
6014#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6015 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6016 return 0;
6017 }
6018#endif
6019 (void) kdf_alg;
6020 return 1;
6021}
John Durkop07cc04a2020-11-16 22:08:34 -08006022#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006023
Gilles Peskine449bd832023-01-11 14:50:10 +01006024psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6025 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006026{
6027 psa_status_t status;
6028
Gilles Peskine449bd832023-01-11 14:50:10 +01006029 if (operation->alg != 0) {
6030 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006031 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006032
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6034 return PSA_ERROR_INVALID_ARGUMENT;
6035 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6036#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6037 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6038 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6039 status = psa_key_agreement_try_support(ka_alg);
6040 if (status != PSA_SUCCESS) {
6041 return status;
6042 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006043 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6044 return PSA_ERROR_INVALID_ARGUMENT;
6045 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6047#else
6048 return PSA_ERROR_NOT_SUPPORTED;
6049#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6050 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6051#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6052 status = psa_key_derivation_setup_kdf(operation, alg);
6053#else
6054 return PSA_ERROR_NOT_SUPPORTED;
6055#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6056 } else {
6057 return PSA_ERROR_INVALID_ARGUMENT;
6058 }
6059
6060 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006061 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 }
6063 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006064}
6065
Przemek Stekiel69c46792022-06-10 12:59:51 +02006066#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006067static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6068 psa_algorithm_t kdf_alg,
6069 psa_key_derivation_step_t step,
6070 const uint8_t *data,
6071 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006072{
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006074 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006076 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006077#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6079 return PSA_ERROR_INVALID_ARGUMENT;
6080 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006081#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 if (hkdf->state != HKDF_STATE_INIT) {
6083 return PSA_ERROR_BAD_STATE;
6084 } else {
6085 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6086 hash_alg,
6087 data, data_length);
6088 if (status != PSA_SUCCESS) {
6089 return status;
6090 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006091 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006093 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006094 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006095#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006097 /* We shouldn't be in different state as HKDF_EXPAND only allows
6098 * two inputs: SECRET (this case) and INFO which does not modify
6099 * the state. It could happen only if the hkdf
6100 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 if (hkdf->state != HKDF_STATE_INIT) {
6102 return PSA_ERROR_BAD_STATE;
6103 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006104
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006105 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006106 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6107 return PSA_ERROR_INVALID_ARGUMENT;
6108 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006109
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 memcpy(hkdf->prk, data, data_length);
6111 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006112#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006113 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006114 /* HKDF: If no salt was provided, use an empty salt.
6115 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006116 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006117#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6119 return PSA_ERROR_BAD_STATE;
6120 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006121#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6123 hash_alg,
6124 NULL, 0);
6125 if (status != PSA_SUCCESS) {
6126 return status;
6127 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006128 hkdf->state = HKDF_STATE_STARTED;
6129 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 if (hkdf->state != HKDF_STATE_STARTED) {
6131 return PSA_ERROR_BAD_STATE;
6132 }
6133 status = psa_mac_update(&hkdf->hmac,
6134 data, data_length);
6135 if (status != PSA_SUCCESS) {
6136 return status;
6137 }
6138 status = psa_mac_sign_finish(&hkdf->hmac,
6139 hkdf->prk,
6140 sizeof(hkdf->prk),
6141 &data_length);
6142 if (status != PSA_SUCCESS) {
6143 return status;
6144 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006145 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006146
Gilles Peskine2b522db2019-04-12 15:11:49 +02006147 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006148 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006149#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006151 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006152 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006153 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006155#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006156 {
6157 /* Block 0 is empty, and the next block will be
6158 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006159 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006160 }
6161
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006163 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006164#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6166 return PSA_ERROR_INVALID_ARGUMENT;
6167 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006168#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006169#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6171 hkdf->state == HKDF_STATE_INIT) {
6172 return PSA_ERROR_BAD_STATE;
6173 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006174#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006175 if (hkdf->state == HKDF_STATE_OUTPUT) {
6176 return PSA_ERROR_BAD_STATE;
6177 }
6178 if (hkdf->info_set) {
6179 return PSA_ERROR_BAD_STATE;
6180 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006181 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 if (data_length != 0) {
6183 hkdf->info = mbedtls_calloc(1, data_length);
6184 if (hkdf->info == NULL) {
6185 return PSA_ERROR_INSUFFICIENT_MEMORY;
6186 }
6187 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006188 }
6189 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006191 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006192 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006193 }
6194}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006195#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006196
John Durkop07cc04a2020-11-16 22:08:34 -08006197#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6198 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006199static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6200 const uint8_t *data,
6201 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006202{
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6204 return PSA_ERROR_BAD_STATE;
6205 }
Janos Follathf08e2652019-06-13 09:05:41 +01006206
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 if (data_length != 0) {
6208 prf->seed = mbedtls_calloc(1, data_length);
6209 if (prf->seed == NULL) {
6210 return PSA_ERROR_INSUFFICIENT_MEMORY;
6211 }
Janos Follathf08e2652019-06-13 09:05:41 +01006212
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006214 prf->seed_length = data_length;
6215 }
Janos Follathf08e2652019-06-13 09:05:41 +01006216
Gilles Peskine43f958b2020-12-13 14:55:14 +01006217 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006218
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006220}
6221
Gilles Peskine449bd832023-01-11 14:50:10 +01006222static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6223 const uint8_t *data,
6224 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006225{
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6227 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6228 return PSA_ERROR_BAD_STATE;
6229 }
Janos Follath81550542019-06-13 14:26:34 +01006230
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 if (data_length != 0) {
6232 prf->secret = mbedtls_calloc(1, data_length);
6233 if (prf->secret == NULL) {
6234 return PSA_ERROR_INSUFFICIENT_MEMORY;
6235 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006236
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006238 prf->secret_length = data_length;
6239 }
Janos Follath81550542019-06-13 14:26:34 +01006240
Gilles Peskine43f958b2020-12-13 14:55:14 +01006241 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006242
Gilles Peskine449bd832023-01-11 14:50:10 +01006243 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006244}
6245
Gilles Peskine449bd832023-01-11 14:50:10 +01006246static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6247 const uint8_t *data,
6248 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006249{
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6251 return PSA_ERROR_BAD_STATE;
6252 }
Janos Follath63028dd2019-06-13 09:15:47 +01006253
Gilles Peskine449bd832023-01-11 14:50:10 +01006254 if (data_length != 0) {
6255 prf->label = mbedtls_calloc(1, data_length);
6256 if (prf->label == NULL) {
6257 return PSA_ERROR_INSUFFICIENT_MEMORY;
6258 }
Janos Follath63028dd2019-06-13 09:15:47 +01006259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006261 prf->label_length = data_length;
6262 }
Janos Follath63028dd2019-06-13 09:15:47 +01006263
Gilles Peskine43f958b2020-12-13 14:55:14 +01006264 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006265
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006267}
6268
Gilles Peskine449bd832023-01-11 14:50:10 +01006269static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6270 psa_key_derivation_step_t step,
6271 const uint8_t *data,
6272 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006273{
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006275 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006277 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006279 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006281 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006283 }
6284}
John Durkop07cc04a2020-11-16 22:08:34 -08006285#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6286 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6287
6288#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6289static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6290 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006291 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006293{
6294 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6296 4 + data_length + prf->other_secret_length :
6297 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006298
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6300 return PSA_ERROR_INVALID_ARGUMENT;
6301 }
John Durkop07cc04a2020-11-16 22:08:34 -08006302
Gilles Peskine449bd832023-01-11 14:50:10 +01006303 uint8_t *pms = mbedtls_calloc(1, pms_len);
6304 if (pms == NULL) {
6305 return PSA_ERROR_INSUFFICIENT_MEMORY;
6306 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006307 uint8_t *cur = pms;
6308
6309 /* pure-PSK:
6310 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006311 *
6312 * The premaster secret is formed as follows: if the PSK is N octets
6313 * long, concatenate a uint16 with the value N, N zero octets, a second
6314 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006315 *
6316 * mixed-PSK:
6317 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6318 * follows: concatenate a uint16 with the length of the other secret,
6319 * the other secret itself, uint16 with the length of PSK, and the
6320 * PSK itself.
6321 * For details please check:
6322 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6323 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6324 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006325 */
6326
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6328 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6329 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6330 if (prf->other_secret_length != 0) {
6331 memcpy(cur, prf->other_secret, prf->other_secret_length);
6332 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006333 cur += prf->other_secret_length;
6334 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 } else {
6336 *cur++ = MBEDTLS_BYTE_1(data_length);
6337 *cur++ = MBEDTLS_BYTE_0(data_length);
6338 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006339 cur += data_length;
6340 }
6341
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 *cur++ = MBEDTLS_BYTE_1(data_length);
6343 *cur++ = MBEDTLS_BYTE_0(data_length);
6344 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006345 cur += data_length;
6346
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006348
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 mbedtls_platform_zeroize(pms, pms_len);
6350 mbedtls_free(pms);
6351 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006352}
Janos Follath6660f0e2019-06-17 08:44:03 +01006353
Przemek Stekiele47201b2022-04-19 13:53:28 +02006354static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6355 psa_tls12_prf_key_derivation_t *prf,
6356 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006358{
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6360 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006361 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006362
6363 if (data_length != 0) {
6364 prf->other_secret = mbedtls_calloc(1, data_length);
6365 if (prf->other_secret == NULL) {
6366 return PSA_ERROR_INSUFFICIENT_MEMORY;
6367 }
6368
6369 memcpy(prf->other_secret, data, data_length);
6370 prf->other_secret_length = data_length;
6371 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006372 prf->other_secret_length = 0;
6373 }
6374
6375 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6376
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006378}
6379
Janos Follath6660f0e2019-06-17 08:44:03 +01006380static psa_status_t psa_tls12_prf_psk_to_ms_input(
6381 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006382 psa_key_derivation_step_t step,
6383 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006385{
Gilles Peskine449bd832023-01-11 14:50:10 +01006386 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006387 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 return psa_tls12_prf_psk_to_ms_set_key(prf,
6389 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006390 break;
6391 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6393 data,
6394 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006395 break;
6396 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006398 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006399
Przemek Stekiele47201b2022-04-19 13:53:28 +02006400 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006401}
John Durkop07cc04a2020-11-16 22:08:34 -08006402#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006403
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006404#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6405static psa_status_t psa_tls12_ecjpake_to_pms_input(
6406 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006407 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006408 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006410{
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6412 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6413 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006414 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006415
6416 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 if (data[0] != 0x04) {
6418 return PSA_ERROR_INVALID_ARGUMENT;
6419 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006420
6421 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006423
Gilles Peskine449bd832023-01-11 14:50:10 +01006424 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006425}
6426#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006427/** Check whether the given key type is acceptable for the given
6428 * input step of a key derivation.
6429 *
6430 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6431 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6432 * Both secret and non-secret inputs can alternatively have the type
6433 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6434 * that the input was passed as a buffer rather than via a key object.
6435 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006436static int psa_key_derivation_check_input_type(
6437 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006439{
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006441 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006442 if (key_type == PSA_KEY_TYPE_DERIVE) {
6443 return PSA_SUCCESS;
6444 }
6445 if (key_type == PSA_KEY_TYPE_NONE) {
6446 return PSA_SUCCESS;
6447 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006448 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006449 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 if (key_type == PSA_KEY_TYPE_DERIVE) {
6451 return PSA_SUCCESS;
6452 }
6453 if (key_type == PSA_KEY_TYPE_NONE) {
6454 return PSA_SUCCESS;
6455 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006456 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006457 case PSA_KEY_DERIVATION_INPUT_LABEL:
6458 case PSA_KEY_DERIVATION_INPUT_SALT:
6459 case PSA_KEY_DERIVATION_INPUT_INFO:
6460 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006461 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6462 return PSA_SUCCESS;
6463 }
6464 if (key_type == PSA_KEY_TYPE_NONE) {
6465 return PSA_SUCCESS;
6466 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006467 break;
6468 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006470}
6471
Janos Follathb80a94e2019-06-12 15:54:46 +01006472static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006473 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006474 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006475 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006476 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006478{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006479 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006481
Gilles Peskine449bd832023-01-11 14:50:10 +01006482 status = psa_key_derivation_check_input_type(step, key_type);
6483 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006484 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006486
Przemek Stekiel69c46792022-06-10 12:59:51 +02006487#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6489 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6490 step, data, data_length);
6491 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006492#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006493#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6495 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6496 step, data, data_length);
6497 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006498#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6499#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6501 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6502 step, data, data_length);
6503 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006504#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006505#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006506 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006507 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6509 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006510#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006511 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006512 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006513 (void) data;
6514 (void) data_length;
6515 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006516 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006517 }
6518
Gilles Peskine224b0d62019-09-23 18:13:17 +02006519exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006520 if (status != PSA_SUCCESS) {
6521 psa_key_derivation_abort(operation);
6522 }
6523 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006524}
6525
Janos Follath51f4a0f2019-06-14 11:35:55 +01006526psa_status_t psa_key_derivation_input_bytes(
6527 psa_key_derivation_operation_t *operation,
6528 psa_key_derivation_step_t step,
6529 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006530 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006531{
Gilles Peskine449bd832023-01-11 14:50:10 +01006532 return psa_key_derivation_input_internal(operation, step,
6533 PSA_KEY_TYPE_NONE,
6534 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006535}
6536
Janos Follath51f4a0f2019-06-14 11:35:55 +01006537psa_status_t psa_key_derivation_input_key(
6538 psa_key_derivation_operation_t *operation,
6539 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006540 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006541{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006542 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006543 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006544 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006545
Ronald Cron5c522922020-11-14 16:35:34 +01006546 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006547 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6548 if (status != PSA_SUCCESS) {
6549 psa_key_derivation_abort(operation);
6550 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006551 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006552
6553 /* Passing a key object as a SECRET input unlocks the permission
6554 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006555 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006556 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006558
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 status = psa_key_derivation_input_internal(operation,
6560 step, slot->attr.type,
6561 slot->key.data,
6562 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006563
Gilles Peskine449bd832023-01-11 14:50:10 +01006564 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006565
Gilles Peskine449bd832023-01-11 14:50:10 +01006566 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006567}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006568
6569
6570
6571/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006572/* Key agreement */
6573/****************************************************************/
6574
Gilles Peskine449bd832023-01-11 14:50:10 +01006575psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6576 const uint8_t *key_buffer,
6577 size_t key_buffer_size,
6578 psa_algorithm_t alg,
6579 const uint8_t *peer_key,
6580 size_t peer_key_length,
6581 uint8_t *shared_secret,
6582 size_t shared_secret_size,
6583 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006584{
Gilles Peskine449bd832023-01-11 14:50:10 +01006585 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006586#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006587 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6589 key_buffer_size, alg,
6590 peer_key, peer_key_length,
6591 shared_secret,
6592 shared_secret_size,
6593 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006594#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006595
6596#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6597 case PSA_ALG_FFDH:
Przemek Stekiel359f4622022-12-05 14:11:55 +01006598 return mbedtls_psa_key_agreement_ffdh(attributes,
6599 peer_key,
6600 peer_key_length,
6601 key_buffer,
6602 key_buffer_size,
6603 shared_secret,
6604 shared_secret_size,
6605 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006606#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6607
Gilles Peskine01d718c2018-09-18 12:01:02 +02006608 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006609 (void) attributes;
6610 (void) key_buffer;
6611 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006612 (void) peer_key;
6613 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006614 (void) shared_secret;
6615 (void) shared_secret_size;
6616 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006617 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006618 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006619}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006620
Aditya Deshpande17845b82022-10-13 17:21:01 +01006621/** Internal function for raw key agreement
6622 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006623 * to the driver's implementation if a driver is present.
6624 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006625 * (psa_key_agreement_raw_builtin).
6626 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006627static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6628 psa_key_slot_t *private_key,
6629 const uint8_t *peer_key,
6630 size_t peer_key_length,
6631 uint8_t *shared_secret,
6632 size_t shared_secret_size,
6633 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006634{
Gilles Peskine449bd832023-01-11 14:50:10 +01006635 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6636 return PSA_ERROR_NOT_SUPPORTED;
6637 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006638
6639 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006640 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006641 };
6642
Gilles Peskine449bd832023-01-11 14:50:10 +01006643 return psa_driver_wrapper_key_agreement(&attributes,
6644 private_key->key.data,
6645 private_key->key.bytes, alg,
6646 peer_key, peer_key_length,
6647 shared_secret,
6648 shared_secret_size,
6649 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006650}
6651
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006652/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006653 * to potentially free embedded data structures and wipe confidential data.
6654 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006655static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6656 psa_key_derivation_step_t step,
6657 psa_key_slot_t *private_key,
6658 const uint8_t *peer_key,
6659 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006660{
6661 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006662 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006663 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006664 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006665
6666 /* Step 1: run the secret agreement algorithm to generate the shared
6667 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006668 status = psa_key_agreement_raw_internal(ka_alg,
6669 private_key,
6670 peer_key, peer_key_length,
6671 shared_secret,
6672 sizeof(shared_secret),
6673 &shared_secret_length);
6674 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006675 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006676 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006677
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006678 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006679 * the shared secret. A shared secret is permitted wherever a key
6680 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006681 status = psa_key_derivation_input_internal(operation, step,
6682 PSA_KEY_TYPE_DERIVE,
6683 shared_secret,
6684 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006685exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006686 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6687 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006688}
6689
Gilles Peskine449bd832023-01-11 14:50:10 +01006690psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6691 psa_key_derivation_step_t step,
6692 mbedtls_svc_key_id_t private_key,
6693 const uint8_t *peer_key,
6694 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006695{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006696 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006697 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006698 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006699
Gilles Peskine449bd832023-01-11 14:50:10 +01006700 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6701 return PSA_ERROR_INVALID_ARGUMENT;
6702 }
Ronald Cron5c522922020-11-14 16:35:34 +01006703 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006704 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6705 if (status != PSA_SUCCESS) {
6706 return status;
6707 }
6708 status = psa_key_agreement_internal(operation, step,
6709 slot,
6710 peer_key, peer_key_length);
6711 if (status != PSA_SUCCESS) {
6712 psa_key_derivation_abort(operation);
6713 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006714 /* If a private key has been added as SECRET, we allow the derived
6715 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006716 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006717 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006718 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006719 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006720
Gilles Peskine449bd832023-01-11 14:50:10 +01006721 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006722
Gilles Peskine449bd832023-01-11 14:50:10 +01006723 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006724}
6725
Gilles Peskine449bd832023-01-11 14:50:10 +01006726psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6727 mbedtls_svc_key_id_t private_key,
6728 const uint8_t *peer_key,
6729 size_t peer_key_length,
6730 uint8_t *output,
6731 size_t output_size,
6732 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006733{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006734 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006735 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006736 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006737
Gilles Peskine449bd832023-01-11 14:50:10 +01006738 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006739 status = PSA_ERROR_INVALID_ARGUMENT;
6740 goto exit;
6741 }
Ronald Cron5c522922020-11-14 16:35:34 +01006742 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006743 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6744 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006745 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006746 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006747
Gilles Peskine3e561302022-04-14 00:17:15 +02006748 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6749 * for the output size. The PSA specification only guarantees that this
6750 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6751 * but it might be nice to allow smaller buffers if the output fits.
6752 * At the time of writing this comment, with only ECDH implemented,
6753 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6754 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6755 * be exact for it as well. */
6756 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006757 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6758 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006759 status = PSA_ERROR_BUFFER_TOO_SMALL;
6760 goto exit;
6761 }
6762
Gilles Peskine449bd832023-01-11 14:50:10 +01006763 status = psa_key_agreement_raw_internal(alg, slot,
6764 peer_key, peer_key_length,
6765 output, output_size,
6766 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006767
6768exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006769 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006770 /* If an error happens and is not handled properly, the output
6771 * may be used as a key to protect sensitive data. Arrange for such
6772 * a key to be random, which is likely to result in decryption or
6773 * verification errors. This is better than filling the buffer with
6774 * some constant data such as zeros, which would result in the data
6775 * being protected with a reproducible, easily knowable key.
6776 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006777 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006778 *output_length = output_size;
6779 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006780
Gilles Peskine449bd832023-01-11 14:50:10 +01006781 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006782
Gilles Peskine449bd832023-01-11 14:50:10 +01006783 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006784}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006785
6786
Gilles Peskine30524eb2020-11-13 17:02:26 +01006787
Gilles Peskine86a440b2018-11-12 18:39:40 +01006788/****************************************************************/
6789/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006790/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006791
Gilles Peskine30524eb2020-11-13 17:02:26 +01006792/** Initialize the PSA random generator.
6793 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006794static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006795{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006796#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006797 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006798#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6799
Gilles Peskine30524eb2020-11-13 17:02:26 +01006800 /* Set default configuration if
6801 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006802 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006803 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006804 }
6805 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006806 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006807 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006808
Gilles Peskine449bd832023-01-11 14:50:10 +01006809 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006810#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6811 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6812 /* The PSA entropy injection feature depends on using NV seed as an entropy
6813 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006814 mbedtls_entropy_add_source(&rng->entropy,
6815 mbedtls_nv_seed_poll, NULL,
6816 MBEDTLS_ENTROPY_BLOCK_SIZE,
6817 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006818#endif
6819
Gilles Peskine449bd832023-01-11 14:50:10 +01006820 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006821#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006822}
6823
6824/** Deinitialize the PSA random generator.
6825 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006826static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006827{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006828#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006829 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006830#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01006831 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
6832 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006833#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006834}
6835
6836/** Seed the PSA random generator.
6837 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006838static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006839{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006840#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6841 /* Do nothing: the external RNG seeds itself. */
6842 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006843 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006844#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006845 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01006846 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
6847 drbg_seed, sizeof(drbg_seed) - 1);
6848 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006849#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006850}
6851
Gilles Peskine449bd832023-01-11 14:50:10 +01006852psa_status_t psa_generate_random(uint8_t *output,
6853 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006854{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006855 GUARD_MODULE_INITIALIZED;
6856
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006857#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6858
6859 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006860 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6861 output, output_size,
6862 &output_length);
6863 if (status != PSA_SUCCESS) {
6864 return status;
6865 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006866 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006867 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 if (output_length != output_size) {
6869 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6870 }
6871 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006872
6873#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6874
Gilles Peskine449bd832023-01-11 14:50:10 +01006875 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006876 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006877 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6878 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6879 output_size);
6880 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6881 output, request_size);
6882 if (ret != 0) {
6883 return mbedtls_to_psa_error(ret);
6884 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006885 output_size -= request_size;
6886 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006887 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006888 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006889#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006890}
6891
Gilles Peskine8814fc42020-12-14 15:33:44 +01006892/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006893 *
6894 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6895 * `psa_generate_random(...)`. The state parameter is ignored since the
6896 * PSA API doesn't support passing an explicit state.
6897 *
6898 * In the non-external case, psa_generate_random() calls an
6899 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6900 * and semantics as mbedtls_psa_get_random(). As an optimization,
6901 * instead of doing this back-and-forth between the PSA API and the
6902 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6903 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006904 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006905#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6906int mbedtls_psa_get_random(void *p_rng,
6907 unsigned char *output,
6908 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01006909{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006910 /* This function takes a pointer to the RNG state because that's what
6911 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6912 * its own state internally and doesn't let the caller access that state.
6913 * So we just ignore the state parameter, and in practice we'll pass
6914 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006915 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006916 psa_status_t status = psa_generate_random(output, output_size);
6917 if (status == PSA_SUCCESS) {
6918 return 0;
6919 } else {
6920 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
6921 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01006922}
6923#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6924
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006925#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006926#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006927
Gilles Peskine449bd832023-01-11 14:50:10 +01006928psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
6929 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006930{
Gilles Peskine449bd832023-01-11 14:50:10 +01006931 if (global_data.initialized) {
6932 return PSA_ERROR_NOT_PERMITTED;
6933 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006934
Gilles Peskine449bd832023-01-11 14:50:10 +01006935 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
6936 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
6937 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
6938 return PSA_ERROR_INVALID_ARGUMENT;
6939 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006940
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006942}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006943#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006944
Ronald Cron3772afe2021-02-08 16:10:05 +01006945/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006946 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006947 * \param type The key type
6948 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006949 *
6950 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006951 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006952 * \retval #PSA_ERROR_INVALID_ARGUMENT
6953 * The size in bits of the key is not valid.
6954 * \retval #PSA_ERROR_NOT_SUPPORTED
6955 * The type and/or the size in bits of the key or the combination of
6956 * the two is not supported.
6957 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006958static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006959 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006960{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006961 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006962
Gilles Peskine449bd832023-01-11 14:50:10 +01006963 if (key_type_is_raw_bytes(type)) {
6964 status = psa_validate_unstructured_key_bit_size(type, bits);
6965 if (status != PSA_SUCCESS) {
6966 return status;
6967 }
6968 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006969#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006970 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6971 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
6972 return PSA_ERROR_NOT_SUPPORTED;
6973 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006974
Ronald Cron01b2aba2020-10-05 09:42:02 +02006975 /* Accept only byte-aligned keys, for the same reasons as
6976 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006977 if (bits % 8 != 0) {
6978 return PSA_ERROR_NOT_SUPPORTED;
6979 }
6980 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006981#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006982
Ronald Cron5c4d3862020-12-07 11:07:24 +01006983#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006984 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01006985 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006986 return PSA_SUCCESS;
6987 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006988#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01006989
6990#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR)
6991 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6992 if (bits != 2048 && bits != 3072 && bits != 4096 &&
6993 bits != 6144 && bits != 8192) {
6994 return PSA_ERROR_NOT_SUPPORTED;
6995 }
6996 } else
6997#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006998 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006999 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007000 }
7001
Gilles Peskine449bd832023-01-11 14:50:10 +01007002 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007003}
7004
Ronald Cron55ed0592020-10-05 10:30:40 +02007005psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007006 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007007 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007008{
7009 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007010 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007011
Gilles Peskine449bd832023-01-11 14:50:10 +01007012 if ((attributes->domain_parameters == NULL) &&
7013 (attributes->domain_parameters_size != 0)) {
7014 return PSA_ERROR_INVALID_ARGUMENT;
7015 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007016
Gilles Peskine449bd832023-01-11 14:50:10 +01007017 if (key_type_is_raw_bytes(type)) {
7018 status = psa_generate_random(key_buffer, key_buffer_size);
7019 if (status != PSA_SUCCESS) {
7020 return status;
7021 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007022
David Brown12ca5032021-02-11 11:02:00 -07007023#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007024 if (type == PSA_KEY_TYPE_DES) {
7025 psa_des_set_key_parity(key_buffer, key_buffer_size);
7026 }
David Brown8107e312021-02-12 08:08:46 -07007027#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007028 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007029
Jaeden Amero424fa932021-05-14 08:34:32 +01007030#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
7031 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01007032 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7033 return mbedtls_psa_rsa_generate_key(attributes,
7034 key_buffer,
7035 key_buffer_size,
7036 key_buffer_length);
7037 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01007038#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
7039 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007040
John Durkop9814fa22020-11-04 12:28:15 -08007041#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007042 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7043 return mbedtls_psa_ecp_generate_key(attributes,
7044 key_buffer,
7045 key_buffer_size,
7046 key_buffer_length);
7047 } else
John Durkop9814fa22020-11-04 12:28:15 -08007048#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007049
7050#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR)
7051 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7052 return mbedtls_psa_ffdh_generate_key(attributes,
7053 key_buffer,
7054 key_buffer_size,
7055 key_buffer_length);
7056 } else
7057#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02007058 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007059 (void) key_buffer_length;
7060 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007061 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007062
Gilles Peskine449bd832023-01-11 14:50:10 +01007063 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007064}
Darryl Green0c6575a2018-11-07 16:05:30 +00007065
Gilles Peskine449bd832023-01-11 14:50:10 +01007066psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7067 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007068{
7069 psa_status_t status;
7070 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007071 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007072 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007073
Ronald Cron81709fc2020-11-14 12:10:32 +01007074 *key = MBEDTLS_SVC_KEY_ID_INIT;
7075
Gilles Peskine0f84d622019-09-12 19:03:13 +02007076 /* Reject any attempt to create a zero-length key so that we don't
7077 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007078 if (psa_get_key_bits(attributes) == 0) {
7079 return PSA_ERROR_INVALID_ARGUMENT;
7080 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007081
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007082 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007083 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7084 return PSA_ERROR_INVALID_ARGUMENT;
7085 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007086
Gilles Peskine449bd832023-01-11 14:50:10 +01007087 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7088 &slot, &driver);
7089 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007090 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007091 }
Gilles Peskine11792082019-08-06 18:36:36 +02007092
Ronald Cron977c2472020-10-13 08:32:21 +02007093 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307094 * storage ( thus not in the case of generating a key in a secure element
7095 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7096 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007097 if (slot->key.data == NULL) {
7098 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7099 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007100 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007101 attributes->core.type, attributes->core.bits);
7102 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007103 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007104 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007105
7106 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007107 attributes->core.type,
7108 attributes->core.bits);
7109 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007110 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007111 attributes, &key_buffer_size);
7112 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007113 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007114 }
Ronald Cron977c2472020-10-13 08:32:21 +02007115 }
Ronald Cron977c2472020-10-13 08:32:21 +02007116
Gilles Peskine449bd832023-01-11 14:50:10 +01007117 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7118 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007119 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007120 }
Ronald Cron977c2472020-10-13 08:32:21 +02007121 }
7122
Gilles Peskine449bd832023-01-11 14:50:10 +01007123 status = psa_driver_wrapper_generate_key(attributes,
7124 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007125
Gilles Peskine449bd832023-01-11 14:50:10 +01007126 if (status != PSA_SUCCESS) {
7127 psa_remove_key_data_from_memory(slot);
7128 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007129
Gilles Peskine11792082019-08-06 18:36:36 +02007130exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007131 if (status == PSA_SUCCESS) {
7132 status = psa_finish_key_creation(slot, driver, key);
7133 }
7134 if (status != PSA_SUCCESS) {
7135 psa_fail_key_creation(slot, driver);
7136 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007137
Gilles Peskine449bd832023-01-11 14:50:10 +01007138 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007139}
7140
Gilles Peskinee59236f2018-01-27 23:32:46 +01007141/****************************************************************/
7142/* Module setup */
7143/****************************************************************/
7144
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007145#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007146psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007147 void (* entropy_init)(mbedtls_entropy_context *ctx),
7148 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007149{
Gilles Peskine449bd832023-01-11 14:50:10 +01007150 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7151 return PSA_ERROR_BAD_STATE;
7152 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007153 global_data.rng.entropy_init = entropy_init;
7154 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007155 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007156}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007157#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007158
Gilles Peskine449bd832023-01-11 14:50:10 +01007159void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007160{
Gilles Peskine449bd832023-01-11 14:50:10 +01007161 psa_wipe_all_key_slots();
7162 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7163 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007164 }
7165 /* Wipe all remaining data, including configuration.
7166 * In particular, this sets all state indicator to the value
7167 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007168 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007169
7170 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007171 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007172}
7173
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007174#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7175/** Recover a transaction that was interrupted by a power failure.
7176 *
7177 * This function is called during initialization, before psa_crypto_init()
7178 * returns. If this function returns a failure status, the initialization
7179 * fails.
7180 */
7181static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007182 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007183{
Gilles Peskine449bd832023-01-11 14:50:10 +01007184 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007185 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7186 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007187 /* TODO - fall through to the failure case until this
7188 * is implemented.
7189 * https://github.com/ARMmbed/mbed-crypto/issues/218
7190 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007191 default:
7192 /* We found an unsupported transaction in the storage.
7193 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007194 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007195 }
7196}
7197#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7198
Gilles Peskine449bd832023-01-11 14:50:10 +01007199psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007200{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007201 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007202
Gilles Peskinec6b69072018-11-20 21:42:52 +01007203 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007204 if (global_data.initialized != 0) {
7205 return PSA_SUCCESS;
7206 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007207
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007208 /* Init drivers */
7209 status = psa_driver_wrapper_init();
7210 if (status != PSA_SUCCESS) {
7211 goto exit;
7212 }
7213 global_data.drivers_initialized = 1;
7214
Gilles Peskine30524eb2020-11-13 17:02:26 +01007215 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007216 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007217 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007218 status = mbedtls_psa_random_seed(&global_data.rng);
7219 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007220 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007222 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007223
Gilles Peskine449bd832023-01-11 14:50:10 +01007224 status = psa_initialize_key_slots();
7225 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007226 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007227 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007228
Gilles Peskine4b734222019-07-24 15:56:31 +02007229#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007230 status = psa_crypto_load_transaction();
7231 if (status == PSA_SUCCESS) {
7232 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7233 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007234 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007235 }
7236 status = psa_crypto_stop_transaction();
7237 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007238 /* There's no transaction to complete. It's all good. */
7239 status = PSA_SUCCESS;
7240 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007241#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007242
Gilles Peskinec6b69072018-11-20 21:42:52 +01007243 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007244 global_data.initialized = 1;
7245
7246exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007247 if (status != PSA_SUCCESS) {
7248 mbedtls_psa_crypto_free();
7249 }
7250 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007251}
7252
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007253psa_status_t psa_crypto_driver_pake_get_password_len(
7254 const psa_crypto_driver_pake_inputs_t *inputs,
7255 size_t *password_len)
7256{
7257 if (inputs->password_len == 0) {
7258 return PSA_ERROR_BAD_STATE;
7259 }
7260
7261 *password_len = inputs->password_len;
7262
7263 return PSA_SUCCESS;
7264}
7265
7266psa_status_t psa_crypto_driver_pake_get_password(
7267 const psa_crypto_driver_pake_inputs_t *inputs,
7268 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7269{
7270 if (inputs->password_len == 0) {
7271 return PSA_ERROR_BAD_STATE;
7272 }
7273
7274 if (buffer_size < inputs->password_len) {
7275 return PSA_ERROR_BUFFER_TOO_SMALL;
7276 }
7277
7278 memcpy(buffer, inputs->password, inputs->password_len);
7279 *buffer_length = inputs->password_len;
7280
7281 return PSA_SUCCESS;
7282}
7283
7284psa_status_t psa_crypto_driver_pake_get_role(
7285 const psa_crypto_driver_pake_inputs_t *inputs,
7286 psa_pake_role_t *role)
7287{
7288 if (inputs->role == PSA_PAKE_ROLE_NONE) {
7289 return PSA_ERROR_BAD_STATE;
7290 }
7291
7292 *role = inputs->role;
7293
7294 return PSA_SUCCESS;
7295}
7296
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007297psa_status_t psa_crypto_driver_pake_get_user_len(
7298 const psa_crypto_driver_pake_inputs_t *inputs,
7299 size_t *user_len)
7300{
7301 if (inputs->user_len == 0) {
7302 return PSA_ERROR_BAD_STATE;
7303 }
7304
7305 *user_len = inputs->user_len;
7306
7307 return PSA_SUCCESS;
7308}
7309
7310psa_status_t psa_crypto_driver_pake_get_user(
7311 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007312 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007313{
7314 if (inputs->user_len == 0) {
7315 return PSA_ERROR_BAD_STATE;
7316 }
7317
Przemek Stekielc0e62502023-03-14 11:49:36 +01007318 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007319 return PSA_ERROR_BUFFER_TOO_SMALL;
7320 }
7321
Przemek Stekielc0e62502023-03-14 11:49:36 +01007322 memcpy(user_id, inputs->user, inputs->user_len);
7323 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007324
7325 return PSA_SUCCESS;
7326}
7327
7328psa_status_t psa_crypto_driver_pake_get_peer_len(
7329 const psa_crypto_driver_pake_inputs_t *inputs,
7330 size_t *peer_len)
7331{
7332 if (inputs->peer_len == 0) {
7333 return PSA_ERROR_BAD_STATE;
7334 }
7335
7336 *peer_len = inputs->peer_len;
7337
7338 return PSA_SUCCESS;
7339}
7340
7341psa_status_t psa_crypto_driver_pake_get_peer(
7342 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007343 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007344{
7345 if (inputs->peer_len == 0) {
7346 return PSA_ERROR_BAD_STATE;
7347 }
7348
Przemek Stekielc0e62502023-03-14 11:49:36 +01007349 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007350 return PSA_ERROR_BUFFER_TOO_SMALL;
7351 }
7352
Przemek Stekielc0e62502023-03-14 11:49:36 +01007353 memcpy(peer_id, inputs->peer, inputs->peer_len);
7354 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007355
7356 return PSA_SUCCESS;
7357}
7358
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007359psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7360 const psa_crypto_driver_pake_inputs_t *inputs,
7361 psa_pake_cipher_suite_t *cipher_suite)
7362{
7363 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7364 return PSA_ERROR_BAD_STATE;
7365 }
7366
7367 *cipher_suite = inputs->cipher_suite;
7368
7369 return PSA_SUCCESS;
7370}
7371
Neil Armstronga7d08c32022-06-01 18:21:20 +02007372psa_status_t psa_pake_setup(
7373 psa_pake_operation_t *operation,
7374 const psa_pake_cipher_suite_t *cipher_suite)
7375{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007376 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007377
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007378 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007379 status = PSA_ERROR_BAD_STATE;
7380 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007381 }
7382
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007383 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007384 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007385 status = PSA_ERROR_INVALID_ARGUMENT;
7386 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007387 }
7388
Przemek Stekiel51eac532022-12-07 11:04:51 +01007389 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7390
Przemek Stekiele12ed362022-12-21 12:54:46 +01007391 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007392 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7393 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007394 operation->data.inputs.cipher_suite = *cipher_suite;
7395
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007396#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007397 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007398 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007399 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007400
Przemek Stekiele12ed362022-12-21 12:54:46 +01007401 computation_stage->state = PSA_PAKE_STATE_SETUP;
7402 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7403 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7404 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007405 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007406#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007407 {
7408 status = PSA_ERROR_NOT_SUPPORTED;
7409 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007410 }
7411
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007412 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7413
Przemek Stekiel51eac532022-12-07 11:04:51 +01007414 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007415exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007416 psa_pake_abort(operation);
7417 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007418}
7419
7420psa_status_t psa_pake_set_password_key(
7421 psa_pake_operation_t *operation,
7422 mbedtls_svc_key_id_t password)
7423{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007424 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007425 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7426 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007427
Przemek Stekiel51eac532022-12-07 11:04:51 +01007428 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007429 status = PSA_ERROR_BAD_STATE;
7430 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007431 }
7432
Przemek Stekiel6c764412022-11-22 14:05:12 +01007433 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7434 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007435 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007436 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007437 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007438 }
7439
Przemek Stekiel6c764412022-11-22 14:05:12 +01007440 psa_key_attributes_t attributes = {
7441 .core = slot->attr
7442 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007443
Przemek Stekiel51eac532022-12-07 11:04:51 +01007444 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007445
Przemek Stekiel51eac532022-12-07 11:04:51 +01007446 if (type != PSA_KEY_TYPE_PASSWORD &&
7447 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7448 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007449 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007450 }
7451
Przemek Stekiel51eac532022-12-07 11:04:51 +01007452 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7453 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007454 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007455 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007456 }
7457
7458 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7459 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007460 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007461exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007462 if (status != PSA_SUCCESS) {
7463 psa_pake_abort(operation);
7464 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007465 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007466 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007467}
7468
7469psa_status_t psa_pake_set_user(
7470 psa_pake_operation_t *operation,
7471 const uint8_t *user_id,
7472 size_t user_id_len)
7473{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007474 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007475
7476 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007477 status = PSA_ERROR_BAD_STATE;
7478 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007479 }
7480
Przemek Stekiel51eac532022-12-07 11:04:51 +01007481 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007482 status = PSA_ERROR_INVALID_ARGUMENT;
7483 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007484 }
7485
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007486 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007487 status = PSA_ERROR_BAD_STATE;
7488 goto exit;
7489 }
7490
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007491 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007492 if ((user_id_len != sizeof(jpake_server_id) ||
7493 memcmp(user_id, jpake_server_id, user_id_len) != 0) &&
7494 (user_id_len != sizeof(jpake_client_id) ||
7495 memcmp(user_id, jpake_client_id, user_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007496 status = PSA_ERROR_NOT_SUPPORTED;
7497 goto exit;
7498 }
7499
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007500 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7501 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007502 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7503 goto exit;
7504 }
7505
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007506 memcpy(operation->data.inputs.user, user_id, user_id_len);
7507 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007508
7509 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007510exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007511 psa_pake_abort(operation);
7512 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007513}
7514
7515psa_status_t psa_pake_set_peer(
7516 psa_pake_operation_t *operation,
7517 const uint8_t *peer_id,
7518 size_t peer_id_len)
7519{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007520 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007521
7522 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007523 status = PSA_ERROR_BAD_STATE;
7524 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007525 }
7526
Przemek Stekiel51eac532022-12-07 11:04:51 +01007527 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007528 status = PSA_ERROR_INVALID_ARGUMENT;
7529 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007530 }
7531
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007532 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007533 status = PSA_ERROR_BAD_STATE;
7534 goto exit;
7535 }
7536
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007537 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007538 if ((peer_id_len != sizeof(jpake_server_id) ||
7539 memcmp(peer_id, jpake_server_id, peer_id_len) != 0) &&
7540 (peer_id_len != sizeof(jpake_client_id) ||
7541 memcmp(peer_id, jpake_client_id, peer_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007542 status = PSA_ERROR_NOT_SUPPORTED;
7543 goto exit;
7544 }
7545
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007546 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7547 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007548 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7549 goto exit;
7550 }
7551
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007552 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7553 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007554
7555 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007556exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007557 psa_pake_abort(operation);
7558 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007559}
7560
7561psa_status_t psa_pake_set_role(
7562 psa_pake_operation_t *operation,
7563 psa_pake_role_t role)
7564{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007565 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007566
Przemek Stekiel51eac532022-12-07 11:04:51 +01007567 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007568 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007569 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007570 }
7571
Przemek Stekiel09104b82023-03-06 14:11:51 +01007572 switch (operation->alg) {
7573#if defined(PSA_WANT_ALG_JPAKE)
7574 case PSA_ALG_JPAKE:
7575 if (role == PSA_PAKE_ROLE_NONE) {
7576 return PSA_SUCCESS;
7577 }
7578 status = PSA_ERROR_INVALID_ARGUMENT;
7579 break;
7580#endif
7581 default:
7582 (void) role;
7583 status = PSA_ERROR_NOT_SUPPORTED;
7584 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007585 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007586exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007587 psa_pake_abort(operation);
7588 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007589}
7590
Przemek Stekielb09c4872023-01-17 12:05:38 +01007591/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007592#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007593static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007594 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007595{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007596 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007597 case PSA_PAKE_OUTPUT_X1_X2:
7598 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007599 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007600 case PSA_PAKE_X1_STEP_KEY_SHARE:
7601 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007602 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7603 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007604 case PSA_PAKE_X1_STEP_ZK_PROOF:
7605 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007606 case PSA_PAKE_X2_STEP_KEY_SHARE:
7607 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007608 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7609 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007610 case PSA_PAKE_X2_STEP_ZK_PROOF:
7611 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007612 default:
7613 return PSA_JPAKE_STEP_INVALID;
7614 }
7615 break;
7616 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007617 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007618 case PSA_PAKE_X1_STEP_KEY_SHARE:
7619 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007620 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7621 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007622 case PSA_PAKE_X1_STEP_ZK_PROOF:
7623 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007624 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007625 return PSA_JPAKE_STEP_INVALID;
7626 }
7627 break;
7628 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007629 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007630 case PSA_PAKE_X1_STEP_KEY_SHARE:
7631 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007632 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7633 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007634 case PSA_PAKE_X1_STEP_ZK_PROOF:
7635 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007636 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007637 return PSA_JPAKE_STEP_INVALID;
7638 }
7639 break;
7640 default:
7641 return PSA_JPAKE_STEP_INVALID;
7642 }
7643 return PSA_JPAKE_STEP_INVALID;
7644}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007645#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007646
Przemek Stekiel2797d372022-12-22 11:19:22 +01007647static psa_status_t psa_pake_complete_inputs(
7648 psa_pake_operation_t *operation)
7649{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007650 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007651 /* Create copy of the inputs on stack as inputs share memory
7652 with the driver context which will be setup by the driver. */
7653 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007654
Przemek Stekielfde11282023-03-13 16:06:09 +01007655 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007656 return PSA_ERROR_BAD_STATE;
7657 }
7658
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007659 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007660 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7661 return PSA_ERROR_BAD_STATE;
7662 }
7663 if (memcmp(inputs.user, jpake_client_id, inputs.user_len) == 0 &&
7664 memcmp(inputs.peer, jpake_server_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007665 inputs.role = PSA_PAKE_ROLE_CLIENT;
7666 } else
Przemek Stekielfde11282023-03-13 16:06:09 +01007667 if (memcmp(inputs.user, jpake_server_id, inputs.user_len) == 0 &&
7668 memcmp(inputs.peer, jpake_client_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007669 inputs.role = PSA_PAKE_ROLE_SERVER;
7670 }
7671
7672 if (inputs.role != PSA_PAKE_ROLE_CLIENT &&
7673 inputs.role != PSA_PAKE_ROLE_SERVER) {
7674 return PSA_ERROR_NOT_SUPPORTED;
7675 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007676 }
7677
Przemek Stekiel18620a32023-01-17 16:34:52 +01007678 /* Clear driver context */
7679 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7680
7681 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007682
7683 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007684 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7685 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007686
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007687 /* User and peer are translated to role. */
7688 mbedtls_free(inputs.user);
7689 mbedtls_free(inputs.peer);
7690
Przemek Stekiel2797d372022-12-22 11:19:22 +01007691 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007692#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007693 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007694 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007695 psa_jpake_computation_stage_t *computation_stage =
7696 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007697 computation_stage->state = PSA_PAKE_STATE_READY;
7698 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7699 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7700 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007701 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007702#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007703 {
7704 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007705 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007706 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007707 return status;
7708}
7709
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007710#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007711static psa_status_t psa_jpake_output_prologue(
7712 psa_pake_operation_t *operation,
7713 psa_pake_step_t step)
7714{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007715 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7716 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7717 step != PSA_PAKE_STEP_ZK_PROOF) {
7718 return PSA_ERROR_INVALID_ARGUMENT;
7719 }
7720
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007721 psa_jpake_computation_stage_t *computation_stage =
7722 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007723
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007724 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7725 return PSA_ERROR_BAD_STATE;
7726 }
7727
7728 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7729 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
7730 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
7731 return PSA_ERROR_BAD_STATE;
7732 }
7733
7734 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7735 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007736 return PSA_ERROR_BAD_STATE;
7737 }
7738
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007739 switch (computation_stage->output_step) {
7740 case PSA_PAKE_STEP_X1_X2:
7741 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007742 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007743 case PSA_PAKE_STEP_X2S:
7744 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007745 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007746 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007747 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007748 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007749
7750 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7751 }
7752
7753 /* Check if step matches current sequence */
7754 switch (computation_stage->sequence) {
7755 case PSA_PAKE_X1_STEP_KEY_SHARE:
7756 case PSA_PAKE_X2_STEP_KEY_SHARE:
7757 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7758 return PSA_ERROR_BAD_STATE;
7759 }
7760 break;
7761
7762 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7763 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7764 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7765 return PSA_ERROR_BAD_STATE;
7766 }
7767 break;
7768
7769 case PSA_PAKE_X1_STEP_ZK_PROOF:
7770 case PSA_PAKE_X2_STEP_ZK_PROOF:
7771 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7772 return PSA_ERROR_BAD_STATE;
7773 }
7774 break;
7775
7776 default:
7777 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007778 }
7779
7780 return PSA_SUCCESS;
7781}
7782
7783static psa_status_t psa_jpake_output_epilogue(
7784 psa_pake_operation_t *operation)
7785{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007786 psa_jpake_computation_stage_t *computation_stage =
7787 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007788
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007789 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007790 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007791 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007792 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007793 computation_stage->state = PSA_PAKE_STATE_READY;
7794 computation_stage->output_step++;
7795 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7796 } else {
7797 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007798 }
7799
7800 return PSA_SUCCESS;
7801}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007802#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007803
Neil Armstronga7d08c32022-06-01 18:21:20 +02007804psa_status_t psa_pake_output(
7805 psa_pake_operation_t *operation,
7806 psa_pake_step_t step,
7807 uint8_t *output,
7808 size_t output_size,
7809 size_t *output_length)
7810{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007811 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007812 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007813 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007814
7815 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007816 status = psa_pake_complete_inputs(operation);
7817 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007818 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007819 }
7820 }
7821
7822 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007823 status = PSA_ERROR_BAD_STATE;
7824 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007825 }
7826
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007827 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007828 status = PSA_ERROR_INVALID_ARGUMENT;
7829 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007830 }
7831
Przemek Stekiele12ed362022-12-21 12:54:46 +01007832 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007833#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007834 case PSA_ALG_JPAKE:
7835 status = psa_jpake_output_prologue(operation, step);
7836 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007837 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007838 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007839 driver_step = convert_jpake_computation_stage_to_driver_step(
7840 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007841 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007842#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007843 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007844 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007845 status = PSA_ERROR_NOT_SUPPORTED;
7846 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007847 }
7848
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007849 status = psa_driver_wrapper_pake_output(operation, driver_step,
7850 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007851
7852 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007853 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007854 }
7855
7856 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007857#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007858 case PSA_ALG_JPAKE:
7859 status = psa_jpake_output_epilogue(operation);
7860 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007861 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007862 }
7863 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007864#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007865 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007866 status = PSA_ERROR_NOT_SUPPORTED;
7867 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007868 }
7869
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007870 return PSA_SUCCESS;
7871exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007872 psa_pake_abort(operation);
7873 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007874}
7875
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007876#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007877static psa_status_t psa_jpake_input_prologue(
7878 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007879 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007880{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007881 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7882 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7883 step != PSA_PAKE_STEP_ZK_PROOF) {
7884 return PSA_ERROR_INVALID_ARGUMENT;
7885 }
7886
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007887 psa_jpake_computation_stage_t *computation_stage =
7888 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007889
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007890 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7891 return PSA_ERROR_BAD_STATE;
7892 }
7893
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007894 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7895 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
7896 computation_stage->state != PSA_PAKE_INPUT_X4S) {
7897 return PSA_ERROR_BAD_STATE;
7898 }
7899
7900 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7901 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007902 return PSA_ERROR_BAD_STATE;
7903 }
7904
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007905 switch (computation_stage->input_step) {
7906 case PSA_PAKE_STEP_X1_X2:
7907 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007908 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007909 case PSA_PAKE_STEP_X2S:
7910 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007911 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007912 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007913 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007914 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007915
7916 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7917 }
7918
7919 /* Check if step matches current sequence */
7920 switch (computation_stage->sequence) {
7921 case PSA_PAKE_X1_STEP_KEY_SHARE:
7922 case PSA_PAKE_X2_STEP_KEY_SHARE:
7923 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7924 return PSA_ERROR_BAD_STATE;
7925 }
7926 break;
7927
7928 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7929 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7930 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7931 return PSA_ERROR_BAD_STATE;
7932 }
7933 break;
7934
7935 case PSA_PAKE_X1_STEP_ZK_PROOF:
7936 case PSA_PAKE_X2_STEP_ZK_PROOF:
7937 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7938 return PSA_ERROR_BAD_STATE;
7939 }
7940 break;
7941
7942 default:
7943 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007944 }
7945
7946 return PSA_SUCCESS;
7947}
7948
Przemek Stekiele12ed362022-12-21 12:54:46 +01007949static psa_status_t psa_jpake_input_epilogue(
7950 psa_pake_operation_t *operation)
7951{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007952 psa_jpake_computation_stage_t *computation_stage =
7953 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007954
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007955 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007956 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007957 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007958 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007959 computation_stage->state = PSA_PAKE_STATE_READY;
7960 computation_stage->input_step++;
7961 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7962 } else {
7963 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007964 }
7965
7966 return PSA_SUCCESS;
7967}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007968#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007969
Neil Armstronga7d08c32022-06-01 18:21:20 +02007970psa_status_t psa_pake_input(
7971 psa_pake_operation_t *operation,
7972 psa_pake_step_t step,
7973 const uint8_t *input,
7974 size_t input_length)
7975{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007976 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007977 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01007978 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
7979 operation->primitive,
7980 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007981
7982 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007983 status = psa_pake_complete_inputs(operation);
7984 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007985 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007986 }
7987 }
7988
7989 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007990 status = PSA_ERROR_BAD_STATE;
7991 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007992 }
7993
Przemek Stekiel256c75d2023-03-23 14:09:34 +01007994 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007995 status = PSA_ERROR_INVALID_ARGUMENT;
7996 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007997 }
7998
Przemek Stekiele12ed362022-12-21 12:54:46 +01007999 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008000#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008001 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008002 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008003 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008004 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008005 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008006 driver_step = convert_jpake_computation_stage_to_driver_step(
8007 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008008 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008009#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008010 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008011 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008012 status = PSA_ERROR_NOT_SUPPORTED;
8013 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008014 }
8015
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008016 status = psa_driver_wrapper_pake_input(operation, driver_step,
8017 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008018
8019 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008020 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008021 }
8022
8023 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008024#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008025 case PSA_ALG_JPAKE:
8026 status = psa_jpake_input_epilogue(operation);
8027 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008028 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008029 }
8030 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008031#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008032 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008033 status = PSA_ERROR_NOT_SUPPORTED;
8034 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008035 }
8036
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008037 return PSA_SUCCESS;
8038exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008039 psa_pake_abort(operation);
8040 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008041}
8042
8043psa_status_t psa_pake_get_implicit_key(
8044 psa_pake_operation_t *operation,
8045 psa_key_derivation_operation_t *output)
8046{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008047 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008048 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008049 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008050 size_t shared_key_len = 0;
8051
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008052 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008053 status = PSA_ERROR_BAD_STATE;
8054 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008055 }
8056
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008057#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008058 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008059 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008060 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008061 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
8062 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008063 status = PSA_ERROR_BAD_STATE;
8064 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008065 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008066 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008067#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008068 {
8069 status = PSA_ERROR_NOT_SUPPORTED;
8070 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008071 }
8072
Przemek Stekiel0c781802022-11-29 14:53:13 +01008073 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8074 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008075 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008076 &shared_key_len);
8077
8078 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008079 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008080 }
8081
8082 status = psa_key_derivation_input_bytes(output,
8083 PSA_KEY_DERIVATION_INPUT_SECRET,
8084 shared_key,
8085 shared_key_len);
8086
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008087 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008088exit:
8089 abort_status = psa_pake_abort(operation);
8090 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008091}
8092
8093psa_status_t psa_pake_abort(
8094 psa_pake_operation_t *operation)
8095{
Przemek Stekield69dca92023-01-31 19:59:20 +01008096 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008097
Przemek Stekield69dca92023-01-31 19:59:20 +01008098 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008099 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008100 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008101
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008102 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8103 if (operation->data.inputs.password != NULL) {
8104 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008105 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008106 mbedtls_free(operation->data.inputs.password);
8107 }
8108 if (operation->data.inputs.user != NULL) {
8109 mbedtls_free(operation->data.inputs.user);
8110 }
8111 if (operation->data.inputs.peer != NULL) {
8112 mbedtls_free(operation->data.inputs.peer);
8113 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008114 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008115 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008116
Przemek Stekield69dca92023-01-31 19:59:20 +01008117 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008118}
Neil Armstronga7d08c32022-06-01 18:21:20 +02008119
Gilles Peskinee59236f2018-01-27 23:32:46 +01008120#endif /* MBEDTLS_PSA_CRYPTO_C */