blob: 76632694d7cebc2344083181db8cbc5f33783c4f [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Ronald Cronafbc7ed2023-01-24 16:02:04 +010022#include "psa_crypto_core_common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010023
24#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030025
John Durkop07cc04a2020-11-16 22:08:34 -080026#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
27#include "check_crypto_config.h"
28#endif
29
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010031#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010032
Ronald Crond6d28882020-12-14 14:56:02 +010033#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010034#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010035#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020036#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010037#include "psa_crypto_ecp.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010038#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010039#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010040#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010041#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020042#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020043#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020044#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010045#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010046/* Include internal declarations that are useful for implementing persistently
47 * stored keys. */
48#include "psa_crypto_storage.h"
49
Gilles Peskineb2b64d32020-12-14 16:43:58 +010050#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010051
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010052#include <stdlib.h>
53#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010054#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010055
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010056#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020057#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000058#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020059#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020061#include "mbedtls/chacha20.h"
62#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/cipher.h"
64#include "mbedtls/ccm.h"
65#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020067#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010068#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010069#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/error.h"
71#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010072#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000074#include "md_wrap.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000076#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000078#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010080#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/sha1.h"
82#include "mbedtls/sha256.h"
83#include "mbedtls/sha512.h"
Paul Elliott588f8ed2022-12-02 18:10:26 +000084#include "hash_info.h"
Przemek Stekiel472b3f32022-12-01 14:38:49 +010085#include "mbedtls/dhm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086
Gilles Peskine449bd832023-01-11 14:50:10 +010087#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020088
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020089#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
90 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
91 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020092#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020093#endif
94
Przemek Stekielfde11282023-03-13 16:06:09 +010095/* The only two JPAKE user/peer identifiers supported for the time being. */
96static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
97static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
Przemek Stekiel18cd6c92023-03-06 15:40:35 +010098
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010099/****************************************************************/
100/* Global data, support functions and library management */
101/****************************************************************/
102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200104{
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200106}
107
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100108/* Values for psa_global_data_t::rng_state */
109#define RNG_NOT_INITIALIZED 0
110#define RNG_INITIALIZED 1
111#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100114 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100115 unsigned rng_state : 2;
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100116 unsigned drivers_initialized : 1;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100117 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118} psa_global_data_t;
119
120static psa_global_data_t global_data;
121
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100122#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
123mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
124 &global_data.rng.drbg;
125#endif
126
itayzafrir0adf0fc2018-09-06 16:24:41 +0300127#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 if (global_data.initialized == 0) \
129 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300130
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100131int psa_can_do_hash(psa_algorithm_t hash_alg)
132{
133 (void) hash_alg;
134 return global_data.drivers_initialized;
135}
136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100138{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100139 /* Mbed TLS error codes can combine a high-level error code and a
140 * low-level error code. The low-level error usually reflects the
141 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 int low_level_ret = -(-ret & 0x007f);
143 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100144 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100146
147 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
148 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200150 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
151 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
152 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
153 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
154 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200156 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200158 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200160
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000162 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000163#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100164 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100166
167 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100169 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100171
Gilles Peskine26869f22019-05-06 15:25:00 +0200172 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200174
175 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200177 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200179
Gilles Peskinea5905292018-02-07 20:59:33 +0100180 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100182 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100184 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100186 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100188 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100190 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100192 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
196 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100197 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
198 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100199 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100201 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
202 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100204 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100206#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100207
208 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100210
Gilles Peskinee59236f2018-01-27 23:32:46 +0100211 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
212 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
213 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100215
216 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200218 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100220 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100222
Gilles Peskine82e57d12020-11-13 21:31:17 +0100223#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100225 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
226 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100227 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100229 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
230 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100232 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100234#endif
235
Gilles Peskinea5905292018-02-07 20:59:33 +0100236 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100238 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100240 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100242 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100244
Gilles Peskinef76aa772018-10-29 19:24:33 +0100245 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100247 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100249 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100251 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100253 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100255 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100257 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100259 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100261
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100262 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100264 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
265 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100267 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100269 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
270 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100272 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100274 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
275 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100277 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100279 case MBEDTLS_ERR_PK_INVALID_ALG:
280 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
281 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100283 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200285 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100287
Gilles Peskineff2d2002019-05-06 15:26:23 +0200288 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200290 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200292
Gilles Peskinea5905292018-02-07 20:59:33 +0100293 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100295 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100297 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100299 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100301 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
302 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100308 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200310
itayzafrir5c753392018-05-08 11:18:38 +0300311 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300312 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300314 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300316 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300318 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
319 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300321 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100323 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100325
Paul Elliott588f8ed2022-12-02 18:10:26 +0000326 case MBEDTLS_ERR_ECP_IN_PROGRESS:
327 return PSA_OPERATION_INCOMPLETE;
328
Janos Follatha13b9052019-11-22 12:48:59 +0000329 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300331
Gilles Peskinee59236f2018-01-27 23:32:46 +0100332 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100334 }
335}
336
Paul Elliottdc42ca82023-02-24 18:11:59 +0000337/**
338 * \brief For output buffers which contain "tags"
339 * (outputs that may be checked for validity like
340 * hashes, MACs and signatures), fill the unused
341 * part of the output buffer (the whole buffer on
342 * error, the trailing part on success) with
343 * something that isn't a valid tag (barring an
344 * attack on the tag and deliberately-crafted
345 * input), in case the caller doesn't check the
346 * return status properly.
347 *
348 * \param output_buffer Pointer to buffer to wipe. May not be NULL
349 * unless \p output_buffer_size is zero.
350 * \param status Status of function called to generate
351 * output_buffer originally
352 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
353 * could be NULL.
354 * \param output_buffer_length Length of data written to output_buffer, must be
355 * less than \p output_buffer_size
356 */
357static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000358 size_t output_buffer_size, size_t output_buffer_length)
359{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000360 size_t offset = 0;
361
362 if (output_buffer_size == 0) {
363 /* If output_buffer_size is 0 then we have nothing to do. We must not
364 call memset because output_buffer may be NULL in this case */
365 return;
366 }
367
368 if (status == PSA_SUCCESS) {
369 offset = output_buffer_length;
370 }
371
372 memset(output_buffer + offset, '!', output_buffer_size - offset);
373}
374
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200375
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200376
377
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100378/****************************************************************/
379/* Key management */
380/****************************************************************/
381
Valerio Settid4a5d462023-04-05 18:19:01 +0200382#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100383mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
384 size_t bits,
385 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200386{
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100388 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100390#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100391 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100393#endif
394#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100395 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100397#endif
398#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100399 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100401#endif
402#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100403 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100405#endif
406#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100407 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100409 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 if (bits_is_sloppy) {
411 return MBEDTLS_ECP_DP_SECP521R1;
412 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100413 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100414#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100415 }
416 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100417
Paul Elliott8ff510a2020-06-02 17:19:28 +0100418 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100420#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100421 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100423#endif
424#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100425 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100427#endif
428#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100429 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100431#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100432 }
433 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100434
Paul Elliott8ff510a2020-06-02 17:19:28 +0100435 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100437#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100438 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100440 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 if (bits_is_sloppy) {
442 return MBEDTLS_ECP_DP_CURVE25519;
443 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100444 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100445#endif
446#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100447 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100449#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100450 }
451 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100452
Paul Elliott8ff510a2020-06-02 17:19:28 +0100453 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100455#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100456 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100458#endif
459#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100460 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100462#endif
463#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100464 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100466#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100467 }
468 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200469 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100470
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100471 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200473}
Valerio Settid4a5d462023-04-05 18:19:01 +0200474#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
477 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200478{
479 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200481 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200482 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200483 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200484 case PSA_KEY_TYPE_PASSWORD:
485 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200486 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100487#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200488 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (bits != 128 && bits != 192 && bits != 256) {
490 return PSA_ERROR_INVALID_ARGUMENT;
491 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200492 break;
493#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200494#if defined(PSA_WANT_KEY_TYPE_ARIA)
495 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 if (bits != 128 && bits != 192 && bits != 256) {
497 return PSA_ERROR_INVALID_ARGUMENT;
498 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200499 break;
500#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100501#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200502 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if (bits != 128 && bits != 192 && bits != 256) {
504 return PSA_ERROR_INVALID_ARGUMENT;
505 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200506 break;
507#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100508#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200509 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if (bits != 64 && bits != 128 && bits != 192) {
511 return PSA_ERROR_INVALID_ARGUMENT;
512 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200513 break;
514#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100515#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200516 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if (bits != 256) {
518 return PSA_ERROR_INVALID_ARGUMENT;
519 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200520 break;
521#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200522 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200524 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 if (bits % 8 != 0) {
526 return PSA_ERROR_INVALID_ARGUMENT;
527 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200530}
531
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100532/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100533 *
Steven Cooremancd640932021-03-08 12:00:27 +0100534 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
535 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100536 *
537 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100538 * \param[in] key_type The key type of the key to be used with the
539 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100540 *
541 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100542 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100543 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100544 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100545 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100546MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100547 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100549{
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 if (PSA_ALG_IS_HMAC(algorithm)) {
551 if (key_type == PSA_KEY_TYPE_HMAC) {
552 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100553 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100554 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
557 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
558 * key. */
559 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
560 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
561 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
562 * the block length (larger than 1) for block ciphers. */
563 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
564 return PSA_SUCCESS;
565 }
566 }
567 }
568
569 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100570}
571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
573 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200574{
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 if (slot->key.data != NULL) {
576 return PSA_ERROR_ALREADY_EXISTS;
577 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 slot->key.data = mbedtls_calloc(1, buffer_length);
580 if (slot->key.data == NULL) {
581 return PSA_ERROR_INSUFFICIENT_MEMORY;
582 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200583
Ronald Cronea0f8a62020-11-25 17:52:23 +0100584 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200586}
587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
589 const uint8_t *data,
590 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200591{
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 psa_status_t status = psa_allocate_buffer_to_slot(slot,
593 data_length);
594 if (status != PSA_SUCCESS) {
595 return status;
596 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 memcpy(slot->key.data, data, data_length);
599 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200600}
601
Ronald Crona0fe59f2020-11-29 11:14:53 +0100602psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100603 const psa_key_attributes_t *attributes,
604 const uint8_t *data, size_t data_length,
605 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100607{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100608 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
609 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100610
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200611 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 if (data_length == 0) {
613 return PSA_ERROR_NOT_SUPPORTED;
614 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 if (key_type_is_raw_bytes(type)) {
617 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
620 *bits);
621 if (status != PSA_SUCCESS) {
622 return status;
623 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200624
Ronald Crondd04d422020-11-28 15:14:42 +0100625 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100627 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 return PSA_SUCCESS;
631 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100632#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) || \
633 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY)
634 if (PSA_KEY_TYPE_IS_DH(type)) {
635 if (data_length != 256 && data_length != 384 &&
636 data_length != 512 && data_length != 768 &&
637 data_length != 1024) {
638 return PSA_ERROR_INVALID_ARGUMENT;
639 }
640
641 /* Copy the key material. */
642 memcpy(key_buffer, data, data_length);
643 *key_buffer_length = data_length;
644 *bits = PSA_BYTES_TO_BITS(data_length);
645 (void) key_buffer_size;
646
647 return PSA_SUCCESS;
648 }
649#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) ||
650 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY) */
John Durkop9814fa22020-11-04 12:28:15 -0800651#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
653 if (PSA_KEY_TYPE_IS_ECC(type)) {
654 return mbedtls_psa_ecp_import_key(attributes,
655 data, data_length,
656 key_buffer, key_buffer_size,
657 key_buffer_length,
658 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100659 }
John Durkop9814fa22020-11-04 12:28:15 -0800660#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
661 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700662#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
664 if (PSA_KEY_TYPE_IS_RSA(type)) {
665 return mbedtls_psa_rsa_import_key(attributes,
666 data, data_length,
667 key_buffer, key_buffer_size,
668 key_buffer_length,
669 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200670 }
John Durkop9814fa22020-11-04 12:28:15 -0800671#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
672 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100673 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100676}
677
Gilles Peskinef603c712019-01-19 13:40:11 +0100678/** Calculate the intersection of two algorithm usage policies.
679 *
680 * Return 0 (which allows no operation) on incompatibility.
681 */
682static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100683 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100684 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100686{
Gilles Peskine549ea862019-05-22 11:45:59 +0200687 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 if (alg1 == alg2) {
689 return alg1;
690 }
Steven Cooreman03488022021-02-18 12:07:20 +0100691 /* If the policies are from the same hash-and-sign family, check
692 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
694 PSA_ALG_IS_SIGN_HASH(alg2) &&
695 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
696 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
697 return alg2;
698 }
699 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
700 return alg1;
701 }
Steven Cooreman03488022021-02-18 12:07:20 +0100702 }
703 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100704 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100705 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
707 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
708 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
709 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
710 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100711 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100712
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100713 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
715 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
716 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
717 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100718 }
719 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
721 (alg1_len <= alg2_len)) {
722 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100723 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
725 (alg2_len <= alg1_len)) {
726 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100727 }
728 }
729 /* If the policies are from the same MAC family, check whether one
730 * of them is a minimum-MAC-length policy. Calculate the most
731 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
733 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
734 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100735 /* Validate the combination of key type and algorithm. Since the base
736 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
738 return 0;
739 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100740
Steven Cooreman31a876d2021-03-03 20:47:40 +0100741 /* Get the (exact or at-least) output lengths for both sides of the
742 * requested intersection. None of the currently supported algorithms
743 * have an output length dependent on the actual key size, so setting it
744 * to a bogus value of 0 is currently OK.
745 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100746 * Note that for at-least-this-length wildcard algorithms, the output
747 * length is set to the shortest allowed length, which allows us to
748 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
750 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100751 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100752
Steven Cooremana1d83222021-02-25 10:20:29 +0100753 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
755 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
756 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100757 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100758
759 /* If only one is an at-least-this-length policy, the intersection would
760 * be the other (fixed-length) policy as long as said fixed length is
761 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
763 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100764 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
766 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100767 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100768
Steven Cooremancd640932021-03-08 12:00:27 +0100769 /* If none of them are wildcards, check whether they define the same tag
770 * length. This is still possible here when one is default-length and
771 * the other specific-length. Ensure to always return the
772 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (alg1_len == alg2_len) {
774 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
775 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100776 }
777 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100779}
780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781static int psa_key_algorithm_permits(psa_key_type_t key_type,
782 psa_algorithm_t policy_alg,
783 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200784{
Gilles Peskine549ea862019-05-22 11:45:59 +0200785 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if (requested_alg == policy_alg) {
787 return 1;
788 }
Steven Cooreman03488022021-02-18 12:07:20 +0100789 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
790 * and requested_alg is the same hash-and-sign family with any hash,
791 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
793 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
794 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
795 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100796 }
797 /* If policy_alg is a wildcard AEAD algorithm of the same base as
798 * the requested algorithm, check the requested tag length to be
799 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if (PSA_ALG_IS_AEAD(policy_alg) &&
801 PSA_ALG_IS_AEAD(requested_alg) &&
802 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
803 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
804 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
805 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
806 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100807 }
Steven Cooremancd640932021-03-08 12:00:27 +0100808 /* If policy_alg is a MAC algorithm of the same base as the requested
809 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 if (PSA_ALG_IS_MAC(policy_alg) &&
811 PSA_ALG_IS_MAC(requested_alg) &&
812 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
813 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100814 /* Validate the combination of key type and algorithm. Since the policy
815 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
817 return 0;
818 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100819
Steven Cooreman31a876d2021-03-03 20:47:40 +0100820 /* Get both the requested output length for the algorithm which is to be
821 * verified, and the default output length for the base algorithm.
822 * Note that none of the currently supported algorithms have an output
823 * length dependent on actual key size, so setting it to a bogus value
824 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100825 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100827 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 key_type, 0,
829 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100830
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100831 /* If the policy is default-length, only allow an algorithm with
832 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
834 return requested_output_length == default_output_length;
835 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100836
837 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100838 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
840 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
841 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100842 }
843
Steven Cooremancd640932021-03-08 12:00:27 +0100844 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
845 * check for the requested MAC length to be equal to or longer than the
846 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
848 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
849 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100850 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200851 }
Steven Cooremance48e852020-10-05 16:02:45 +0200852 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200853 * a key derivation with that key agreement should also be allowed. This
854 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
856 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
857 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
858 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200859 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100860 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200862}
863
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100864/** Test whether a policy permits an algorithm.
865 *
866 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100867 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100868 * \note This function requires providing the key type for which the policy is
869 * being validated, since some algorithm policy definitions (e.g. MAC)
870 * have different properties depending on what kind of cipher it is
871 * combined with.
872 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100873 * \retval PSA_SUCCESS When \p alg is a specific algorithm
874 * allowed by the \p policy.
875 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
876 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
877 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100878 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100879static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
880 psa_key_type_t key_type,
881 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100882{
Steven Cooremand788fab2021-02-25 11:29:17 +0100883 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if (alg == 0) {
885 return PSA_ERROR_INVALID_ARGUMENT;
886 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100887
Steven Cooreman7e39f052021-02-23 14:18:32 +0100888 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if (PSA_ALG_IS_WILDCARD(alg)) {
890 return PSA_ERROR_INVALID_ARGUMENT;
891 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
894 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
895 return PSA_SUCCESS;
896 } else {
897 return PSA_ERROR_NOT_PERMITTED;
898 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100899}
900
Gilles Peskinef603c712019-01-19 13:40:11 +0100901/** Restrict a key policy based on a constraint.
902 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100903 * \note This function requires providing the key type for which the policy is
904 * being restricted, since some algorithm policy definitions (e.g. MAC)
905 * have different properties depending on what kind of cipher it is
906 * combined with.
907 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100908 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100909 * \param[in,out] policy The policy to restrict.
910 * \param[in] constraint The policy constraint to apply.
911 *
912 * \retval #PSA_SUCCESS
913 * \c *policy contains the intersection of the original value of
914 * \c *policy and \c *constraint.
915 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100916 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100917 * \c *policy is unchanged.
918 */
919static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100920 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100921 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100923{
924 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 psa_key_policy_algorithm_intersection(key_type, policy->alg,
926 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200927 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
929 constraint->alg2);
930 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
931 return PSA_ERROR_INVALID_ARGUMENT;
932 }
933 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
934 return PSA_ERROR_INVALID_ARGUMENT;
935 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100936 policy->usage &= constraint->usage;
937 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200938 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100940}
941
Przemek Stekiel061f6942022-11-30 10:51:35 +0100942/** Get the description of a key given its identifier and policy constraints
943 * and lock it.
944 *
945 * The key must have allow all the usage flags set in \p usage. If \p alg is
946 * nonzero, the key must allow operations with this algorithm. If \p alg is
947 * zero, the algorithm is not checked.
948 *
949 * In case of a persistent key, the function loads the description of the key
950 * into a key slot if not already done.
951 *
952 * On success, the returned key slot is locked. It is the responsibility of
953 * the caller to unlock the key slot when it does not access it anymore.
954 */
955static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100956 mbedtls_svc_key_id_t key,
957 psa_key_slot_t **p_slot,
958 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100960{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200961 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100962 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 status = psa_get_and_lock_key_slot(key, p_slot);
965 if (status != PSA_SUCCESS) {
966 return status;
967 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200968 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100969
970 /* Enforce that usage policy for the key slot contains all the flags
971 * required by the usage parameter. There is one exception: public
972 * keys can always be exported, so we treat public key objects as
973 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100975 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100979 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200980 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100981 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100982
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800983 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 if (alg != 0) {
985 status = psa_key_policy_permits(&slot->attr.policy,
986 slot->attr.type,
987 alg);
988 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100989 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100991 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100992
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200994
995error:
996 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001000}
Darryl Green940d72c2018-07-13 13:18:51 +01001001
Ronald Cron5c522922020-11-14 16:35:34 +01001002/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001003 *
1004 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001005 * available, as opposed to a key in a secure element and/or to be used
1006 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001007 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001008 * This is a temporary function that may be used instead of
1009 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1010 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001011 *
Ronald Cron5c522922020-11-14 16:35:34 +01001012 * On success, the returned key slot is locked. It is the responsibility of the
1013 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001014 */
Ronald Cron5c522922020-11-14 16:35:34 +01001015static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1016 mbedtls_svc_key_id_t key,
1017 psa_key_slot_t **p_slot,
1018 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001020{
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1022 usage, alg);
1023 if (status != PSA_SUCCESS) {
1024 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001025 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1028 psa_unlock_key_slot(*p_slot);
1029 *p_slot = NULL;
1030 return PSA_ERROR_NOT_SUPPORTED;
1031 }
1032
1033 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001034}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001035
Gilles Peskine449bd832023-01-11 14:50:10 +01001036psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001037{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001038 /* Data pointer will always be either a valid pointer or NULL in an
1039 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 if (slot->key.data != NULL) {
1041 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1042 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001043
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001045 slot->key.data = NULL;
1046 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001049}
1050
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001051/** Completely wipe a slot in memory, including its policy.
1052 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001053psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001054{
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 /*
1058 * As the return error code may not be handled in case of multiple errors,
1059 * do our best to report an unexpected lock counter. Assert with
1060 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1061 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1062 * function is called as part of the execution of a test suite, the
1063 * execution of the test suite is stopped in error if the assertion fails.
1064 */
1065 if (slot->lock_count != 1) {
1066 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001067 status = PSA_ERROR_CORRUPTION_DETECTED;
1068 }
1069
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001070 /* Multipart operations may still be using the key. This is safe
1071 * because all multipart operation objects are independent from
1072 * the key slot: if they need to access the key after the setup
1073 * phase, they have a copy of the key. Note that this means that
1074 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001075 /* At this point, key material and other type-specific content has
1076 * been wiped. Clear remaining metadata. We can call memset and not
1077 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 memset(slot, 0, sizeof(*slot));
1079 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001080}
1081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001083{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001084 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001085 psa_status_t status; /* status of the last operation */
1086 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001087#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1088 psa_se_drv_table_entry_t *driver;
1089#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (mbedtls_svc_key_id_is_null(key)) {
1092 return PSA_SUCCESS;
1093 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001094
Ronald Cronf2911112020-10-29 17:51:10 +01001095 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001096 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001097 * key, this will load the key description from persistent memory if not
1098 * done yet. We cannot avoid this loading as without it we don't know if
1099 * the key is operated by an SE or not and this information is needed by
1100 * the current implementation.
1101 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 status = psa_get_and_lock_key_slot(key, &slot);
1103 if (status != PSA_SUCCESS) {
1104 return status;
1105 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001106
Ronald Cronf2911112020-10-29 17:51:10 +01001107 /*
1108 * If the key slot containing the key description is under access by the
1109 * library (apart from the present access), the key cannot be destroyed
1110 * yet. For the time being, just return in error. Eventually (to be
1111 * implemented), the key should be destroyed when all accesses have
1112 * stopped.
1113 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 if (slot->lock_count > 1) {
1115 psa_unlock_key_slot(slot);
1116 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001117 }
1118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001120 /* Refuse the destruction of a read-only key (which may or may not work
1121 * if we attempt it, depending on whether the key is merely read-only
1122 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001123 * Just do the best we can, which is to wipe the copy in memory
1124 * (done in this function's cleanup code). */
1125 overall_status = PSA_ERROR_NOT_PERMITTED;
1126 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001127 }
1128
Gilles Peskine354f7672019-07-12 23:46:38 +02001129#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1131 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001132 /* For a key in a secure element, we need to do three things:
1133 * remove the key file in internal storage, destroy the
1134 * key inside the secure element, and update the driver's
1135 * persistent data. Start a transaction that will encompass these
1136 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001138 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001140 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 status = psa_crypto_save_transaction();
1142 if (status != PSA_SUCCESS) {
1143 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001144 /* We should still try to destroy the key in the secure
1145 * element and the key metadata in storage. This is especially
1146 * important if the error is that the storage is full.
1147 * But how to do it exactly without risking an inconsistent
1148 * state after a reset?
1149 * https://github.com/ARMmbed/mbed-crypto/issues/215
1150 */
1151 overall_status = status;
1152 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001153 }
1154
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 status = psa_destroy_se_key(driver,
1156 psa_key_slot_get_slot_number(slot));
1157 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001158 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001160 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001161#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1162
Darryl Greend49a4992018-06-18 17:27:26 +01001163#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1165 status = psa_destroy_persistent_key(slot->attr.id);
1166 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001167 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001169
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001170 /* TODO: other slots may have a copy of the same key. We should
1171 * invalidate them.
1172 * https://github.com/ARMmbed/mbed-crypto/issues/214
1173 */
Darryl Greend49a4992018-06-18 17:27:26 +01001174 }
1175#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001176
Gilles Peskinefc762652019-07-22 19:30:34 +02001177#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 if (driver != NULL) {
1179 status = psa_save_se_persistent_data(driver);
1180 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001181 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 }
1183 status = psa_crypto_stop_transaction();
1184 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001185 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001187 }
1188#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1189
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001190exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001192 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001194 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 }
1196 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001197}
1198
John Durkop07cc04a2020-11-16 22:08:34 -08001199#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001200 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001201static psa_status_t psa_get_rsa_public_exponent(
1202 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001204{
1205 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001207 uint8_t *buffer = NULL;
1208 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001210
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1212 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001213 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 }
1215 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001216 /* It's the default value, which is reported as an empty string,
1217 * so there's nothing to do. */
1218 goto exit;
1219 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 buflen = mbedtls_mpi_size(&mpi);
1222 buffer = mbedtls_calloc(1, buflen);
1223 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001224 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1225 goto exit;
1226 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1228 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001229 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001231 attributes->domain_parameters = buffer;
1232 attributes->domain_parameters_size = buflen;
1233
1234exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 mbedtls_mpi_free(&mpi);
1236 if (ret != 0) {
1237 mbedtls_free(buffer);
1238 }
1239 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001240}
John Durkop07cc04a2020-11-16 22:08:34 -08001241#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001242 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001243
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001244/** Retrieve all the publicly-accessible attributes of a key.
1245 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001246psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1247 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001248{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001249 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001250 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001251 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1256 if (status != PSA_SUCCESS) {
1257 return status;
1258 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001259
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001260 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1262 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001263
1264#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1266 psa_set_key_slot_number(attributes,
1267 psa_key_slot_get_slot_number(slot));
1268 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001269#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001272#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001273 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001274 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001275 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001276 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001277 * is not yet implemented.
1278 * https://github.com/ARMmbed/mbed-crypto/issues/216
1279 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001281 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001282
Ronald Cron90857082020-11-25 14:58:33 +01001283 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 slot->attr.type,
1285 slot->key.data,
1286 slot->key.bytes,
1287 &rsa);
1288 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001289 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 status = psa_get_rsa_public_exponent(rsa,
1293 attributes);
1294 mbedtls_rsa_free(rsa);
1295 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001296 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001297 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001298#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001299 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001300 default:
1301 /* Nothing else to do. */
1302 break;
1303 }
1304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 if (status != PSA_SUCCESS) {
1306 psa_reset_key_attributes(attributes);
1307 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001312}
1313
Gilles Peskinec8000c02019-08-02 20:15:51 +02001314#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1315psa_status_t psa_get_key_slot_number(
1316 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001318{
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001320 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 return PSA_SUCCESS;
1322 } else {
1323 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001324 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001325}
1326#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1329 size_t key_buffer_size,
1330 uint8_t *data,
1331 size_t data_size,
1332 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001333{
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 if (key_buffer_size > data_size) {
1335 return PSA_ERROR_BUFFER_TOO_SMALL;
1336 }
1337 memcpy(data, key_buffer, key_buffer_size);
1338 memset(data + key_buffer_size, 0,
1339 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001340 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001342}
1343
Ronald Cron7285cda2020-11-26 14:46:37 +01001344psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001345 const psa_key_attributes_t *attributes,
1346 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001348{
Ronald Crond18b5f82020-11-25 16:46:05 +01001349 psa_key_type_t type = attributes->core.type;
1350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 if (key_type_is_raw_bytes(type) ||
1352 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001353 PSA_KEY_TYPE_IS_ECC(type) ||
1354 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 return psa_export_key_buffer_internal(
1356 key_buffer, key_buffer_size,
1357 data, data_size, data_length);
1358 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001359 /* This shouldn't happen in the reference implementation, but
1360 it is valid for a special-purpose implementation to omit
1361 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001363 }
1364}
1365
Gilles Peskine449bd832023-01-11 14:50:10 +01001366psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1367 uint8_t *data,
1368 size_t data_size,
1369 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001370{
1371 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1372 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1373 psa_key_slot_t *slot;
1374
Ronald Crone907e552021-01-18 13:22:38 +01001375 /* Reject a zero-length output buffer now, since this can never be a
1376 * valid key representation. This way we know that data must be a valid
1377 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 if (data_size == 0) {
1379 return PSA_ERROR_BUFFER_TOO_SMALL;
1380 }
Ronald Crone907e552021-01-18 13:22:38 +01001381
Ronald Cron9486f9d2020-11-26 13:36:23 +01001382 /* Set the key to empty now, so that even when there are errors, we always
1383 * set data_length to a value between 0 and data_size. On error, setting
1384 * the key to empty is a good choice because an empty key representation is
1385 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001386 *data_length = 0;
1387
Ronald Cron9486f9d2020-11-26 13:36:23 +01001388 /* Export requires the EXPORT flag. There is an exception for public keys,
1389 * which don't require any flag, but
1390 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1391 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1393 PSA_KEY_USAGE_EXPORT, 0);
1394 if (status != PSA_SUCCESS) {
1395 return status;
1396 }
mohammad160306e79202018-03-28 13:17:44 +03001397
Ronald Crond18b5f82020-11-25 16:46:05 +01001398 psa_key_attributes_t attributes = {
1399 .core = slot->attr
1400 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 status = psa_driver_wrapper_export_key(&attributes,
1402 slot->key.data, slot->key.bytes,
1403 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001408}
1409
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001410#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) || \
1411 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY)
1412static psa_status_t psa_ffdh_set_prime_generator(size_t key_size,
1413 mbedtls_mpi *P,
1414 mbedtls_mpi *G)
1415{
1416 const unsigned char *dhm_P = NULL;
1417 const unsigned char *dhm_G = NULL;
1418 size_t dhm_size_P = 0;
1419 size_t dhm_size_G = 0;
1420 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1421
1422 if (P == NULL && G == NULL) {
1423 return PSA_ERROR_INVALID_ARGUMENT;
1424 }
1425
1426 static const unsigned char dhm_P_2048[] =
1427 MBEDTLS_DHM_RFC7919_FFDHE2048_P_BIN;
1428 static const unsigned char dhm_P_3072[] =
1429 MBEDTLS_DHM_RFC7919_FFDHE3072_P_BIN;
1430 static const unsigned char dhm_P_4096[] =
1431 MBEDTLS_DHM_RFC7919_FFDHE4096_P_BIN;
1432 static const unsigned char dhm_P_6144[] =
1433 MBEDTLS_DHM_RFC7919_FFDHE6144_P_BIN;
1434 static const unsigned char dhm_P_8192[] =
1435 MBEDTLS_DHM_RFC7919_FFDHE8192_P_BIN;
1436 static const unsigned char dhm_G_2048[] =
1437 MBEDTLS_DHM_RFC7919_FFDHE2048_G_BIN;
1438 static const unsigned char dhm_G_3072[] =
1439 MBEDTLS_DHM_RFC7919_FFDHE3072_G_BIN;
1440 static const unsigned char dhm_G_4096[] =
1441 MBEDTLS_DHM_RFC7919_FFDHE4096_G_BIN;
1442 static const unsigned char dhm_G_6144[] =
1443 MBEDTLS_DHM_RFC7919_FFDHE6144_G_BIN;
1444 static const unsigned char dhm_G_8192[] =
1445 MBEDTLS_DHM_RFC7919_FFDHE8192_G_BIN;
1446
1447 if (key_size <= 256) {
1448 dhm_P = dhm_P_2048;
1449 dhm_G = dhm_G_2048;
1450 dhm_size_P = sizeof(dhm_P_2048);
1451 dhm_size_G = sizeof(dhm_G_2048);
1452 } else if (key_size <= 384) {
1453 dhm_P = dhm_P_3072;
1454 dhm_G = dhm_G_3072;
1455 dhm_size_P = sizeof(dhm_P_3072);
1456 dhm_size_G = sizeof(dhm_G_3072);
1457 } else if (key_size <= 512) {
1458 dhm_P = dhm_P_4096;
1459 dhm_G = dhm_G_4096;
1460 dhm_size_P = sizeof(dhm_P_4096);
1461 dhm_size_G = sizeof(dhm_G_4096);
1462 } else if (key_size <= 768) {
1463 dhm_P = dhm_P_6144;
1464 dhm_G = dhm_G_6144;
1465 dhm_size_P = sizeof(dhm_P_6144);
1466 dhm_size_G = sizeof(dhm_G_6144);
1467 } else if (key_size <= 1024) {
1468 dhm_P = dhm_P_8192;
1469 dhm_G = dhm_G_8192;
1470 dhm_size_P = sizeof(dhm_P_8192);
1471 dhm_size_G = sizeof(dhm_G_8192);
1472 } else {
1473 return PSA_ERROR_INVALID_ARGUMENT;
1474 }
1475
1476 if (P != NULL) {
1477 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(P, dhm_P,
1478 dhm_size_P));
1479 }
1480 if (G != NULL) {
1481 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(G, dhm_G,
1482 dhm_size_G));
1483 }
1484
1485cleanup:
1486 if (ret != 0) {
1487 return mbedtls_to_psa_error(ret);
1488 }
1489
1490 return PSA_SUCCESS;
1491}
1492
Przemek Stekielfb3dd542022-12-01 14:59:15 +01001493#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
1494static psa_status_t psa_key_agreement_ffdh(const uint8_t *peer_key,
1495 size_t peer_key_length,
1496 const uint8_t *our_key,
1497 size_t our_key_length,
1498 uint8_t *shared_secret,
1499 size_t shared_secret_size,
1500 size_t *shared_secret_length)
1501{
1502 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1503 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1504 mbedtls_mpi P, G, X, GY, K;
1505 const size_t calculated_shared_secret_size = peer_key_length;
1506
1507 if (peer_key_length != our_key_length ||
1508 calculated_shared_secret_size > shared_secret_size) {
1509 return PSA_ERROR_INVALID_ARGUMENT;
1510 }
1511
1512 mbedtls_mpi_init(&P); mbedtls_mpi_init(&G);
1513 mbedtls_mpi_init(&X); mbedtls_mpi_init(&GY);
1514 mbedtls_mpi_init(&K);
1515
1516 status = psa_ffdh_set_prime_generator(peer_key_length, &P, &G);
1517
1518 if (status == PSA_SUCCESS) {
1519 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&X, our_key,
1520 our_key_length));
1521
1522 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&GY, peer_key,
1523 peer_key_length));
1524
1525 /* Calculate shared secret public key: K = G^(XY) mod P */
1526 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&K, &GY, &X, &P, NULL));
1527
1528 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&K, shared_secret,
1529 calculated_shared_secret_size));
1530
1531 *shared_secret_length = calculated_shared_secret_size;
1532 }
1533cleanup:
1534 mbedtls_mpi_free(&P); mbedtls_mpi_free(&G);
1535 mbedtls_mpi_free(&X); mbedtls_mpi_free(&GY);
1536 mbedtls_mpi_free(&K);
1537
1538 if (status == PSA_SUCCESS && ret != 0) {
1539 return mbedtls_to_psa_error(ret);
1540 }
1541
1542 return PSA_SUCCESS;
1543}
1544#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
1545
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001546static psa_status_t psa_export_ffdh_public_key_internal(
1547 const uint8_t *key_buffer,
1548 size_t key_buffer_size,
1549 uint8_t *data,
1550 size_t data_size,
1551 size_t *data_length)
1552{
1553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1554 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1555 mbedtls_mpi GX, G, X, P;
1556
1557 mbedtls_mpi_init(&GX); mbedtls_mpi_init(&G);
1558 mbedtls_mpi_init(&X); mbedtls_mpi_init(&P);
1559
1560 status = psa_ffdh_set_prime_generator(key_buffer_size, &P, &G);
1561
1562 if (status == PSA_SUCCESS) {
1563 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&X, key_buffer,
1564 key_buffer_size));
1565
1566 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&GX, &G, &X, &P, NULL));
1567 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&GX, data, data_size));
1568
1569 *data_length = mbedtls_mpi_size(&GX);
1570 }
1571cleanup:
1572 mbedtls_mpi_free(&P); mbedtls_mpi_free(&G);
1573 mbedtls_mpi_free(&X); mbedtls_mpi_free(&GX);
1574
1575 if (status == PSA_SUCCESS && ret != 0) {
1576 return mbedtls_to_psa_error(ret);
1577 }
1578
1579 return status;
1580}
1581
1582#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR ||
1583 MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY */
1584
Ronald Cron7285cda2020-11-26 14:46:37 +01001585psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001586 const psa_key_attributes_t *attributes,
1587 const uint8_t *key_buffer,
1588 size_t key_buffer_size,
1589 uint8_t *data,
1590 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001592{
Ronald Crond18b5f82020-11-25 16:46:05 +01001593 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001594
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001595 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1596 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001598 /* Exporting public -> public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 return psa_export_key_buffer_internal(
1600 key_buffer, key_buffer_size,
1601 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001602 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001603
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001605#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1607 return mbedtls_psa_rsa_export_public_key(attributes,
1608 key_buffer,
1609 key_buffer_size,
1610 data,
1611 data_size,
1612 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001613#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001614 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001616#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1617 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001618 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
John Durkop6ba40d12020-11-10 08:50:04 -08001619#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1621 return mbedtls_psa_ecp_export_public_key(attributes,
1622 key_buffer,
1623 key_buffer_size,
1624 data,
1625 data_size,
1626 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001627#else
1628 /* We don't know how to convert a private ECC key to public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001630#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1631 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001632 }
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001633#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) || \
1634 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY)
1635 else {
1636 return psa_export_ffdh_public_key_internal(key_buffer,
1637 key_buffer_size,
1638 data, data_size,
1639 data_length);
1640 }
1641#else
1642 /* We don't know how to convert a private FFDH key to public */
1643 return PSA_ERROR_NOT_SUPPORTED;
1644#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) ||
1645 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001647 /* This shouldn't happen in the reference implementation, but
1648 it is valid for a special-purpose implementation to omit
1649 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001651 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001652}
Ronald Crond18b5f82020-11-25 16:46:05 +01001653
Gilles Peskine449bd832023-01-11 14:50:10 +01001654psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1655 uint8_t *data,
1656 size_t data_size,
1657 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001658{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001659 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001660 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001661 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001662
Ronald Crone907e552021-01-18 13:22:38 +01001663 /* Reject a zero-length output buffer now, since this can never be a
1664 * valid key representation. This way we know that data must be a valid
1665 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 if (data_size == 0) {
1667 return PSA_ERROR_BUFFER_TOO_SMALL;
1668 }
Ronald Crone907e552021-01-18 13:22:38 +01001669
Darryl Greendd8fb772018-11-07 16:00:44 +00001670 /* Set the key to empty now, so that even when there are errors, we always
1671 * set data_length to a value between 0 and data_size. On error, setting
1672 * the key to empty is a good choice because an empty key representation is
1673 * unlikely to be accepted anywhere. */
1674 *data_length = 0;
1675
1676 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1678 if (status != PSA_SUCCESS) {
1679 return status;
1680 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001681
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1683 status = PSA_ERROR_INVALID_ARGUMENT;
1684 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001685 }
1686
Ronald Crond18b5f82020-11-25 16:46:05 +01001687 psa_key_attributes_t attributes = {
1688 .core = slot->attr
1689 };
Ronald Cron67227982020-11-26 15:16:05 +01001690 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001691 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001693
1694exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001696
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001698}
1699
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001700MBEDTLS_STATIC_ASSERT(
1701 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1702 "One or more key attribute flag is listed as both external-only and dual-use")
1703MBEDTLS_STATIC_ASSERT(
1704 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1705 "One or more key attribute flag is listed as both internal-only and dual-use")
1706MBEDTLS_STATIC_ASSERT(
1707 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1708 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001709
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001710/** Validate that a key policy is internally well-formed.
1711 *
1712 * This function only rejects invalid policies. It does not validate the
1713 * consistency of the policy with respect to other attributes of the key
1714 * such as the key type.
1715 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001716static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001717{
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1719 PSA_KEY_USAGE_COPY |
1720 PSA_KEY_USAGE_ENCRYPT |
1721 PSA_KEY_USAGE_DECRYPT |
1722 PSA_KEY_USAGE_SIGN_MESSAGE |
1723 PSA_KEY_USAGE_VERIFY_MESSAGE |
1724 PSA_KEY_USAGE_SIGN_HASH |
1725 PSA_KEY_USAGE_VERIFY_HASH |
1726 PSA_KEY_USAGE_VERIFY_DERIVATION |
1727 PSA_KEY_USAGE_DERIVE)) != 0) {
1728 return PSA_ERROR_INVALID_ARGUMENT;
1729 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001730
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001732}
1733
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001734/** Validate the internal consistency of key attributes.
1735 *
1736 * This function only rejects invalid attribute values. If does not
1737 * validate the consistency of the attributes with any key data that may
1738 * be involved in the creation of the key.
1739 *
1740 * Call this function early in the key creation process.
1741 *
1742 * \param[in] attributes Key attributes for the new key.
1743 * \param[out] p_drv On any return, the driver for the key, if any.
1744 * NULL for a transparent key.
1745 *
1746 */
1747static psa_status_t psa_validate_key_attributes(
1748 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001750{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001751 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1753 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001754
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 status = psa_validate_key_location(lifetime, p_drv);
1756 if (status != PSA_SUCCESS) {
1757 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001758 }
1759
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 status = psa_validate_key_persistence(lifetime);
1761 if (status != PSA_SUCCESS) {
1762 return status;
1763 }
1764
1765 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1766 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1767 return PSA_ERROR_INVALID_ARGUMENT;
1768 }
1769 } else {
1770 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1771 return PSA_ERROR_INVALID_ARGUMENT;
1772 }
1773 }
1774
1775 status = psa_validate_key_policy(&attributes->core.policy);
1776 if (status != PSA_SUCCESS) {
1777 return status;
1778 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001779
1780 /* Refuse to create overly large keys.
1781 * Note that this doesn't trigger on import if the attributes don't
1782 * explicitly specify a size (so psa_get_key_bits returns 0), so
1783 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1785 return PSA_ERROR_NOT_SUPPORTED;
1786 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001787
Gilles Peskine91e8c332019-08-02 19:19:39 +02001788 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1790 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1791 return PSA_ERROR_INVALID_ARGUMENT;
1792 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001793
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001795}
1796
Gilles Peskine4747d192019-04-17 15:05:45 +02001797/** Prepare a key slot to receive key material.
1798 *
1799 * This function allocates a key slot and sets its metadata.
1800 *
1801 * If this function fails, call psa_fail_key_creation().
1802 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001803 * This function is intended to be used as follows:
1804 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001805 * it with the specified attributes, and in case of a volatile key assign it
1806 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001807 * -# Populate the slot with the key material.
1808 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1809 * In case of failure at any step, stop the sequence and call
1810 * psa_fail_key_creation().
1811 *
Ronald Cron5c522922020-11-14 16:35:34 +01001812 * On success, the key slot is locked. It is the responsibility of the caller
1813 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001814 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001815 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001816 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001817 * \param[out] p_slot On success, a pointer to the prepared slot.
1818 * \param[out] p_drv On any return, the driver for the key, if any.
1819 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001820 *
1821 * \retval #PSA_SUCCESS
1822 * The key slot is ready to receive key material.
1823 * \return If this function fails, the key slot is an invalid state.
1824 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001825 */
1826static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001827 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001828 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001829 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001831{
1832 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001833 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001834 psa_key_slot_t *slot;
1835
Gilles Peskinedf179142019-07-15 22:02:14 +02001836 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001837 *p_drv = NULL;
1838
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 status = psa_validate_key_attributes(attributes, p_drv);
1840 if (status != PSA_SUCCESS) {
1841 return status;
1842 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1845 if (status != PSA_SUCCESS) {
1846 return status;
1847 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001848 slot = *p_slot;
1849
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001850 /* We're storing the declared bit-size of the key. It's up to each
1851 * creation mechanism to verify that this information is correct.
1852 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001853 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001854 * is optional (import, copy). In case of a volatile key, assign it the
1855 * volatile key identifier associated to the slot returned to contain its
1856 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001857
1858 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001860#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1861 slot->attr.id = volatile_key_id;
1862#else
1863 slot->attr.id.key_id = volatile_key_id;
1864#endif
1865 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001866
Gilles Peskine91e8c332019-08-02 19:19:39 +02001867 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001868 * external-only flags, query `attributes`. Thanks to the check
1869 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001870 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001871 * may have set. */
1872 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001873
Gilles Peskinecbaff462019-07-12 23:46:04 +02001874#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001875 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001876 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001877 * create the key file in internal storage, create the
1878 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001879 * persistent data. This is done by starting a transaction that will
1880 * encompass these three actions.
1881 * For registering a volatile key, we just need to find an appropriate
1882 * slot number inside the SE. Since the key is designated volatile, creating
1883 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001884 /* The first thing to do is to find a slot number for the new key.
1885 * We save the slot number in persistent storage as part of the
1886 * transaction data. It will be needed to recover if the power
1887 * fails during the key creation process, to clean up on the secure
1888 * element side after restarting. Obtaining a slot number from the
1889 * secure element driver updates its persistent state, but we do not yet
1890 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001891 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001893 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1895 &slot_number);
1896 if (status != PSA_SUCCESS) {
1897 return status;
1898 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001899
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1901 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001902 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001903 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001904 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 status = psa_crypto_save_transaction();
1906 if (status != PSA_SUCCESS) {
1907 (void) psa_crypto_stop_transaction();
1908 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001909 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001910 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001911
1912 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001914 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001915
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001917 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001919 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001920#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1921
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001923}
Gilles Peskine4747d192019-04-17 15:05:45 +02001924
1925/** Finalize the creation of a key once its key material has been set.
1926 *
1927 * This entails writing the key to persistent storage.
1928 *
1929 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001930 * See the documentation of psa_start_key_creation() for the intended use
1931 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001932 *
Ronald Cron5c522922020-11-14 16:35:34 +01001933 * If the finalization succeeds, the function unlocks the key slot (it was
1934 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1935 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001936 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001937 * \param[in,out] slot Pointer to the slot with key material.
1938 * \param[in] driver The secure element driver for the key,
1939 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001940 * \param[out] key On success, identifier of the key. Note that the
1941 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001942 *
1943 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001944 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001945 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1946 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1947 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1948 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1949 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1950 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001951 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001952 * \return If this function fails, the key slot is an invalid state.
1953 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001954 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001955static psa_status_t psa_finish_key_creation(
1956 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001957 psa_se_drv_table_entry_t *driver,
1958 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001959{
1960 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001961 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001962 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001963
1964#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001966#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001968 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001969 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001971
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001972 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1973 sizeof(data.slot_number),
1974 "Slot number size does not match psa_se_key_data_storage_t");
1975
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1977 status = psa_save_persistent_key(&slot->attr,
1978 (uint8_t *) &data,
1979 sizeof(data));
1980 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001981#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1982 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001983 /* Key material is saved in export representation in the slot, so
1984 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 status = psa_save_persistent_key(&slot->attr,
1986 slot->key.data,
1987 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001988 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001989 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001990#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1991
Gilles Peskinecbaff462019-07-12 23:46:04 +02001992#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001993 /* Finish the transaction for a key creation. This does not
1994 * happen when registering an existing key. Detect this case
1995 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001996 * creation of a persistent key in a secure element requires a transaction,
1997 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 if (driver != NULL &&
1999 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
2000 status = psa_save_se_persistent_data(driver);
2001 if (status != PSA_SUCCESS) {
2002 psa_destroy_persistent_key(slot->attr.id);
2003 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02002004 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02002006 }
2007#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2008
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01002010 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 status = psa_unlock_key_slot(slot);
2012 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01002013 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 }
Ronald Cron81709fc2020-11-14 12:10:32 +01002015 }
Ronald Cron50972942020-11-14 11:28:25 +01002016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002018}
2019
2020/** Abort the creation of a key.
2021 *
2022 * You may call this function after calling psa_start_key_creation(),
2023 * or after psa_finish_key_creation() fails. In other circumstances, this
2024 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002025 * See the documentation of psa_start_key_creation() for the intended use
2026 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002027 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002028 * \param[in,out] slot Pointer to the slot with key material.
2029 * \param[in] driver The secure element driver for the key,
2030 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002031 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002032static void psa_fail_key_creation(psa_key_slot_t *slot,
2033 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02002034{
Gilles Peskine011e4282019-06-26 18:34:38 +02002035 (void) driver;
2036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02002038 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 }
Gilles Peskine011e4282019-06-26 18:34:38 +02002040
2041#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002042 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002043 * element, and the failure happened later (when saving metadata
2044 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002045 * element.
2046 * https://github.com/ARMmbed/mbed-crypto/issues/217
2047 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002048
Gilles Peskined7729582019-08-05 15:55:54 +02002049 /* Abort the ongoing transaction if any (there may not be one if
2050 * the creation process failed before starting one, or if the
2051 * key creation is a registration of a key in a secure element).
2052 * Earlier functions must already have done what it takes to undo any
2053 * partial creation. All that's left is to update the transaction data
2054 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02002056#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2057
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02002059}
2060
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002061/** Validate optional attributes during key creation.
2062 *
2063 * Some key attributes are optional during key creation. If they are
2064 * specified in the attributes structure, check that they are consistent
2065 * with the data in the slot.
2066 *
2067 * This function should be called near the end of key creation, after
2068 * the slot in memory is fully populated but before saving persistent data.
2069 */
2070static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002071 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002073{
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 if (attributes->core.type != 0) {
2075 if (attributes->core.type != slot->attr.type) {
2076 return PSA_ERROR_INVALID_ARGUMENT;
2077 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002078 }
2079
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07002081#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2083 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002084 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002085 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002086 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002087
Ronald Cron90857082020-11-25 14:58:33 +01002088 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 slot->attr.type,
2090 slot->key.data,
2091 slot->key.bytes,
2092 &rsa);
2093 if (status != PSA_SUCCESS) {
2094 return status;
2095 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002096
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 mbedtls_mpi_init(&actual);
2098 mbedtls_mpi_init(&required);
2099 ret = mbedtls_rsa_export(rsa,
2100 NULL, NULL, NULL, NULL, &actual);
2101 mbedtls_rsa_free(rsa);
2102 mbedtls_free(rsa);
2103 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002104 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 }
2106 ret = mbedtls_mpi_read_binary(&required,
2107 attributes->domain_parameters,
2108 attributes->domain_parameters_size);
2109 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002110 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 }
2112 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002113 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 }
2115rsa_exit:
2116 mbedtls_mpi_free(&actual);
2117 mbedtls_mpi_free(&required);
2118 if (ret != 0) {
2119 return mbedtls_to_psa_error(ret);
2120 }
2121 } else
John Durkop9814fa22020-11-04 12:28:15 -08002122#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2123 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002124 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002126 }
2127 }
2128
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 if (attributes->core.bits != 0) {
2130 if (attributes->core.bits != slot->attr.bits) {
2131 return PSA_ERROR_INVALID_ARGUMENT;
2132 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002133 }
2134
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002136}
2137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2139 const uint8_t *data,
2140 size_t data_length,
2141 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002142{
2143 psa_status_t status;
2144 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002145 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002146 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302147 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002148
Ronald Cron81709fc2020-11-14 12:10:32 +01002149 *key = MBEDTLS_SVC_KEY_ID_INIT;
2150
Gilles Peskine0f84d622019-09-12 19:03:13 +02002151 /* Reject zero-length symmetric keys (including raw data key objects).
2152 * This also rejects any key which might be encoded as an empty string,
2153 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 if (data_length == 0) {
2155 return PSA_ERROR_INVALID_ARGUMENT;
2156 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002157
Archana449608b2021-09-08 15:36:05 +05302158 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 if (data_length > SIZE_MAX / 8) {
2160 return PSA_ERROR_NOT_SUPPORTED;
2161 }
Archana6ed4bda2021-08-04 10:47:15 +05302162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2164 &slot, &driver);
2165 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002166 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002168
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002169 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302170 * storage ( thus not in the case of importing a key in a secure element
2171 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2172 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 if (slot->key.data == NULL) {
2174 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302175 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 attributes, data, data_length, &storage_size);
2177 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302178 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 }
Archanad8a83dc2021-06-14 10:04:16 +05302180 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 status = psa_allocate_buffer_to_slot(slot, storage_size);
2182 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002183 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002185 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002186
2187 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 status = psa_driver_wrapper_import_key(attributes,
2189 data, data_length,
2190 slot->key.data,
2191 slot->key.bytes,
2192 &slot->key.bytes, &bits);
2193 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002194 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002196
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002198 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002200 status = PSA_ERROR_INVALID_ARGUMENT;
2201 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002202 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002203
Archana6ed4bda2021-08-04 10:47:15 +05302204 /* Enforce a size limit, and in particular ensure that the bit
2205 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302207 status = PSA_ERROR_NOT_SUPPORTED;
2208 goto exit;
2209 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 status = psa_validate_optional_attributes(slot, attributes);
2211 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002212 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002214
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002216exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 if (status != PSA_SUCCESS) {
2218 psa_fail_key_creation(slot, driver);
2219 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002222}
2223
Gilles Peskined7729582019-08-05 15:55:54 +02002224#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2225psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002227{
2228 psa_status_t status;
2229 psa_key_slot_t *slot = NULL;
2230 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002231 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002232
2233 /* Leaving attributes unspecified is not currently supported.
2234 * It could make sense to query the key type and size from the
2235 * secure element, but not all secure elements support this
2236 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2238 return PSA_ERROR_NOT_SUPPORTED;
2239 }
2240 if (psa_get_key_bits(attributes) == 0) {
2241 return PSA_ERROR_NOT_SUPPORTED;
2242 }
Gilles Peskined7729582019-08-05 15:55:54 +02002243
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2245 &slot, &driver);
2246 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002247 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 }
Gilles Peskined7729582019-08-05 15:55:54 +02002249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002251
2252exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 if (status != PSA_SUCCESS) {
2254 psa_fail_key_creation(slot, driver);
2255 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002256
Gilles Peskined7729582019-08-05 15:55:54 +02002257 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 psa_close_key(key);
2259 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002260}
2261#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2262
Gilles Peskine449bd832023-01-11 14:50:10 +01002263psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2264 const psa_key_attributes_t *specified_attributes,
2265 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002266{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002267 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002268 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002269 psa_key_slot_t *source_slot = NULL;
2270 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002271 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002272 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302273 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002274
Ronald Cron81709fc2020-11-14 12:10:32 +01002275 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2276
Archana8a180362021-07-05 02:18:48 +05302277 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2279 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002280 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 status = psa_validate_optional_attributes(source_slot,
2284 specified_attributes);
2285 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002286 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002288
Archana9d17bf42021-09-10 06:22:44 +05302289 /* The target key type and number of bits have been validated by
2290 * psa_validate_optional_attributes() to be either equal to zero or
2291 * equal to the ones of the source key. So it is safe to inherit
2292 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302293 * */
Archana9d17bf42021-09-10 06:22:44 +05302294 actual_attributes.core.bits = source_slot->attr.bits;
2295 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302296
2297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 status = psa_restrict_key_policy(source_slot->attr.type,
2299 &actual_attributes.core.policy,
2300 &source_slot->attr.policy);
2301 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002302 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002304
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2306 &target_slot, &driver);
2307 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002308 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 }
2310 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2311 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302312 /*
Archana9d17bf42021-09-10 06:22:44 +05302313 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302314 * the source key would need to be exported as plaintext and re-imported
2315 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302316 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302317 * appropriate API invocations from the application, if needed.
2318 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002319 status = PSA_ERROR_NOT_SUPPORTED;
2320 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002321 }
Archana8a180362021-07-05 02:18:48 +05302322 /*
2323 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302324 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302325 * - For opaque keys this translates to an invocation of the drivers'
2326 * copy_key entry point through the dispatch layer.
2327 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2329 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2330 &storage_size);
2331 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302332 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 }
Archana374fe5b2021-09-08 15:50:28 +05302334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2336 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302337 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 }
Archana374fe5b2021-09-08 15:50:28 +05302339
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 status = psa_driver_wrapper_copy_key(&actual_attributes,
2341 source_slot->key.data,
2342 source_slot->key.bytes,
2343 target_slot->key.data,
2344 target_slot->key.bytes,
2345 &target_slot->key.bytes);
2346 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302347 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 }
2349 } else {
2350 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302351 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 source_slot->key.bytes);
2353 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302354 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 }
Archana8a180362021-07-05 02:18:48 +05302356 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002358exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 if (status != PSA_SUCCESS) {
2360 psa_fail_key_creation(target_slot, driver);
2361 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002362
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002364
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002366}
2367
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002368
2369
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002370/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002371/* Message digests */
2372/****************************************************************/
2373
Gilles Peskine449bd832023-01-11 14:50:10 +01002374psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002375{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002376 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 if (operation->id == 0) {
2378 return PSA_SUCCESS;
2379 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002382 operation->id = 0;
2383
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002385}
2386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2388 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002389{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002390 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002391
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002392 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002394 status = PSA_ERROR_BAD_STATE;
2395 goto exit;
2396 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002399 status = PSA_ERROR_INVALID_ARGUMENT;
2400 goto exit;
2401 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002402
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002403 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2404 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002406
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002408
2409exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 if (status != PSA_SUCCESS) {
2411 psa_hash_abort(operation);
2412 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002413
2414 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002415}
2416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2418 const uint8_t *input,
2419 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002420{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002421 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2422
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002424 status = PSA_ERROR_BAD_STATE;
2425 goto exit;
2426 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002427
Steven Cooremanc8288352021-03-04 14:02:19 +01002428 /* Don't require hash implementations to behave correctly on a
2429 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 if (input_length == 0) {
2431 return PSA_SUCCESS;
2432 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002433
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002435
2436exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 if (status != PSA_SUCCESS) {
2438 psa_hash_abort(operation);
2439 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002440
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002442}
2443
Gilles Peskine449bd832023-01-11 14:50:10 +01002444psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2445 uint8_t *hash,
2446 size_t hash_size,
2447 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002448{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002449 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 if (operation->id == 0) {
2451 return PSA_ERROR_BAD_STATE;
2452 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002453
Steven Cooreman1e582352021-02-18 17:24:37 +01002454 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 operation, hash, hash_size, hash_length);
2456 psa_hash_abort(operation);
2457 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002458}
2459
Gilles Peskine449bd832023-01-11 14:50:10 +01002460psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2461 const uint8_t *hash,
2462 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002463{
Ronald Cron69a63422021-10-18 09:47:58 +02002464 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002465 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002466 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 operation,
2468 actual_hash, sizeof(actual_hash),
2469 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002470
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002472 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002476 status = PSA_ERROR_INVALID_SIGNATURE;
2477 goto exit;
2478 }
2479
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002481 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002483
2484exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2486 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002487 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002489
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002491}
2492
Gilles Peskine449bd832023-01-11 14:50:10 +01002493psa_status_t psa_hash_compute(psa_algorithm_t alg,
2494 const uint8_t *input, size_t input_length,
2495 uint8_t *hash, size_t hash_size,
2496 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002497{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002498 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 if (!PSA_ALG_IS_HASH(alg)) {
2500 return PSA_ERROR_INVALID_ARGUMENT;
2501 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2504 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002505}
2506
Gilles Peskine449bd832023-01-11 14:50:10 +01002507psa_status_t psa_hash_compare(psa_algorithm_t alg,
2508 const uint8_t *input, size_t input_length,
2509 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002510{
Ronald Cron69a63422021-10-18 09:47:58 +02002511 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002512 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002513
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 if (!PSA_ALG_IS_HASH(alg)) {
2515 return PSA_ERROR_INVALID_ARGUMENT;
2516 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002517
Steven Cooreman1e582352021-02-18 17:24:37 +01002518 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 alg, input, input_length,
2520 actual_hash, sizeof(actual_hash),
2521 &actual_hash_length);
2522 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002523 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 }
2525 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002526 status = PSA_ERROR_INVALID_SIGNATURE;
2527 goto exit;
2528 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002530 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002532
2533exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2535 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002536}
2537
Gilles Peskine449bd832023-01-11 14:50:10 +01002538psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2539 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002540{
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 if (source_operation->id == 0 ||
2542 target_operation->id != 0) {
2543 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002544 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2547 target_operation);
2548 if (status != PSA_SUCCESS) {
2549 psa_hash_abort(target_operation);
2550 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002551
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002553}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002554
2555
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002556/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002557/* MAC */
2558/****************************************************************/
2559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002561{
Steven Cooremane6804192021-03-19 18:28:56 +01002562 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 if (operation->id == 0) {
2564 return PSA_SUCCESS;
2565 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002566
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002568 operation->mac_size = 0;
2569 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002570 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002573}
2574
Ronald Cron2dff3b22021-06-17 16:33:22 +02002575static psa_status_t psa_mac_finalize_alg_and_key_validation(
2576 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002577 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002579{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002580 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 psa_key_type_t key_type = psa_get_key_type(attributes);
2582 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002583
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 if (!PSA_ALG_IS_MAC(alg)) {
2585 return PSA_ERROR_INVALID_ARGUMENT;
2586 }
Steven Cooremane6804192021-03-19 18:28:56 +01002587
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002588 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 status = psa_mac_key_can_do(alg, key_type);
2590 if (status != PSA_SUCCESS) {
2591 return status;
2592 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002593
Steven Cooremand1a68f12021-05-10 11:13:41 +02002594 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002596
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002598 /* A very short MAC is too short for security since it can be
2599 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2600 * so we make this our minimum, even though 32 bits is still
2601 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002603 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2606 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002607 /* It's impossible to "truncate" to a larger length than the full length
2608 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002610 }
2611
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002613 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2614 * that is disabled in the compile-time configuration. The result can
2615 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2616 * configuration into account. In this case, force a return of
2617 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2618 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2619 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2620 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2621 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002623 }
2624
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002626}
2627
Gilles Peskine449bd832023-01-11 14:50:10 +01002628static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2629 mbedtls_svc_key_id_t key,
2630 psa_algorithm_t alg,
2631 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002632{
2633 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2634 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002635 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002636
2637 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002639 status = PSA_ERROR_BAD_STATE;
2640 goto exit;
2641 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002642
2643 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 key,
2645 &slot,
2646 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2647 alg);
2648 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002649 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002651
2652 psa_key_attributes_t attributes = {
2653 .core = slot->attr
2654 };
2655
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2657 &operation->mac_size);
2658 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002659 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002661
2662 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002663 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 if (is_sign) {
2665 status = psa_driver_wrapper_mac_sign_setup(operation,
2666 &attributes,
2667 slot->key.data,
2668 slot->key.bytes,
2669 alg);
2670 } else {
2671 status = psa_driver_wrapper_mac_verify_setup(operation,
2672 &attributes,
2673 slot->key.data,
2674 slot->key.bytes,
2675 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002676 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002677
Gilles Peskinefbfac682018-07-08 20:51:54 +02002678exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 if (status != PSA_SUCCESS) {
2680 psa_mac_abort(operation);
2681 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002684
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002686}
2687
Gilles Peskine449bd832023-01-11 14:50:10 +01002688psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2689 mbedtls_svc_key_id_t key,
2690 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002691{
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002693}
2694
Gilles Peskine449bd832023-01-11 14:50:10 +01002695psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2696 mbedtls_svc_key_id_t key,
2697 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002698{
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002700}
2701
Gilles Peskine449bd832023-01-11 14:50:10 +01002702psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2703 const uint8_t *input,
2704 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002705{
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 if (operation->id == 0) {
2707 return PSA_ERROR_BAD_STATE;
2708 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002709
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002710 /* Don't require hash implementations to behave correctly on a
2711 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 if (input_length == 0) {
2713 return PSA_SUCCESS;
2714 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002715
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2717 input, input_length);
2718 if (status != PSA_SUCCESS) {
2719 psa_mac_abort(operation);
2720 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002723}
2724
Gilles Peskine449bd832023-01-11 14:50:10 +01002725psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2726 uint8_t *mac,
2727 size_t mac_size,
2728 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002729{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002730 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2731 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002732
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002734 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002735 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002736 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002737
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002739 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002740 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002741 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002742
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002743 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2744 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002746 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002747 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002748 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002749
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002751 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002752 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002753 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002754
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 status = psa_driver_wrapper_mac_sign_finish(operation,
2756 mac, operation->mac_size,
2757 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002758
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002759exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002760 /* In case of success, set the potential excess room in the output buffer
2761 * to an invalid value, to avoid potentially leaking a longer MAC.
2762 * In case of error, set the output length and content to a safe default,
2763 * such that in case the caller misses an error check, the output would be
2764 * an unachievable MAC.
2765 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002767 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002768 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002769 }
mohammad16036df908f2018-04-02 08:34:15 -07002770
Paul Elliottdc42ca82023-02-24 18:11:59 +00002771 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002772
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002774
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002776}
2777
Gilles Peskine449bd832023-01-11 14:50:10 +01002778psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2779 const uint8_t *mac,
2780 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002781{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002782 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2783 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002784
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002786 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002787 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002788 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002789
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002791 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002792 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002793 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002794
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002796 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002797 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002798 }
2799
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 status = psa_driver_wrapper_mac_verify_finish(operation,
2801 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002802
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002803exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002805
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002807}
2808
Gilles Peskine449bd832023-01-11 14:50:10 +01002809static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2810 psa_algorithm_t alg,
2811 const uint8_t *input,
2812 size_t input_length,
2813 uint8_t *mac,
2814 size_t mac_size,
2815 size_t *mac_length,
2816 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002817{
2818 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002819 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2820 psa_key_slot_t *slot;
2821 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002822
Ronald Cron51131b52021-06-17 17:17:20 +02002823 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 key,
2825 &slot,
2826 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2827 alg);
2828 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002829 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002831
Ronald Cron51131b52021-06-17 17:17:20 +02002832 psa_key_attributes_t attributes = {
2833 .core = slot->attr
2834 };
2835
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2837 &operation_mac_size);
2838 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002839 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002841
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002843 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002844 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002845 }
2846
2847 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 &attributes,
2849 slot->key.data, slot->key.bytes,
2850 alg,
2851 input, input_length,
2852 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002853
2854exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002855 /* In case of success, set the potential excess room in the output buffer
2856 * to an invalid value, to avoid potentially leaking a longer MAC.
2857 * In case of error, set the output length and content to a safe default,
2858 * such that in case the caller misses an error check, the output would be
2859 * an unachievable MAC.
2860 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002862 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002863 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002864 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002865
2866 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002867
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002869
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002871}
2872
Gilles Peskine449bd832023-01-11 14:50:10 +01002873psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002874 psa_algorithm_t alg,
2875 const uint8_t *input,
2876 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 uint8_t *mac,
2878 size_t mac_size,
2879 size_t *mac_length)
2880{
2881 return psa_mac_compute_internal(key, alg,
2882 input, input_length,
2883 mac, mac_size, mac_length, 1);
2884}
2885
2886psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2887 psa_algorithm_t alg,
2888 const uint8_t *input,
2889 size_t input_length,
2890 const uint8_t *mac,
2891 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002892{
2893 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002894 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2895 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002896
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 status = psa_mac_compute_internal(key, alg,
2898 input, input_length,
2899 actual_mac, sizeof(actual_mac),
2900 &actual_mac_length, 0);
2901 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002902 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002904
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002906 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002907 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002908 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002910 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002911 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002912 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002913
2914exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002916
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002918}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002919
Gilles Peskinee59236f2018-01-27 23:32:46 +01002920/****************************************************************/
2921/* Asymmetric cryptography */
2922/****************************************************************/
2923
Gilles Peskine449bd832023-01-11 14:50:10 +01002924static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2925 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002926{
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 if (input_is_message) {
2928 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2929 return PSA_ERROR_INVALID_ARGUMENT;
2930 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002931
Gilles Peskine449bd832023-01-11 14:50:10 +01002932 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2933 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2934 return PSA_ERROR_INVALID_ARGUMENT;
2935 }
2936 }
2937 } else {
2938 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2939 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002940 }
2941 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002942
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002944}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002945
Gilles Peskine449bd832023-01-11 14:50:10 +01002946static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2947 int input_is_message,
2948 psa_algorithm_t alg,
2949 const uint8_t *input,
2950 size_t input_length,
2951 uint8_t *signature,
2952 size_t signature_size,
2953 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002954{
2955 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2956 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2957 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002958
2959 *signature_length = 0;
2960
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 status = psa_sign_verify_check_alg(input_is_message, alg);
2962 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002963 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002965
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002966 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002967 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002968 * buffer can in principle be empty since it doesn't actually have
2969 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 if (signature_size == 0) {
2971 return PSA_ERROR_BUFFER_TOO_SMALL;
2972 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002973
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002974 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 key, &slot,
2976 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2977 PSA_KEY_USAGE_SIGN_HASH,
2978 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002979
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002981 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002983
Gilles Peskine449bd832023-01-11 14:50:10 +01002984 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002985 status = PSA_ERROR_INVALID_ARGUMENT;
2986 goto exit;
2987 }
2988
2989 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002991 };
2992
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002994 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002995 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002996 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 signature, signature_size, signature_length);
2998 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002999
3000 status = psa_driver_wrapper_sign_hash(
3001 &attributes, slot->key.data, slot->key.bytes,
3002 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003004 }
3005
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003006
3007exit:
Paul Elliott59200a22023-02-21 15:07:40 +00003008 psa_wipe_tag_output_buffer(signature, status, signature_size,
3009 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003010
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003012
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003014}
3015
Gilles Peskine449bd832023-01-11 14:50:10 +01003016static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
3017 int input_is_message,
3018 psa_algorithm_t alg,
3019 const uint8_t *input,
3020 size_t input_length,
3021 const uint8_t *signature,
3022 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003023{
3024 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3025 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3026 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003027
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 status = psa_sign_verify_check_alg(input_is_message, alg);
3029 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003030 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003032
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003033 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 key, &slot,
3035 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
3036 PSA_KEY_USAGE_VERIFY_HASH,
3037 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003038
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 if (status != PSA_SUCCESS) {
3040 return status;
3041 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003042
3043 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003045 };
3046
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003048 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003049 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003050 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 signature, signature_length);
3052 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003053 status = psa_driver_wrapper_verify_hash(
3054 &attributes, slot->key.data, slot->key.bytes,
3055 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003057 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003058
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003060
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003062
3063}
3064
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003065psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003066 const psa_key_attributes_t *attributes,
3067 const uint8_t *key_buffer,
3068 size_t key_buffer_size,
3069 psa_algorithm_t alg,
3070 const uint8_t *input,
3071 size_t input_length,
3072 uint8_t *signature,
3073 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003075{
3076 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003077
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003079 size_t hash_length;
3080 uint8_t hash[PSA_HASH_MAX_SIZE];
3081
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003082 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 PSA_ALG_SIGN_GET_HASH(alg),
3084 input, input_length,
3085 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003086
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003088 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003089 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003090
3091 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 attributes, key_buffer, key_buffer_size,
3093 alg, hash, hash_length,
3094 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003095 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003096
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003098}
3099
Gilles Peskine449bd832023-01-11 14:50:10 +01003100psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3101 psa_algorithm_t alg,
3102 const uint8_t *input,
3103 size_t input_length,
3104 uint8_t *signature,
3105 size_t signature_size,
3106 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003107{
3108 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003109 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003111}
3112
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003113psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003114 const psa_key_attributes_t *attributes,
3115 const uint8_t *key_buffer,
3116 size_t key_buffer_size,
3117 psa_algorithm_t alg,
3118 const uint8_t *input,
3119 size_t input_length,
3120 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003121 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003122{
3123 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003124
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003126 size_t hash_length;
3127 uint8_t hash[PSA_HASH_MAX_SIZE];
3128
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003129 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 PSA_ALG_SIGN_GET_HASH(alg),
3131 input, input_length,
3132 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003133
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003135 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003137
3138 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 attributes, key_buffer, key_buffer_size,
3140 alg, hash, hash_length,
3141 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003142 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003143
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003145}
3146
Gilles Peskine449bd832023-01-11 14:50:10 +01003147psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3148 psa_algorithm_t alg,
3149 const uint8_t *input,
3150 size_t input_length,
3151 const uint8_t *signature,
3152 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003153{
3154 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003155 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003156 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003157}
3158
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003159psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003160 const psa_key_attributes_t *attributes,
3161 const uint8_t *key_buffer, size_t key_buffer_size,
3162 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003164{
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3166 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3167 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003168#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003169 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3170 return mbedtls_psa_rsa_sign_hash(
3171 attributes,
3172 key_buffer, key_buffer_size,
3173 alg, hash, hash_length,
3174 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003175#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3176 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003177 } else {
3178 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003179 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3181 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003182#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003183 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3184 return mbedtls_psa_ecdsa_sign_hash(
3185 attributes,
3186 key_buffer, key_buffer_size,
3187 alg, hash, hash_length,
3188 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003189#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3190 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 } else {
3192 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003193 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003194 }
Ronald Cron4501c982021-03-19 20:09:32 +01003195
Gilles Peskine449bd832023-01-11 14:50:10 +01003196 (void) key_buffer;
3197 (void) key_buffer_size;
3198 (void) hash;
3199 (void) hash_length;
3200 (void) signature;
3201 (void) signature_size;
3202 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003203
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003205}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003206
Gilles Peskine449bd832023-01-11 14:50:10 +01003207psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3208 psa_algorithm_t alg,
3209 const uint8_t *hash,
3210 size_t hash_length,
3211 uint8_t *signature,
3212 size_t signature_size,
3213 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003214{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003215 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003216 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003217 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003218}
3219
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003220psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003221 const psa_key_attributes_t *attributes,
3222 const uint8_t *key_buffer, size_t key_buffer_size,
3223 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003225{
Gilles Peskine449bd832023-01-11 14:50:10 +01003226 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3227 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3228 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003229#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003230 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3231 return mbedtls_psa_rsa_verify_hash(
3232 attributes,
3233 key_buffer, key_buffer_size,
3234 alg, hash, hash_length,
3235 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003236#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3237 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003238 } else {
3239 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003240 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003241 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3242 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003243#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003244 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3245 return mbedtls_psa_ecdsa_verify_hash(
3246 attributes,
3247 key_buffer, key_buffer_size,
3248 alg, hash, hash_length,
3249 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003250#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3251 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 } else {
3253 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003254 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003255 }
Ronald Cron4501c982021-03-19 20:09:32 +01003256
Gilles Peskine449bd832023-01-11 14:50:10 +01003257 (void) key_buffer;
3258 (void) key_buffer_size;
3259 (void) hash;
3260 (void) hash_length;
3261 (void) signature;
3262 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003263
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003265}
3266
Gilles Peskine449bd832023-01-11 14:50:10 +01003267psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3268 psa_algorithm_t alg,
3269 const uint8_t *hash,
3270 size_t hash_length,
3271 const uint8_t *signature,
3272 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003273{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003274 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003275 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003276 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003277}
3278
Gilles Peskine449bd832023-01-11 14:50:10 +01003279psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3280 psa_algorithm_t alg,
3281 const uint8_t *input,
3282 size_t input_length,
3283 const uint8_t *salt,
3284 size_t salt_length,
3285 uint8_t *output,
3286 size_t output_size,
3287 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003288{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003289 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003290 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003291 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003292
Darryl Green5cc689a2018-07-24 15:34:10 +01003293 (void) input;
3294 (void) input_length;
3295 (void) salt;
3296 (void) output;
3297 (void) output_size;
3298
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003299 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003300
Gilles Peskine449bd832023-01-11 14:50:10 +01003301 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3302 return PSA_ERROR_INVALID_ARGUMENT;
3303 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003304
Ronald Cron5c522922020-11-14 16:35:34 +01003305 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3307 if (status != PSA_SUCCESS) {
3308 return status;
3309 }
3310 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3311 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003312 status = PSA_ERROR_INVALID_ARGUMENT;
3313 goto exit;
3314 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003315
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003316 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003318 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003319
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003320 status = psa_driver_wrapper_asymmetric_encrypt(
3321 &attributes, slot->key.data, slot->key.bytes,
3322 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003324exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003325 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003326
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003328}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003329
Gilles Peskine449bd832023-01-11 14:50:10 +01003330psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3331 psa_algorithm_t alg,
3332 const uint8_t *input,
3333 size_t input_length,
3334 const uint8_t *salt,
3335 size_t salt_length,
3336 uint8_t *output,
3337 size_t output_size,
3338 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003339{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003340 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003341 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003342 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003343
Darryl Green5cc689a2018-07-24 15:34:10 +01003344 (void) input;
3345 (void) input_length;
3346 (void) salt;
3347 (void) output;
3348 (void) output_size;
3349
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003350 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003351
Gilles Peskine449bd832023-01-11 14:50:10 +01003352 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3353 return PSA_ERROR_INVALID_ARGUMENT;
3354 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003355
Ronald Cron5c522922020-11-14 16:35:34 +01003356 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003357 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3358 if (status != PSA_SUCCESS) {
3359 return status;
3360 }
3361 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003362 status = PSA_ERROR_INVALID_ARGUMENT;
3363 goto exit;
3364 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003365
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003366 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003367 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003368 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003369
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003370 status = psa_driver_wrapper_asymmetric_decrypt(
3371 &attributes, slot->key.data, slot->key.bytes,
3372 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003374
3375exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003376 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003377
Gilles Peskine449bd832023-01-11 14:50:10 +01003378 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003379}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003380
Paul Elliott9fe12f62022-11-30 19:16:02 +00003381/****************************************************************/
3382/* Asymmetric interruptible cryptography */
3383/****************************************************************/
3384
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003385static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3386
Paul Elliott9fe12f62022-11-30 19:16:02 +00003387void psa_interruptible_set_max_ops(uint32_t max_ops)
3388{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003389 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003390}
3391
3392uint32_t psa_interruptible_get_max_ops(void)
3393{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003394 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003395}
3396
Paul Elliott9fe12f62022-11-30 19:16:02 +00003397uint32_t psa_sign_hash_get_num_ops(
3398 const psa_sign_hash_interruptible_operation_t *operation)
3399{
Paul Elliott296ede92022-12-15 17:00:30 +00003400 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003401}
3402
3403uint32_t psa_verify_hash_get_num_ops(
3404 const psa_verify_hash_interruptible_operation_t *operation)
3405{
Paul Elliott296ede92022-12-15 17:00:30 +00003406 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003407}
3408
Paul Elliott59ad9452022-12-18 15:09:02 +00003409static psa_status_t psa_sign_hash_abort_internal(
3410 psa_sign_hash_interruptible_operation_t *operation)
3411{
3412 if (operation->id == 0) {
3413 /* The object has (apparently) been initialized but it is not (yet)
3414 * in use. It's ok to call abort on such an object, and there's
3415 * nothing to do. */
3416 return PSA_SUCCESS;
3417 }
3418
3419 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3420
3421 status = psa_driver_wrapper_sign_hash_abort(operation);
3422
3423 operation->id = 0;
3424
Paul Elliotte6145dc2023-02-07 12:51:21 +00003425 /* Do not clear either the error_occurred or num_ops elements here as they
3426 * only want to be cleared by the application calling abort, not by abort
3427 * being called at completion of an operation. */
3428
Paul Elliott59ad9452022-12-18 15:09:02 +00003429 return status;
3430}
3431
Paul Elliott9fe12f62022-11-30 19:16:02 +00003432psa_status_t psa_sign_hash_start(
3433 psa_sign_hash_interruptible_operation_t *operation,
3434 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3435 const uint8_t *hash, size_t hash_length)
3436{
3437 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3438 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3439 psa_key_slot_t *slot;
3440
Paul Elliottc9774412023-02-06 15:14:07 +00003441 /* Check that start has not been previously called, or operation has not
3442 * previously errored. */
3443 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003444 return PSA_ERROR_BAD_STATE;
3445 }
3446
Paul Elliott9fe12f62022-11-30 19:16:02 +00003447 status = psa_sign_verify_check_alg(0, alg);
3448 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003449 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003450 return status;
3451 }
3452
3453 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3454 PSA_KEY_USAGE_SIGN_HASH,
3455 alg);
3456
3457 if (status != PSA_SUCCESS) {
3458 goto exit;
3459 }
3460
3461 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3462 status = PSA_ERROR_INVALID_ARGUMENT;
3463 goto exit;
3464 }
3465
3466 psa_key_attributes_t attributes = {
3467 .core = slot->attr
3468 };
3469
Paul Elliott296ede92022-12-15 17:00:30 +00003470 /* Ensure ops count gets reset, in case of operation re-use. */
3471 operation->num_ops = 0;
3472
Paul Elliott9fe12f62022-11-30 19:16:02 +00003473 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3474 slot->key.data,
3475 slot->key.bytes, alg,
3476 hash, hash_length);
3477exit:
3478
3479 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003480 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003481 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003482 }
3483
3484 unlock_status = psa_unlock_key_slot(slot);
3485
Paul Elliottc9774412023-02-06 15:14:07 +00003486 if (unlock_status != PSA_SUCCESS) {
3487 operation->error_occurred = 1;
3488 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003489
Paul Elliottc9774412023-02-06 15:14:07 +00003490 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003491}
3492
3493
3494psa_status_t psa_sign_hash_complete(
3495 psa_sign_hash_interruptible_operation_t *operation,
3496 uint8_t *signature, size_t signature_size,
3497 size_t *signature_length)
3498{
3499 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3500
3501 *signature_length = 0;
3502
Paul Elliottc9774412023-02-06 15:14:07 +00003503 /* Check that start has been called first, and that operation has not
3504 * previously errored. */
3505 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003506 status = PSA_ERROR_BAD_STATE;
3507 goto exit;
3508 }
3509
Paul Elliott096abc42023-02-03 18:33:23 +00003510 /* Immediately reject a zero-length signature buffer. This guarantees that
3511 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003512 if (signature_size == 0) {
3513 status = PSA_ERROR_BUFFER_TOO_SMALL;
3514 goto exit;
3515 }
3516
3517 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3518 signature_size,
3519 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003520
Paul Elliott296ede92022-12-15 17:00:30 +00003521 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003522 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003523
Paul Elliottebe225c2023-02-10 14:32:53 +00003524exit:
3525
Paul Elliott59200a22023-02-21 15:07:40 +00003526 psa_wipe_tag_output_buffer(signature, status, signature_size,
3527 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003528
Paul Elliott53bb3122023-02-10 14:22:22 +00003529 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003530 if (status != PSA_SUCCESS) {
3531 operation->error_occurred = 1;
3532 }
3533
Paul Elliott59ad9452022-12-18 15:09:02 +00003534 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003535 }
3536
3537 return status;
3538}
3539
3540psa_status_t psa_sign_hash_abort(
3541 psa_sign_hash_interruptible_operation_t *operation)
3542{
Paul Elliott59ad9452022-12-18 15:09:02 +00003543 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3544
3545 status = psa_sign_hash_abort_internal(operation);
3546
3547 /* We clear the number of ops done here, so that it is not cleared when
3548 * the operation fails or succeeds, only on manual abort. */
3549 operation->num_ops = 0;
3550
Paul Elliottc9774412023-02-06 15:14:07 +00003551 /* Likewise, failure state. */
3552 operation->error_occurred = 0;
3553
Paul Elliott59ad9452022-12-18 15:09:02 +00003554 return status;
3555}
3556
3557static psa_status_t psa_verify_hash_abort_internal(
3558 psa_verify_hash_interruptible_operation_t *operation)
3559{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003560 if (operation->id == 0) {
3561 /* The object has (apparently) been initialized but it is not (yet)
3562 * in use. It's ok to call abort on such an object, and there's
3563 * nothing to do. */
3564 return PSA_SUCCESS;
3565 }
3566
Paul Elliott59ad9452022-12-18 15:09:02 +00003567 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3568
3569 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003570
3571 operation->id = 0;
3572
Paul Elliotte6145dc2023-02-07 12:51:21 +00003573 /* Do not clear either the error_occurred or num_ops elements here as they
3574 * only want to be cleared by the application calling abort, not by abort
3575 * being called at completion of an operation. */
3576
Paul Elliott59ad9452022-12-18 15:09:02 +00003577 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003578}
3579
3580psa_status_t psa_verify_hash_start(
3581 psa_verify_hash_interruptible_operation_t *operation,
3582 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3583 const uint8_t *hash, size_t hash_length,
3584 const uint8_t *signature, size_t signature_length)
3585{
3586 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3587 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3588 psa_key_slot_t *slot;
3589
Paul Elliottc9774412023-02-06 15:14:07 +00003590 /* Check that start has not been previously called, or operation has not
3591 * previously errored. */
3592 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003593 return PSA_ERROR_BAD_STATE;
3594 }
3595
3596 status = psa_sign_verify_check_alg(0, alg);
3597 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003598 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003599 return status;
3600 }
3601
3602 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3603 PSA_KEY_USAGE_VERIFY_HASH,
3604 alg);
3605
3606 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003607 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003608 return status;
3609 }
3610
3611 psa_key_attributes_t attributes = {
3612 .core = slot->attr
3613 };
3614
Paul Elliott296ede92022-12-15 17:00:30 +00003615 /* Ensure ops count gets reset, in case of operation re-use. */
3616 operation->num_ops = 0;
3617
Paul Elliott9fe12f62022-11-30 19:16:02 +00003618 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3619 slot->key.data,
3620 slot->key.bytes,
3621 alg, hash, hash_length,
3622 signature, signature_length);
3623
3624 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003625 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003626 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003627 }
3628
3629 unlock_status = psa_unlock_key_slot(slot);
3630
Paul Elliottc9774412023-02-06 15:14:07 +00003631 if (unlock_status != PSA_SUCCESS) {
3632 operation->error_occurred = 1;
3633 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003634
Paul Elliottc9774412023-02-06 15:14:07 +00003635 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003636}
3637
3638psa_status_t psa_verify_hash_complete(
3639 psa_verify_hash_interruptible_operation_t *operation)
3640{
3641 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3642
Paul Elliottc9774412023-02-06 15:14:07 +00003643 /* Check that start has been called first, and that operation has not
3644 * previously errored. */
3645 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003646 status = PSA_ERROR_BAD_STATE;
3647 goto exit;
3648 }
3649
3650 status = psa_driver_wrapper_verify_hash_complete(operation);
3651
Paul Elliott296ede92022-12-15 17:00:30 +00003652 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003653 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003654 operation);
3655
Paul Elliottebe225c2023-02-10 14:32:53 +00003656exit:
3657
Paul Elliott9fe12f62022-11-30 19:16:02 +00003658 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003659 if (status != PSA_SUCCESS) {
3660 operation->error_occurred = 1;
3661 }
3662
Paul Elliott59ad9452022-12-18 15:09:02 +00003663 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003664 }
3665
3666 return status;
3667}
3668
3669psa_status_t psa_verify_hash_abort(
3670 psa_verify_hash_interruptible_operation_t *operation)
3671{
Paul Elliott59ad9452022-12-18 15:09:02 +00003672 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003673
Paul Elliott59ad9452022-12-18 15:09:02 +00003674 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003675
Paul Elliott59ad9452022-12-18 15:09:02 +00003676 /* We clear the number of ops done here, so that it is not cleared when
3677 * the operation fails or succeeds, only on manual abort. */
3678 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003679
Paul Elliottc9774412023-02-06 15:14:07 +00003680 /* Likewise, failure state. */
3681 operation->error_occurred = 0;
3682
Paul Elliott59ad9452022-12-18 15:09:02 +00003683 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003684}
3685
Paul Elliott588f8ed2022-12-02 18:10:26 +00003686/****************************************************************/
3687/* Asymmetric interruptible cryptography internal */
3688/* implementations */
3689/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003690
Paul Elliott588f8ed2022-12-02 18:10:26 +00003691void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3692{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003693
Paul Elliott588f8ed2022-12-02 18:10:26 +00003694#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3695 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3696 defined(MBEDTLS_ECP_RESTARTABLE)
3697
3698 /* Internal implementation uses zero to indicate infinite number max ops,
3699 * therefore avoid this value, and set to minimum possible. */
3700 if (max_ops == 0) {
3701 max_ops = 1;
3702 }
3703
Paul Elliott588f8ed2022-12-02 18:10:26 +00003704 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003705#else
3706 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003707#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3708 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3709 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3710}
3711
Paul Elliott588f8ed2022-12-02 18:10:26 +00003712uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003713 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003714{
3715#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3716 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3717 defined(MBEDTLS_ECP_RESTARTABLE)
3718
Paul Elliott93d9ca82023-02-15 18:14:21 +00003719 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003720#else
3721 (void) operation;
3722 return 0;
3723#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3724 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3725 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3726}
3727
3728uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003729 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003730{
3731 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3732 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3733 defined(MBEDTLS_ECP_RESTARTABLE)
3734
Paul Elliott93d9ca82023-02-15 18:14:21 +00003735 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003736#else
3737 (void) operation;
3738 return 0;
3739#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3740 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3741 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3742}
3743
3744psa_status_t mbedtls_psa_sign_hash_start(
3745 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3746 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3747 size_t key_buffer_size, psa_algorithm_t alg,
3748 const uint8_t *hash, size_t hash_length)
3749{
3750 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003751 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003752
Paul Elliott068fe072023-01-16 13:59:15 +00003753 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3754 return PSA_ERROR_NOT_SUPPORTED;
3755 }
3756
3757 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003758 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003759 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003760
3761#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003762 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3763 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003764
Paul Elliotta1c94092023-02-15 16:38:04 +00003765 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3766
Paul Elliott93d9ca82023-02-15 18:14:21 +00003767 /* Ensure num_ops is zero'ed in case of context re-use. */
3768 operation->num_ops = 0;
3769
Paul Elliott068fe072023-01-16 13:59:15 +00003770 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3771 attributes->core.bits,
3772 key_buffer,
3773 key_buffer_size,
3774 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003775
Paul Elliott068fe072023-01-16 13:59:15 +00003776 if (status != PSA_SUCCESS) {
3777 return status;
3778 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003779
Paul Elliott1bc59df2023-02-05 13:41:57 +00003780 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003781 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003782
Paul Elliott068fe072023-01-16 13:59:15 +00003783 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3784 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3785 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003786
Paul Elliott0290a762023-02-09 14:30:24 +00003787 /* We only need to store the same length of hash as the private key size
3788 * here, it would be truncated by the internal implementation anyway. */
3789 required_hash_length = (hash_length < operation->coordinate_bytes ?
3790 hash_length : operation->coordinate_bytes);
3791
Paul Elliottba70ad42023-02-15 18:23:53 +00003792 if (required_hash_length > sizeof(operation->hash)) {
3793 /* Shouldn't happen, but better safe than sorry. */
3794 return PSA_ERROR_CORRUPTION_DETECTED;
3795 }
3796
Paul Elliott0290a762023-02-09 14:30:24 +00003797 memcpy(operation->hash, hash, required_hash_length);
3798 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003799
3800 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003801
3802#else
Paul Elliott068fe072023-01-16 13:59:15 +00003803 (void) operation;
3804 (void) key_buffer;
3805 (void) key_buffer_size;
3806 (void) alg;
3807 (void) hash;
3808 (void) hash_length;
3809 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003810 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003811
Paul Elliott068fe072023-01-16 13:59:15 +00003812 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003813#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3814 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3815 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003816}
3817
3818psa_status_t mbedtls_psa_sign_hash_complete(
3819 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3820 uint8_t *signature, size_t signature_size,
3821 size_t *signature_length)
3822{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003823#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3824 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3825 defined(MBEDTLS_ECP_RESTARTABLE)
3826
Paul Elliotta1c94092023-02-15 16:38:04 +00003827 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003828 mbedtls_mpi r;
3829 mbedtls_mpi s;
3830
Paul Elliott46845252023-02-03 14:59:11 +00003831 mbedtls_mpi_init(&r);
3832 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003833
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003834 /* Ensure max_ops is set to the current value (or default). */
3835 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3836
Paul Elliotta1c94092023-02-15 16:38:04 +00003837 if (signature_size < 2 * operation->coordinate_bytes) {
3838 status = PSA_ERROR_BUFFER_TOO_SMALL;
3839 goto exit;
3840 }
3841
Paul Elliott588f8ed2022-12-02 18:10:26 +00003842 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003843
Paul Elliott588f8ed2022-12-02 18:10:26 +00003844#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3845 status = mbedtls_to_psa_error(
3846 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003847 &r,
3848 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003849 &operation->ctx->d,
3850 operation->hash,
3851 operation->hash_length,
3852 operation->md_alg,
3853 mbedtls_psa_get_random,
3854 MBEDTLS_PSA_RANDOM_STATE,
3855 &operation->restart_ctx));
3856#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003857 status = PSA_ERROR_NOT_SUPPORTED;
3858 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003859#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3860 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003861 status = mbedtls_to_psa_error(
3862 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003863 &r,
3864 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003865 &operation->ctx->d,
3866 operation->hash,
3867 operation->hash_length,
3868 mbedtls_psa_get_random,
3869 MBEDTLS_PSA_RANDOM_STATE,
3870 mbedtls_psa_get_random,
3871 MBEDTLS_PSA_RANDOM_STATE,
3872 &operation->restart_ctx));
3873 }
3874
Paul Elliottf8e5b562023-02-19 18:43:45 +00003875 /* Hide the fact that the restart context only holds a delta of number of
3876 * ops done during the last operation, not an absolute value. */
3877 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3878
Paul Elliott724bd252023-02-08 12:35:08 +00003879 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003880 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003881 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003882 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003883 operation->coordinate_bytes)
3884 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003885
3886 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003887 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003888 }
3889
3890 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003891 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003892 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003893 operation->coordinate_bytes,
3894 operation->coordinate_bytes)
3895 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003896
3897 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003898 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003899 }
3900
Paul Elliott1bc59df2023-02-05 13:41:57 +00003901 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003902
Paul Elliott724bd252023-02-08 12:35:08 +00003903 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003904 }
Paul Elliott724bd252023-02-08 12:35:08 +00003905
3906exit:
3907
3908 mbedtls_mpi_free(&r);
3909 mbedtls_mpi_free(&s);
3910 return status;
3911
Paul Elliott588f8ed2022-12-02 18:10:26 +00003912 #else
3913
3914 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003915 (void) signature;
3916 (void) signature_size;
3917 (void) signature_length;
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_sign_hash_abort(
3927 mbedtls_psa_sign_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 return PSA_SUCCESS;
3945
3946#else
3947
3948 (void) operation;
3949
3950 return PSA_ERROR_NOT_SUPPORTED;
3951
3952#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3953 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3954 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3955}
3956
3957psa_status_t mbedtls_psa_verify_hash_start(
3958 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3959 const psa_key_attributes_t *attributes,
3960 const uint8_t *key_buffer, size_t key_buffer_size,
3961 psa_algorithm_t alg,
3962 const uint8_t *hash, size_t hash_length,
3963 const uint8_t *signature, size_t signature_length)
3964{
3965 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003966 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003967 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003968
Paul Elliott068fe072023-01-16 13:59:15 +00003969 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3970 return PSA_ERROR_NOT_SUPPORTED;
3971 }
3972
3973 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003974 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003975 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003976
3977#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003978 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3979 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003980
Paul Elliotta1c94092023-02-15 16:38:04 +00003981 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3982 mbedtls_mpi_init(&operation->r);
3983 mbedtls_mpi_init(&operation->s);
3984
Paul Elliott93d9ca82023-02-15 18:14:21 +00003985 /* Ensure num_ops is zero'ed in case of context re-use. */
3986 operation->num_ops = 0;
3987
Paul Elliott068fe072023-01-16 13:59:15 +00003988 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3989 attributes->core.bits,
3990 key_buffer,
3991 key_buffer_size,
3992 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003993
Paul Elliott068fe072023-01-16 13:59:15 +00003994 if (status != PSA_SUCCESS) {
3995 return status;
3996 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003997
Paul Elliottc569fc22023-02-10 13:02:54 +00003998 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003999
Paul Elliott1bc59df2023-02-05 13:41:57 +00004000 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00004001 return PSA_ERROR_INVALID_SIGNATURE;
4002 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004003
Paul Elliott068fe072023-01-16 13:59:15 +00004004 status = mbedtls_to_psa_error(
4005 mbedtls_mpi_read_binary(&operation->r,
4006 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00004007 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00004008
Paul Elliott068fe072023-01-16 13:59:15 +00004009 if (status != PSA_SUCCESS) {
4010 return status;
4011 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004012
Paul Elliott068fe072023-01-16 13:59:15 +00004013 status = mbedtls_to_psa_error(
4014 mbedtls_mpi_read_binary(&operation->s,
4015 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00004016 coordinate_bytes,
4017 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00004018
Paul Elliott068fe072023-01-16 13:59:15 +00004019 if (status != PSA_SUCCESS) {
4020 return status;
4021 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004022
Paul Elliott2c9843f2023-02-15 17:32:42 +00004023 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004024
Paul Elliott2c9843f2023-02-15 17:32:42 +00004025 if (status != PSA_SUCCESS) {
4026 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00004027 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004028
Paul Elliott0290a762023-02-09 14:30:24 +00004029 /* We only need to store the same length of hash as the private key size
4030 * here, it would be truncated by the internal implementation anyway. */
4031 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
4032 coordinate_bytes);
4033
Paul Elliottba70ad42023-02-15 18:23:53 +00004034 if (required_hash_length > sizeof(operation->hash)) {
4035 /* Shouldn't happen, but better safe than sorry. */
4036 return PSA_ERROR_CORRUPTION_DETECTED;
4037 }
4038
Paul Elliott0290a762023-02-09 14:30:24 +00004039 memcpy(operation->hash, hash, required_hash_length);
4040 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00004041
4042 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004043#else
Paul Elliott068fe072023-01-16 13:59:15 +00004044 (void) operation;
4045 (void) key_buffer;
4046 (void) key_buffer_size;
4047 (void) alg;
4048 (void) hash;
4049 (void) hash_length;
4050 (void) signature;
4051 (void) signature_length;
4052 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00004053 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00004054 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004055
Paul Elliott068fe072023-01-16 13:59:15 +00004056 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004057#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4058 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4059 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004060}
4061
4062psa_status_t mbedtls_psa_verify_hash_complete(
4063 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4064{
4065
4066#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4067 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4068 defined(MBEDTLS_ECP_RESTARTABLE)
4069
Paul Elliottf8e5b562023-02-19 18:43:45 +00004070 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4071
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004072 /* Ensure max_ops is set to the current value (or default). */
4073 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4074
Paul Elliottf8e5b562023-02-19 18:43:45 +00004075 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004076 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4077 operation->hash,
4078 operation->hash_length,
4079 &operation->ctx->Q,
4080 &operation->r,
4081 &operation->s,
4082 &operation->restart_ctx));
4083
Paul Elliottf8e5b562023-02-19 18:43:45 +00004084 /* Hide the fact that the restart context only holds a delta of number of
4085 * ops done during the last operation, not an absolute value. */
4086 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4087
4088 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004089#else
4090 (void) operation;
4091
4092 return PSA_ERROR_NOT_SUPPORTED;
4093
4094#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4095 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4096 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4097}
4098
4099psa_status_t mbedtls_psa_verify_hash_abort(
4100 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4101{
4102
4103#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4104 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4105 defined(MBEDTLS_ECP_RESTARTABLE)
4106
4107 if (operation->ctx) {
4108 mbedtls_ecdsa_free(operation->ctx);
4109 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004110 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004111 }
4112
4113 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4114
Paul Elliott93d9ca82023-02-15 18:14:21 +00004115 operation->num_ops = 0;
4116
Paul Elliott588f8ed2022-12-02 18:10:26 +00004117 mbedtls_mpi_free(&operation->r);
4118 mbedtls_mpi_free(&operation->s);
4119
4120 return PSA_SUCCESS;
4121
4122#else
4123 (void) operation;
4124
4125 return PSA_ERROR_NOT_SUPPORTED;
4126
4127#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4128 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4129 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4130}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004131
mohammad1603503973b2018-03-12 15:59:30 +02004132/****************************************************************/
4133/* Symmetric cryptography */
4134/****************************************************************/
4135
Gilles Peskine449bd832023-01-11 14:50:10 +01004136static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4137 mbedtls_svc_key_id_t key,
4138 psa_algorithm_t alg,
4139 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004140{
4141 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4142 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004143 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004144 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4145 PSA_KEY_USAGE_ENCRYPT :
4146 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02004147
4148 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004149 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004150 status = PSA_ERROR_BAD_STATE;
4151 goto exit;
4152 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004153
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004155 status = PSA_ERROR_INVALID_ARGUMENT;
4156 goto exit;
4157 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004158
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4160 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004161 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004162 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004163
Ronald Cron49fafa92021-03-10 08:34:23 +01004164 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004165 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004166 * so we only set it (in the driver wrapper) after resources have been
4167 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004168 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004169 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004170 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004171 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004172 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004173 }
4174 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004175
Ronald Crona4af55f2020-12-14 14:36:06 +01004176 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004177 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004178 };
4179
Ronald Cronab99ac22020-10-01 16:38:28 +02004180 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004181 if (cipher_operation == MBEDTLS_ENCRYPT) {
4182 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4183 &attributes,
4184 slot->key.data,
4185 slot->key.bytes,
4186 alg);
4187 } else {
4188 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4189 &attributes,
4190 slot->key.data,
4191 slot->key.bytes,
4192 alg);
4193 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004194
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004195exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004196 if (status != PSA_SUCCESS) {
4197 psa_cipher_abort(operation);
4198 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004199
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004201
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004203}
4204
Gilles Peskine449bd832023-01-11 14:50:10 +01004205psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4206 mbedtls_svc_key_id_t key,
4207 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004208{
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004210}
4211
Gilles Peskine449bd832023-01-11 14:50:10 +01004212psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4213 mbedtls_svc_key_id_t key,
4214 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004215{
Gilles Peskine449bd832023-01-11 14:50:10 +01004216 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004217}
4218
Gilles Peskine449bd832023-01-11 14:50:10 +01004219psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4220 uint8_t *iv,
4221 size_t iv_size,
4222 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004223{
Ronald Cron6d051732020-10-01 14:10:20 +02004224 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004225 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4226 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004227
Gilles Peskine449bd832023-01-11 14:50:10 +01004228 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004229 status = PSA_ERROR_BAD_STATE;
4230 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004231 }
4232
Gilles Peskine449bd832023-01-11 14:50:10 +01004233 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004234 status = PSA_ERROR_BAD_STATE;
4235 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004236 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004237
Ronald Cron2fb90522021-07-05 12:31:44 +02004238 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004239 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004240 status = PSA_ERROR_BUFFER_TOO_SMALL;
4241 goto exit;
4242 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004243
Gilles Peskine449bd832023-01-11 14:50:10 +01004244 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004245 status = PSA_ERROR_GENERIC_ERROR;
4246 goto exit;
4247 }
4248
Gilles Peskine449bd832023-01-11 14:50:10 +01004249 status = psa_generate_random(local_iv, default_iv_length);
4250 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004251 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004252 }
Ronald Cron5618a392021-03-26 09:52:26 +01004253
Gilles Peskine449bd832023-01-11 14:50:10 +01004254 status = psa_driver_wrapper_cipher_set_iv(operation,
4255 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004256
4257exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004258 if (status == PSA_SUCCESS) {
4259 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004260 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004261 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004262 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004263 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004264 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004265 }
Ronald Cron6d051732020-10-01 14:10:20 +02004266
Gilles Peskine449bd832023-01-11 14:50:10 +01004267 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004268}
4269
Gilles Peskine449bd832023-01-11 14:50:10 +01004270psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4271 const uint8_t *iv,
4272 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004273{
Ronald Cron6d051732020-10-01 14:10:20 +02004274 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004275
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004277 status = PSA_ERROR_BAD_STATE;
4278 goto exit;
4279 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004280
Gilles Peskine449bd832023-01-11 14:50:10 +01004281 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004282 status = PSA_ERROR_BAD_STATE;
4283 goto exit;
4284 }
Ronald Crona0d68172021-03-26 10:15:08 +01004285
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004287 status = PSA_ERROR_INVALID_ARGUMENT;
4288 goto exit;
4289 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004290
Gilles Peskine449bd832023-01-11 14:50:10 +01004291 status = psa_driver_wrapper_cipher_set_iv(operation,
4292 iv,
4293 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004294
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004295exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004296 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004297 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 } else {
4299 psa_cipher_abort(operation);
4300 }
4301 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004302}
4303
Gilles Peskine449bd832023-01-11 14:50:10 +01004304psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4305 const uint8_t *input,
4306 size_t input_length,
4307 uint8_t *output,
4308 size_t output_size,
4309 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004310{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004311 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004312
Gilles Peskine449bd832023-01-11 14:50:10 +01004313 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004314 status = PSA_ERROR_BAD_STATE;
4315 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004316 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004317
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004319 status = PSA_ERROR_BAD_STATE;
4320 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004321 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004322
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 status = psa_driver_wrapper_cipher_update(operation,
4324 input,
4325 input_length,
4326 output,
4327 output_size,
4328 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004329
4330exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004331 if (status != PSA_SUCCESS) {
4332 psa_cipher_abort(operation);
4333 }
Ronald Cron6d051732020-10-01 14:10:20 +02004334
Gilles Peskine449bd832023-01-11 14:50:10 +01004335 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004336}
4337
Gilles Peskine449bd832023-01-11 14:50:10 +01004338psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4339 uint8_t *output,
4340 size_t output_size,
4341 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004342{
David Saadab4ecc272019-02-14 13:48:10 +02004343 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004344
Gilles Peskine449bd832023-01-11 14:50:10 +01004345 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004346 status = PSA_ERROR_BAD_STATE;
4347 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004348 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004349
Gilles Peskine449bd832023-01-11 14:50:10 +01004350 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004351 status = PSA_ERROR_BAD_STATE;
4352 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004353 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004354
Gilles Peskine449bd832023-01-11 14:50:10 +01004355 status = psa_driver_wrapper_cipher_finish(operation,
4356 output,
4357 output_size,
4358 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004359
4360exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004361 if (status == PSA_SUCCESS) {
4362 return psa_cipher_abort(operation);
4363 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004364 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004366
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004368 }
mohammad1603503973b2018-03-12 15:59:30 +02004369}
4370
Gilles Peskine449bd832023-01-11 14:50:10 +01004371psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004372{
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004374 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004375 * in use. It's ok to call abort on such an object, and there's
4376 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004377 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004378 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004379
Gilles Peskine449bd832023-01-11 14:50:10 +01004380 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004381
Ronald Cron49fafa92021-03-10 08:34:23 +01004382 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004383 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004384 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004385
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004387}
4388
Gilles Peskine449bd832023-01-11 14:50:10 +01004389psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4390 psa_algorithm_t alg,
4391 const uint8_t *input,
4392 size_t input_length,
4393 uint8_t *output,
4394 size_t output_size,
4395 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004396{
4397 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004398 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004399 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004400 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4401 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004402
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004404 status = PSA_ERROR_INVALID_ARGUMENT;
4405 goto exit;
4406 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004407
Gilles Peskine449bd832023-01-11 14:50:10 +01004408 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4409 PSA_KEY_USAGE_ENCRYPT,
4410 alg);
4411 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004412 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004413 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004414
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004415 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004416 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004417 };
4418
Gilles Peskine449bd832023-01-11 14:50:10 +01004419 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4420 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004421 status = PSA_ERROR_GENERIC_ERROR;
4422 goto exit;
4423 }
4424
Gilles Peskine449bd832023-01-11 14:50:10 +01004425 if (default_iv_length > 0) {
4426 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004427 status = PSA_ERROR_BUFFER_TOO_SMALL;
4428 goto exit;
4429 }
4430
Gilles Peskine449bd832023-01-11 14:50:10 +01004431 status = psa_generate_random(local_iv, default_iv_length);
4432 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004433 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004434 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004435 }
4436
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004437 status = psa_driver_wrapper_cipher_encrypt(
4438 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004439 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004440 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004442
4443exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 unlock_status = psa_unlock_key_slot(slot);
4445 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004446 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004447 }
Ronald Cron23919522021-07-08 19:00:07 +02004448
Gilles Peskine449bd832023-01-11 14:50:10 +01004449 if (status == PSA_SUCCESS) {
4450 if (default_iv_length > 0) {
4451 memcpy(output, local_iv, default_iv_length);
4452 }
4453 *output_length += default_iv_length;
4454 } else {
4455 *output_length = 0;
4456 }
4457
4458 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004459}
4460
Gilles Peskine449bd832023-01-11 14:50:10 +01004461psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4462 psa_algorithm_t alg,
4463 const uint8_t *input,
4464 size_t input_length,
4465 uint8_t *output,
4466 size_t output_size,
4467 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004468{
4469 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004470 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004471 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004472
Gilles Peskine449bd832023-01-11 14:50:10 +01004473 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004474 status = PSA_ERROR_INVALID_ARGUMENT;
4475 goto exit;
4476 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004477
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4479 PSA_KEY_USAGE_DECRYPT,
4480 alg);
4481 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004482 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004483 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004484
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004485 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004486 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004487 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004488
Gilles Peskine449bd832023-01-11 14:50:10 +01004489 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4490 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004491 status = PSA_ERROR_INVALID_ARGUMENT;
4492 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004493 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004494 status = PSA_ERROR_INVALID_ARGUMENT;
4495 goto exit;
4496 }
4497
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004498 status = psa_driver_wrapper_cipher_decrypt(
4499 &attributes, slot->key.data, slot->key.bytes,
4500 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004501 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004502
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004503exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 unlock_status = psa_unlock_key_slot(slot);
4505 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004506 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004508
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004510 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 }
Ronald Cron23919522021-07-08 19:00:07 +02004512
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004514}
4515
4516
mohammad16035955c982018-04-26 00:53:03 +03004517/****************************************************************/
4518/* AEAD */
4519/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004520
Paul Elliottbaff51c2021-09-28 17:44:45 +01004521/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004522static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004523{
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004525}
4526
4527/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004528static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4529 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004530{
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004532
Gilles Peskine449bd832023-01-11 14:50:10 +01004533 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004534#if defined(PSA_WANT_ALG_GCM)
4535 case PSA_ALG_GCM:
4536 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004537 * arbitrarily large nonces. Please note that we do not generally
4538 * recommend the usage of nonces of greater length than
4539 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4540 * size, which can then lead to collisions if you encrypt a very
4541 * large number of messages.*/
4542 if (nonce_length != 0) {
4543 return PSA_SUCCESS;
4544 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004545 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004546#endif /* PSA_WANT_ALG_GCM */
4547#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004548 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 if (nonce_length >= 7 && nonce_length <= 13) {
4550 return PSA_SUCCESS;
4551 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004552 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004553#endif /* PSA_WANT_ALG_CCM */
4554#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004555 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 if (nonce_length == 12) {
4557 return PSA_SUCCESS;
4558 } else if (nonce_length == 8) {
4559 return PSA_ERROR_NOT_SUPPORTED;
4560 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004561 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004562#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004563 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004564 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004566 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004567
Gilles Peskine449bd832023-01-11 14:50:10 +01004568 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004569}
4570
Gilles Peskine449bd832023-01-11 14:50:10 +01004571static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004572{
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4574 return PSA_ERROR_INVALID_ARGUMENT;
4575 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004576
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004578}
4579
Gilles Peskine449bd832023-01-11 14:50:10 +01004580psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4581 psa_algorithm_t alg,
4582 const uint8_t *nonce,
4583 size_t nonce_length,
4584 const uint8_t *additional_data,
4585 size_t additional_data_length,
4586 const uint8_t *plaintext,
4587 size_t plaintext_length,
4588 uint8_t *ciphertext,
4589 size_t ciphertext_size,
4590 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004591{
Ronald Cron215633c2021-03-16 17:15:37 +01004592 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4593 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004594
mohammad1603f08a5502018-06-03 15:05:47 +03004595 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004596
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 status = psa_aead_check_algorithm(alg);
4598 if (status != PSA_SUCCESS) {
4599 return status;
4600 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004601
Ronald Cron9a986162021-03-26 12:40:07 +01004602 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004603 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4604 if (status != PSA_SUCCESS) {
4605 return status;
4606 }
mohammad16035955c982018-04-26 00:53:03 +03004607
Ronald Cron215633c2021-03-16 17:15:37 +01004608 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004609 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004610 };
mohammad16035955c982018-04-26 00:53:03 +03004611
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 status = psa_aead_check_nonce_length(alg, nonce_length);
4613 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004614 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004615 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004616
Ronald Cronde822812021-03-17 16:08:20 +01004617 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004618 &attributes, slot->key.data, slot->key.bytes,
4619 alg,
4620 nonce, nonce_length,
4621 additional_data, additional_data_length,
4622 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004624
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4626 memset(ciphertext, 0, ciphertext_size);
4627 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004628
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004629exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004631
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004633}
4634
Gilles Peskine449bd832023-01-11 14:50:10 +01004635psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4636 psa_algorithm_t alg,
4637 const uint8_t *nonce,
4638 size_t nonce_length,
4639 const uint8_t *additional_data,
4640 size_t additional_data_length,
4641 const uint8_t *ciphertext,
4642 size_t ciphertext_length,
4643 uint8_t *plaintext,
4644 size_t plaintext_size,
4645 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004646{
Ronald Cron215633c2021-03-16 17:15:37 +01004647 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4648 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004649
Gilles Peskineee652a32018-06-01 19:23:52 +02004650 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004651
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 status = psa_aead_check_algorithm(alg);
4653 if (status != PSA_SUCCESS) {
4654 return status;
4655 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004656
Ronald Cron9a986162021-03-26 12:40:07 +01004657 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4659 if (status != PSA_SUCCESS) {
4660 return status;
4661 }
mohammad16035955c982018-04-26 00:53:03 +03004662
Ronald Cron215633c2021-03-16 17:15:37 +01004663 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004665 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004666
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 status = psa_aead_check_nonce_length(alg, nonce_length);
4668 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004669 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004670 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004671
Ronald Cronde822812021-03-17 16:08:20 +01004672 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004673 &attributes, slot->key.data, slot->key.bytes,
4674 alg,
4675 nonce, nonce_length,
4676 additional_data, additional_data_length,
4677 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004679
Gilles Peskine449bd832023-01-11 14:50:10 +01004680 if (status != PSA_SUCCESS && plaintext_size != 0) {
4681 memset(plaintext, 0, plaintext_size);
4682 }
mohammad160360a64d02018-06-03 17:20:42 +03004683
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004684exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004686
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 return status;
mohammad16035955c982018-04-26 00:53:03 +03004688}
4689
Gilles Peskine449bd832023-01-11 14:50:10 +01004690static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4691{
4692 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004693
Gilles Peskine449bd832023-01-11 14:50:10 +01004694 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004695#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004697 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4699 return PSA_ERROR_INVALID_ARGUMENT;
4700 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004701 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004702#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004703
Przemek Stekiel86679c72022-10-06 17:06:56 +02004704#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004706 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004707 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4708 return PSA_ERROR_INVALID_ARGUMENT;
4709 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004710 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004711#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004712
Przemek Stekiel86679c72022-10-06 17:06:56 +02004713#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004715 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 if (tag_len != 16) {
4717 return PSA_ERROR_INVALID_ARGUMENT;
4718 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004719 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004720#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004721
4722 default:
4723 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004725 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004727}
4728
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004729/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004730static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4731 int is_encrypt,
4732 mbedtls_svc_key_id_t key,
4733 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004734{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004735 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4736 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4737 psa_key_slot_t *slot = NULL;
4738 psa_key_usage_t key_usage = 0;
4739
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 status = psa_aead_check_algorithm(alg);
4741 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004742 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004743 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004744
Gilles Peskine449bd832023-01-11 14:50:10 +01004745 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004746 status = PSA_ERROR_BAD_STATE;
4747 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004748 }
4749
Gilles Peskine449bd832023-01-11 14:50:10 +01004750 if (operation->nonce_set || operation->lengths_set ||
4751 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004752 status = PSA_ERROR_BAD_STATE;
4753 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004754 }
4755
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004757 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004759 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004761
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4763 alg);
4764 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004765 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004766 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004767
4768 psa_key_attributes_t attributes = {
4769 .core = slot->attr
4770 };
4771
Gilles Peskine449bd832023-01-11 14:50:10 +01004772 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004773 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004774 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004775
Gilles Peskine449bd832023-01-11 14:50:10 +01004776 if (is_encrypt) {
4777 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4778 &attributes,
4779 slot->key.data,
4780 slot->key.bytes,
4781 alg);
4782 } else {
4783 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4784 &attributes,
4785 slot->key.data,
4786 slot->key.bytes,
4787 alg);
4788 }
4789 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004790 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004791 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004792
Gilles Peskine449bd832023-01-11 14:50:10 +01004793 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004794
4795exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004796 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004797
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004799 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004800 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004801 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004802 } else {
4803 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004804 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004805
Gilles Peskine449bd832023-01-11 14:50:10 +01004806 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004807}
4808
Paul Elliott302ff6b2021-04-20 18:10:30 +01004809/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004810psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4811 mbedtls_svc_key_id_t key,
4812 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004813{
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004815}
4816
4817/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004818psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4819 mbedtls_svc_key_id_t key,
4820 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004821{
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004823}
4824
4825/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004826psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4827 uint8_t *nonce,
4828 size_t nonce_size,
4829 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004830{
4831 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004832 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004833 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004834
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004835 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004836
Gilles Peskine449bd832023-01-11 14:50:10 +01004837 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004838 status = PSA_ERROR_BAD_STATE;
4839 goto exit;
4840 }
4841
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004843 status = PSA_ERROR_BAD_STATE;
4844 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004845 }
4846
Paul Elliotte193ea82021-10-01 13:00:16 +01004847 /* For CCM, this size may not be correct according to the PSA
4848 * specification. The PSA Crypto 1.0.1 specification states:
4849 *
4850 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4851 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4852 *
4853 * However this restriction that L has to be the smallest integer is not
4854 * applied in practice, and it is not implementable here since the
4855 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004856 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4857 operation->alg);
4858 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004859 status = PSA_ERROR_BUFFER_TOO_SMALL;
4860 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004861 }
4862
Gilles Peskine449bd832023-01-11 14:50:10 +01004863 status = psa_generate_random(local_nonce, required_nonce_size);
4864 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004865 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004866 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004869
Paul Elliott39dc6b82021-05-11 19:16:09 +01004870exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 if (status == PSA_SUCCESS) {
4872 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004873 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 } else {
4875 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004876 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004877
Gilles Peskine449bd832023-01-11 14:50:10 +01004878 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004879}
4880
4881/* Set the nonce for a multipart authenticated encryption or decryption
4882 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004883psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4884 const uint8_t *nonce,
4885 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004886{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004887 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4888
Gilles Peskine449bd832023-01-11 14:50:10 +01004889 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004890 status = PSA_ERROR_BAD_STATE;
4891 goto exit;
4892 }
4893
Gilles Peskine449bd832023-01-11 14:50:10 +01004894 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004895 status = PSA_ERROR_BAD_STATE;
4896 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004897 }
4898
Gilles Peskine449bd832023-01-11 14:50:10 +01004899 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4900 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004901 status = PSA_ERROR_INVALID_ARGUMENT;
4902 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004903 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004904
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4906 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004907
Paul Elliott39dc6b82021-05-11 19:16:09 +01004908exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004909 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004910 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 } else {
4912 psa_aead_abort(operation);
4913 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004914
Gilles Peskine449bd832023-01-11 14:50:10 +01004915 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004916}
4917
4918/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004919psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4920 size_t ad_length,
4921 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004922{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004923 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4924
Gilles Peskine449bd832023-01-11 14:50:10 +01004925 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004926 status = PSA_ERROR_BAD_STATE;
4927 goto exit;
4928 }
4929
Gilles Peskine449bd832023-01-11 14:50:10 +01004930 if (operation->lengths_set || operation->ad_started ||
4931 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004932 status = PSA_ERROR_BAD_STATE;
4933 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004934 }
4935
Gilles Peskine449bd832023-01-11 14:50:10 +01004936 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004937#if defined(PSA_WANT_ALG_GCM)
4938 case PSA_ALG_GCM:
4939 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004940 * bits. Without the guard this code will generate warnings on 32bit
4941 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004942#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 if (((uint64_t) ad_length) >> 61 != 0 ||
4944 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004945 status = PSA_ERROR_INVALID_ARGUMENT;
4946 goto exit;
4947 }
Paul Elliott325d3742021-09-27 17:56:28 +01004948#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004949 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004950#endif /* PSA_WANT_ALG_GCM */
4951#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004952 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004954 status = PSA_ERROR_INVALID_ARGUMENT;
4955 goto exit;
4956 }
4957 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004958#endif /* PSA_WANT_ALG_CCM */
4959#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004960 case PSA_ALG_CHACHA20_POLY1305:
4961 /* No length restrictions for ChaChaPoly. */
4962 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004963#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004964 default:
4965 break;
4966 }
Paul Elliott325d3742021-09-27 17:56:28 +01004967
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4969 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004970
Paul Elliott39dc6b82021-05-11 19:16:09 +01004971exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004973 operation->ad_remaining = ad_length;
4974 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004975 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004976 } else {
4977 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004978 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004979
Gilles Peskine449bd832023-01-11 14:50:10 +01004980 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004981}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004982
4983/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004984psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4985 const uint8_t *input,
4986 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004987{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004988 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4989
Gilles Peskine449bd832023-01-11 14:50:10 +01004990 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004991 status = PSA_ERROR_BAD_STATE;
4992 goto exit;
4993 }
4994
Gilles Peskine449bd832023-01-11 14:50:10 +01004995 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004996 status = PSA_ERROR_BAD_STATE;
4997 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004998 }
4999
Gilles Peskine449bd832023-01-11 14:50:10 +01005000 if (operation->lengths_set) {
5001 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005002 status = PSA_ERROR_INVALID_ARGUMENT;
5003 goto exit;
5004 }
5005
5006 operation->ad_remaining -= input_length;
5007 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005008#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005009 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005010 status = PSA_ERROR_BAD_STATE;
5011 goto exit;
5012 }
5013#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005014
Gilles Peskine449bd832023-01-11 14:50:10 +01005015 status = psa_driver_wrapper_aead_update_ad(operation, input,
5016 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005017
Paul Elliott39dc6b82021-05-11 19:16:09 +01005018exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005020 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 } else {
5022 psa_aead_abort(operation);
5023 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005024
Gilles Peskine449bd832023-01-11 14:50:10 +01005025 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005026}
5027
5028/* Encrypt or decrypt a message fragment in an active multipart AEAD
5029 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005030psa_status_t psa_aead_update(psa_aead_operation_t *operation,
5031 const uint8_t *input,
5032 size_t input_length,
5033 uint8_t *output,
5034 size_t output_size,
5035 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005036{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005037 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005038
5039 *output_length = 0;
5040
Gilles Peskine449bd832023-01-11 14:50:10 +01005041 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005042 status = PSA_ERROR_BAD_STATE;
5043 goto exit;
5044 }
5045
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005047 status = PSA_ERROR_BAD_STATE;
5048 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005049 }
5050
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005052 /* Additional data length was supplied, but not all the additional
5053 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005054 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005055 status = PSA_ERROR_INVALID_ARGUMENT;
5056 goto exit;
5057 }
5058
5059 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005061 status = PSA_ERROR_INVALID_ARGUMENT;
5062 goto exit;
5063 }
5064
5065 operation->body_remaining -= input_length;
5066 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005067#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005069 status = PSA_ERROR_BAD_STATE;
5070 goto exit;
5071 }
5072#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005073
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5075 output, output_size,
5076 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005077
Paul Elliott39dc6b82021-05-11 19:16:09 +01005078exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005079 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005080 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 } else {
5082 psa_aead_abort(operation);
5083 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005084
Gilles Peskine449bd832023-01-11 14:50:10 +01005085 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005086}
5087
Gilles Peskine449bd832023-01-11 14:50:10 +01005088static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005089{
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 if (operation->id == 0 || !operation->nonce_set) {
5091 return PSA_ERROR_BAD_STATE;
5092 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005093
Gilles Peskine449bd832023-01-11 14:50:10 +01005094 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5095 operation->body_remaining != 0)) {
5096 return PSA_ERROR_INVALID_ARGUMENT;
5097 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005098
Gilles Peskine449bd832023-01-11 14:50:10 +01005099 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005100}
5101
Paul Elliott302ff6b2021-04-20 18:10:30 +01005102/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005103psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5104 uint8_t *ciphertext,
5105 size_t ciphertext_size,
5106 size_t *ciphertext_length,
5107 uint8_t *tag,
5108 size_t tag_size,
5109 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005110{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005111 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5112
Paul Elliott302ff6b2021-04-20 18:10:30 +01005113 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005114 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005115
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 status = psa_aead_final_checks(operation);
5117 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005118 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005120
Gilles Peskine449bd832023-01-11 14:50:10 +01005121 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005122 status = PSA_ERROR_BAD_STATE;
5123 goto exit;
5124 }
5125
Gilles Peskine449bd832023-01-11 14:50:10 +01005126 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5127 ciphertext_size,
5128 ciphertext_length,
5129 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005130
Paul Elliott39dc6b82021-05-11 19:16:09 +01005131exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005132
5133
Paul Elliottf47b0952021-05-21 18:02:33 +01005134 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005135 * the zero tag size, make sure the tag is set to something implausible.
5136 * Even if the operation succeeds, make sure we clear the rest of the
5137 * buffer to prevent potential leakage of anything previously placed in
5138 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005139 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005140
Gilles Peskine449bd832023-01-11 14:50:10 +01005141 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005142
Gilles Peskine449bd832023-01-11 14:50:10 +01005143 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005144}
5145
5146/* Finish authenticating and decrypting a message in a multipart AEAD
5147 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005148psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5149 uint8_t *plaintext,
5150 size_t plaintext_size,
5151 size_t *plaintext_length,
5152 const uint8_t *tag,
5153 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005154{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005155 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5156
Paul Elliott302ff6b2021-04-20 18:10:30 +01005157 *plaintext_length = 0;
5158
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 status = psa_aead_final_checks(operation);
5160 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005161 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005163
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005165 status = PSA_ERROR_BAD_STATE;
5166 goto exit;
5167 }
5168
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5170 plaintext_size,
5171 plaintext_length,
5172 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005173
5174exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005175 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005176
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005178}
5179
5180/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005181psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005182{
5183 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5184
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005186 /* The object has (apparently) been initialized but it is not (yet)
5187 * in use. It's ok to call abort on such an object, and there's
5188 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005190 }
5191
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005193
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005195
Gilles Peskine449bd832023-01-11 14:50:10 +01005196 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005197}
5198
Gilles Peskinee59236f2018-01-27 23:32:46 +01005199/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005200/* Generators */
5201/****************************************************************/
5202
Przemek Stekiel69c46792022-06-10 12:59:51 +02005203#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005204 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005205 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
5206 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08005207#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005208#endif /* At least one builtin KDF */
5209
Przemek Stekiel69c46792022-06-10 12:59:51 +02005210#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005211 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5212 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5213static psa_status_t psa_key_derivation_start_hmac(
5214 psa_mac_operation_t *operation,
5215 psa_algorithm_t hash_alg,
5216 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005217 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005218{
5219 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5220 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5222 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5223 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005224
Steven Cooreman72f736a2021-05-07 14:14:37 +02005225 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005227
Gilles Peskine449bd832023-01-11 14:50:10 +01005228 status = psa_driver_wrapper_mac_sign_setup(operation,
5229 &attributes,
5230 hmac_key, hmac_key_length,
5231 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005232
Gilles Peskine449bd832023-01-11 14:50:10 +01005233 psa_reset_key_attributes(&attributes);
5234 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005235}
5236#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005237
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005238#define HKDF_STATE_INIT 0 /* no input yet */
5239#define HKDF_STATE_STARTED 1 /* got salt */
5240#define HKDF_STATE_KEYED 2 /* got key */
5241#define HKDF_STATE_OUTPUT 3 /* output started */
5242
Gilles Peskinecbe66502019-05-16 16:59:18 +02005243static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005245{
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5247 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5248 } else {
5249 return operation->alg;
5250 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005251}
5252
Gilles Peskine449bd832023-01-11 14:50:10 +01005253psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005254{
5255 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5257 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005258 /* The object has (apparently) been initialized but it is not
5259 * in use. It's ok to call abort on such an object, and there's
5260 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005261 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005262#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5264 mbedtls_free(operation->ctx.hkdf.info);
5265 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5266 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005267#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005268#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5269 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5271 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5272 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5273 if (operation->ctx.tls12_prf.secret != NULL) {
5274 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5275 operation->ctx.tls12_prf.secret_length);
5276 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005277 }
5278
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 if (operation->ctx.tls12_prf.seed != NULL) {
5280 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5281 operation->ctx.tls12_prf.seed_length);
5282 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005283 }
5284
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 if (operation->ctx.tls12_prf.label != NULL) {
5286 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5287 operation->ctx.tls12_prf.label_length);
5288 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005289 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005290#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 if (operation->ctx.tls12_prf.other_secret != NULL) {
5292 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5293 operation->ctx.tls12_prf.other_secret_length);
5294 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005295 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005296#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005297 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005298
5299 /* We leave the fields Ai and output_block to be erased safely by the
5300 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005302#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5303 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005304#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005305 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5306 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5307 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5308 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005309#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005310 {
5311 status = PSA_ERROR_BAD_STATE;
5312 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 mbedtls_platform_zeroize(operation, sizeof(*operation));
5314 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005315}
5316
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005317psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005319{
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005321 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005323 }
5324
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005325 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005327}
5328
Gilles Peskine449bd832023-01-11 14:50:10 +01005329psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5330 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005331{
Gilles Peskine449bd832023-01-11 14:50:10 +01005332 if (operation->alg == 0) {
5333 return PSA_ERROR_BAD_STATE;
5334 }
5335 if (capacity > operation->capacity) {
5336 return PSA_ERROR_INVALID_ARGUMENT;
5337 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005338 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005340}
5341
Przemek Stekiel69c46792022-06-10 12:59:51 +02005342#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005343/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005344static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5345 psa_algorithm_t kdf_alg,
5346 uint8_t *output,
5347 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005348{
Gilles Peskine449bd832023-01-11 14:50:10 +01005349 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5350 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005351 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005352 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005353#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005355#else
5356 const uint8_t last_block = 0xff;
5357#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005358
Gilles Peskine449bd832023-01-11 14:50:10 +01005359 if (hkdf->state < HKDF_STATE_KEYED ||
5360 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005361#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005363#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005364 )) {
5365 return PSA_ERROR_BAD_STATE;
5366 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005367 hkdf->state = HKDF_STATE_OUTPUT;
5368
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005370 /* Copy what remains of the current block */
5371 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005373 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005374 }
5375 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005376 output += n;
5377 output_length -= n;
5378 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005380 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005382 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005383 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005384 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005385 * object was corrupted or if this function is called directly
5386 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 if (hkdf->block_number == last_block) {
5388 return PSA_ERROR_BAD_STATE;
5389 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005390
5391 /* We need a new block */
5392 ++hkdf->block_number;
5393 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005394
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5396 hash_alg,
5397 hkdf->prk,
5398 hash_length);
5399 if (status != PSA_SUCCESS) {
5400 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005401 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005402
5403 if (hkdf->block_number != 1) {
5404 status = psa_mac_update(&hkdf->hmac,
5405 hkdf->output_block,
5406 hash_length);
5407 if (status != PSA_SUCCESS) {
5408 return status;
5409 }
5410 }
5411 status = psa_mac_update(&hkdf->hmac,
5412 hkdf->info,
5413 hkdf->info_length);
5414 if (status != PSA_SUCCESS) {
5415 return status;
5416 }
5417 status = psa_mac_update(&hkdf->hmac,
5418 &hkdf->block_number, 1);
5419 if (status != PSA_SUCCESS) {
5420 return status;
5421 }
5422 status = psa_mac_sign_finish(&hkdf->hmac,
5423 hkdf->output_block,
5424 sizeof(hkdf->output_block),
5425 &hmac_output_length);
5426 if (status != PSA_SUCCESS) {
5427 return status;
5428 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005429 }
5430
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005432}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005433#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005434
John Durkop07cc04a2020-11-16 22:08:34 -08005435#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5436 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005437static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5438 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005440{
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5442 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005443 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5444 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005445 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005446
Janos Follath7742fee2019-06-17 12:58:10 +01005447 /* We can't be wanting more output after block 0xff, otherwise
5448 * the capacity check in psa_key_derivation_output_bytes() would have
5449 * prevented this call. It could happen only if the operation
5450 * object was corrupted or if this function is called directly
5451 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 if (tls12_prf->block_number == 0xff) {
5453 return PSA_ERROR_CORRUPTION_DETECTED;
5454 }
Janos Follath7742fee2019-06-17 12:58:10 +01005455
5456 /* We need a new block */
5457 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005458 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005459
5460 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5461 *
5462 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5463 *
5464 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5465 * HMAC_hash(secret, A(2) + seed) +
5466 * HMAC_hash(secret, A(3) + seed) + ...
5467 *
5468 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005469 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005470 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005471 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005472 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005473 * is currently extracted as `output_block` and where i is
5474 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005475 */
5476
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 status = psa_key_derivation_start_hmac(&hmac,
5478 hash_alg,
5479 tls12_prf->secret,
5480 tls12_prf->secret_length);
5481 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005482 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005484
5485 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005487 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5488 * the variable seed and in this instance means it in the context of the
5489 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005490 status = psa_mac_update(&hmac,
5491 tls12_prf->label,
5492 tls12_prf->label_length);
5493 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005494 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 }
5496 status = psa_mac_update(&hmac,
5497 tls12_prf->seed,
5498 tls12_prf->seed_length);
5499 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005500 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 }
5502 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005503 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5505 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005506 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005508 }
5509
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 status = psa_mac_sign_finish(&hmac,
5511 tls12_prf->Ai, hash_length,
5512 &hmac_output_length);
5513 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005514 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 }
5516 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005517 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005518 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005519
5520 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 status = psa_key_derivation_start_hmac(&hmac,
5522 hash_alg,
5523 tls12_prf->secret,
5524 tls12_prf->secret_length);
5525 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005526 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 }
5528 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5529 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005530 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 }
5532 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5533 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005534 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 }
5536 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5537 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005538 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005539 }
5540 status = psa_mac_sign_finish(&hmac,
5541 tls12_prf->output_block, hash_length,
5542 &hmac_output_length);
5543 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005544 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005546
Janos Follath7742fee2019-06-17 12:58:10 +01005547
5548cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 cleanup_status = psa_mac_abort(&hmac);
5550 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005551 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005553
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005555}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005556
Janos Follath844eb0e2019-06-19 12:10:49 +01005557static psa_status_t psa_key_derivation_tls12_prf_read(
5558 psa_tls12_prf_key_derivation_t *tls12_prf,
5559 psa_algorithm_t alg,
5560 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005561 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005562{
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5564 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005565 psa_status_t status;
5566 uint8_t offset, length;
5567
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005569 case PSA_TLS12_PRF_STATE_LABEL_SET:
5570 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5571 break;
5572 case PSA_TLS12_PRF_STATE_OUTPUT:
5573 break;
5574 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005576 }
5577
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005579 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 if (tls12_prf->left_in_block == 0) {
5581 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5582 alg);
5583 if (status != PSA_SUCCESS) {
5584 return status;
5585 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005586
5587 continue;
5588 }
5589
Gilles Peskine449bd832023-01-11 14:50:10 +01005590 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005591 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005593 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005594 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005595
5596 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005598 output += length;
5599 output_length -= length;
5600 tls12_prf->left_in_block -= length;
5601 }
5602
Gilles Peskine449bd832023-01-11 14:50:10 +01005603 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005604}
John Durkop07cc04a2020-11-16 22:08:34 -08005605#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5606 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005607
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005608#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5609static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5610 psa_tls12_ecjpake_to_pms_t *ecjpake,
5611 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005612 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005613{
5614 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005615 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005616
Gilles Peskine449bd832023-01-11 14:50:10 +01005617 if (output_length != 32) {
5618 return PSA_ERROR_INVALID_ARGUMENT;
5619 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005620
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5622 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5623 &output_size);
5624 if (status != PSA_SUCCESS) {
5625 return status;
5626 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005627
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 if (output_size != output_length) {
5629 return PSA_ERROR_GENERIC_ERROR;
5630 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005631
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005633}
5634#endif
5635
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005636psa_status_t psa_key_derivation_output_bytes(
5637 psa_key_derivation_operation_t *operation,
5638 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005640{
5641 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005643
Gilles Peskine449bd832023-01-11 14:50:10 +01005644 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005645 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005646 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005647 }
5648
Gilles Peskine449bd832023-01-11 14:50:10 +01005649 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005650 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005651 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005652 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005653 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005654 goto exit;
5655 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005656 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005657 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005658 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005659 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5660 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005661 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005662 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005663 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005664 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005665 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005666
Przemek Stekiel69c46792022-06-10 12:59:51 +02005667#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005668 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5669 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5670 output, output_length);
5671 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005672#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005673#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5674 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005675 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5676 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5677 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5678 kdf_alg, output,
5679 output_length);
5680 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005681#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5682 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005683#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005685 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5687 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005688#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5689
Gilles Peskineeab56e42018-07-12 17:12:33 +02005690 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005691 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005693 }
5694
5695exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005696 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005697 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005698 * This allows us to differentiate between exhausted operations and
5699 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5700 * operations. */
5701 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005702 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005703 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005705 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005706 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005707}
5708
David Brown7807bf72021-02-09 16:28:23 -07005709#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005710static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005711{
Gilles Peskine449bd832023-01-11 14:50:10 +01005712 if (data_size >= 8) {
5713 mbedtls_des_key_set_parity(data);
5714 }
5715 if (data_size >= 16) {
5716 mbedtls_des_key_set_parity(data + 8);
5717 }
5718 if (data_size >= 24) {
5719 mbedtls_des_key_set_parity(data + 16);
5720 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005721}
David Brown7807bf72021-02-09 16:28:23 -07005722#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005723
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005724/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005725 * ECC keys on a Weierstrass elliptic curve require the generation
5726 * of a private key which is an integer
5727 * in the range [1, N - 1], where N is the boundary of the private key domain:
5728 * N is the prime p for Diffie-Hellman, or the order of the
5729 * curve’s base point for ECC.
5730 *
5731 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5732 * This function generates the private key using the following process:
5733 *
5734 * 1. Draw a byte string of length ceiling(m/8) bytes.
5735 * 2. If m is not a multiple of 8, set the most significant
5736 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5737 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5738 * 4. If k > N - 2, discard the result and return to step 1.
5739 * 5. Output k + 1 as the private key.
5740 *
5741 * This method allows compliance to NIST standards, specifically the methods titled
5742 * Key-Pair Generation by Testing Candidates in the following publications:
5743 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5744 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5745 * Diffie-Hellman keys.
5746 *
5747 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5748 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5749 *
5750 * Note: Function allocates memory for *data buffer, so given *data should be
5751 * always NULL.
5752 */
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005753#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5754 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005755 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5756 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5757 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005758static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005759 psa_key_slot_t *slot,
5760 size_t bits,
5761 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005762 uint8_t **data
5763 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005764{
Valerio Setti52789862023-04-07 12:13:11 +02005765#if defined(MBEDTLS_ECP_LIGHT)
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005766 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005767 mbedtls_mpi k;
5768 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005769 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005770 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005771
Gilles Peskine449bd832023-01-11 14:50:10 +01005772 mbedtls_mpi_init(&k);
5773 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005774
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005775 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005776 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005777 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005779
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005781 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005782 goto cleanup;
5783 }
5784
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005785 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005787
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005789
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005790 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005791 /* Let m be the bit size of N. */
5792 size_t m = ecp_group.nbits;
5793
Gilles Peskine449bd832023-01-11 14:50:10 +01005794 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005795
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005796 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005797 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005798
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005799 /* Note: This function is always called with *data == NULL and it
5800 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 *data = mbedtls_calloc(1, m_bytes);
5802 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005803 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005804 goto cleanup;
5805 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005806
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005808 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005810 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005812
5813 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005814 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005815 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005816 * (8 * ceiling(m/8) - m) bits of the first byte in
5817 * the string to zero.
5818 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005819 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005820 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005821 }
5822
5823 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 * big-endian byte string.
5825 */
5826 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005827
5828 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005829 * Result of comparison is returned. When it indicates error
5830 * then this function is called again.
5831 */
5832 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005833 }
5834
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005835 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005836 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5837 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005838cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005839 if (ret != 0) {
5840 status = mbedtls_to_psa_error(ret);
5841 }
5842 if (status != PSA_SUCCESS) {
5843 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005844 *data = NULL;
5845 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005846 mbedtls_mpi_free(&k);
5847 mbedtls_mpi_free(&diff_N_2);
5848 return status;
Valerio Setti52789862023-04-07 12:13:11 +02005849#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005850 (void) slot;
5851 (void) bits;
5852 (void) operation;
5853 (void) data;
5854 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti52789862023-04-07 12:13:11 +02005855#endif /* MBEDTLS_ECP_LIGHT */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005856}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005857
5858/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5859 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5860 *
5861 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5862 * draw a 32-byte string and process it as specified in
5863 * Elliptic Curves for Security [RFC7748] §5.
5864 *
5865 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5866 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5867 *
5868 * Note: Function allocates memory for *data buffer, so given *data should be
5869 * always NULL.
5870 */
5871
5872static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5873 size_t bits,
5874 psa_key_derivation_operation_t *operation,
5875 uint8_t **data
5876 )
5877{
5878 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005879 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005880
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005882 case 255:
5883 output_length = 32;
5884 break;
5885 case 448:
5886 output_length = 56;
5887 break;
5888 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005889 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005890 break;
5891 }
5892
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005894
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 if (*data == NULL) {
5896 return PSA_ERROR_INSUFFICIENT_MEMORY;
5897 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005898
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005900
Gilles Peskine449bd832023-01-11 14:50:10 +01005901 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005902 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005903 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005904
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005906 case 255:
5907 (*data)[0] &= 248;
5908 (*data)[31] &= 127;
5909 (*data)[31] |= 64;
5910 break;
5911 case 448:
5912 (*data)[0] &= 252;
5913 (*data)[55] |= 128;
5914 break;
5915 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005917 break;
5918 }
5919
5920 return status;
5921}
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005922#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5923 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005924 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5925 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5926 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005927
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005928static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005929 psa_key_slot_t *slot,
5930 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005932{
5933 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305935 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005936 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005937
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5939 return PSA_ERROR_INVALID_ARGUMENT;
5940 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005941
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005942#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5943 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005944 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5945 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5946 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5948 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5949 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005950 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5952 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005953 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 }
5955 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005956 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005957 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5958 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005959 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005960 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005961 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005962 } else
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005963#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5964 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005965 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5966 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5967 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005968 if (key_type_is_raw_bytes(slot->attr.type)) {
5969 if (bits % 8 != 0) {
5970 return PSA_ERROR_INVALID_ARGUMENT;
5971 }
5972 data = mbedtls_calloc(1, bytes);
5973 if (data == NULL) {
5974 return PSA_ERROR_INSUFFICIENT_MEMORY;
5975 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005976
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 status = psa_key_derivation_output_bytes(operation, data, bytes);
5978 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005979 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 }
David Brown12ca5032021-02-11 11:02:00 -07005981#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005982 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5983 psa_des_set_key_parity(data, bytes);
5984 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005985#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 } else {
5987 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005988 }
Ronald Crondd04d422020-11-28 15:14:42 +01005989
Ronald Cronbf33c932020-11-28 18:06:53 +01005990 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005991 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005992 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005993 };
5994
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5996 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5997 &storage_size);
5998 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305999 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 }
Archanad8a83dc2021-06-14 10:04:16 +05306001 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006002 status = psa_allocate_buffer_to_slot(slot, storage_size);
6003 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306004 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006005 }
Archanad8a83dc2021-06-14 10:04:16 +05306006
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 status = psa_driver_wrapper_import_key(&attributes,
6008 data, bytes,
6009 slot->key.data,
6010 slot->key.bytes,
6011 &slot->key.bytes, &bits);
6012 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006013 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006014 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006015
6016exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006017 mbedtls_free(data);
6018 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006019}
6020
Gilles Peskine449bd832023-01-11 14:50:10 +01006021psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6022 psa_key_derivation_operation_t *operation,
6023 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006024{
6025 psa_status_t status;
6026 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006027 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006028
Ronald Cron81709fc2020-11-14 12:10:32 +01006029 *key = MBEDTLS_SVC_KEY_ID_INIT;
6030
Gilles Peskine0f84d622019-09-12 19:03:13 +02006031 /* Reject any attempt to create a zero-length key so that we don't
6032 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 if (psa_get_key_bits(attributes) == 0) {
6034 return PSA_ERROR_INVALID_ARGUMENT;
6035 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006036
Gilles Peskine449bd832023-01-11 14:50:10 +01006037 if (operation->alg == PSA_ALG_NONE) {
6038 return PSA_ERROR_BAD_STATE;
6039 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006040
Gilles Peskine449bd832023-01-11 14:50:10 +01006041 if (!operation->can_output_key) {
6042 return PSA_ERROR_NOT_PERMITTED;
6043 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006044
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6046 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006047#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006049 /* Deriving a key in a secure element is not implemented yet. */
6050 status = PSA_ERROR_NOT_SUPPORTED;
6051 }
6052#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 if (status == PSA_SUCCESS) {
6054 status = psa_generate_derived_key_internal(slot,
6055 attributes->core.bits,
6056 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006057 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 if (status == PSA_SUCCESS) {
6059 status = psa_finish_key_creation(slot, driver, key);
6060 }
6061 if (status != PSA_SUCCESS) {
6062 psa_fail_key_creation(slot, driver);
6063 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006064
Gilles Peskine449bd832023-01-11 14:50:10 +01006065 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006066}
6067
Gilles Peskineeab56e42018-07-12 17:12:33 +02006068
6069
6070/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006071/* Key derivation */
6072/****************************************************************/
6073
Steven Cooreman932ffb72021-02-15 12:14:32 +01006074#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006075static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006076{
6077#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6079 return 1;
6080 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006081#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006082#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6084 return 1;
6085 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006086#endif
6087#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6089 return 1;
6090 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006091#endif
6092#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6094 return 1;
6095 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006096#endif
6097#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6099 return 1;
6100 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006101#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006102#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6104 return 1;
6105 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006106#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006108}
6109
Gilles Peskine449bd832023-01-11 14:50:10 +01006110static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006111{
6112 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 psa_status_t status = psa_hash_setup(&operation, alg);
6114 psa_hash_abort(&operation);
6115 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006116}
6117
Gilles Peskine969c5d62019-01-16 15:53:06 +01006118static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006119 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006121{
Janos Follath5fe19732019-06-20 15:09:30 +01006122 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6123 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006124 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006125
Gilles Peskine969c5d62019-01-16 15:53:06 +01006126 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006127 if (!is_kdf_alg_supported(kdf_alg)) {
6128 return PSA_ERROR_NOT_SUPPORTED;
6129 }
John Durkop07cc04a2020-11-16 22:08:34 -08006130
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006131 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04006132 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6134 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
6135 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6136 if (hash_size == 0) {
6137 return PSA_ERROR_NOT_SUPPORTED;
6138 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006139
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006140 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6141 * we might fail later, which is somewhat unfriendly and potentially
6142 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 psa_status_t status = psa_hash_try_support(hash_alg);
6144 if (status != PSA_SUCCESS) {
6145 return status;
6146 }
6147 } else {
6148 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006149 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006150
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6152 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6153 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6154 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006155 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006156#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006157 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006158 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6159 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006160 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006162#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6163 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 operation->capacity = 255 * hash_size;
6165 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006166}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006167
Gilles Peskine449bd832023-01-11 14:50:10 +01006168static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006169{
6170#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006171 if (alg == PSA_ALG_ECDH) {
6172 return PSA_SUCCESS;
6173 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006174#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006175#if defined(PSA_WANT_ALG_FFDH)
6176 if (alg == PSA_ALG_FFDH) {
6177 return PSA_SUCCESS;
6178 }
6179#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006180 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006182}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006183
6184static int psa_key_derivation_allows_free_form_secret_input(
6185 psa_algorithm_t kdf_alg)
6186{
6187#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6188 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6189 return 0;
6190 }
6191#endif
6192 (void) kdf_alg;
6193 return 1;
6194}
John Durkop07cc04a2020-11-16 22:08:34 -08006195#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006196
Gilles Peskine449bd832023-01-11 14:50:10 +01006197psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6198 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006199{
6200 psa_status_t status;
6201
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 if (operation->alg != 0) {
6203 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006204 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006205
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6207 return PSA_ERROR_INVALID_ARGUMENT;
6208 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6209#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6210 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6211 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6212 status = psa_key_agreement_try_support(ka_alg);
6213 if (status != PSA_SUCCESS) {
6214 return status;
6215 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006216 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6217 return PSA_ERROR_INVALID_ARGUMENT;
6218 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6220#else
6221 return PSA_ERROR_NOT_SUPPORTED;
6222#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6223 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6224#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6225 status = psa_key_derivation_setup_kdf(operation, alg);
6226#else
6227 return PSA_ERROR_NOT_SUPPORTED;
6228#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6229 } else {
6230 return PSA_ERROR_INVALID_ARGUMENT;
6231 }
6232
6233 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006234 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 }
6236 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006237}
6238
Przemek Stekiel69c46792022-06-10 12:59:51 +02006239#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006240static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6241 psa_algorithm_t kdf_alg,
6242 psa_key_derivation_step_t step,
6243 const uint8_t *data,
6244 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006245{
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006247 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006249 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006250#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006251 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6252 return PSA_ERROR_INVALID_ARGUMENT;
6253 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006254#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 if (hkdf->state != HKDF_STATE_INIT) {
6256 return PSA_ERROR_BAD_STATE;
6257 } else {
6258 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6259 hash_alg,
6260 data, data_length);
6261 if (status != PSA_SUCCESS) {
6262 return status;
6263 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006264 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006265 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006266 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006267 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006268#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006270 /* We shouldn't be in different state as HKDF_EXPAND only allows
6271 * two inputs: SECRET (this case) and INFO which does not modify
6272 * the state. It could happen only if the hkdf
6273 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 if (hkdf->state != HKDF_STATE_INIT) {
6275 return PSA_ERROR_BAD_STATE;
6276 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006277
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006278 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6280 return PSA_ERROR_INVALID_ARGUMENT;
6281 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006282
Gilles Peskine449bd832023-01-11 14:50:10 +01006283 memcpy(hkdf->prk, data, data_length);
6284 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006285#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006286 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006287 /* HKDF: If no salt was provided, use an empty salt.
6288 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006290#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6292 return PSA_ERROR_BAD_STATE;
6293 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006294#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6296 hash_alg,
6297 NULL, 0);
6298 if (status != PSA_SUCCESS) {
6299 return status;
6300 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006301 hkdf->state = HKDF_STATE_STARTED;
6302 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006303 if (hkdf->state != HKDF_STATE_STARTED) {
6304 return PSA_ERROR_BAD_STATE;
6305 }
6306 status = psa_mac_update(&hkdf->hmac,
6307 data, data_length);
6308 if (status != PSA_SUCCESS) {
6309 return status;
6310 }
6311 status = psa_mac_sign_finish(&hkdf->hmac,
6312 hkdf->prk,
6313 sizeof(hkdf->prk),
6314 &data_length);
6315 if (status != PSA_SUCCESS) {
6316 return status;
6317 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006318 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006319
Gilles Peskine2b522db2019-04-12 15:11:49 +02006320 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006321 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006322#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006323 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006324 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006326 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006328#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006329 {
6330 /* Block 0 is empty, and the next block will be
6331 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006332 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006333 }
6334
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006336 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006337#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6339 return PSA_ERROR_INVALID_ARGUMENT;
6340 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006341#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006342#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6344 hkdf->state == HKDF_STATE_INIT) {
6345 return PSA_ERROR_BAD_STATE;
6346 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006347#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006348 if (hkdf->state == HKDF_STATE_OUTPUT) {
6349 return PSA_ERROR_BAD_STATE;
6350 }
6351 if (hkdf->info_set) {
6352 return PSA_ERROR_BAD_STATE;
6353 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006354 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006355 if (data_length != 0) {
6356 hkdf->info = mbedtls_calloc(1, data_length);
6357 if (hkdf->info == NULL) {
6358 return PSA_ERROR_INSUFFICIENT_MEMORY;
6359 }
6360 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006361 }
6362 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006364 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006366 }
6367}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006368#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006369
John Durkop07cc04a2020-11-16 22:08:34 -08006370#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6371 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006372static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6373 const uint8_t *data,
6374 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006375{
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6377 return PSA_ERROR_BAD_STATE;
6378 }
Janos Follathf08e2652019-06-13 09:05:41 +01006379
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 if (data_length != 0) {
6381 prf->seed = mbedtls_calloc(1, data_length);
6382 if (prf->seed == NULL) {
6383 return PSA_ERROR_INSUFFICIENT_MEMORY;
6384 }
Janos Follathf08e2652019-06-13 09:05:41 +01006385
Gilles Peskine449bd832023-01-11 14:50:10 +01006386 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006387 prf->seed_length = data_length;
6388 }
Janos Follathf08e2652019-06-13 09:05:41 +01006389
Gilles Peskine43f958b2020-12-13 14:55:14 +01006390 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006391
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006393}
6394
Gilles Peskine449bd832023-01-11 14:50:10 +01006395static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6396 const uint8_t *data,
6397 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006398{
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6400 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6401 return PSA_ERROR_BAD_STATE;
6402 }
Janos Follath81550542019-06-13 14:26:34 +01006403
Gilles Peskine449bd832023-01-11 14:50:10 +01006404 if (data_length != 0) {
6405 prf->secret = mbedtls_calloc(1, data_length);
6406 if (prf->secret == NULL) {
6407 return PSA_ERROR_INSUFFICIENT_MEMORY;
6408 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006409
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006411 prf->secret_length = data_length;
6412 }
Janos Follath81550542019-06-13 14:26:34 +01006413
Gilles Peskine43f958b2020-12-13 14:55:14 +01006414 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006415
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006417}
6418
Gilles Peskine449bd832023-01-11 14:50:10 +01006419static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6420 const uint8_t *data,
6421 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006422{
Gilles Peskine449bd832023-01-11 14:50:10 +01006423 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6424 return PSA_ERROR_BAD_STATE;
6425 }
Janos Follath63028dd2019-06-13 09:15:47 +01006426
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 if (data_length != 0) {
6428 prf->label = mbedtls_calloc(1, data_length);
6429 if (prf->label == NULL) {
6430 return PSA_ERROR_INSUFFICIENT_MEMORY;
6431 }
Janos Follath63028dd2019-06-13 09:15:47 +01006432
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006434 prf->label_length = data_length;
6435 }
Janos Follath63028dd2019-06-13 09:15:47 +01006436
Gilles Peskine43f958b2020-12-13 14:55:14 +01006437 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006438
Gilles Peskine449bd832023-01-11 14:50:10 +01006439 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006440}
6441
Gilles Peskine449bd832023-01-11 14:50:10 +01006442static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6443 psa_key_derivation_step_t step,
6444 const uint8_t *data,
6445 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006446{
Gilles Peskine449bd832023-01-11 14:50:10 +01006447 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006448 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006450 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006452 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006453 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006454 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006455 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006456 }
6457}
John Durkop07cc04a2020-11-16 22:08:34 -08006458#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6459 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6460
6461#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6462static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6463 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006464 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006466{
6467 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006468 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6469 4 + data_length + prf->other_secret_length :
6470 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006471
Gilles Peskine449bd832023-01-11 14:50:10 +01006472 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6473 return PSA_ERROR_INVALID_ARGUMENT;
6474 }
John Durkop07cc04a2020-11-16 22:08:34 -08006475
Gilles Peskine449bd832023-01-11 14:50:10 +01006476 uint8_t *pms = mbedtls_calloc(1, pms_len);
6477 if (pms == NULL) {
6478 return PSA_ERROR_INSUFFICIENT_MEMORY;
6479 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006480 uint8_t *cur = pms;
6481
6482 /* pure-PSK:
6483 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006484 *
6485 * The premaster secret is formed as follows: if the PSK is N octets
6486 * long, concatenate a uint16 with the value N, N zero octets, a second
6487 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006488 *
6489 * mixed-PSK:
6490 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6491 * follows: concatenate a uint16 with the length of the other secret,
6492 * the other secret itself, uint16 with the length of PSK, and the
6493 * PSK itself.
6494 * For details please check:
6495 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6496 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6497 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006498 */
6499
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6501 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6502 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6503 if (prf->other_secret_length != 0) {
6504 memcpy(cur, prf->other_secret, prf->other_secret_length);
6505 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006506 cur += prf->other_secret_length;
6507 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 } else {
6509 *cur++ = MBEDTLS_BYTE_1(data_length);
6510 *cur++ = MBEDTLS_BYTE_0(data_length);
6511 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006512 cur += data_length;
6513 }
6514
Gilles Peskine449bd832023-01-11 14:50:10 +01006515 *cur++ = MBEDTLS_BYTE_1(data_length);
6516 *cur++ = MBEDTLS_BYTE_0(data_length);
6517 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006518 cur += data_length;
6519
Gilles Peskine449bd832023-01-11 14:50:10 +01006520 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006521
Gilles Peskine449bd832023-01-11 14:50:10 +01006522 mbedtls_platform_zeroize(pms, pms_len);
6523 mbedtls_free(pms);
6524 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006525}
Janos Follath6660f0e2019-06-17 08:44:03 +01006526
Przemek Stekiele47201b2022-04-19 13:53:28 +02006527static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6528 psa_tls12_prf_key_derivation_t *prf,
6529 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006530 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006531{
Gilles Peskine449bd832023-01-11 14:50:10 +01006532 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6533 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006534 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006535
6536 if (data_length != 0) {
6537 prf->other_secret = mbedtls_calloc(1, data_length);
6538 if (prf->other_secret == NULL) {
6539 return PSA_ERROR_INSUFFICIENT_MEMORY;
6540 }
6541
6542 memcpy(prf->other_secret, data, data_length);
6543 prf->other_secret_length = data_length;
6544 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006545 prf->other_secret_length = 0;
6546 }
6547
6548 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6549
Gilles Peskine449bd832023-01-11 14:50:10 +01006550 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006551}
6552
Janos Follath6660f0e2019-06-17 08:44:03 +01006553static psa_status_t psa_tls12_prf_psk_to_ms_input(
6554 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006555 psa_key_derivation_step_t step,
6556 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006558{
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006560 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006561 return psa_tls12_prf_psk_to_ms_set_key(prf,
6562 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006563 break;
6564 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006565 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6566 data,
6567 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006568 break;
6569 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006570 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006571 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006572
Przemek Stekiele47201b2022-04-19 13:53:28 +02006573 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006574}
John Durkop07cc04a2020-11-16 22:08:34 -08006575#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006576
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006577#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6578static psa_status_t psa_tls12_ecjpake_to_pms_input(
6579 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006580 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006581 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006583{
Gilles Peskine449bd832023-01-11 14:50:10 +01006584 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6585 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6586 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006587 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006588
6589 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006590 if (data[0] != 0x04) {
6591 return PSA_ERROR_INVALID_ARGUMENT;
6592 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006593
6594 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006595 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006596
Gilles Peskine449bd832023-01-11 14:50:10 +01006597 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006598}
6599#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006600/** Check whether the given key type is acceptable for the given
6601 * input step of a key derivation.
6602 *
6603 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6604 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6605 * Both secret and non-secret inputs can alternatively have the type
6606 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6607 * that the input was passed as a buffer rather than via a key object.
6608 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006609static int psa_key_derivation_check_input_type(
6610 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006611 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006612{
Gilles Peskine449bd832023-01-11 14:50:10 +01006613 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006614 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006615 if (key_type == PSA_KEY_TYPE_DERIVE) {
6616 return PSA_SUCCESS;
6617 }
6618 if (key_type == PSA_KEY_TYPE_NONE) {
6619 return PSA_SUCCESS;
6620 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006621 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006622 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006623 if (key_type == PSA_KEY_TYPE_DERIVE) {
6624 return PSA_SUCCESS;
6625 }
6626 if (key_type == PSA_KEY_TYPE_NONE) {
6627 return PSA_SUCCESS;
6628 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006629 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006630 case PSA_KEY_DERIVATION_INPUT_LABEL:
6631 case PSA_KEY_DERIVATION_INPUT_SALT:
6632 case PSA_KEY_DERIVATION_INPUT_INFO:
6633 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006634 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6635 return PSA_SUCCESS;
6636 }
6637 if (key_type == PSA_KEY_TYPE_NONE) {
6638 return PSA_SUCCESS;
6639 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006640 break;
6641 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006642 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006643}
6644
Janos Follathb80a94e2019-06-12 15:54:46 +01006645static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006646 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006647 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006648 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006649 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006650 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006651{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006652 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006653 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006654
Gilles Peskine449bd832023-01-11 14:50:10 +01006655 status = psa_key_derivation_check_input_type(step, key_type);
6656 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006657 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006658 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006659
Przemek Stekiel69c46792022-06-10 12:59:51 +02006660#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006661 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6662 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6663 step, data, data_length);
6664 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006665#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006666#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006667 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6668 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6669 step, data, data_length);
6670 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006671#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6672#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006673 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6674 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6675 step, data, data_length);
6676 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006677#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006678#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006679 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006680 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006681 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6682 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006683#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006684 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006685 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006686 (void) data;
6687 (void) data_length;
6688 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006689 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006690 }
6691
Gilles Peskine224b0d62019-09-23 18:13:17 +02006692exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006693 if (status != PSA_SUCCESS) {
6694 psa_key_derivation_abort(operation);
6695 }
6696 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006697}
6698
Janos Follath51f4a0f2019-06-14 11:35:55 +01006699psa_status_t psa_key_derivation_input_bytes(
6700 psa_key_derivation_operation_t *operation,
6701 psa_key_derivation_step_t step,
6702 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006703 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006704{
Gilles Peskine449bd832023-01-11 14:50:10 +01006705 return psa_key_derivation_input_internal(operation, step,
6706 PSA_KEY_TYPE_NONE,
6707 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006708}
6709
Janos Follath51f4a0f2019-06-14 11:35:55 +01006710psa_status_t psa_key_derivation_input_key(
6711 psa_key_derivation_operation_t *operation,
6712 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006713 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006714{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006715 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006716 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006717 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006718
Ronald Cron5c522922020-11-14 16:35:34 +01006719 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006720 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6721 if (status != PSA_SUCCESS) {
6722 psa_key_derivation_abort(operation);
6723 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006724 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006725
6726 /* Passing a key object as a SECRET input unlocks the permission
6727 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006728 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006729 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006730 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006731
Gilles Peskine449bd832023-01-11 14:50:10 +01006732 status = psa_key_derivation_input_internal(operation,
6733 step, slot->attr.type,
6734 slot->key.data,
6735 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006736
Gilles Peskine449bd832023-01-11 14:50:10 +01006737 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006738
Gilles Peskine449bd832023-01-11 14:50:10 +01006739 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006740}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006741
6742
6743
6744/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006745/* Key agreement */
6746/****************************************************************/
6747
Gilles Peskine449bd832023-01-11 14:50:10 +01006748psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6749 const uint8_t *key_buffer,
6750 size_t key_buffer_size,
6751 psa_algorithm_t alg,
6752 const uint8_t *peer_key,
6753 size_t peer_key_length,
6754 uint8_t *shared_secret,
6755 size_t shared_secret_size,
6756 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006757{
Gilles Peskine449bd832023-01-11 14:50:10 +01006758 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006759#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006760 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006761 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6762 key_buffer_size, alg,
6763 peer_key, peer_key_length,
6764 shared_secret,
6765 shared_secret_size,
6766 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006767#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006768
6769#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6770 case PSA_ALG_FFDH:
6771 if (!PSA_KEY_TYPE_IS_DH_KEY_PAIR(psa_get_key_type(attributes))) {
6772 return PSA_ERROR_INVALID_ARGUMENT;
6773 }
6774
6775 return psa_key_agreement_ffdh(peer_key,
6776 peer_key_length,
6777 key_buffer,
6778 key_buffer_size,
6779 shared_secret,
6780 shared_secret_size,
6781 shared_secret_length);
6782#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6783
Gilles Peskine01d718c2018-09-18 12:01:02 +02006784 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006785 (void) attributes;
6786 (void) key_buffer;
6787 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006788 (void) peer_key;
6789 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006790 (void) shared_secret;
6791 (void) shared_secret_size;
6792 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006793 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006794 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006795}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006796
Aditya Deshpande17845b82022-10-13 17:21:01 +01006797/** Internal function for raw key agreement
6798 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006799 * to the driver's implementation if a driver is present.
6800 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006801 * (psa_key_agreement_raw_builtin).
6802 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006803static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6804 psa_key_slot_t *private_key,
6805 const uint8_t *peer_key,
6806 size_t peer_key_length,
6807 uint8_t *shared_secret,
6808 size_t shared_secret_size,
6809 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006810{
Gilles Peskine449bd832023-01-11 14:50:10 +01006811 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6812 return PSA_ERROR_NOT_SUPPORTED;
6813 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006814
6815 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006816 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006817 };
6818
Gilles Peskine449bd832023-01-11 14:50:10 +01006819 return psa_driver_wrapper_key_agreement(&attributes,
6820 private_key->key.data,
6821 private_key->key.bytes, alg,
6822 peer_key, peer_key_length,
6823 shared_secret,
6824 shared_secret_size,
6825 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006826}
6827
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006828/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006829 * to potentially free embedded data structures and wipe confidential data.
6830 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006831static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6832 psa_key_derivation_step_t step,
6833 psa_key_slot_t *private_key,
6834 const uint8_t *peer_key,
6835 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006836{
6837 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006838 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006839 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006840 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006841
6842 /* Step 1: run the secret agreement algorithm to generate the shared
6843 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006844 status = psa_key_agreement_raw_internal(ka_alg,
6845 private_key,
6846 peer_key, peer_key_length,
6847 shared_secret,
6848 sizeof(shared_secret),
6849 &shared_secret_length);
6850 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006851 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006852 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006853
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006854 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006855 * the shared secret. A shared secret is permitted wherever a key
6856 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006857 status = psa_key_derivation_input_internal(operation, step,
6858 PSA_KEY_TYPE_DERIVE,
6859 shared_secret,
6860 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006861exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006862 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6863 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006864}
6865
Gilles Peskine449bd832023-01-11 14:50:10 +01006866psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6867 psa_key_derivation_step_t step,
6868 mbedtls_svc_key_id_t private_key,
6869 const uint8_t *peer_key,
6870 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006871{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006872 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006873 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006874 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006875
Gilles Peskine449bd832023-01-11 14:50:10 +01006876 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6877 return PSA_ERROR_INVALID_ARGUMENT;
6878 }
Ronald Cron5c522922020-11-14 16:35:34 +01006879 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006880 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6881 if (status != PSA_SUCCESS) {
6882 return status;
6883 }
6884 status = psa_key_agreement_internal(operation, step,
6885 slot,
6886 peer_key, peer_key_length);
6887 if (status != PSA_SUCCESS) {
6888 psa_key_derivation_abort(operation);
6889 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006890 /* If a private key has been added as SECRET, we allow the derived
6891 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006892 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006893 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006894 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006895 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006896
Gilles Peskine449bd832023-01-11 14:50:10 +01006897 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006898
Gilles Peskine449bd832023-01-11 14:50:10 +01006899 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006900}
6901
Gilles Peskine449bd832023-01-11 14:50:10 +01006902psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6903 mbedtls_svc_key_id_t private_key,
6904 const uint8_t *peer_key,
6905 size_t peer_key_length,
6906 uint8_t *output,
6907 size_t output_size,
6908 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006909{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006910 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006911 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006912 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006913
Gilles Peskine449bd832023-01-11 14:50:10 +01006914 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006915 status = PSA_ERROR_INVALID_ARGUMENT;
6916 goto exit;
6917 }
Ronald Cron5c522922020-11-14 16:35:34 +01006918 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006919 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6920 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006921 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006922 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006923
Gilles Peskine3e561302022-04-14 00:17:15 +02006924 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6925 * for the output size. The PSA specification only guarantees that this
6926 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6927 * but it might be nice to allow smaller buffers if the output fits.
6928 * At the time of writing this comment, with only ECDH implemented,
6929 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6930 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6931 * be exact for it as well. */
6932 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006933 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6934 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006935 status = PSA_ERROR_BUFFER_TOO_SMALL;
6936 goto exit;
6937 }
6938
Gilles Peskine449bd832023-01-11 14:50:10 +01006939 status = psa_key_agreement_raw_internal(alg, slot,
6940 peer_key, peer_key_length,
6941 output, output_size,
6942 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006943
6944exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006946 /* If an error happens and is not handled properly, the output
6947 * may be used as a key to protect sensitive data. Arrange for such
6948 * a key to be random, which is likely to result in decryption or
6949 * verification errors. This is better than filling the buffer with
6950 * some constant data such as zeros, which would result in the data
6951 * being protected with a reproducible, easily knowable key.
6952 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006953 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006954 *output_length = output_size;
6955 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006956
Gilles Peskine449bd832023-01-11 14:50:10 +01006957 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006958
Gilles Peskine449bd832023-01-11 14:50:10 +01006959 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006960}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006961
6962
Gilles Peskine30524eb2020-11-13 17:02:26 +01006963
Gilles Peskine86a440b2018-11-12 18:39:40 +01006964/****************************************************************/
6965/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006966/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006967
Gilles Peskine30524eb2020-11-13 17:02:26 +01006968/** Initialize the PSA random generator.
6969 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006970static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006971{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006972#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006973 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006974#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6975
Gilles Peskine30524eb2020-11-13 17:02:26 +01006976 /* Set default configuration if
6977 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006978 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006979 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006980 }
6981 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006982 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006983 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006984
Gilles Peskine449bd832023-01-11 14:50:10 +01006985 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006986#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6987 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6988 /* The PSA entropy injection feature depends on using NV seed as an entropy
6989 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006990 mbedtls_entropy_add_source(&rng->entropy,
6991 mbedtls_nv_seed_poll, NULL,
6992 MBEDTLS_ENTROPY_BLOCK_SIZE,
6993 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006994#endif
6995
Gilles Peskine449bd832023-01-11 14:50:10 +01006996 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006997#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006998}
6999
7000/** Deinitialize the PSA random generator.
7001 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007002static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007003{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007004#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007005 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007006#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007007 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7008 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007009#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007010}
7011
7012/** Seed the PSA random generator.
7013 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007014static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007015{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007016#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7017 /* Do nothing: the external RNG seeds itself. */
7018 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007019 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007020#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007021 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007022 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7023 drbg_seed, sizeof(drbg_seed) - 1);
7024 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007025#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007026}
7027
Gilles Peskine449bd832023-01-11 14:50:10 +01007028psa_status_t psa_generate_random(uint8_t *output,
7029 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007030{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007031 GUARD_MODULE_INITIALIZED;
7032
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007033#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7034
7035 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007036 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7037 output, output_size,
7038 &output_length);
7039 if (status != PSA_SUCCESS) {
7040 return status;
7041 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007042 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007043 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007044 if (output_length != output_size) {
7045 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7046 }
7047 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007048
7049#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7050
Gilles Peskine449bd832023-01-11 14:50:10 +01007051 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007052 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007053 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7054 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7055 output_size);
7056 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7057 output, request_size);
7058 if (ret != 0) {
7059 return mbedtls_to_psa_error(ret);
7060 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007061 output_size -= request_size;
7062 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007063 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007065#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007066}
7067
Gilles Peskine8814fc42020-12-14 15:33:44 +01007068/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007069 *
7070 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7071 * `psa_generate_random(...)`. The state parameter is ignored since the
7072 * PSA API doesn't support passing an explicit state.
7073 *
7074 * In the non-external case, psa_generate_random() calls an
7075 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7076 * and semantics as mbedtls_psa_get_random(). As an optimization,
7077 * instead of doing this back-and-forth between the PSA API and the
7078 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7079 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007080 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007081#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7082int mbedtls_psa_get_random(void *p_rng,
7083 unsigned char *output,
7084 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007085{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007086 /* This function takes a pointer to the RNG state because that's what
7087 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7088 * its own state internally and doesn't let the caller access that state.
7089 * So we just ignore the state parameter, and in practice we'll pass
7090 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007091 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007092 psa_status_t status = psa_generate_random(output, output_size);
7093 if (status == PSA_SUCCESS) {
7094 return 0;
7095 } else {
7096 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7097 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007098}
7099#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7100
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007101#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00007102#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007103
Gilles Peskine449bd832023-01-11 14:50:10 +01007104psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7105 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007106{
Gilles Peskine449bd832023-01-11 14:50:10 +01007107 if (global_data.initialized) {
7108 return PSA_ERROR_NOT_PERMITTED;
7109 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007110
Gilles Peskine449bd832023-01-11 14:50:10 +01007111 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7112 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7113 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7114 return PSA_ERROR_INVALID_ARGUMENT;
7115 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007116
Gilles Peskine449bd832023-01-11 14:50:10 +01007117 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007118}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007119#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007120
Ronald Cron3772afe2021-02-08 16:10:05 +01007121/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007122 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007123 * \param type The key type
7124 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007125 *
7126 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007127 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007128 * \retval #PSA_ERROR_INVALID_ARGUMENT
7129 * The size in bits of the key is not valid.
7130 * \retval #PSA_ERROR_NOT_SUPPORTED
7131 * The type and/or the size in bits of the key or the combination of
7132 * the two is not supported.
7133 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007134static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007135 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007136{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007137 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007138
Gilles Peskine449bd832023-01-11 14:50:10 +01007139 if (key_type_is_raw_bytes(type)) {
7140 status = psa_validate_unstructured_key_bit_size(type, bits);
7141 if (status != PSA_SUCCESS) {
7142 return status;
7143 }
7144 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007145#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007146 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7147 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7148 return PSA_ERROR_NOT_SUPPORTED;
7149 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007150
Ronald Cron01b2aba2020-10-05 09:42:02 +02007151 /* Accept only byte-aligned keys, for the same reasons as
7152 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007153 if (bits % 8 != 0) {
7154 return PSA_ERROR_NOT_SUPPORTED;
7155 }
7156 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007157#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007158
Ronald Cron5c4d3862020-12-07 11:07:24 +01007159#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007160 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007161 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007162 return PSA_SUCCESS;
7163 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007164#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007165 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007166 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007167 }
7168
Gilles Peskine449bd832023-01-11 14:50:10 +01007169 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007170}
7171
Ronald Cron55ed0592020-10-05 10:30:40 +02007172psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007173 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007174 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007175{
7176 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007177 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007178
Gilles Peskine449bd832023-01-11 14:50:10 +01007179 if ((attributes->domain_parameters == NULL) &&
7180 (attributes->domain_parameters_size != 0)) {
7181 return PSA_ERROR_INVALID_ARGUMENT;
7182 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007183
Gilles Peskine449bd832023-01-11 14:50:10 +01007184 if (key_type_is_raw_bytes(type)) {
7185 status = psa_generate_random(key_buffer, key_buffer_size);
7186 if (status != PSA_SUCCESS) {
7187 return status;
7188 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007189
David Brown12ca5032021-02-11 11:02:00 -07007190#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007191 if (type == PSA_KEY_TYPE_DES) {
7192 psa_des_set_key_parity(key_buffer, key_buffer_size);
7193 }
David Brown8107e312021-02-12 08:08:46 -07007194#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007195 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007196
Jaeden Amero424fa932021-05-14 08:34:32 +01007197#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
7198 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01007199 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7200 return mbedtls_psa_rsa_generate_key(attributes,
7201 key_buffer,
7202 key_buffer_size,
7203 key_buffer_length);
7204 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01007205#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
7206 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007207
John Durkop9814fa22020-11-04 12:28:15 -08007208#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007209 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7210 return mbedtls_psa_ecp_generate_key(attributes,
7211 key_buffer,
7212 key_buffer_size,
7213 key_buffer_length);
7214 } else
John Durkop9814fa22020-11-04 12:28:15 -08007215#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02007216 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007217 (void) key_buffer_length;
7218 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007219 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007220
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007222}
Darryl Green0c6575a2018-11-07 16:05:30 +00007223
Gilles Peskine449bd832023-01-11 14:50:10 +01007224psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7225 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007226{
7227 psa_status_t status;
7228 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007229 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007230 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007231
Ronald Cron81709fc2020-11-14 12:10:32 +01007232 *key = MBEDTLS_SVC_KEY_ID_INIT;
7233
Gilles Peskine0f84d622019-09-12 19:03:13 +02007234 /* Reject any attempt to create a zero-length key so that we don't
7235 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007236 if (psa_get_key_bits(attributes) == 0) {
7237 return PSA_ERROR_INVALID_ARGUMENT;
7238 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007239
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007240 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007241 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7242 return PSA_ERROR_INVALID_ARGUMENT;
7243 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007244
Gilles Peskine449bd832023-01-11 14:50:10 +01007245 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7246 &slot, &driver);
7247 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007248 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007249 }
Gilles Peskine11792082019-08-06 18:36:36 +02007250
Ronald Cron977c2472020-10-13 08:32:21 +02007251 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307252 * storage ( thus not in the case of generating a key in a secure element
7253 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7254 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007255 if (slot->key.data == NULL) {
7256 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7257 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007258 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007259 attributes->core.type, attributes->core.bits);
7260 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007261 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007262 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007263
7264 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007265 attributes->core.type,
7266 attributes->core.bits);
7267 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007268 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007269 attributes, &key_buffer_size);
7270 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007271 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007272 }
Ronald Cron977c2472020-10-13 08:32:21 +02007273 }
Ronald Cron977c2472020-10-13 08:32:21 +02007274
Gilles Peskine449bd832023-01-11 14:50:10 +01007275 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7276 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007277 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007278 }
Ronald Cron977c2472020-10-13 08:32:21 +02007279 }
7280
Gilles Peskine449bd832023-01-11 14:50:10 +01007281 status = psa_driver_wrapper_generate_key(attributes,
7282 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007283
Gilles Peskine449bd832023-01-11 14:50:10 +01007284 if (status != PSA_SUCCESS) {
7285 psa_remove_key_data_from_memory(slot);
7286 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007287
Gilles Peskine11792082019-08-06 18:36:36 +02007288exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007289 if (status == PSA_SUCCESS) {
7290 status = psa_finish_key_creation(slot, driver, key);
7291 }
7292 if (status != PSA_SUCCESS) {
7293 psa_fail_key_creation(slot, driver);
7294 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007295
Gilles Peskine449bd832023-01-11 14:50:10 +01007296 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007297}
7298
Gilles Peskinee59236f2018-01-27 23:32:46 +01007299/****************************************************************/
7300/* Module setup */
7301/****************************************************************/
7302
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007303#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007304psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007305 void (* entropy_init)(mbedtls_entropy_context *ctx),
7306 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007307{
Gilles Peskine449bd832023-01-11 14:50:10 +01007308 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7309 return PSA_ERROR_BAD_STATE;
7310 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007311 global_data.rng.entropy_init = entropy_init;
7312 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007313 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007314}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007315#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007316
Gilles Peskine449bd832023-01-11 14:50:10 +01007317void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007318{
Gilles Peskine449bd832023-01-11 14:50:10 +01007319 psa_wipe_all_key_slots();
7320 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7321 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007322 }
7323 /* Wipe all remaining data, including configuration.
7324 * In particular, this sets all state indicator to the value
7325 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007326 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007327
7328 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007329 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007330}
7331
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007332#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7333/** Recover a transaction that was interrupted by a power failure.
7334 *
7335 * This function is called during initialization, before psa_crypto_init()
7336 * returns. If this function returns a failure status, the initialization
7337 * fails.
7338 */
7339static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007340 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007341{
Gilles Peskine449bd832023-01-11 14:50:10 +01007342 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007343 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7344 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007345 /* TODO - fall through to the failure case until this
7346 * is implemented.
7347 * https://github.com/ARMmbed/mbed-crypto/issues/218
7348 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007349 default:
7350 /* We found an unsupported transaction in the storage.
7351 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007352 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007353 }
7354}
7355#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7356
Gilles Peskine449bd832023-01-11 14:50:10 +01007357psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007358{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007359 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007360
Gilles Peskinec6b69072018-11-20 21:42:52 +01007361 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007362 if (global_data.initialized != 0) {
7363 return PSA_SUCCESS;
7364 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007365
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007366 /* Init drivers */
7367 status = psa_driver_wrapper_init();
7368 if (status != PSA_SUCCESS) {
7369 goto exit;
7370 }
7371 global_data.drivers_initialized = 1;
7372
Gilles Peskine30524eb2020-11-13 17:02:26 +01007373 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007374 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007375 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007376 status = mbedtls_psa_random_seed(&global_data.rng);
7377 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007378 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007379 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007380 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007381
Gilles Peskine449bd832023-01-11 14:50:10 +01007382 status = psa_initialize_key_slots();
7383 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007384 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007385 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007386
Gilles Peskine4b734222019-07-24 15:56:31 +02007387#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007388 status = psa_crypto_load_transaction();
7389 if (status == PSA_SUCCESS) {
7390 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7391 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007392 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007393 }
7394 status = psa_crypto_stop_transaction();
7395 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007396 /* There's no transaction to complete. It's all good. */
7397 status = PSA_SUCCESS;
7398 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007399#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007400
Gilles Peskinec6b69072018-11-20 21:42:52 +01007401 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007402 global_data.initialized = 1;
7403
7404exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007405 if (status != PSA_SUCCESS) {
7406 mbedtls_psa_crypto_free();
7407 }
7408 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007409}
7410
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007411psa_status_t psa_crypto_driver_pake_get_password_len(
7412 const psa_crypto_driver_pake_inputs_t *inputs,
7413 size_t *password_len)
7414{
7415 if (inputs->password_len == 0) {
7416 return PSA_ERROR_BAD_STATE;
7417 }
7418
7419 *password_len = inputs->password_len;
7420
7421 return PSA_SUCCESS;
7422}
7423
7424psa_status_t psa_crypto_driver_pake_get_password(
7425 const psa_crypto_driver_pake_inputs_t *inputs,
7426 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7427{
7428 if (inputs->password_len == 0) {
7429 return PSA_ERROR_BAD_STATE;
7430 }
7431
7432 if (buffer_size < inputs->password_len) {
7433 return PSA_ERROR_BUFFER_TOO_SMALL;
7434 }
7435
7436 memcpy(buffer, inputs->password, inputs->password_len);
7437 *buffer_length = inputs->password_len;
7438
7439 return PSA_SUCCESS;
7440}
7441
7442psa_status_t psa_crypto_driver_pake_get_role(
7443 const psa_crypto_driver_pake_inputs_t *inputs,
7444 psa_pake_role_t *role)
7445{
7446 if (inputs->role == PSA_PAKE_ROLE_NONE) {
7447 return PSA_ERROR_BAD_STATE;
7448 }
7449
7450 *role = inputs->role;
7451
7452 return PSA_SUCCESS;
7453}
7454
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007455psa_status_t psa_crypto_driver_pake_get_user_len(
7456 const psa_crypto_driver_pake_inputs_t *inputs,
7457 size_t *user_len)
7458{
7459 if (inputs->user_len == 0) {
7460 return PSA_ERROR_BAD_STATE;
7461 }
7462
7463 *user_len = inputs->user_len;
7464
7465 return PSA_SUCCESS;
7466}
7467
7468psa_status_t psa_crypto_driver_pake_get_user(
7469 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007470 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007471{
7472 if (inputs->user_len == 0) {
7473 return PSA_ERROR_BAD_STATE;
7474 }
7475
Przemek Stekielc0e62502023-03-14 11:49:36 +01007476 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007477 return PSA_ERROR_BUFFER_TOO_SMALL;
7478 }
7479
Przemek Stekielc0e62502023-03-14 11:49:36 +01007480 memcpy(user_id, inputs->user, inputs->user_len);
7481 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007482
7483 return PSA_SUCCESS;
7484}
7485
7486psa_status_t psa_crypto_driver_pake_get_peer_len(
7487 const psa_crypto_driver_pake_inputs_t *inputs,
7488 size_t *peer_len)
7489{
7490 if (inputs->peer_len == 0) {
7491 return PSA_ERROR_BAD_STATE;
7492 }
7493
7494 *peer_len = inputs->peer_len;
7495
7496 return PSA_SUCCESS;
7497}
7498
7499psa_status_t psa_crypto_driver_pake_get_peer(
7500 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007501 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007502{
7503 if (inputs->peer_len == 0) {
7504 return PSA_ERROR_BAD_STATE;
7505 }
7506
Przemek Stekielc0e62502023-03-14 11:49:36 +01007507 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007508 return PSA_ERROR_BUFFER_TOO_SMALL;
7509 }
7510
Przemek Stekielc0e62502023-03-14 11:49:36 +01007511 memcpy(peer_id, inputs->peer, inputs->peer_len);
7512 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007513
7514 return PSA_SUCCESS;
7515}
7516
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007517psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7518 const psa_crypto_driver_pake_inputs_t *inputs,
7519 psa_pake_cipher_suite_t *cipher_suite)
7520{
7521 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7522 return PSA_ERROR_BAD_STATE;
7523 }
7524
7525 *cipher_suite = inputs->cipher_suite;
7526
7527 return PSA_SUCCESS;
7528}
7529
Neil Armstronga7d08c32022-06-01 18:21:20 +02007530psa_status_t psa_pake_setup(
7531 psa_pake_operation_t *operation,
7532 const psa_pake_cipher_suite_t *cipher_suite)
7533{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007534 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007535
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007536 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007537 status = PSA_ERROR_BAD_STATE;
7538 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007539 }
7540
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007541 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007542 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007543 status = PSA_ERROR_INVALID_ARGUMENT;
7544 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007545 }
7546
Przemek Stekiel51eac532022-12-07 11:04:51 +01007547 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7548
Przemek Stekiele12ed362022-12-21 12:54:46 +01007549 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007550 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7551 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007552 operation->data.inputs.cipher_suite = *cipher_suite;
7553
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007554#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007555 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007556 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007557 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007558
Przemek Stekiele12ed362022-12-21 12:54:46 +01007559 computation_stage->state = PSA_PAKE_STATE_SETUP;
7560 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7561 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7562 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007563 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007564#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007565 {
7566 status = PSA_ERROR_NOT_SUPPORTED;
7567 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007568 }
7569
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007570 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7571
Przemek Stekiel51eac532022-12-07 11:04:51 +01007572 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007573exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007574 psa_pake_abort(operation);
7575 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007576}
7577
7578psa_status_t psa_pake_set_password_key(
7579 psa_pake_operation_t *operation,
7580 mbedtls_svc_key_id_t password)
7581{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007582 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007583 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7584 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007585
Przemek Stekiel51eac532022-12-07 11:04:51 +01007586 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007587 status = PSA_ERROR_BAD_STATE;
7588 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007589 }
7590
Przemek Stekiel6c764412022-11-22 14:05:12 +01007591 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7592 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007593 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007594 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007595 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007596 }
7597
Przemek Stekiel6c764412022-11-22 14:05:12 +01007598 psa_key_attributes_t attributes = {
7599 .core = slot->attr
7600 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007601
Przemek Stekiel51eac532022-12-07 11:04:51 +01007602 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007603
Przemek Stekiel51eac532022-12-07 11:04:51 +01007604 if (type != PSA_KEY_TYPE_PASSWORD &&
7605 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7606 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007607 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007608 }
7609
Przemek Stekiel51eac532022-12-07 11:04:51 +01007610 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7611 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007612 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007613 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007614 }
7615
7616 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7617 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007618 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007619exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007620 if (status != PSA_SUCCESS) {
7621 psa_pake_abort(operation);
7622 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007623 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007624 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007625}
7626
7627psa_status_t psa_pake_set_user(
7628 psa_pake_operation_t *operation,
7629 const uint8_t *user_id,
7630 size_t user_id_len)
7631{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007632 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007633
7634 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007635 status = PSA_ERROR_BAD_STATE;
7636 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007637 }
7638
Przemek Stekiel51eac532022-12-07 11:04:51 +01007639 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007640 status = PSA_ERROR_INVALID_ARGUMENT;
7641 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007642 }
7643
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007644 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007645 status = PSA_ERROR_BAD_STATE;
7646 goto exit;
7647 }
7648
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007649 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007650 if ((user_id_len != sizeof(jpake_server_id) ||
7651 memcmp(user_id, jpake_server_id, user_id_len) != 0) &&
7652 (user_id_len != sizeof(jpake_client_id) ||
7653 memcmp(user_id, jpake_client_id, user_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007654 status = PSA_ERROR_NOT_SUPPORTED;
7655 goto exit;
7656 }
7657
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007658 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7659 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007660 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7661 goto exit;
7662 }
7663
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007664 memcpy(operation->data.inputs.user, user_id, user_id_len);
7665 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007666
7667 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007668exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007669 psa_pake_abort(operation);
7670 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007671}
7672
7673psa_status_t psa_pake_set_peer(
7674 psa_pake_operation_t *operation,
7675 const uint8_t *peer_id,
7676 size_t peer_id_len)
7677{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007678 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007679
7680 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007681 status = PSA_ERROR_BAD_STATE;
7682 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007683 }
7684
Przemek Stekiel51eac532022-12-07 11:04:51 +01007685 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007686 status = PSA_ERROR_INVALID_ARGUMENT;
7687 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007688 }
7689
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007690 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007691 status = PSA_ERROR_BAD_STATE;
7692 goto exit;
7693 }
7694
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007695 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007696 if ((peer_id_len != sizeof(jpake_server_id) ||
7697 memcmp(peer_id, jpake_server_id, peer_id_len) != 0) &&
7698 (peer_id_len != sizeof(jpake_client_id) ||
7699 memcmp(peer_id, jpake_client_id, peer_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007700 status = PSA_ERROR_NOT_SUPPORTED;
7701 goto exit;
7702 }
7703
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007704 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7705 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007706 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7707 goto exit;
7708 }
7709
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007710 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7711 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007712
7713 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007714exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007715 psa_pake_abort(operation);
7716 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007717}
7718
7719psa_status_t psa_pake_set_role(
7720 psa_pake_operation_t *operation,
7721 psa_pake_role_t role)
7722{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007723 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007724
Przemek Stekiel51eac532022-12-07 11:04:51 +01007725 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007726 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007727 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007728 }
7729
Przemek Stekiel09104b82023-03-06 14:11:51 +01007730 switch (operation->alg) {
7731#if defined(PSA_WANT_ALG_JPAKE)
7732 case PSA_ALG_JPAKE:
7733 if (role == PSA_PAKE_ROLE_NONE) {
7734 return PSA_SUCCESS;
7735 }
7736 status = PSA_ERROR_INVALID_ARGUMENT;
7737 break;
7738#endif
7739 default:
7740 (void) role;
7741 status = PSA_ERROR_NOT_SUPPORTED;
7742 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007743 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007744exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007745 psa_pake_abort(operation);
7746 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007747}
7748
Przemek Stekielb09c4872023-01-17 12:05:38 +01007749/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007750#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007751static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007752 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007753{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007754 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007755 case PSA_PAKE_OUTPUT_X1_X2:
7756 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007757 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007758 case PSA_PAKE_X1_STEP_KEY_SHARE:
7759 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007760 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7761 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007762 case PSA_PAKE_X1_STEP_ZK_PROOF:
7763 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007764 case PSA_PAKE_X2_STEP_KEY_SHARE:
7765 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007766 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7767 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007768 case PSA_PAKE_X2_STEP_ZK_PROOF:
7769 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007770 default:
7771 return PSA_JPAKE_STEP_INVALID;
7772 }
7773 break;
7774 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007775 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007776 case PSA_PAKE_X1_STEP_KEY_SHARE:
7777 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007778 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7779 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007780 case PSA_PAKE_X1_STEP_ZK_PROOF:
7781 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007782 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007783 return PSA_JPAKE_STEP_INVALID;
7784 }
7785 break;
7786 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007787 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007788 case PSA_PAKE_X1_STEP_KEY_SHARE:
7789 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007790 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7791 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007792 case PSA_PAKE_X1_STEP_ZK_PROOF:
7793 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007794 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007795 return PSA_JPAKE_STEP_INVALID;
7796 }
7797 break;
7798 default:
7799 return PSA_JPAKE_STEP_INVALID;
7800 }
7801 return PSA_JPAKE_STEP_INVALID;
7802}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007803#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007804
Przemek Stekiel2797d372022-12-22 11:19:22 +01007805static psa_status_t psa_pake_complete_inputs(
7806 psa_pake_operation_t *operation)
7807{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007808 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007809 /* Create copy of the inputs on stack as inputs share memory
7810 with the driver context which will be setup by the driver. */
7811 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007812
Przemek Stekielfde11282023-03-13 16:06:09 +01007813 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007814 return PSA_ERROR_BAD_STATE;
7815 }
7816
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007817 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007818 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7819 return PSA_ERROR_BAD_STATE;
7820 }
7821 if (memcmp(inputs.user, jpake_client_id, inputs.user_len) == 0 &&
7822 memcmp(inputs.peer, jpake_server_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007823 inputs.role = PSA_PAKE_ROLE_CLIENT;
7824 } else
Przemek Stekielfde11282023-03-13 16:06:09 +01007825 if (memcmp(inputs.user, jpake_server_id, inputs.user_len) == 0 &&
7826 memcmp(inputs.peer, jpake_client_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007827 inputs.role = PSA_PAKE_ROLE_SERVER;
7828 }
7829
7830 if (inputs.role != PSA_PAKE_ROLE_CLIENT &&
7831 inputs.role != PSA_PAKE_ROLE_SERVER) {
7832 return PSA_ERROR_NOT_SUPPORTED;
7833 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007834 }
7835
Przemek Stekiel18620a32023-01-17 16:34:52 +01007836 /* Clear driver context */
7837 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7838
7839 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007840
7841 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007842 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7843 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007844
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007845 /* User and peer are translated to role. */
7846 mbedtls_free(inputs.user);
7847 mbedtls_free(inputs.peer);
7848
Przemek Stekiel2797d372022-12-22 11:19:22 +01007849 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007850#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007851 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007852 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007853 psa_jpake_computation_stage_t *computation_stage =
7854 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007855 computation_stage->state = PSA_PAKE_STATE_READY;
7856 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7857 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7858 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007859 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007860#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007861 {
7862 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007863 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007864 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007865 return status;
7866}
7867
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007868#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007869static psa_status_t psa_jpake_output_prologue(
7870 psa_pake_operation_t *operation,
7871 psa_pake_step_t step)
7872{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007873 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7874 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7875 step != PSA_PAKE_STEP_ZK_PROOF) {
7876 return PSA_ERROR_INVALID_ARGUMENT;
7877 }
7878
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007879 psa_jpake_computation_stage_t *computation_stage =
7880 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007881
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007882 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7883 return PSA_ERROR_BAD_STATE;
7884 }
7885
7886 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7887 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
7888 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
7889 return PSA_ERROR_BAD_STATE;
7890 }
7891
7892 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7893 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007894 return PSA_ERROR_BAD_STATE;
7895 }
7896
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007897 switch (computation_stage->output_step) {
7898 case PSA_PAKE_STEP_X1_X2:
7899 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007900 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007901 case PSA_PAKE_STEP_X2S:
7902 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007903 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007904 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007905 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007906 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007907
7908 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7909 }
7910
7911 /* Check if step matches current sequence */
7912 switch (computation_stage->sequence) {
7913 case PSA_PAKE_X1_STEP_KEY_SHARE:
7914 case PSA_PAKE_X2_STEP_KEY_SHARE:
7915 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7916 return PSA_ERROR_BAD_STATE;
7917 }
7918 break;
7919
7920 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7921 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7922 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7923 return PSA_ERROR_BAD_STATE;
7924 }
7925 break;
7926
7927 case PSA_PAKE_X1_STEP_ZK_PROOF:
7928 case PSA_PAKE_X2_STEP_ZK_PROOF:
7929 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7930 return PSA_ERROR_BAD_STATE;
7931 }
7932 break;
7933
7934 default:
7935 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007936 }
7937
7938 return PSA_SUCCESS;
7939}
7940
7941static psa_status_t psa_jpake_output_epilogue(
7942 psa_pake_operation_t *operation)
7943{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007944 psa_jpake_computation_stage_t *computation_stage =
7945 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007946
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007947 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007948 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007949 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007950 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007951 computation_stage->state = PSA_PAKE_STATE_READY;
7952 computation_stage->output_step++;
7953 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7954 } else {
7955 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007956 }
7957
7958 return PSA_SUCCESS;
7959}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007960#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007961
Neil Armstronga7d08c32022-06-01 18:21:20 +02007962psa_status_t psa_pake_output(
7963 psa_pake_operation_t *operation,
7964 psa_pake_step_t step,
7965 uint8_t *output,
7966 size_t output_size,
7967 size_t *output_length)
7968{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007969 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007970 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007971 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007972
7973 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007974 status = psa_pake_complete_inputs(operation);
7975 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007976 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007977 }
7978 }
7979
7980 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007981 status = PSA_ERROR_BAD_STATE;
7982 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007983 }
7984
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007985 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007986 status = PSA_ERROR_INVALID_ARGUMENT;
7987 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007988 }
7989
Przemek Stekiele12ed362022-12-21 12:54:46 +01007990 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007991#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007992 case PSA_ALG_JPAKE:
7993 status = psa_jpake_output_prologue(operation, step);
7994 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007995 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007996 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007997 driver_step = convert_jpake_computation_stage_to_driver_step(
7998 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007999 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008000#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008001 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008002 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008003 status = PSA_ERROR_NOT_SUPPORTED;
8004 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008005 }
8006
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008007 status = psa_driver_wrapper_pake_output(operation, driver_step,
8008 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008009
8010 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008011 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008012 }
8013
8014 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008015#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008016 case PSA_ALG_JPAKE:
8017 status = psa_jpake_output_epilogue(operation);
8018 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008019 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008020 }
8021 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008022#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008023 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008024 status = PSA_ERROR_NOT_SUPPORTED;
8025 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008026 }
8027
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008028 return PSA_SUCCESS;
8029exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008030 psa_pake_abort(operation);
8031 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008032}
8033
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008034#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008035static psa_status_t psa_jpake_input_prologue(
8036 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008037 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008038{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008039 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8040 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8041 step != PSA_PAKE_STEP_ZK_PROOF) {
8042 return PSA_ERROR_INVALID_ARGUMENT;
8043 }
8044
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008045 psa_jpake_computation_stage_t *computation_stage =
8046 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008047
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008048 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
8049 return PSA_ERROR_BAD_STATE;
8050 }
8051
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008052 if (computation_stage->state != PSA_PAKE_STATE_READY &&
8053 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
8054 computation_stage->state != PSA_PAKE_INPUT_X4S) {
8055 return PSA_ERROR_BAD_STATE;
8056 }
8057
8058 if (computation_stage->state == PSA_PAKE_STATE_READY) {
8059 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01008060 return PSA_ERROR_BAD_STATE;
8061 }
8062
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008063 switch (computation_stage->input_step) {
8064 case PSA_PAKE_STEP_X1_X2:
8065 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008066 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008067 case PSA_PAKE_STEP_X2S:
8068 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008069 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008070 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01008071 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008072 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008073
8074 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
8075 }
8076
8077 /* Check if step matches current sequence */
8078 switch (computation_stage->sequence) {
8079 case PSA_PAKE_X1_STEP_KEY_SHARE:
8080 case PSA_PAKE_X2_STEP_KEY_SHARE:
8081 if (step != PSA_PAKE_STEP_KEY_SHARE) {
8082 return PSA_ERROR_BAD_STATE;
8083 }
8084 break;
8085
8086 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
8087 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
8088 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
8089 return PSA_ERROR_BAD_STATE;
8090 }
8091 break;
8092
8093 case PSA_PAKE_X1_STEP_ZK_PROOF:
8094 case PSA_PAKE_X2_STEP_ZK_PROOF:
8095 if (step != PSA_PAKE_STEP_ZK_PROOF) {
8096 return PSA_ERROR_BAD_STATE;
8097 }
8098 break;
8099
8100 default:
8101 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008102 }
8103
8104 return PSA_SUCCESS;
8105}
8106
Przemek Stekiele12ed362022-12-21 12:54:46 +01008107static psa_status_t psa_jpake_input_epilogue(
8108 psa_pake_operation_t *operation)
8109{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008110 psa_jpake_computation_stage_t *computation_stage =
8111 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008112
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008113 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01008114 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008115 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01008116 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008117 computation_stage->state = PSA_PAKE_STATE_READY;
8118 computation_stage->input_step++;
8119 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
8120 } else {
8121 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008122 }
8123
8124 return PSA_SUCCESS;
8125}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008126#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008127
Neil Armstronga7d08c32022-06-01 18:21:20 +02008128psa_status_t psa_pake_input(
8129 psa_pake_operation_t *operation,
8130 psa_pake_step_t step,
8131 const uint8_t *input,
8132 size_t input_length)
8133{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008134 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008135 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008136 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8137 operation->primitive,
8138 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008139
8140 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008141 status = psa_pake_complete_inputs(operation);
8142 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008143 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008144 }
8145 }
8146
8147 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008148 status = PSA_ERROR_BAD_STATE;
8149 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008150 }
8151
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008152 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008153 status = PSA_ERROR_INVALID_ARGUMENT;
8154 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008155 }
8156
Przemek Stekiele12ed362022-12-21 12:54:46 +01008157 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008158#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008159 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008160 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008161 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008162 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008163 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008164 driver_step = convert_jpake_computation_stage_to_driver_step(
8165 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008166 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008167#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008168 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008169 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008170 status = PSA_ERROR_NOT_SUPPORTED;
8171 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008172 }
8173
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008174 status = psa_driver_wrapper_pake_input(operation, driver_step,
8175 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008176
8177 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008178 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008179 }
8180
8181 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008182#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008183 case PSA_ALG_JPAKE:
8184 status = psa_jpake_input_epilogue(operation);
8185 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008186 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008187 }
8188 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008189#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008190 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008191 status = PSA_ERROR_NOT_SUPPORTED;
8192 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008193 }
8194
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008195 return PSA_SUCCESS;
8196exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008197 psa_pake_abort(operation);
8198 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008199}
8200
8201psa_status_t psa_pake_get_implicit_key(
8202 psa_pake_operation_t *operation,
8203 psa_key_derivation_operation_t *output)
8204{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008205 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008206 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008207 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008208 size_t shared_key_len = 0;
8209
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008210 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008211 status = PSA_ERROR_BAD_STATE;
8212 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008213 }
8214
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008215#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008216 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008217 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008218 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008219 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
8220 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008221 status = PSA_ERROR_BAD_STATE;
8222 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008223 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008224 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008225#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008226 {
8227 status = PSA_ERROR_NOT_SUPPORTED;
8228 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008229 }
8230
Przemek Stekiel0c781802022-11-29 14:53:13 +01008231 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8232 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008233 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008234 &shared_key_len);
8235
8236 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008237 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008238 }
8239
8240 status = psa_key_derivation_input_bytes(output,
8241 PSA_KEY_DERIVATION_INPUT_SECRET,
8242 shared_key,
8243 shared_key_len);
8244
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008245 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008246exit:
8247 abort_status = psa_pake_abort(operation);
8248 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008249}
8250
8251psa_status_t psa_pake_abort(
8252 psa_pake_operation_t *operation)
8253{
Przemek Stekield69dca92023-01-31 19:59:20 +01008254 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008255
Przemek Stekield69dca92023-01-31 19:59:20 +01008256 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008257 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008258 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008259
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008260 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8261 if (operation->data.inputs.password != NULL) {
8262 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008263 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008264 mbedtls_free(operation->data.inputs.password);
8265 }
8266 if (operation->data.inputs.user != NULL) {
8267 mbedtls_free(operation->data.inputs.user);
8268 }
8269 if (operation->data.inputs.peer != NULL) {
8270 mbedtls_free(operation->data.inputs.peer);
8271 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008272 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008273 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008274
Przemek Stekield69dca92023-01-31 19:59:20 +01008275 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008276}
Neil Armstronga7d08c32022-06-01 18:21:20 +02008277
Gilles Peskinee59236f2018-01-27 23:32:46 +01008278#endif /* MBEDTLS_PSA_CRYPTO_C */