blob: bc19ed07c7f21600583fcb9ae8753a6c4bf8cdb4 [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"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
John Durkop07cc04a2020-11-16 22:08:34 -080025#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
26#include "check_crypto_config.h"
27#endif
28
Gilles Peskinee59236f2018-01-27 23:32:46 +010029#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010030#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031
Ronald Crond6d28882020-12-14 14:56:02 +010032#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020035#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010036#include "psa_crypto_ecp.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010037#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010038#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010039#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010040#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020041#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020042#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020043#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010044#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010045/* Include internal declarations that are useful for implementing persistently
46 * stored keys. */
47#include "psa_crypto_storage.h"
48
Gilles Peskineb2b64d32020-12-14 16:43:58 +010049#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010050
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010051#include <stdlib.h>
52#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010054
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010055#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020056#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000057#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020058#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020060#include "mbedtls/chacha20.h"
61#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/cipher.h"
63#include "mbedtls/ccm.h"
64#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010065#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020066#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010067#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010068#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010069#include "mbedtls/error.h"
70#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010072#include "mbedtls/md.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000073#include "md_wrap.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010074#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000075#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010076#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000077#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010079#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/sha1.h"
81#include "mbedtls/sha256.h"
82#include "mbedtls/sha512.h"
Paul Elliott588f8ed2022-12-02 18:10:26 +000083#include "hash_info.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010084
Gilles Peskine449bd832023-01-11 14:50:10 +010085#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020086
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020087#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
88 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
89 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020090#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020091#endif
92
Przemek Stekielfde11282023-03-13 16:06:09 +010093/* The only two JPAKE user/peer identifiers supported for the time being. */
94static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
95static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
Przemek Stekiel18cd6c92023-03-06 15:40:35 +010096
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010097/****************************************************************/
98/* Global data, support functions and library management */
99/****************************************************************/
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200102{
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200104}
105
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100106/* Values for psa_global_data_t::rng_state */
107#define RNG_NOT_INITIALIZED 0
108#define RNG_INITIALIZED 1
109#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100112 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100113 unsigned rng_state : 2;
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100114 unsigned drivers_initialized : 1;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100115 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100116} psa_global_data_t;
117
118static psa_global_data_t global_data;
119
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100120#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
121mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
122 &global_data.rng.drbg;
123#endif
124
itayzafrir0adf0fc2018-09-06 16:24:41 +0300125#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 if (global_data.initialized == 0) \
127 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300128
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100129int psa_can_do_hash(psa_algorithm_t hash_alg)
130{
131 (void) hash_alg;
132 return global_data.drivers_initialized;
133}
134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100136{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100137 /* Mbed TLS error codes can combine a high-level error code and a
138 * low-level error code. The low-level error usually reflects the
139 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 int low_level_ret = -(-ret & 0x007f);
141 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100142 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100144
145 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
146 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200148 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
149 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
150 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
151 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
152 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200154 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200156 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200158
Jaeden Amero93e21112019-02-20 13:57:28 +0000159#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000160 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100162 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100164
165 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100167 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100169
Gilles Peskine26869f22019-05-06 15:25:00 +0200170 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200172
173 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200175 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200177
Gilles Peskinea5905292018-02-07 20:59:33 +0100178 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100180 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100182 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100184 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100186 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100188 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100190 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
194 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100195 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
196 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100199 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
200 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100202 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100204#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100205
206 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100208
Gilles Peskinee59236f2018-01-27 23:32:46 +0100209 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
210 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
211 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100213
214 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200216 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100218 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100220
Gilles Peskine82e57d12020-11-13 21:31:17 +0100221#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100223 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
224 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100225 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100227 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
228 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100230 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100232#endif
233
Gilles Peskinea5905292018-02-07 20:59:33 +0100234 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100236 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100238 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100240 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100242
Gilles Peskinef76aa772018-10-29 19:24:33 +0100243 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100245 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100247 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
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_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100251 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100253 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
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_NOT_ACCEPTABLE:
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_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100259
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100260 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100262 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
263 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100265 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100267 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
268 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100270 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100272 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
273 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100275 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100277 case MBEDTLS_ERR_PK_INVALID_ALG:
278 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
279 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200283 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100285
Gilles Peskineff2d2002019-05-06 15:26:23 +0200286 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200288 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200290
Gilles Peskinea5905292018-02-07 20:59:33 +0100291 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100293 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100295 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100297 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100299 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
300 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100302 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200308
itayzafrir5c753392018-05-08 11:18:38 +0300309 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300310 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300312 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300314 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300316 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
317 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300319 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100321 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100323
Paul Elliott588f8ed2022-12-02 18:10:26 +0000324 case MBEDTLS_ERR_ECP_IN_PROGRESS:
325 return PSA_OPERATION_INCOMPLETE;
326
Janos Follatha13b9052019-11-22 12:48:59 +0000327 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300329
Gilles Peskinee59236f2018-01-27 23:32:46 +0100330 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100332 }
333}
334
Paul Elliottdc42ca82023-02-24 18:11:59 +0000335/**
336 * \brief For output buffers which contain "tags"
337 * (outputs that may be checked for validity like
338 * hashes, MACs and signatures), fill the unused
339 * part of the output buffer (the whole buffer on
340 * error, the trailing part on success) with
341 * something that isn't a valid tag (barring an
342 * attack on the tag and deliberately-crafted
343 * input), in case the caller doesn't check the
344 * return status properly.
345 *
346 * \param output_buffer Pointer to buffer to wipe. May not be NULL
347 * unless \p output_buffer_size is zero.
348 * \param status Status of function called to generate
349 * output_buffer originally
350 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
351 * could be NULL.
352 * \param output_buffer_length Length of data written to output_buffer, must be
353 * less than \p output_buffer_size
354 */
355static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000356 size_t output_buffer_size, size_t output_buffer_length)
357{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000358 size_t offset = 0;
359
360 if (output_buffer_size == 0) {
361 /* If output_buffer_size is 0 then we have nothing to do. We must not
362 call memset because output_buffer may be NULL in this case */
363 return;
364 }
365
366 if (status == PSA_SUCCESS) {
367 offset = output_buffer_length;
368 }
369
370 memset(output_buffer + offset, '!', output_buffer_size - offset);
371}
372
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200373
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200374
375
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100376/****************************************************************/
377/* Key management */
378/****************************************************************/
379
Joakim Anderssonbb576fe2023-03-01 11:23:02 +0100380#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
381 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cronf467d632021-11-19 18:02:34 +0100382 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
383 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
384 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100385mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
386 size_t bits,
387 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200388{
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100390 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100392#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100393 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100395#endif
396#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100397 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100399#endif
400#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100401 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100403#endif
404#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100405 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100407#endif
408#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100409 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100411 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if (bits_is_sloppy) {
413 return MBEDTLS_ECP_DP_SECP521R1;
414 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100415 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100416#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100417 }
418 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100419
Paul Elliott8ff510a2020-06-02 17:19:28 +0100420 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100422#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100423 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100425#endif
426#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100427 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100429#endif
430#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100431 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100433#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100434 }
435 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100436
Paul Elliott8ff510a2020-06-02 17:19:28 +0100437 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100439#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100440 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100442 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if (bits_is_sloppy) {
444 return MBEDTLS_ECP_DP_CURVE25519;
445 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100446 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100447#endif
448#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100449 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100451#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100452 }
453 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100454
Paul Elliott8ff510a2020-06-02 17:19:28 +0100455 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100457#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100458 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100460#endif
461#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100462 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100464#endif
465#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100466 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100468#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100469 }
470 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200471 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100472
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100473 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200475}
Joakim Anderssonbb576fe2023-03-01 11:23:02 +0100476#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
477 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Ronald Cronf467d632021-11-19 18:02:34 +0100478 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
479 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
480 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
483 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200484{
485 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200487 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200488 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200489 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200490 case PSA_KEY_TYPE_PASSWORD:
491 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200492 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100493#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200494 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if (bits != 128 && bits != 192 && bits != 256) {
496 return PSA_ERROR_INVALID_ARGUMENT;
497 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200498 break;
499#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200500#if defined(PSA_WANT_KEY_TYPE_ARIA)
501 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 if (bits != 128 && bits != 192 && bits != 256) {
503 return PSA_ERROR_INVALID_ARGUMENT;
504 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200505 break;
506#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100507#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200508 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if (bits != 128 && bits != 192 && bits != 256) {
510 return PSA_ERROR_INVALID_ARGUMENT;
511 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200512 break;
513#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100514#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200515 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 if (bits != 64 && bits != 128 && bits != 192) {
517 return PSA_ERROR_INVALID_ARGUMENT;
518 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200519 break;
520#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100521#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200522 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 if (bits != 256) {
524 return PSA_ERROR_INVALID_ARGUMENT;
525 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200526 break;
527#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200528 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200530 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 if (bits % 8 != 0) {
532 return PSA_ERROR_INVALID_ARGUMENT;
533 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200536}
537
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100538/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100539 *
Steven Cooremancd640932021-03-08 12:00:27 +0100540 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
541 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100542 *
543 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100544 * \param[in] key_type The key type of the key to be used with the
545 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100546 *
547 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100548 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100549 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100550 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100551 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100552MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100553 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100555{
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (PSA_ALG_IS_HMAC(algorithm)) {
557 if (key_type == PSA_KEY_TYPE_HMAC) {
558 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100559 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100560 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
563 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
564 * key. */
565 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
566 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
567 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
568 * the block length (larger than 1) for block ciphers. */
569 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
570 return PSA_SUCCESS;
571 }
572 }
573 }
574
575 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100576}
577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
579 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200580{
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 if (slot->key.data != NULL) {
582 return PSA_ERROR_ALREADY_EXISTS;
583 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200584
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 slot->key.data = mbedtls_calloc(1, buffer_length);
586 if (slot->key.data == NULL) {
587 return PSA_ERROR_INSUFFICIENT_MEMORY;
588 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200589
Ronald Cronea0f8a62020-11-25 17:52:23 +0100590 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200592}
593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
595 const uint8_t *data,
596 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200597{
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 psa_status_t status = psa_allocate_buffer_to_slot(slot,
599 data_length);
600 if (status != PSA_SUCCESS) {
601 return status;
602 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 memcpy(slot->key.data, data, data_length);
605 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200606}
607
Ronald Crona0fe59f2020-11-29 11:14:53 +0100608psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100609 const psa_key_attributes_t *attributes,
610 const uint8_t *data, size_t data_length,
611 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100613{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100614 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
615 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100616
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200617 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 if (data_length == 0) {
619 return PSA_ERROR_NOT_SUPPORTED;
620 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200621
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 if (key_type_is_raw_bytes(type)) {
623 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
626 *bits);
627 if (status != PSA_SUCCESS) {
628 return status;
629 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200630
Ronald Crondd04d422020-11-28 15:14:42 +0100631 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100633 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 return PSA_SUCCESS;
637 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800638#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
640 if (PSA_KEY_TYPE_IS_ECC(type)) {
641 return mbedtls_psa_ecp_import_key(attributes,
642 data, data_length,
643 key_buffer, key_buffer_size,
644 key_buffer_length,
645 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100646 }
John Durkop9814fa22020-11-04 12:28:15 -0800647#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
648 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700649#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
651 if (PSA_KEY_TYPE_IS_RSA(type)) {
652 return mbedtls_psa_rsa_import_key(attributes,
653 data, data_length,
654 key_buffer, key_buffer_size,
655 key_buffer_length,
656 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200657 }
John Durkop9814fa22020-11-04 12:28:15 -0800658#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
659 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100660 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100663}
664
Gilles Peskinef603c712019-01-19 13:40:11 +0100665/** Calculate the intersection of two algorithm usage policies.
666 *
667 * Return 0 (which allows no operation) on incompatibility.
668 */
669static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100670 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100671 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100673{
Gilles Peskine549ea862019-05-22 11:45:59 +0200674 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 if (alg1 == alg2) {
676 return alg1;
677 }
Steven Cooreman03488022021-02-18 12:07:20 +0100678 /* If the policies are from the same hash-and-sign family, check
679 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
681 PSA_ALG_IS_SIGN_HASH(alg2) &&
682 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
683 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
684 return alg2;
685 }
686 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
687 return alg1;
688 }
Steven Cooreman03488022021-02-18 12:07:20 +0100689 }
690 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100691 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100692 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
694 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
695 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
696 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
697 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100698 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100699
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100700 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
702 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
703 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
704 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100705 }
706 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
708 (alg1_len <= alg2_len)) {
709 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100710 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
712 (alg2_len <= alg1_len)) {
713 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100714 }
715 }
716 /* If the policies are from the same MAC family, check whether one
717 * of them is a minimum-MAC-length policy. Calculate the most
718 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
720 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
721 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100722 /* Validate the combination of key type and algorithm. Since the base
723 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
725 return 0;
726 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100727
Steven Cooreman31a876d2021-03-03 20:47:40 +0100728 /* Get the (exact or at-least) output lengths for both sides of the
729 * requested intersection. None of the currently supported algorithms
730 * have an output length dependent on the actual key size, so setting it
731 * to a bogus value of 0 is currently OK.
732 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100733 * Note that for at-least-this-length wildcard algorithms, the output
734 * length is set to the shortest allowed length, which allows us to
735 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
737 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100738 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100739
Steven Cooremana1d83222021-02-25 10:20:29 +0100740 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
742 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
743 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100744 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100745
746 /* If only one is an at-least-this-length policy, the intersection would
747 * be the other (fixed-length) policy as long as said fixed length is
748 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
750 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100751 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
753 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100754 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100755
Steven Cooremancd640932021-03-08 12:00:27 +0100756 /* If none of them are wildcards, check whether they define the same tag
757 * length. This is still possible here when one is default-length and
758 * the other specific-length. Ensure to always return the
759 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 if (alg1_len == alg2_len) {
761 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
762 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100763 }
764 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100766}
767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768static int psa_key_algorithm_permits(psa_key_type_t key_type,
769 psa_algorithm_t policy_alg,
770 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200771{
Gilles Peskine549ea862019-05-22 11:45:59 +0200772 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (requested_alg == policy_alg) {
774 return 1;
775 }
Steven Cooreman03488022021-02-18 12:07:20 +0100776 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
777 * and requested_alg is the same hash-and-sign family with any hash,
778 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
780 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
781 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
782 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100783 }
784 /* If policy_alg is a wildcard AEAD algorithm of the same base as
785 * the requested algorithm, check the requested tag length to be
786 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 if (PSA_ALG_IS_AEAD(policy_alg) &&
788 PSA_ALG_IS_AEAD(requested_alg) &&
789 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
790 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
791 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
792 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
793 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100794 }
Steven Cooremancd640932021-03-08 12:00:27 +0100795 /* If policy_alg is a MAC algorithm of the same base as the requested
796 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 if (PSA_ALG_IS_MAC(policy_alg) &&
798 PSA_ALG_IS_MAC(requested_alg) &&
799 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
800 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100801 /* Validate the combination of key type and algorithm. Since the policy
802 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
804 return 0;
805 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100806
Steven Cooreman31a876d2021-03-03 20:47:40 +0100807 /* Get both the requested output length for the algorithm which is to be
808 * verified, and the default output length for the base algorithm.
809 * Note that none of the currently supported algorithms have an output
810 * length dependent on actual key size, so setting it to a bogus value
811 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100812 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100814 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 key_type, 0,
816 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100817
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100818 /* If the policy is default-length, only allow an algorithm with
819 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
821 return requested_output_length == default_output_length;
822 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100823
824 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100825 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
827 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
828 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100829 }
830
Steven Cooremancd640932021-03-08 12:00:27 +0100831 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
832 * check for the requested MAC length to be equal to or longer than the
833 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
835 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
836 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100837 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200838 }
Steven Cooremance48e852020-10-05 16:02:45 +0200839 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200840 * a key derivation with that key agreement should also be allowed. This
841 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
843 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
844 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
845 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200846 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100847 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200849}
850
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100851/** Test whether a policy permits an algorithm.
852 *
853 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100854 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100855 * \note This function requires providing the key type for which the policy is
856 * being validated, since some algorithm policy definitions (e.g. MAC)
857 * have different properties depending on what kind of cipher it is
858 * combined with.
859 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100860 * \retval PSA_SUCCESS When \p alg is a specific algorithm
861 * allowed by the \p policy.
862 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
863 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
864 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100865 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100866static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
867 psa_key_type_t key_type,
868 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100869{
Steven Cooremand788fab2021-02-25 11:29:17 +0100870 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 if (alg == 0) {
872 return PSA_ERROR_INVALID_ARGUMENT;
873 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100874
Steven Cooreman7e39f052021-02-23 14:18:32 +0100875 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 if (PSA_ALG_IS_WILDCARD(alg)) {
877 return PSA_ERROR_INVALID_ARGUMENT;
878 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100879
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
881 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
882 return PSA_SUCCESS;
883 } else {
884 return PSA_ERROR_NOT_PERMITTED;
885 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100886}
887
Gilles Peskinef603c712019-01-19 13:40:11 +0100888/** Restrict a key policy based on a constraint.
889 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100890 * \note This function requires providing the key type for which the policy is
891 * being restricted, since some algorithm policy definitions (e.g. MAC)
892 * have different properties depending on what kind of cipher it is
893 * combined with.
894 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100895 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100896 * \param[in,out] policy The policy to restrict.
897 * \param[in] constraint The policy constraint to apply.
898 *
899 * \retval #PSA_SUCCESS
900 * \c *policy contains the intersection of the original value of
901 * \c *policy and \c *constraint.
902 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100903 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100904 * \c *policy is unchanged.
905 */
906static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100907 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100908 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100910{
911 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 psa_key_policy_algorithm_intersection(key_type, policy->alg,
913 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200914 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
916 constraint->alg2);
917 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
918 return PSA_ERROR_INVALID_ARGUMENT;
919 }
920 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
921 return PSA_ERROR_INVALID_ARGUMENT;
922 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100923 policy->usage &= constraint->usage;
924 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200925 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100927}
928
Przemek Stekiel061f6942022-11-30 10:51:35 +0100929/** Get the description of a key given its identifier and policy constraints
930 * and lock it.
931 *
932 * The key must have allow all the usage flags set in \p usage. If \p alg is
933 * nonzero, the key must allow operations with this algorithm. If \p alg is
934 * zero, the algorithm is not checked.
935 *
936 * In case of a persistent key, the function loads the description of the key
937 * into a key slot if not already done.
938 *
939 * On success, the returned key slot is locked. It is the responsibility of
940 * the caller to unlock the key slot when it does not access it anymore.
941 */
942static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100943 mbedtls_svc_key_id_t key,
944 psa_key_slot_t **p_slot,
945 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100947{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200948 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100949 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100950
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 status = psa_get_and_lock_key_slot(key, p_slot);
952 if (status != PSA_SUCCESS) {
953 return status;
954 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200955 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100956
957 /* Enforce that usage policy for the key slot contains all the flags
958 * required by the usage parameter. There is one exception: public
959 * keys can always be exported, so we treat public key objects as
960 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100962 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200964
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100966 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200967 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100968 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100969
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800970 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 if (alg != 0) {
972 status = psa_key_policy_permits(&slot->attr.policy,
973 slot->attr.type,
974 alg);
975 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100976 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100978 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100979
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200981
982error:
983 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200985
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100987}
Darryl Green940d72c2018-07-13 13:18:51 +0100988
Ronald Cron5c522922020-11-14 16:35:34 +0100989/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200990 *
991 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200992 * available, as opposed to a key in a secure element and/or to be used
993 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200994 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200995 * This is a temporary function that may be used instead of
996 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
997 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200998 *
Ronald Cron5c522922020-11-14 16:35:34 +0100999 * On success, the returned key slot is locked. It is the responsibility of the
1000 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001001 */
Ronald Cron5c522922020-11-14 16:35:34 +01001002static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1003 mbedtls_svc_key_id_t key,
1004 psa_key_slot_t **p_slot,
1005 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001007{
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1009 usage, alg);
1010 if (status != PSA_SUCCESS) {
1011 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001012 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1015 psa_unlock_key_slot(*p_slot);
1016 *p_slot = NULL;
1017 return PSA_ERROR_NOT_SUPPORTED;
1018 }
1019
1020 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001021}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001024{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001025 /* Data pointer will always be either a valid pointer or NULL in an
1026 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if (slot->key.data != NULL) {
1028 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1029 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001032 slot->key.data = NULL;
1033 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001034
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001036}
1037
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001038/** Completely wipe a slot in memory, including its policy.
1039 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001040psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001041{
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001043
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 /*
1045 * As the return error code may not be handled in case of multiple errors,
1046 * do our best to report an unexpected lock counter. Assert with
1047 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1048 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1049 * function is called as part of the execution of a test suite, the
1050 * execution of the test suite is stopped in error if the assertion fails.
1051 */
1052 if (slot->lock_count != 1) {
1053 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001054 status = PSA_ERROR_CORRUPTION_DETECTED;
1055 }
1056
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001057 /* Multipart operations may still be using the key. This is safe
1058 * because all multipart operation objects are independent from
1059 * the key slot: if they need to access the key after the setup
1060 * phase, they have a copy of the key. Note that this means that
1061 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001062 /* At this point, key material and other type-specific content has
1063 * been wiped. Clear remaining metadata. We can call memset and not
1064 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 memset(slot, 0, sizeof(*slot));
1066 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001067}
1068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001070{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001071 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001072 psa_status_t status; /* status of the last operation */
1073 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001074#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1075 psa_se_drv_table_entry_t *driver;
1076#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001077
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 if (mbedtls_svc_key_id_is_null(key)) {
1079 return PSA_SUCCESS;
1080 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001081
Ronald Cronf2911112020-10-29 17:51:10 +01001082 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001083 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001084 * key, this will load the key description from persistent memory if not
1085 * done yet. We cannot avoid this loading as without it we don't know if
1086 * the key is operated by an SE or not and this information is needed by
1087 * the current implementation.
1088 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 status = psa_get_and_lock_key_slot(key, &slot);
1090 if (status != PSA_SUCCESS) {
1091 return status;
1092 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001093
Ronald Cronf2911112020-10-29 17:51:10 +01001094 /*
1095 * If the key slot containing the key description is under access by the
1096 * library (apart from the present access), the key cannot be destroyed
1097 * yet. For the time being, just return in error. Eventually (to be
1098 * implemented), the key should be destroyed when all accesses have
1099 * stopped.
1100 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 if (slot->lock_count > 1) {
1102 psa_unlock_key_slot(slot);
1103 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001104 }
1105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001107 /* Refuse the destruction of a read-only key (which may or may not work
1108 * if we attempt it, depending on whether the key is merely read-only
1109 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001110 * Just do the best we can, which is to wipe the copy in memory
1111 * (done in this function's cleanup code). */
1112 overall_status = PSA_ERROR_NOT_PERMITTED;
1113 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001114 }
1115
Gilles Peskine354f7672019-07-12 23:46:38 +02001116#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1118 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001119 /* For a key in a secure element, we need to do three things:
1120 * remove the key file in internal storage, destroy the
1121 * key inside the secure element, and update the driver's
1122 * persistent data. Start a transaction that will encompass these
1123 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001125 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001127 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 status = psa_crypto_save_transaction();
1129 if (status != PSA_SUCCESS) {
1130 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001131 /* We should still try to destroy the key in the secure
1132 * element and the key metadata in storage. This is especially
1133 * important if the error is that the storage is full.
1134 * But how to do it exactly without risking an inconsistent
1135 * state after a reset?
1136 * https://github.com/ARMmbed/mbed-crypto/issues/215
1137 */
1138 overall_status = status;
1139 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001140 }
1141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 status = psa_destroy_se_key(driver,
1143 psa_key_slot_get_slot_number(slot));
1144 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001145 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001147 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001148#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1149
Darryl Greend49a4992018-06-18 17:27:26 +01001150#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1152 status = psa_destroy_persistent_key(slot->attr.id);
1153 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001154 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001156
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001157 /* TODO: other slots may have a copy of the same key. We should
1158 * invalidate them.
1159 * https://github.com/ARMmbed/mbed-crypto/issues/214
1160 */
Darryl Greend49a4992018-06-18 17:27:26 +01001161 }
1162#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001163
Gilles Peskinefc762652019-07-22 19:30:34 +02001164#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 if (driver != NULL) {
1166 status = psa_save_se_persistent_data(driver);
1167 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001168 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 }
1170 status = psa_crypto_stop_transaction();
1171 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001172 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001174 }
1175#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1176
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001177exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001179 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001181 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 }
1183 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001184}
1185
John Durkop07cc04a2020-11-16 22:08:34 -08001186#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001187 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001188static psa_status_t psa_get_rsa_public_exponent(
1189 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001191{
1192 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001193 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001194 uint8_t *buffer = NULL;
1195 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001197
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1199 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001200 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 }
1202 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001203 /* It's the default value, which is reported as an empty string,
1204 * so there's nothing to do. */
1205 goto exit;
1206 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 buflen = mbedtls_mpi_size(&mpi);
1209 buffer = mbedtls_calloc(1, buflen);
1210 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001211 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1212 goto exit;
1213 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1215 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001216 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001218 attributes->domain_parameters = buffer;
1219 attributes->domain_parameters_size = buflen;
1220
1221exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 mbedtls_mpi_free(&mpi);
1223 if (ret != 0) {
1224 mbedtls_free(buffer);
1225 }
1226 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001227}
John Durkop07cc04a2020-11-16 22:08:34 -08001228#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001229 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001230
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001231/** Retrieve all the publicly-accessible attributes of a key.
1232 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001233psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1234 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001235{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001236 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001237 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001238 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001239
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1243 if (status != PSA_SUCCESS) {
1244 return status;
1245 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001246
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001247 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1249 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001250
1251#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1253 psa_set_key_slot_number(attributes,
1254 psa_key_slot_get_slot_number(slot));
1255 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001256#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001259#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001260 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001261 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001262 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001263 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001264 * is not yet implemented.
1265 * https://github.com/ARMmbed/mbed-crypto/issues/216
1266 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001268 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001269
Ronald Cron90857082020-11-25 14:58:33 +01001270 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 slot->attr.type,
1272 slot->key.data,
1273 slot->key.bytes,
1274 &rsa);
1275 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001276 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 status = psa_get_rsa_public_exponent(rsa,
1280 attributes);
1281 mbedtls_rsa_free(rsa);
1282 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001283 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001284 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001285#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001286 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001287 default:
1288 /* Nothing else to do. */
1289 break;
1290 }
1291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 if (status != PSA_SUCCESS) {
1293 psa_reset_key_attributes(attributes);
1294 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001299}
1300
Gilles Peskinec8000c02019-08-02 20:15:51 +02001301#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1302psa_status_t psa_get_key_slot_number(
1303 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001305{
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001307 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 return PSA_SUCCESS;
1309 } else {
1310 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001311 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001312}
1313#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1316 size_t key_buffer_size,
1317 uint8_t *data,
1318 size_t data_size,
1319 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001320{
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 if (key_buffer_size > data_size) {
1322 return PSA_ERROR_BUFFER_TOO_SMALL;
1323 }
1324 memcpy(data, key_buffer, key_buffer_size);
1325 memset(data + key_buffer_size, 0,
1326 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001327 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001329}
1330
Ronald Cron7285cda2020-11-26 14:46:37 +01001331psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001332 const psa_key_attributes_t *attributes,
1333 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001335{
Ronald Crond18b5f82020-11-25 16:46:05 +01001336 psa_key_type_t type = attributes->core.type;
1337
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 if (key_type_is_raw_bytes(type) ||
1339 PSA_KEY_TYPE_IS_RSA(type) ||
1340 PSA_KEY_TYPE_IS_ECC(type)) {
1341 return psa_export_key_buffer_internal(
1342 key_buffer, key_buffer_size,
1343 data, data_size, data_length);
1344 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001345 /* This shouldn't happen in the reference implementation, but
1346 it is valid for a special-purpose implementation to omit
1347 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001349 }
1350}
1351
Gilles Peskine449bd832023-01-11 14:50:10 +01001352psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1353 uint8_t *data,
1354 size_t data_size,
1355 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001356{
1357 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1358 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1359 psa_key_slot_t *slot;
1360
Ronald Crone907e552021-01-18 13:22:38 +01001361 /* Reject a zero-length output buffer now, since this can never be a
1362 * valid key representation. This way we know that data must be a valid
1363 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 if (data_size == 0) {
1365 return PSA_ERROR_BUFFER_TOO_SMALL;
1366 }
Ronald Crone907e552021-01-18 13:22:38 +01001367
Ronald Cron9486f9d2020-11-26 13:36:23 +01001368 /* Set the key to empty now, so that even when there are errors, we always
1369 * set data_length to a value between 0 and data_size. On error, setting
1370 * the key to empty is a good choice because an empty key representation is
1371 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001372 *data_length = 0;
1373
Ronald Cron9486f9d2020-11-26 13:36:23 +01001374 /* Export requires the EXPORT flag. There is an exception for public keys,
1375 * which don't require any flag, but
1376 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1377 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1379 PSA_KEY_USAGE_EXPORT, 0);
1380 if (status != PSA_SUCCESS) {
1381 return status;
1382 }
mohammad160306e79202018-03-28 13:17:44 +03001383
Ronald Crond18b5f82020-11-25 16:46:05 +01001384 psa_key_attributes_t attributes = {
1385 .core = slot->attr
1386 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 status = psa_driver_wrapper_export_key(&attributes,
1388 slot->key.data, slot->key.bytes,
1389 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001392
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001394}
1395
Ronald Cron7285cda2020-11-26 14:46:37 +01001396psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001397 const psa_key_attributes_t *attributes,
1398 const uint8_t *key_buffer,
1399 size_t key_buffer_size,
1400 uint8_t *data,
1401 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001403{
Ronald Crond18b5f82020-11-25 16:46:05 +01001404 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1407 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001408 /* Exporting public -> public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 return psa_export_key_buffer_internal(
1410 key_buffer, key_buffer_size,
1411 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001412 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001413
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001415#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1417 return mbedtls_psa_rsa_export_public_key(attributes,
1418 key_buffer,
1419 key_buffer_size,
1420 data,
1421 data_size,
1422 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001423#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001424 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001426#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1427 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001429#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1431 return mbedtls_psa_ecp_export_public_key(attributes,
1432 key_buffer,
1433 key_buffer_size,
1434 data,
1435 data_size,
1436 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001437#else
1438 /* We don't know how to convert a private ECC key to public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001440#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1441 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001442 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001444 /* This shouldn't happen in the reference implementation, but
1445 it is valid for a special-purpose implementation to omit
1446 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001448 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001449}
Ronald Crond18b5f82020-11-25 16:46:05 +01001450
Gilles Peskine449bd832023-01-11 14:50:10 +01001451psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1452 uint8_t *data,
1453 size_t data_size,
1454 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001455{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001456 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001457 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001458 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001459
Ronald Crone907e552021-01-18 13:22:38 +01001460 /* Reject a zero-length output buffer now, since this can never be a
1461 * valid key representation. This way we know that data must be a valid
1462 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 if (data_size == 0) {
1464 return PSA_ERROR_BUFFER_TOO_SMALL;
1465 }
Ronald Crone907e552021-01-18 13:22:38 +01001466
Darryl Greendd8fb772018-11-07 16:00:44 +00001467 /* Set the key to empty now, so that even when there are errors, we always
1468 * set data_length to a value between 0 and data_size. On error, setting
1469 * the key to empty is a good choice because an empty key representation is
1470 * unlikely to be accepted anywhere. */
1471 *data_length = 0;
1472
1473 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1475 if (status != PSA_SUCCESS) {
1476 return status;
1477 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1480 status = PSA_ERROR_INVALID_ARGUMENT;
1481 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001482 }
1483
Ronald Crond18b5f82020-11-25 16:46:05 +01001484 psa_key_attributes_t attributes = {
1485 .core = slot->attr
1486 };
Ronald Cron67227982020-11-26 15:16:05 +01001487 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001488 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001490
1491exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001495}
1496
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001497MBEDTLS_STATIC_ASSERT(
1498 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1499 "One or more key attribute flag is listed as both external-only and dual-use")
1500MBEDTLS_STATIC_ASSERT(
1501 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1502 "One or more key attribute flag is listed as both internal-only and dual-use")
1503MBEDTLS_STATIC_ASSERT(
1504 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1505 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001506
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001507/** Validate that a key policy is internally well-formed.
1508 *
1509 * This function only rejects invalid policies. It does not validate the
1510 * consistency of the policy with respect to other attributes of the key
1511 * such as the key type.
1512 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001513static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001514{
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1516 PSA_KEY_USAGE_COPY |
1517 PSA_KEY_USAGE_ENCRYPT |
1518 PSA_KEY_USAGE_DECRYPT |
1519 PSA_KEY_USAGE_SIGN_MESSAGE |
1520 PSA_KEY_USAGE_VERIFY_MESSAGE |
1521 PSA_KEY_USAGE_SIGN_HASH |
1522 PSA_KEY_USAGE_VERIFY_HASH |
1523 PSA_KEY_USAGE_VERIFY_DERIVATION |
1524 PSA_KEY_USAGE_DERIVE)) != 0) {
1525 return PSA_ERROR_INVALID_ARGUMENT;
1526 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001527
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001529}
1530
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001531/** Validate the internal consistency of key attributes.
1532 *
1533 * This function only rejects invalid attribute values. If does not
1534 * validate the consistency of the attributes with any key data that may
1535 * be involved in the creation of the key.
1536 *
1537 * Call this function early in the key creation process.
1538 *
1539 * \param[in] attributes Key attributes for the new key.
1540 * \param[out] p_drv On any return, the driver for the key, if any.
1541 * NULL for a transparent key.
1542 *
1543 */
1544static psa_status_t psa_validate_key_attributes(
1545 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001547{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001548 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1550 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001551
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 status = psa_validate_key_location(lifetime, p_drv);
1553 if (status != PSA_SUCCESS) {
1554 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001555 }
1556
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 status = psa_validate_key_persistence(lifetime);
1558 if (status != PSA_SUCCESS) {
1559 return status;
1560 }
1561
1562 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1563 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1564 return PSA_ERROR_INVALID_ARGUMENT;
1565 }
1566 } else {
1567 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1568 return PSA_ERROR_INVALID_ARGUMENT;
1569 }
1570 }
1571
1572 status = psa_validate_key_policy(&attributes->core.policy);
1573 if (status != PSA_SUCCESS) {
1574 return status;
1575 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001576
1577 /* Refuse to create overly large keys.
1578 * Note that this doesn't trigger on import if the attributes don't
1579 * explicitly specify a size (so psa_get_key_bits returns 0), so
1580 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1582 return PSA_ERROR_NOT_SUPPORTED;
1583 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001584
Gilles Peskine91e8c332019-08-02 19:19:39 +02001585 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1587 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1588 return PSA_ERROR_INVALID_ARGUMENT;
1589 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001592}
1593
Gilles Peskine4747d192019-04-17 15:05:45 +02001594/** Prepare a key slot to receive key material.
1595 *
1596 * This function allocates a key slot and sets its metadata.
1597 *
1598 * If this function fails, call psa_fail_key_creation().
1599 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001600 * This function is intended to be used as follows:
1601 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001602 * it with the specified attributes, and in case of a volatile key assign it
1603 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001604 * -# Populate the slot with the key material.
1605 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1606 * In case of failure at any step, stop the sequence and call
1607 * psa_fail_key_creation().
1608 *
Ronald Cron5c522922020-11-14 16:35:34 +01001609 * On success, the key slot is locked. It is the responsibility of the caller
1610 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001611 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001612 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001613 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001614 * \param[out] p_slot On success, a pointer to the prepared slot.
1615 * \param[out] p_drv On any return, the driver for the key, if any.
1616 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001617 *
1618 * \retval #PSA_SUCCESS
1619 * The key slot is ready to receive key material.
1620 * \return If this function fails, the key slot is an invalid state.
1621 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001622 */
1623static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001624 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001625 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001626 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001628{
1629 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001630 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001631 psa_key_slot_t *slot;
1632
Gilles Peskinedf179142019-07-15 22:02:14 +02001633 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001634 *p_drv = NULL;
1635
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 status = psa_validate_key_attributes(attributes, p_drv);
1637 if (status != PSA_SUCCESS) {
1638 return status;
1639 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1642 if (status != PSA_SUCCESS) {
1643 return status;
1644 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001645 slot = *p_slot;
1646
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001647 /* We're storing the declared bit-size of the key. It's up to each
1648 * creation mechanism to verify that this information is correct.
1649 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001650 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001651 * is optional (import, copy). In case of a volatile key, assign it the
1652 * volatile key identifier associated to the slot returned to contain its
1653 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001654
1655 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001657#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1658 slot->attr.id = volatile_key_id;
1659#else
1660 slot->attr.id.key_id = volatile_key_id;
1661#endif
1662 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001663
Gilles Peskine91e8c332019-08-02 19:19:39 +02001664 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001665 * external-only flags, query `attributes`. Thanks to the check
1666 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001667 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001668 * may have set. */
1669 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001670
Gilles Peskinecbaff462019-07-12 23:46:04 +02001671#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001672 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001673 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001674 * create the key file in internal storage, create the
1675 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001676 * persistent data. This is done by starting a transaction that will
1677 * encompass these three actions.
1678 * For registering a volatile key, we just need to find an appropriate
1679 * slot number inside the SE. Since the key is designated volatile, creating
1680 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001681 /* The first thing to do is to find a slot number for the new key.
1682 * We save the slot number in persistent storage as part of the
1683 * transaction data. It will be needed to recover if the power
1684 * fails during the key creation process, to clean up on the secure
1685 * element side after restarting. Obtaining a slot number from the
1686 * secure element driver updates its persistent state, but we do not yet
1687 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001688 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001690 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1692 &slot_number);
1693 if (status != PSA_SUCCESS) {
1694 return status;
1695 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001696
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1698 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001699 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001700 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001701 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 status = psa_crypto_save_transaction();
1703 if (status != PSA_SUCCESS) {
1704 (void) psa_crypto_stop_transaction();
1705 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001706 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001707 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001708
1709 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001711 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001712
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001714 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001716 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001717#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1718
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001720}
Gilles Peskine4747d192019-04-17 15:05:45 +02001721
1722/** Finalize the creation of a key once its key material has been set.
1723 *
1724 * This entails writing the key to persistent storage.
1725 *
1726 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001727 * See the documentation of psa_start_key_creation() for the intended use
1728 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001729 *
Ronald Cron5c522922020-11-14 16:35:34 +01001730 * If the finalization succeeds, the function unlocks the key slot (it was
1731 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1732 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001733 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001734 * \param[in,out] slot Pointer to the slot with key material.
1735 * \param[in] driver The secure element driver for the key,
1736 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001737 * \param[out] key On success, identifier of the key. Note that the
1738 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001739 *
1740 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001741 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001742 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1743 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1744 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1745 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1746 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1747 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001748 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001749 * \return If this function fails, the key slot is an invalid state.
1750 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001751 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001752static psa_status_t psa_finish_key_creation(
1753 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001754 psa_se_drv_table_entry_t *driver,
1755 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001756{
1757 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001758 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001759 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001760
1761#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001763#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001765 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001766 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001768
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001769 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1770 sizeof(data.slot_number),
1771 "Slot number size does not match psa_se_key_data_storage_t");
1772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1774 status = psa_save_persistent_key(&slot->attr,
1775 (uint8_t *) &data,
1776 sizeof(data));
1777 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001778#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1779 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001780 /* Key material is saved in export representation in the slot, so
1781 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 status = psa_save_persistent_key(&slot->attr,
1783 slot->key.data,
1784 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001785 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001786 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001787#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1788
Gilles Peskinecbaff462019-07-12 23:46:04 +02001789#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001790 /* Finish the transaction for a key creation. This does not
1791 * happen when registering an existing key. Detect this case
1792 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001793 * creation of a persistent key in a secure element requires a transaction,
1794 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 if (driver != NULL &&
1796 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1797 status = psa_save_se_persistent_data(driver);
1798 if (status != PSA_SUCCESS) {
1799 psa_destroy_persistent_key(slot->attr.id);
1800 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001801 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001803 }
1804#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1805
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001807 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 status = psa_unlock_key_slot(slot);
1809 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001810 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001812 }
Ronald Cron50972942020-11-14 11:28:25 +01001813
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001815}
1816
1817/** Abort the creation of a key.
1818 *
1819 * You may call this function after calling psa_start_key_creation(),
1820 * or after psa_finish_key_creation() fails. In other circumstances, this
1821 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001822 * See the documentation of psa_start_key_creation() for the intended use
1823 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001824 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001825 * \param[in,out] slot Pointer to the slot with key material.
1826 * \param[in] driver The secure element driver for the key,
1827 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001828 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001829static void psa_fail_key_creation(psa_key_slot_t *slot,
1830 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001831{
Gilles Peskine011e4282019-06-26 18:34:38 +02001832 (void) driver;
1833
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001835 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001837
1838#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001839 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001840 * element, and the failure happened later (when saving metadata
1841 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001842 * element.
1843 * https://github.com/ARMmbed/mbed-crypto/issues/217
1844 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001845
Gilles Peskined7729582019-08-05 15:55:54 +02001846 /* Abort the ongoing transaction if any (there may not be one if
1847 * the creation process failed before starting one, or if the
1848 * key creation is a registration of a key in a secure element).
1849 * Earlier functions must already have done what it takes to undo any
1850 * partial creation. All that's left is to update the transaction data
1851 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001853#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1854
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001856}
1857
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001858/** Validate optional attributes during key creation.
1859 *
1860 * Some key attributes are optional during key creation. If they are
1861 * specified in the attributes structure, check that they are consistent
1862 * with the data in the slot.
1863 *
1864 * This function should be called near the end of key creation, after
1865 * the slot in memory is fully populated but before saving persistent data.
1866 */
1867static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001868 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001870{
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 if (attributes->core.type != 0) {
1872 if (attributes->core.type != slot->attr.type) {
1873 return PSA_ERROR_INVALID_ARGUMENT;
1874 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001875 }
1876
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001878#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1880 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001881 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001882 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001883 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001884
Ronald Cron90857082020-11-25 14:58:33 +01001885 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 slot->attr.type,
1887 slot->key.data,
1888 slot->key.bytes,
1889 &rsa);
1890 if (status != PSA_SUCCESS) {
1891 return status;
1892 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001893
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 mbedtls_mpi_init(&actual);
1895 mbedtls_mpi_init(&required);
1896 ret = mbedtls_rsa_export(rsa,
1897 NULL, NULL, NULL, NULL, &actual);
1898 mbedtls_rsa_free(rsa);
1899 mbedtls_free(rsa);
1900 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001901 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 }
1903 ret = mbedtls_mpi_read_binary(&required,
1904 attributes->domain_parameters,
1905 attributes->domain_parameters_size);
1906 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001907 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 }
1909 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001910 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 }
1912rsa_exit:
1913 mbedtls_mpi_free(&actual);
1914 mbedtls_mpi_free(&required);
1915 if (ret != 0) {
1916 return mbedtls_to_psa_error(ret);
1917 }
1918 } else
John Durkop9814fa22020-11-04 12:28:15 -08001919#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1920 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001921 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001923 }
1924 }
1925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 if (attributes->core.bits != 0) {
1927 if (attributes->core.bits != slot->attr.bits) {
1928 return PSA_ERROR_INVALID_ARGUMENT;
1929 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001930 }
1931
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001933}
1934
Gilles Peskine449bd832023-01-11 14:50:10 +01001935psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1936 const uint8_t *data,
1937 size_t data_length,
1938 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001939{
1940 psa_status_t status;
1941 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001942 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001943 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301944 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001945
Ronald Cron81709fc2020-11-14 12:10:32 +01001946 *key = MBEDTLS_SVC_KEY_ID_INIT;
1947
Gilles Peskine0f84d622019-09-12 19:03:13 +02001948 /* Reject zero-length symmetric keys (including raw data key objects).
1949 * This also rejects any key which might be encoded as an empty string,
1950 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 if (data_length == 0) {
1952 return PSA_ERROR_INVALID_ARGUMENT;
1953 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001954
Archana449608b2021-09-08 15:36:05 +05301955 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 if (data_length > SIZE_MAX / 8) {
1957 return PSA_ERROR_NOT_SUPPORTED;
1958 }
Archana6ed4bda2021-08-04 10:47:15 +05301959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1961 &slot, &driver);
1962 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001963 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001965
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001966 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301967 * storage ( thus not in the case of importing a key in a secure element
1968 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1969 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 if (slot->key.data == NULL) {
1971 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301972 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 attributes, data, data_length, &storage_size);
1974 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301975 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 }
Archanad8a83dc2021-06-14 10:04:16 +05301977 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 status = psa_allocate_buffer_to_slot(slot, storage_size);
1979 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001980 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001982 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001983
1984 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 status = psa_driver_wrapper_import_key(attributes,
1986 data, data_length,
1987 slot->key.data,
1988 slot->key.bytes,
1989 &slot->key.bytes, &bits);
1990 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001991 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001995 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001997 status = PSA_ERROR_INVALID_ARGUMENT;
1998 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001999 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002000
Archana6ed4bda2021-08-04 10:47:15 +05302001 /* Enforce a size limit, and in particular ensure that the bit
2002 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302004 status = PSA_ERROR_NOT_SUPPORTED;
2005 goto exit;
2006 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 status = psa_validate_optional_attributes(slot, attributes);
2008 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002009 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002011
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002013exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 if (status != PSA_SUCCESS) {
2015 psa_fail_key_creation(slot, driver);
2016 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002017
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002019}
2020
Gilles Peskined7729582019-08-05 15:55:54 +02002021#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2022psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002024{
2025 psa_status_t status;
2026 psa_key_slot_t *slot = NULL;
2027 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002028 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002029
2030 /* Leaving attributes unspecified is not currently supported.
2031 * It could make sense to query the key type and size from the
2032 * secure element, but not all secure elements support this
2033 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2035 return PSA_ERROR_NOT_SUPPORTED;
2036 }
2037 if (psa_get_key_bits(attributes) == 0) {
2038 return PSA_ERROR_NOT_SUPPORTED;
2039 }
Gilles Peskined7729582019-08-05 15:55:54 +02002040
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2042 &slot, &driver);
2043 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002044 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 }
Gilles Peskined7729582019-08-05 15:55:54 +02002046
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002048
2049exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 if (status != PSA_SUCCESS) {
2051 psa_fail_key_creation(slot, driver);
2052 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002053
Gilles Peskined7729582019-08-05 15:55:54 +02002054 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 psa_close_key(key);
2056 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002057}
2058#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2059
Gilles Peskine449bd832023-01-11 14:50:10 +01002060psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2061 const psa_key_attributes_t *specified_attributes,
2062 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002063{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002064 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002065 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002066 psa_key_slot_t *source_slot = NULL;
2067 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002068 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002069 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302070 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002071
Ronald Cron81709fc2020-11-14 12:10:32 +01002072 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2073
Archana8a180362021-07-05 02:18:48 +05302074 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2076 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002077 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002079
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 status = psa_validate_optional_attributes(source_slot,
2081 specified_attributes);
2082 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002083 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002085
Archana9d17bf42021-09-10 06:22:44 +05302086 /* The target key type and number of bits have been validated by
2087 * psa_validate_optional_attributes() to be either equal to zero or
2088 * equal to the ones of the source key. So it is safe to inherit
2089 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302090 * */
Archana9d17bf42021-09-10 06:22:44 +05302091 actual_attributes.core.bits = source_slot->attr.bits;
2092 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302093
2094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 status = psa_restrict_key_policy(source_slot->attr.type,
2096 &actual_attributes.core.policy,
2097 &source_slot->attr.policy);
2098 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002099 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002101
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2103 &target_slot, &driver);
2104 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002105 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 }
2107 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2108 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302109 /*
Archana9d17bf42021-09-10 06:22:44 +05302110 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302111 * the source key would need to be exported as plaintext and re-imported
2112 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302113 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302114 * appropriate API invocations from the application, if needed.
2115 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002116 status = PSA_ERROR_NOT_SUPPORTED;
2117 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002118 }
Archana8a180362021-07-05 02:18:48 +05302119 /*
2120 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302121 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302122 * - For opaque keys this translates to an invocation of the drivers'
2123 * copy_key entry point through the dispatch layer.
2124 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2126 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2127 &storage_size);
2128 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302129 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 }
Archana374fe5b2021-09-08 15:50:28 +05302131
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2133 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302134 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 }
Archana374fe5b2021-09-08 15:50:28 +05302136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 status = psa_driver_wrapper_copy_key(&actual_attributes,
2138 source_slot->key.data,
2139 source_slot->key.bytes,
2140 target_slot->key.data,
2141 target_slot->key.bytes,
2142 &target_slot->key.bytes);
2143 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302144 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 }
2146 } else {
2147 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302148 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 source_slot->key.bytes);
2150 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302151 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 }
Archana8a180362021-07-05 02:18:48 +05302153 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002155exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 if (status != PSA_SUCCESS) {
2157 psa_fail_key_creation(target_slot, driver);
2158 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002161
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002163}
2164
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002165
2166
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002167/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002168/* Message digests */
2169/****************************************************************/
2170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002172{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002173 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 if (operation->id == 0) {
2175 return PSA_SUCCESS;
2176 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002179 operation->id = 0;
2180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002182}
2183
Gilles Peskine449bd832023-01-11 14:50:10 +01002184psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2185 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002186{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002187 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002188
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002189 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002191 status = PSA_ERROR_BAD_STATE;
2192 goto exit;
2193 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002194
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002196 status = PSA_ERROR_INVALID_ARGUMENT;
2197 goto exit;
2198 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002199
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002200 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2201 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002203
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002205
2206exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 if (status != PSA_SUCCESS) {
2208 psa_hash_abort(operation);
2209 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002210
2211 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002212}
2213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2215 const uint8_t *input,
2216 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002217{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002218 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002221 status = PSA_ERROR_BAD_STATE;
2222 goto exit;
2223 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002224
Steven Cooremanc8288352021-03-04 14:02:19 +01002225 /* Don't require hash implementations to behave correctly on a
2226 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 if (input_length == 0) {
2228 return PSA_SUCCESS;
2229 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002230
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002232
2233exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 if (status != PSA_SUCCESS) {
2235 psa_hash_abort(operation);
2236 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002237
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002239}
2240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2242 uint8_t *hash,
2243 size_t hash_size,
2244 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002245{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002246 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 if (operation->id == 0) {
2248 return PSA_ERROR_BAD_STATE;
2249 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002250
Steven Cooreman1e582352021-02-18 17:24:37 +01002251 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 operation, hash, hash_size, hash_length);
2253 psa_hash_abort(operation);
2254 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002255}
2256
Gilles Peskine449bd832023-01-11 14:50:10 +01002257psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2258 const uint8_t *hash,
2259 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002260{
Ronald Cron69a63422021-10-18 09:47:58 +02002261 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002262 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002263 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 operation,
2265 actual_hash, sizeof(actual_hash),
2266 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002267
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002269 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002271
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002273 status = PSA_ERROR_INVALID_SIGNATURE;
2274 goto exit;
2275 }
2276
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002278 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002280
2281exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2283 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002284 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002286
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002288}
2289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290psa_status_t psa_hash_compute(psa_algorithm_t alg,
2291 const uint8_t *input, size_t input_length,
2292 uint8_t *hash, size_t hash_size,
2293 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002294{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002295 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 if (!PSA_ALG_IS_HASH(alg)) {
2297 return PSA_ERROR_INVALID_ARGUMENT;
2298 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002299
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2301 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002302}
2303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304psa_status_t psa_hash_compare(psa_algorithm_t alg,
2305 const uint8_t *input, size_t input_length,
2306 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002307{
Ronald Cron69a63422021-10-18 09:47:58 +02002308 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002309 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002310
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 if (!PSA_ALG_IS_HASH(alg)) {
2312 return PSA_ERROR_INVALID_ARGUMENT;
2313 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002314
Steven Cooreman1e582352021-02-18 17:24:37 +01002315 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 alg, input, input_length,
2317 actual_hash, sizeof(actual_hash),
2318 &actual_hash_length);
2319 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002320 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 }
2322 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002323 status = PSA_ERROR_INVALID_SIGNATURE;
2324 goto exit;
2325 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002327 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002329
2330exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2332 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002333}
2334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2336 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002337{
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 if (source_operation->id == 0 ||
2339 target_operation->id != 0) {
2340 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002341 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2344 target_operation);
2345 if (status != PSA_SUCCESS) {
2346 psa_hash_abort(target_operation);
2347 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002348
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002350}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002351
2352
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002353/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002354/* MAC */
2355/****************************************************************/
2356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002358{
Steven Cooremane6804192021-03-19 18:28:56 +01002359 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 if (operation->id == 0) {
2361 return PSA_SUCCESS;
2362 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002365 operation->mac_size = 0;
2366 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002367 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002370}
2371
Ronald Cron2dff3b22021-06-17 16:33:22 +02002372static psa_status_t psa_mac_finalize_alg_and_key_validation(
2373 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002374 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002376{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002377 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 psa_key_type_t key_type = psa_get_key_type(attributes);
2379 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 if (!PSA_ALG_IS_MAC(alg)) {
2382 return PSA_ERROR_INVALID_ARGUMENT;
2383 }
Steven Cooremane6804192021-03-19 18:28:56 +01002384
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002385 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 status = psa_mac_key_can_do(alg, key_type);
2387 if (status != PSA_SUCCESS) {
2388 return status;
2389 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002390
Steven Cooremand1a68f12021-05-10 11:13:41 +02002391 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002393
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002395 /* A very short MAC is too short for security since it can be
2396 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2397 * so we make this our minimum, even though 32 bits is still
2398 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002400 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002401
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2403 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002404 /* It's impossible to "truncate" to a larger length than the full length
2405 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002407 }
2408
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002410 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2411 * that is disabled in the compile-time configuration. The result can
2412 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2413 * configuration into account. In this case, force a return of
2414 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2415 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2416 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2417 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2418 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002420 }
2421
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002423}
2424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2426 mbedtls_svc_key_id_t key,
2427 psa_algorithm_t alg,
2428 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002429{
2430 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2431 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002432 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002433
2434 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002436 status = PSA_ERROR_BAD_STATE;
2437 goto exit;
2438 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002439
2440 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 key,
2442 &slot,
2443 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2444 alg);
2445 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002446 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002448
2449 psa_key_attributes_t attributes = {
2450 .core = slot->attr
2451 };
2452
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2454 &operation->mac_size);
2455 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002456 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002458
2459 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002460 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 if (is_sign) {
2462 status = psa_driver_wrapper_mac_sign_setup(operation,
2463 &attributes,
2464 slot->key.data,
2465 slot->key.bytes,
2466 alg);
2467 } else {
2468 status = psa_driver_wrapper_mac_verify_setup(operation,
2469 &attributes,
2470 slot->key.data,
2471 slot->key.bytes,
2472 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002473 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002474
Gilles Peskinefbfac682018-07-08 20:51:54 +02002475exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 if (status != PSA_SUCCESS) {
2477 psa_mac_abort(operation);
2478 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002479
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002481
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002483}
2484
Gilles Peskine449bd832023-01-11 14:50:10 +01002485psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2486 mbedtls_svc_key_id_t key,
2487 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002488{
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002490}
2491
Gilles Peskine449bd832023-01-11 14:50:10 +01002492psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2493 mbedtls_svc_key_id_t key,
2494 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002495{
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002497}
2498
Gilles Peskine449bd832023-01-11 14:50:10 +01002499psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2500 const uint8_t *input,
2501 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002502{
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 if (operation->id == 0) {
2504 return PSA_ERROR_BAD_STATE;
2505 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002506
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002507 /* Don't require hash implementations to behave correctly on a
2508 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 if (input_length == 0) {
2510 return PSA_SUCCESS;
2511 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2514 input, input_length);
2515 if (status != PSA_SUCCESS) {
2516 psa_mac_abort(operation);
2517 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002518
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002520}
2521
Gilles Peskine449bd832023-01-11 14:50:10 +01002522psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2523 uint8_t *mac,
2524 size_t mac_size,
2525 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002526{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002527 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2528 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002529
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002531 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002532 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002533 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002534
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002536 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002537 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002538 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002539
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002540 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2541 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002543 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002544 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002545 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002546
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002548 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002549 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002550 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002551
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 status = psa_driver_wrapper_mac_sign_finish(operation,
2553 mac, operation->mac_size,
2554 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002555
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002556exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002557 /* In case of success, set the potential excess room in the output buffer
2558 * to an invalid value, to avoid potentially leaking a longer MAC.
2559 * In case of error, set the output length and content to a safe default,
2560 * such that in case the caller misses an error check, the output would be
2561 * an unachievable MAC.
2562 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002564 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002565 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002566 }
mohammad16036df908f2018-04-02 08:34:15 -07002567
Paul Elliottdc42ca82023-02-24 18:11:59 +00002568 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002569
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002573}
2574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2576 const uint8_t *mac,
2577 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002578{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002579 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2580 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002583 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002584 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002585 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002586
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002588 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002589 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002590 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002591
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002593 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002594 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002595 }
2596
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 status = psa_driver_wrapper_mac_verify_finish(operation,
2598 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002599
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002600exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002604}
2605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2607 psa_algorithm_t alg,
2608 const uint8_t *input,
2609 size_t input_length,
2610 uint8_t *mac,
2611 size_t mac_size,
2612 size_t *mac_length,
2613 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002614{
2615 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002616 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2617 psa_key_slot_t *slot;
2618 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002619
Ronald Cron51131b52021-06-17 17:17:20 +02002620 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 key,
2622 &slot,
2623 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2624 alg);
2625 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002626 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002628
Ronald Cron51131b52021-06-17 17:17:20 +02002629 psa_key_attributes_t attributes = {
2630 .core = slot->attr
2631 };
2632
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2634 &operation_mac_size);
2635 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002636 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002638
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002640 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002641 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002642 }
2643
2644 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 &attributes,
2646 slot->key.data, slot->key.bytes,
2647 alg,
2648 input, input_length,
2649 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002650
2651exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002652 /* In case of success, set the potential excess room in the output buffer
2653 * to an invalid value, to avoid potentially leaking a longer MAC.
2654 * In case of error, set the output length and content to a safe default,
2655 * such that in case the caller misses an error check, the output would be
2656 * an unachievable MAC.
2657 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002659 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002660 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002661 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002662
2663 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002664
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002666
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002668}
2669
Gilles Peskine449bd832023-01-11 14:50:10 +01002670psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002671 psa_algorithm_t alg,
2672 const uint8_t *input,
2673 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 uint8_t *mac,
2675 size_t mac_size,
2676 size_t *mac_length)
2677{
2678 return psa_mac_compute_internal(key, alg,
2679 input, input_length,
2680 mac, mac_size, mac_length, 1);
2681}
2682
2683psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2684 psa_algorithm_t alg,
2685 const uint8_t *input,
2686 size_t input_length,
2687 const uint8_t *mac,
2688 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002689{
2690 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002691 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2692 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002693
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 status = psa_mac_compute_internal(key, alg,
2695 input, input_length,
2696 actual_mac, sizeof(actual_mac),
2697 &actual_mac_length, 0);
2698 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002699 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002701
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002703 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002704 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002705 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002707 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002708 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002709 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002710
2711exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002713
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002715}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002716
Gilles Peskinee59236f2018-01-27 23:32:46 +01002717/****************************************************************/
2718/* Asymmetric cryptography */
2719/****************************************************************/
2720
Gilles Peskine449bd832023-01-11 14:50:10 +01002721static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2722 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002723{
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 if (input_is_message) {
2725 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2726 return PSA_ERROR_INVALID_ARGUMENT;
2727 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2730 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2731 return PSA_ERROR_INVALID_ARGUMENT;
2732 }
2733 }
2734 } else {
2735 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2736 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002737 }
2738 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002739
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002741}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002742
Gilles Peskine449bd832023-01-11 14:50:10 +01002743static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2744 int input_is_message,
2745 psa_algorithm_t alg,
2746 const uint8_t *input,
2747 size_t input_length,
2748 uint8_t *signature,
2749 size_t signature_size,
2750 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002751{
2752 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2753 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2754 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002755
2756 *signature_length = 0;
2757
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 status = psa_sign_verify_check_alg(input_is_message, alg);
2759 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002760 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002762
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002763 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002764 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002765 * buffer can in principle be empty since it doesn't actually have
2766 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 if (signature_size == 0) {
2768 return PSA_ERROR_BUFFER_TOO_SMALL;
2769 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002770
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002771 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002772 key, &slot,
2773 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2774 PSA_KEY_USAGE_SIGN_HASH,
2775 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002776
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002778 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002780
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002782 status = PSA_ERROR_INVALID_ARGUMENT;
2783 goto exit;
2784 }
2785
2786 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002788 };
2789
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002791 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002792 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002793 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 signature, signature_size, signature_length);
2795 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002796
2797 status = psa_driver_wrapper_sign_hash(
2798 &attributes, slot->key.data, slot->key.bytes,
2799 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002801 }
2802
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002803
2804exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002805 psa_wipe_tag_output_buffer(signature, status, signature_size,
2806 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002807
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002809
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002811}
2812
Gilles Peskine449bd832023-01-11 14:50:10 +01002813static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2814 int input_is_message,
2815 psa_algorithm_t alg,
2816 const uint8_t *input,
2817 size_t input_length,
2818 const uint8_t *signature,
2819 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002820{
2821 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2822 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2823 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002824
Gilles Peskine449bd832023-01-11 14:50:10 +01002825 status = psa_sign_verify_check_alg(input_is_message, alg);
2826 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002827 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002828 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002829
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002830 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002831 key, &slot,
2832 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2833 PSA_KEY_USAGE_VERIFY_HASH,
2834 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002835
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 if (status != PSA_SUCCESS) {
2837 return status;
2838 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002839
2840 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002842 };
2843
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002845 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002846 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002847 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 signature, signature_length);
2849 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002850 status = psa_driver_wrapper_verify_hash(
2851 &attributes, slot->key.data, slot->key.bytes,
2852 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002854 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002855
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002857
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002859
2860}
2861
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002862psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002863 const psa_key_attributes_t *attributes,
2864 const uint8_t *key_buffer,
2865 size_t key_buffer_size,
2866 psa_algorithm_t alg,
2867 const uint8_t *input,
2868 size_t input_length,
2869 uint8_t *signature,
2870 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002872{
2873 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002874
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002876 size_t hash_length;
2877 uint8_t hash[PSA_HASH_MAX_SIZE];
2878
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002879 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 PSA_ALG_SIGN_GET_HASH(alg),
2881 input, input_length,
2882 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002883
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002885 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002887
2888 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002889 attributes, key_buffer, key_buffer_size,
2890 alg, hash, hash_length,
2891 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002892 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002893
Gilles Peskine449bd832023-01-11 14:50:10 +01002894 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002895}
2896
Gilles Peskine449bd832023-01-11 14:50:10 +01002897psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2898 psa_algorithm_t alg,
2899 const uint8_t *input,
2900 size_t input_length,
2901 uint8_t *signature,
2902 size_t signature_size,
2903 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002904{
2905 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002906 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002908}
2909
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002910psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002911 const psa_key_attributes_t *attributes,
2912 const uint8_t *key_buffer,
2913 size_t key_buffer_size,
2914 psa_algorithm_t alg,
2915 const uint8_t *input,
2916 size_t input_length,
2917 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002919{
2920 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002921
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002923 size_t hash_length;
2924 uint8_t hash[PSA_HASH_MAX_SIZE];
2925
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002926 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 PSA_ALG_SIGN_GET_HASH(alg),
2928 input, input_length,
2929 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002930
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002932 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002934
2935 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 attributes, key_buffer, key_buffer_size,
2937 alg, hash, hash_length,
2938 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002939 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002940
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002942}
2943
Gilles Peskine449bd832023-01-11 14:50:10 +01002944psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2945 psa_algorithm_t alg,
2946 const uint8_t *input,
2947 size_t input_length,
2948 const uint8_t *signature,
2949 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002950{
2951 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002952 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002954}
2955
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002956psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002957 const psa_key_attributes_t *attributes,
2958 const uint8_t *key_buffer, size_t key_buffer_size,
2959 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002961{
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2963 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2964 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002965#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2967 return mbedtls_psa_rsa_sign_hash(
2968 attributes,
2969 key_buffer, key_buffer_size,
2970 alg, hash, hash_length,
2971 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002972#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2973 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 } else {
2975 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002976 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2978 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002979#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2981 return mbedtls_psa_ecdsa_sign_hash(
2982 attributes,
2983 key_buffer, key_buffer_size,
2984 alg, hash, hash_length,
2985 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002986#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2987 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 } else {
2989 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002990 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002991 }
Ronald Cron4501c982021-03-19 20:09:32 +01002992
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 (void) key_buffer;
2994 (void) key_buffer_size;
2995 (void) hash;
2996 (void) hash_length;
2997 (void) signature;
2998 (void) signature_size;
2999 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003000
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003002}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003003
Gilles Peskine449bd832023-01-11 14:50:10 +01003004psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3005 psa_algorithm_t alg,
3006 const uint8_t *hash,
3007 size_t hash_length,
3008 uint8_t *signature,
3009 size_t signature_size,
3010 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003011{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003012 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003013 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003015}
3016
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003017psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003018 const psa_key_attributes_t *attributes,
3019 const uint8_t *key_buffer, size_t key_buffer_size,
3020 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003022{
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3024 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3025 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003026#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3028 return mbedtls_psa_rsa_verify_hash(
3029 attributes,
3030 key_buffer, key_buffer_size,
3031 alg, hash, hash_length,
3032 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003033#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3034 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 } else {
3036 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003037 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3039 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003040#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3042 return mbedtls_psa_ecdsa_verify_hash(
3043 attributes,
3044 key_buffer, key_buffer_size,
3045 alg, hash, hash_length,
3046 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003047#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3048 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 } else {
3050 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003051 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003052 }
Ronald Cron4501c982021-03-19 20:09:32 +01003053
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 (void) key_buffer;
3055 (void) key_buffer_size;
3056 (void) hash;
3057 (void) hash_length;
3058 (void) signature;
3059 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003060
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003062}
3063
Gilles Peskine449bd832023-01-11 14:50:10 +01003064psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3065 psa_algorithm_t alg,
3066 const uint8_t *hash,
3067 size_t hash_length,
3068 const uint8_t *signature,
3069 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003070{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003071 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003072 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003074}
3075
Gilles Peskine449bd832023-01-11 14:50:10 +01003076psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3077 psa_algorithm_t alg,
3078 const uint8_t *input,
3079 size_t input_length,
3080 const uint8_t *salt,
3081 size_t salt_length,
3082 uint8_t *output,
3083 size_t output_size,
3084 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003085{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003086 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003087 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003088 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003089
Darryl Green5cc689a2018-07-24 15:34:10 +01003090 (void) input;
3091 (void) input_length;
3092 (void) salt;
3093 (void) output;
3094 (void) output_size;
3095
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003096 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003097
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3099 return PSA_ERROR_INVALID_ARGUMENT;
3100 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003101
Ronald Cron5c522922020-11-14 16:35:34 +01003102 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3104 if (status != PSA_SUCCESS) {
3105 return status;
3106 }
3107 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3108 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003109 status = PSA_ERROR_INVALID_ARGUMENT;
3110 goto exit;
3111 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003112
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003113 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003115 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003116
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003117 status = psa_driver_wrapper_asymmetric_encrypt(
3118 &attributes, slot->key.data, slot->key.bytes,
3119 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003121exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003123
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003125}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003126
Gilles Peskine449bd832023-01-11 14:50:10 +01003127psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3128 psa_algorithm_t alg,
3129 const uint8_t *input,
3130 size_t input_length,
3131 const uint8_t *salt,
3132 size_t salt_length,
3133 uint8_t *output,
3134 size_t output_size,
3135 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003136{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003137 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003138 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003139 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003140
Darryl Green5cc689a2018-07-24 15:34:10 +01003141 (void) input;
3142 (void) input_length;
3143 (void) salt;
3144 (void) output;
3145 (void) output_size;
3146
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003147 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003148
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3150 return PSA_ERROR_INVALID_ARGUMENT;
3151 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003152
Ronald Cron5c522922020-11-14 16:35:34 +01003153 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3155 if (status != PSA_SUCCESS) {
3156 return status;
3157 }
3158 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003159 status = PSA_ERROR_INVALID_ARGUMENT;
3160 goto exit;
3161 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003162
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003163 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003165 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003166
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003167 status = psa_driver_wrapper_asymmetric_decrypt(
3168 &attributes, slot->key.data, slot->key.bytes,
3169 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003171
3172exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003173 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003174
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003176}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003177
Paul Elliott9fe12f62022-11-30 19:16:02 +00003178/****************************************************************/
3179/* Asymmetric interruptible cryptography */
3180/****************************************************************/
3181
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003182static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3183
Paul Elliott9fe12f62022-11-30 19:16:02 +00003184void psa_interruptible_set_max_ops(uint32_t max_ops)
3185{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003186 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003187}
3188
3189uint32_t psa_interruptible_get_max_ops(void)
3190{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003191 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003192}
3193
Paul Elliott9fe12f62022-11-30 19:16:02 +00003194uint32_t psa_sign_hash_get_num_ops(
3195 const psa_sign_hash_interruptible_operation_t *operation)
3196{
Paul Elliott296ede92022-12-15 17:00:30 +00003197 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003198}
3199
3200uint32_t psa_verify_hash_get_num_ops(
3201 const psa_verify_hash_interruptible_operation_t *operation)
3202{
Paul Elliott296ede92022-12-15 17:00:30 +00003203 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003204}
3205
Paul Elliott59ad9452022-12-18 15:09:02 +00003206static psa_status_t psa_sign_hash_abort_internal(
3207 psa_sign_hash_interruptible_operation_t *operation)
3208{
3209 if (operation->id == 0) {
3210 /* The object has (apparently) been initialized but it is not (yet)
3211 * in use. It's ok to call abort on such an object, and there's
3212 * nothing to do. */
3213 return PSA_SUCCESS;
3214 }
3215
3216 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3217
3218 status = psa_driver_wrapper_sign_hash_abort(operation);
3219
3220 operation->id = 0;
3221
Paul Elliotte6145dc2023-02-07 12:51:21 +00003222 /* Do not clear either the error_occurred or num_ops elements here as they
3223 * only want to be cleared by the application calling abort, not by abort
3224 * being called at completion of an operation. */
3225
Paul Elliott59ad9452022-12-18 15:09:02 +00003226 return status;
3227}
3228
Paul Elliott9fe12f62022-11-30 19:16:02 +00003229psa_status_t psa_sign_hash_start(
3230 psa_sign_hash_interruptible_operation_t *operation,
3231 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3232 const uint8_t *hash, size_t hash_length)
3233{
3234 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3235 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3236 psa_key_slot_t *slot;
3237
Paul Elliottc9774412023-02-06 15:14:07 +00003238 /* Check that start has not been previously called, or operation has not
3239 * previously errored. */
3240 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003241 return PSA_ERROR_BAD_STATE;
3242 }
3243
Paul Elliott9fe12f62022-11-30 19:16:02 +00003244 status = psa_sign_verify_check_alg(0, alg);
3245 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003246 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003247 return status;
3248 }
3249
3250 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3251 PSA_KEY_USAGE_SIGN_HASH,
3252 alg);
3253
3254 if (status != PSA_SUCCESS) {
3255 goto exit;
3256 }
3257
3258 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3259 status = PSA_ERROR_INVALID_ARGUMENT;
3260 goto exit;
3261 }
3262
3263 psa_key_attributes_t attributes = {
3264 .core = slot->attr
3265 };
3266
Paul Elliott296ede92022-12-15 17:00:30 +00003267 /* Ensure ops count gets reset, in case of operation re-use. */
3268 operation->num_ops = 0;
3269
Paul Elliott9fe12f62022-11-30 19:16:02 +00003270 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3271 slot->key.data,
3272 slot->key.bytes, alg,
3273 hash, hash_length);
3274exit:
3275
3276 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003277 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003278 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003279 }
3280
3281 unlock_status = psa_unlock_key_slot(slot);
3282
Paul Elliottc9774412023-02-06 15:14:07 +00003283 if (unlock_status != PSA_SUCCESS) {
3284 operation->error_occurred = 1;
3285 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003286
Paul Elliottc9774412023-02-06 15:14:07 +00003287 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003288}
3289
3290
3291psa_status_t psa_sign_hash_complete(
3292 psa_sign_hash_interruptible_operation_t *operation,
3293 uint8_t *signature, size_t signature_size,
3294 size_t *signature_length)
3295{
3296 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3297
3298 *signature_length = 0;
3299
Paul Elliottc9774412023-02-06 15:14:07 +00003300 /* Check that start has been called first, and that operation has not
3301 * previously errored. */
3302 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003303 status = PSA_ERROR_BAD_STATE;
3304 goto exit;
3305 }
3306
Paul Elliott096abc42023-02-03 18:33:23 +00003307 /* Immediately reject a zero-length signature buffer. This guarantees that
3308 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003309 if (signature_size == 0) {
3310 status = PSA_ERROR_BUFFER_TOO_SMALL;
3311 goto exit;
3312 }
3313
3314 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3315 signature_size,
3316 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003317
Paul Elliott296ede92022-12-15 17:00:30 +00003318 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003319 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003320
Paul Elliottebe225c2023-02-10 14:32:53 +00003321exit:
3322
Paul Elliott59200a22023-02-21 15:07:40 +00003323 psa_wipe_tag_output_buffer(signature, status, signature_size,
3324 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003325
Paul Elliott53bb3122023-02-10 14:22:22 +00003326 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003327 if (status != PSA_SUCCESS) {
3328 operation->error_occurred = 1;
3329 }
3330
Paul Elliott59ad9452022-12-18 15:09:02 +00003331 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003332 }
3333
3334 return status;
3335}
3336
3337psa_status_t psa_sign_hash_abort(
3338 psa_sign_hash_interruptible_operation_t *operation)
3339{
Paul Elliott59ad9452022-12-18 15:09:02 +00003340 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3341
3342 status = psa_sign_hash_abort_internal(operation);
3343
3344 /* We clear the number of ops done here, so that it is not cleared when
3345 * the operation fails or succeeds, only on manual abort. */
3346 operation->num_ops = 0;
3347
Paul Elliottc9774412023-02-06 15:14:07 +00003348 /* Likewise, failure state. */
3349 operation->error_occurred = 0;
3350
Paul Elliott59ad9452022-12-18 15:09:02 +00003351 return status;
3352}
3353
3354static psa_status_t psa_verify_hash_abort_internal(
3355 psa_verify_hash_interruptible_operation_t *operation)
3356{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003357 if (operation->id == 0) {
3358 /* The object has (apparently) been initialized but it is not (yet)
3359 * in use. It's ok to call abort on such an object, and there's
3360 * nothing to do. */
3361 return PSA_SUCCESS;
3362 }
3363
Paul Elliott59ad9452022-12-18 15:09:02 +00003364 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3365
3366 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003367
3368 operation->id = 0;
3369
Paul Elliotte6145dc2023-02-07 12:51:21 +00003370 /* Do not clear either the error_occurred or num_ops elements here as they
3371 * only want to be cleared by the application calling abort, not by abort
3372 * being called at completion of an operation. */
3373
Paul Elliott59ad9452022-12-18 15:09:02 +00003374 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003375}
3376
3377psa_status_t psa_verify_hash_start(
3378 psa_verify_hash_interruptible_operation_t *operation,
3379 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3380 const uint8_t *hash, size_t hash_length,
3381 const uint8_t *signature, size_t signature_length)
3382{
3383 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3384 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3385 psa_key_slot_t *slot;
3386
Paul Elliottc9774412023-02-06 15:14:07 +00003387 /* Check that start has not been previously called, or operation has not
3388 * previously errored. */
3389 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003390 return PSA_ERROR_BAD_STATE;
3391 }
3392
3393 status = psa_sign_verify_check_alg(0, alg);
3394 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003395 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003396 return status;
3397 }
3398
3399 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3400 PSA_KEY_USAGE_VERIFY_HASH,
3401 alg);
3402
3403 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003404 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003405 return status;
3406 }
3407
3408 psa_key_attributes_t attributes = {
3409 .core = slot->attr
3410 };
3411
Paul Elliott296ede92022-12-15 17:00:30 +00003412 /* Ensure ops count gets reset, in case of operation re-use. */
3413 operation->num_ops = 0;
3414
Paul Elliott9fe12f62022-11-30 19:16:02 +00003415 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3416 slot->key.data,
3417 slot->key.bytes,
3418 alg, hash, hash_length,
3419 signature, signature_length);
3420
3421 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003422 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003423 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003424 }
3425
3426 unlock_status = psa_unlock_key_slot(slot);
3427
Paul Elliottc9774412023-02-06 15:14:07 +00003428 if (unlock_status != PSA_SUCCESS) {
3429 operation->error_occurred = 1;
3430 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003431
Paul Elliottc9774412023-02-06 15:14:07 +00003432 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003433}
3434
3435psa_status_t psa_verify_hash_complete(
3436 psa_verify_hash_interruptible_operation_t *operation)
3437{
3438 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3439
Paul Elliottc9774412023-02-06 15:14:07 +00003440 /* Check that start has been called first, and that operation has not
3441 * previously errored. */
3442 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003443 status = PSA_ERROR_BAD_STATE;
3444 goto exit;
3445 }
3446
3447 status = psa_driver_wrapper_verify_hash_complete(operation);
3448
Paul Elliott296ede92022-12-15 17:00:30 +00003449 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003450 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003451 operation);
3452
Paul Elliottebe225c2023-02-10 14:32:53 +00003453exit:
3454
Paul Elliott9fe12f62022-11-30 19:16:02 +00003455 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003456 if (status != PSA_SUCCESS) {
3457 operation->error_occurred = 1;
3458 }
3459
Paul Elliott59ad9452022-12-18 15:09:02 +00003460 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003461 }
3462
3463 return status;
3464}
3465
3466psa_status_t psa_verify_hash_abort(
3467 psa_verify_hash_interruptible_operation_t *operation)
3468{
Paul Elliott59ad9452022-12-18 15:09:02 +00003469 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003470
Paul Elliott59ad9452022-12-18 15:09:02 +00003471 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003472
Paul Elliott59ad9452022-12-18 15:09:02 +00003473 /* We clear the number of ops done here, so that it is not cleared when
3474 * the operation fails or succeeds, only on manual abort. */
3475 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003476
Paul Elliottc9774412023-02-06 15:14:07 +00003477 /* Likewise, failure state. */
3478 operation->error_occurred = 0;
3479
Paul Elliott59ad9452022-12-18 15:09:02 +00003480 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003481}
3482
Paul Elliott588f8ed2022-12-02 18:10:26 +00003483/****************************************************************/
3484/* Asymmetric interruptible cryptography internal */
3485/* implementations */
3486/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003487
Paul Elliott588f8ed2022-12-02 18:10:26 +00003488void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3489{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003490
Paul Elliott588f8ed2022-12-02 18:10:26 +00003491#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3492 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3493 defined(MBEDTLS_ECP_RESTARTABLE)
3494
3495 /* Internal implementation uses zero to indicate infinite number max ops,
3496 * therefore avoid this value, and set to minimum possible. */
3497 if (max_ops == 0) {
3498 max_ops = 1;
3499 }
3500
Paul Elliott588f8ed2022-12-02 18:10:26 +00003501 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003502#else
3503 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003504#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3505 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3506 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3507}
3508
Paul Elliott588f8ed2022-12-02 18:10:26 +00003509uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003510 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003511{
3512#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3513 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3514 defined(MBEDTLS_ECP_RESTARTABLE)
3515
Paul Elliott93d9ca82023-02-15 18:14:21 +00003516 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003517#else
3518 (void) operation;
3519 return 0;
3520#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3521 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3522 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3523}
3524
3525uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003526 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003527{
3528 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3529 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3530 defined(MBEDTLS_ECP_RESTARTABLE)
3531
Paul Elliott93d9ca82023-02-15 18:14:21 +00003532 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003533#else
3534 (void) operation;
3535 return 0;
3536#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3537 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3538 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3539}
3540
3541psa_status_t mbedtls_psa_sign_hash_start(
3542 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3543 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3544 size_t key_buffer_size, psa_algorithm_t alg,
3545 const uint8_t *hash, size_t hash_length)
3546{
3547 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003548 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003549
Paul Elliott068fe072023-01-16 13:59:15 +00003550 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3551 return PSA_ERROR_NOT_SUPPORTED;
3552 }
3553
3554 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003555 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003556 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003557
3558#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003559 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3560 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003561
Paul Elliotta1c94092023-02-15 16:38:04 +00003562 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3563
Paul Elliott93d9ca82023-02-15 18:14:21 +00003564 /* Ensure num_ops is zero'ed in case of context re-use. */
3565 operation->num_ops = 0;
3566
Paul Elliott068fe072023-01-16 13:59:15 +00003567 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3568 attributes->core.bits,
3569 key_buffer,
3570 key_buffer_size,
3571 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003572
Paul Elliott068fe072023-01-16 13:59:15 +00003573 if (status != PSA_SUCCESS) {
3574 return status;
3575 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003576
Paul Elliott1bc59df2023-02-05 13:41:57 +00003577 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003578 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003579
Paul Elliott068fe072023-01-16 13:59:15 +00003580 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3581 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3582 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003583
Paul Elliott0290a762023-02-09 14:30:24 +00003584 /* We only need to store the same length of hash as the private key size
3585 * here, it would be truncated by the internal implementation anyway. */
3586 required_hash_length = (hash_length < operation->coordinate_bytes ?
3587 hash_length : operation->coordinate_bytes);
3588
Paul Elliottba70ad42023-02-15 18:23:53 +00003589 if (required_hash_length > sizeof(operation->hash)) {
3590 /* Shouldn't happen, but better safe than sorry. */
3591 return PSA_ERROR_CORRUPTION_DETECTED;
3592 }
3593
Paul Elliott0290a762023-02-09 14:30:24 +00003594 memcpy(operation->hash, hash, required_hash_length);
3595 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003596
3597 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003598
3599#else
Paul Elliott068fe072023-01-16 13:59:15 +00003600 (void) operation;
3601 (void) key_buffer;
3602 (void) key_buffer_size;
3603 (void) alg;
3604 (void) hash;
3605 (void) hash_length;
3606 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003607 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003608
Paul Elliott068fe072023-01-16 13:59:15 +00003609 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003610#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3611 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3612 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003613}
3614
3615psa_status_t mbedtls_psa_sign_hash_complete(
3616 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3617 uint8_t *signature, size_t signature_size,
3618 size_t *signature_length)
3619{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003620#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3621 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3622 defined(MBEDTLS_ECP_RESTARTABLE)
3623
Paul Elliotta1c94092023-02-15 16:38:04 +00003624 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003625 mbedtls_mpi r;
3626 mbedtls_mpi s;
3627
Paul Elliott46845252023-02-03 14:59:11 +00003628 mbedtls_mpi_init(&r);
3629 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003630
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003631 /* Ensure max_ops is set to the current value (or default). */
3632 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3633
Paul Elliotta1c94092023-02-15 16:38:04 +00003634 if (signature_size < 2 * operation->coordinate_bytes) {
3635 status = PSA_ERROR_BUFFER_TOO_SMALL;
3636 goto exit;
3637 }
3638
Paul Elliott588f8ed2022-12-02 18:10:26 +00003639 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003640
Paul Elliott588f8ed2022-12-02 18:10:26 +00003641#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3642 status = mbedtls_to_psa_error(
3643 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003644 &r,
3645 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003646 &operation->ctx->d,
3647 operation->hash,
3648 operation->hash_length,
3649 operation->md_alg,
3650 mbedtls_psa_get_random,
3651 MBEDTLS_PSA_RANDOM_STATE,
3652 &operation->restart_ctx));
3653#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003654 status = PSA_ERROR_NOT_SUPPORTED;
3655 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003656#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3657 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003658 status = mbedtls_to_psa_error(
3659 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003660 &r,
3661 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003662 &operation->ctx->d,
3663 operation->hash,
3664 operation->hash_length,
3665 mbedtls_psa_get_random,
3666 MBEDTLS_PSA_RANDOM_STATE,
3667 mbedtls_psa_get_random,
3668 MBEDTLS_PSA_RANDOM_STATE,
3669 &operation->restart_ctx));
3670 }
3671
Paul Elliottf8e5b562023-02-19 18:43:45 +00003672 /* Hide the fact that the restart context only holds a delta of number of
3673 * ops done during the last operation, not an absolute value. */
3674 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3675
Paul Elliott724bd252023-02-08 12:35:08 +00003676 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003677 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003678 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003679 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003680 operation->coordinate_bytes)
3681 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003682
3683 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003684 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003685 }
3686
3687 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003688 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003689 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003690 operation->coordinate_bytes,
3691 operation->coordinate_bytes)
3692 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003693
3694 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003695 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003696 }
3697
Paul Elliott1bc59df2023-02-05 13:41:57 +00003698 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003699
Paul Elliott724bd252023-02-08 12:35:08 +00003700 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003701 }
Paul Elliott724bd252023-02-08 12:35:08 +00003702
3703exit:
3704
3705 mbedtls_mpi_free(&r);
3706 mbedtls_mpi_free(&s);
3707 return status;
3708
Paul Elliott588f8ed2022-12-02 18:10:26 +00003709 #else
3710
3711 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003712 (void) signature;
3713 (void) signature_size;
3714 (void) signature_length;
3715
3716 return PSA_ERROR_NOT_SUPPORTED;
3717
3718#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3719 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3720 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3721}
3722
3723psa_status_t mbedtls_psa_sign_hash_abort(
3724 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3725{
3726
3727#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3728 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3729 defined(MBEDTLS_ECP_RESTARTABLE)
3730
3731 if (operation->ctx) {
3732 mbedtls_ecdsa_free(operation->ctx);
3733 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003734 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003735 }
3736
3737 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3738
Paul Elliott93d9ca82023-02-15 18:14:21 +00003739 operation->num_ops = 0;
3740
Paul Elliott588f8ed2022-12-02 18:10:26 +00003741 return PSA_SUCCESS;
3742
3743#else
3744
3745 (void) operation;
3746
3747 return PSA_ERROR_NOT_SUPPORTED;
3748
3749#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3750 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3751 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3752}
3753
3754psa_status_t mbedtls_psa_verify_hash_start(
3755 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3756 const psa_key_attributes_t *attributes,
3757 const uint8_t *key_buffer, size_t key_buffer_size,
3758 psa_algorithm_t alg,
3759 const uint8_t *hash, size_t hash_length,
3760 const uint8_t *signature, size_t signature_length)
3761{
3762 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003763 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003764 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003765
Paul Elliott068fe072023-01-16 13:59:15 +00003766 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3767 return PSA_ERROR_NOT_SUPPORTED;
3768 }
3769
3770 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003771 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003772 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003773
3774#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003775 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3776 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003777
Paul Elliotta1c94092023-02-15 16:38:04 +00003778 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3779 mbedtls_mpi_init(&operation->r);
3780 mbedtls_mpi_init(&operation->s);
3781
Paul Elliott93d9ca82023-02-15 18:14:21 +00003782 /* Ensure num_ops is zero'ed in case of context re-use. */
3783 operation->num_ops = 0;
3784
Paul Elliott068fe072023-01-16 13:59:15 +00003785 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3786 attributes->core.bits,
3787 key_buffer,
3788 key_buffer_size,
3789 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003790
Paul Elliott068fe072023-01-16 13:59:15 +00003791 if (status != PSA_SUCCESS) {
3792 return status;
3793 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003794
Paul Elliottc569fc22023-02-10 13:02:54 +00003795 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003796
Paul Elliott1bc59df2023-02-05 13:41:57 +00003797 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003798 return PSA_ERROR_INVALID_SIGNATURE;
3799 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003800
Paul Elliott068fe072023-01-16 13:59:15 +00003801 status = mbedtls_to_psa_error(
3802 mbedtls_mpi_read_binary(&operation->r,
3803 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003804 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003805
Paul Elliott068fe072023-01-16 13:59:15 +00003806 if (status != PSA_SUCCESS) {
3807 return status;
3808 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003809
Paul Elliott068fe072023-01-16 13:59:15 +00003810 status = mbedtls_to_psa_error(
3811 mbedtls_mpi_read_binary(&operation->s,
3812 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003813 coordinate_bytes,
3814 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003815
Paul Elliott068fe072023-01-16 13:59:15 +00003816 if (status != PSA_SUCCESS) {
3817 return status;
3818 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003819
Paul Elliott2c9843f2023-02-15 17:32:42 +00003820 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003821
Paul Elliott2c9843f2023-02-15 17:32:42 +00003822 if (status != PSA_SUCCESS) {
3823 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003824 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003825
Paul Elliott0290a762023-02-09 14:30:24 +00003826 /* We only need to store the same length of hash as the private key size
3827 * here, it would be truncated by the internal implementation anyway. */
3828 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3829 coordinate_bytes);
3830
Paul Elliottba70ad42023-02-15 18:23:53 +00003831 if (required_hash_length > sizeof(operation->hash)) {
3832 /* Shouldn't happen, but better safe than sorry. */
3833 return PSA_ERROR_CORRUPTION_DETECTED;
3834 }
3835
Paul Elliott0290a762023-02-09 14:30:24 +00003836 memcpy(operation->hash, hash, required_hash_length);
3837 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003838
3839 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003840#else
Paul Elliott068fe072023-01-16 13:59:15 +00003841 (void) operation;
3842 (void) key_buffer;
3843 (void) key_buffer_size;
3844 (void) alg;
3845 (void) hash;
3846 (void) hash_length;
3847 (void) signature;
3848 (void) signature_length;
3849 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003850 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003851 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003852
Paul Elliott068fe072023-01-16 13:59:15 +00003853 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003854#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3855 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3856 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003857}
3858
3859psa_status_t mbedtls_psa_verify_hash_complete(
3860 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3861{
3862
3863#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3864 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3865 defined(MBEDTLS_ECP_RESTARTABLE)
3866
Paul Elliottf8e5b562023-02-19 18:43:45 +00003867 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3868
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003869 /* Ensure max_ops is set to the current value (or default). */
3870 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3871
Paul Elliottf8e5b562023-02-19 18:43:45 +00003872 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003873 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3874 operation->hash,
3875 operation->hash_length,
3876 &operation->ctx->Q,
3877 &operation->r,
3878 &operation->s,
3879 &operation->restart_ctx));
3880
Paul Elliottf8e5b562023-02-19 18:43:45 +00003881 /* Hide the fact that the restart context only holds a delta of number of
3882 * ops done during the last operation, not an absolute value. */
3883 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3884
3885 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003886#else
3887 (void) operation;
3888
3889 return PSA_ERROR_NOT_SUPPORTED;
3890
3891#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3892 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3893 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3894}
3895
3896psa_status_t mbedtls_psa_verify_hash_abort(
3897 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3898{
3899
3900#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3901 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3902 defined(MBEDTLS_ECP_RESTARTABLE)
3903
3904 if (operation->ctx) {
3905 mbedtls_ecdsa_free(operation->ctx);
3906 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003907 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003908 }
3909
3910 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3911
Paul Elliott93d9ca82023-02-15 18:14:21 +00003912 operation->num_ops = 0;
3913
Paul Elliott588f8ed2022-12-02 18:10:26 +00003914 mbedtls_mpi_free(&operation->r);
3915 mbedtls_mpi_free(&operation->s);
3916
3917 return PSA_SUCCESS;
3918
3919#else
3920 (void) operation;
3921
3922 return PSA_ERROR_NOT_SUPPORTED;
3923
3924#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3925 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3926 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3927}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003928
mohammad1603503973b2018-03-12 15:59:30 +02003929/****************************************************************/
3930/* Symmetric cryptography */
3931/****************************************************************/
3932
Gilles Peskine449bd832023-01-11 14:50:10 +01003933static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3934 mbedtls_svc_key_id_t key,
3935 psa_algorithm_t alg,
3936 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003937{
3938 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3939 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003940 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003941 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3942 PSA_KEY_USAGE_ENCRYPT :
3943 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003944
3945 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003946 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003947 status = PSA_ERROR_BAD_STATE;
3948 goto exit;
3949 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003950
Gilles Peskine449bd832023-01-11 14:50:10 +01003951 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003952 status = PSA_ERROR_INVALID_ARGUMENT;
3953 goto exit;
3954 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003955
Gilles Peskine449bd832023-01-11 14:50:10 +01003956 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3957 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003958 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003959 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003960
Ronald Cron49fafa92021-03-10 08:34:23 +01003961 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003962 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003963 * so we only set it (in the driver wrapper) after resources have been
3964 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003965 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003966 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003967 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003968 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003969 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003970 }
3971 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003972
Ronald Crona4af55f2020-12-14 14:36:06 +01003973 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003974 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003975 };
3976
Ronald Cronab99ac22020-10-01 16:38:28 +02003977 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003978 if (cipher_operation == MBEDTLS_ENCRYPT) {
3979 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3980 &attributes,
3981 slot->key.data,
3982 slot->key.bytes,
3983 alg);
3984 } else {
3985 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3986 &attributes,
3987 slot->key.data,
3988 slot->key.bytes,
3989 alg);
3990 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003991
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003992exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003993 if (status != PSA_SUCCESS) {
3994 psa_cipher_abort(operation);
3995 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003996
Gilles Peskine449bd832023-01-11 14:50:10 +01003997 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003998
Gilles Peskine449bd832023-01-11 14:50:10 +01003999 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004000}
4001
Gilles Peskine449bd832023-01-11 14:50:10 +01004002psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4003 mbedtls_svc_key_id_t key,
4004 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004005{
Gilles Peskine449bd832023-01-11 14:50:10 +01004006 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004007}
4008
Gilles Peskine449bd832023-01-11 14:50:10 +01004009psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4010 mbedtls_svc_key_id_t key,
4011 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004012{
Gilles Peskine449bd832023-01-11 14:50:10 +01004013 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004014}
4015
Gilles Peskine449bd832023-01-11 14:50:10 +01004016psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4017 uint8_t *iv,
4018 size_t iv_size,
4019 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004020{
Ronald Cron6d051732020-10-01 14:10:20 +02004021 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004022 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4023 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004024
Gilles Peskine449bd832023-01-11 14:50:10 +01004025 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004026 status = PSA_ERROR_BAD_STATE;
4027 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004028 }
4029
Gilles Peskine449bd832023-01-11 14:50:10 +01004030 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004031 status = PSA_ERROR_BAD_STATE;
4032 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004033 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004034
Ronald Cron2fb90522021-07-05 12:31:44 +02004035 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004036 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004037 status = PSA_ERROR_BUFFER_TOO_SMALL;
4038 goto exit;
4039 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004040
Gilles Peskine449bd832023-01-11 14:50:10 +01004041 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004042 status = PSA_ERROR_GENERIC_ERROR;
4043 goto exit;
4044 }
4045
Gilles Peskine449bd832023-01-11 14:50:10 +01004046 status = psa_generate_random(local_iv, default_iv_length);
4047 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004048 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004049 }
Ronald Cron5618a392021-03-26 09:52:26 +01004050
Gilles Peskine449bd832023-01-11 14:50:10 +01004051 status = psa_driver_wrapper_cipher_set_iv(operation,
4052 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004053
4054exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004055 if (status == PSA_SUCCESS) {
4056 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004057 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004058 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004059 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004060 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004061 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004062 }
Ronald Cron6d051732020-10-01 14:10:20 +02004063
Gilles Peskine449bd832023-01-11 14:50:10 +01004064 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004065}
4066
Gilles Peskine449bd832023-01-11 14:50:10 +01004067psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4068 const uint8_t *iv,
4069 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004070{
Ronald Cron6d051732020-10-01 14:10:20 +02004071 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004072
Gilles Peskine449bd832023-01-11 14:50:10 +01004073 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004074 status = PSA_ERROR_BAD_STATE;
4075 goto exit;
4076 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004077
Gilles Peskine449bd832023-01-11 14:50:10 +01004078 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004079 status = PSA_ERROR_BAD_STATE;
4080 goto exit;
4081 }
Ronald Crona0d68172021-03-26 10:15:08 +01004082
Gilles Peskine449bd832023-01-11 14:50:10 +01004083 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004084 status = PSA_ERROR_INVALID_ARGUMENT;
4085 goto exit;
4086 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004087
Gilles Peskine449bd832023-01-11 14:50:10 +01004088 status = psa_driver_wrapper_cipher_set_iv(operation,
4089 iv,
4090 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004091
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004092exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004093 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004094 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004095 } else {
4096 psa_cipher_abort(operation);
4097 }
4098 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004099}
4100
Gilles Peskine449bd832023-01-11 14:50:10 +01004101psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4102 const uint8_t *input,
4103 size_t input_length,
4104 uint8_t *output,
4105 size_t output_size,
4106 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004107{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004108 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004109
Gilles Peskine449bd832023-01-11 14:50:10 +01004110 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004111 status = PSA_ERROR_BAD_STATE;
4112 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004113 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004114
Gilles Peskine449bd832023-01-11 14:50:10 +01004115 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004116 status = PSA_ERROR_BAD_STATE;
4117 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004118 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004119
Gilles Peskine449bd832023-01-11 14:50:10 +01004120 status = psa_driver_wrapper_cipher_update(operation,
4121 input,
4122 input_length,
4123 output,
4124 output_size,
4125 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004126
4127exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004128 if (status != PSA_SUCCESS) {
4129 psa_cipher_abort(operation);
4130 }
Ronald Cron6d051732020-10-01 14:10:20 +02004131
Gilles Peskine449bd832023-01-11 14:50:10 +01004132 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004133}
4134
Gilles Peskine449bd832023-01-11 14:50:10 +01004135psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4136 uint8_t *output,
4137 size_t output_size,
4138 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004139{
David Saadab4ecc272019-02-14 13:48:10 +02004140 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004141
Gilles Peskine449bd832023-01-11 14:50:10 +01004142 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004143 status = PSA_ERROR_BAD_STATE;
4144 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004145 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004146
Gilles Peskine449bd832023-01-11 14:50:10 +01004147 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004148 status = PSA_ERROR_BAD_STATE;
4149 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004150 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004151
Gilles Peskine449bd832023-01-11 14:50:10 +01004152 status = psa_driver_wrapper_cipher_finish(operation,
4153 output,
4154 output_size,
4155 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004156
4157exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004158 if (status == PSA_SUCCESS) {
4159 return psa_cipher_abort(operation);
4160 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004161 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004162 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004163
Gilles Peskine449bd832023-01-11 14:50:10 +01004164 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004165 }
mohammad1603503973b2018-03-12 15:59:30 +02004166}
4167
Gilles Peskine449bd832023-01-11 14:50:10 +01004168psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004169{
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004171 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004172 * in use. It's ok to call abort on such an object, and there's
4173 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004175 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004176
Gilles Peskine449bd832023-01-11 14:50:10 +01004177 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004178
Ronald Cron49fafa92021-03-10 08:34:23 +01004179 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004180 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004181 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004182
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004184}
4185
Gilles Peskine449bd832023-01-11 14:50:10 +01004186psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4187 psa_algorithm_t alg,
4188 const uint8_t *input,
4189 size_t input_length,
4190 uint8_t *output,
4191 size_t output_size,
4192 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004193{
4194 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004195 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004196 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004197 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4198 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004199
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004201 status = PSA_ERROR_INVALID_ARGUMENT;
4202 goto exit;
4203 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004204
Gilles Peskine449bd832023-01-11 14:50:10 +01004205 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4206 PSA_KEY_USAGE_ENCRYPT,
4207 alg);
4208 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004209 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004210 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004211
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004212 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004214 };
4215
Gilles Peskine449bd832023-01-11 14:50:10 +01004216 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4217 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004218 status = PSA_ERROR_GENERIC_ERROR;
4219 goto exit;
4220 }
4221
Gilles Peskine449bd832023-01-11 14:50:10 +01004222 if (default_iv_length > 0) {
4223 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004224 status = PSA_ERROR_BUFFER_TOO_SMALL;
4225 goto exit;
4226 }
4227
Gilles Peskine449bd832023-01-11 14:50:10 +01004228 status = psa_generate_random(local_iv, default_iv_length);
4229 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004230 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004232 }
4233
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004234 status = psa_driver_wrapper_cipher_encrypt(
4235 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004236 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 mbedtls_buffer_offset(output, default_iv_length),
4238 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004239
4240exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004241 unlock_status = psa_unlock_key_slot(slot);
4242 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004243 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004244 }
Ronald Cron23919522021-07-08 19:00:07 +02004245
Gilles Peskine449bd832023-01-11 14:50:10 +01004246 if (status == PSA_SUCCESS) {
4247 if (default_iv_length > 0) {
4248 memcpy(output, local_iv, default_iv_length);
4249 }
4250 *output_length += default_iv_length;
4251 } else {
4252 *output_length = 0;
4253 }
4254
4255 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004256}
4257
Gilles Peskine449bd832023-01-11 14:50:10 +01004258psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4259 psa_algorithm_t alg,
4260 const uint8_t *input,
4261 size_t input_length,
4262 uint8_t *output,
4263 size_t output_size,
4264 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004265{
4266 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004267 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004268 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004269
Gilles Peskine449bd832023-01-11 14:50:10 +01004270 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004271 status = PSA_ERROR_INVALID_ARGUMENT;
4272 goto exit;
4273 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004274
Gilles Peskine449bd832023-01-11 14:50:10 +01004275 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4276 PSA_KEY_USAGE_DECRYPT,
4277 alg);
4278 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004279 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004280 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004281
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004282 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004283 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004284 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004285
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4287 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004288 status = PSA_ERROR_INVALID_ARGUMENT;
4289 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004290 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004291 status = PSA_ERROR_INVALID_ARGUMENT;
4292 goto exit;
4293 }
4294
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004295 status = psa_driver_wrapper_cipher_decrypt(
4296 &attributes, slot->key.data, slot->key.bytes,
4297 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004299
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004300exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004301 unlock_status = psa_unlock_key_slot(slot);
4302 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004303 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004304 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004305
Gilles Peskine449bd832023-01-11 14:50:10 +01004306 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004307 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004308 }
Ronald Cron23919522021-07-08 19:00:07 +02004309
Gilles Peskine449bd832023-01-11 14:50:10 +01004310 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004311}
4312
4313
mohammad16035955c982018-04-26 00:53:03 +03004314/****************************************************************/
4315/* AEAD */
4316/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004317
Paul Elliottbaff51c2021-09-28 17:44:45 +01004318/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004319static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004320{
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004322}
4323
4324/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004325static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4326 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004327{
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004329
Gilles Peskine449bd832023-01-11 14:50:10 +01004330 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004331#if defined(PSA_WANT_ALG_GCM)
4332 case PSA_ALG_GCM:
4333 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004334 * arbitrarily large nonces. Please note that we do not generally
4335 * recommend the usage of nonces of greater length than
4336 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4337 * size, which can then lead to collisions if you encrypt a very
4338 * large number of messages.*/
4339 if (nonce_length != 0) {
4340 return PSA_SUCCESS;
4341 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004342 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004343#endif /* PSA_WANT_ALG_GCM */
4344#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004345 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004346 if (nonce_length >= 7 && nonce_length <= 13) {
4347 return PSA_SUCCESS;
4348 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004349 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004350#endif /* PSA_WANT_ALG_CCM */
4351#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004352 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 if (nonce_length == 12) {
4354 return PSA_SUCCESS;
4355 } else if (nonce_length == 8) {
4356 return PSA_ERROR_NOT_SUPPORTED;
4357 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004358 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004359#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004360 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004361 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004362 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004363 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004364
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004366}
4367
Gilles Peskine449bd832023-01-11 14:50:10 +01004368static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004369{
Gilles Peskine449bd832023-01-11 14:50:10 +01004370 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4371 return PSA_ERROR_INVALID_ARGUMENT;
4372 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004373
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004375}
4376
Gilles Peskine449bd832023-01-11 14:50:10 +01004377psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4378 psa_algorithm_t alg,
4379 const uint8_t *nonce,
4380 size_t nonce_length,
4381 const uint8_t *additional_data,
4382 size_t additional_data_length,
4383 const uint8_t *plaintext,
4384 size_t plaintext_length,
4385 uint8_t *ciphertext,
4386 size_t ciphertext_size,
4387 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004388{
Ronald Cron215633c2021-03-16 17:15:37 +01004389 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4390 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004391
mohammad1603f08a5502018-06-03 15:05:47 +03004392 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004393
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 status = psa_aead_check_algorithm(alg);
4395 if (status != PSA_SUCCESS) {
4396 return status;
4397 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004398
Ronald Cron9a986162021-03-26 12:40:07 +01004399 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4401 if (status != PSA_SUCCESS) {
4402 return status;
4403 }
mohammad16035955c982018-04-26 00:53:03 +03004404
Ronald Cron215633c2021-03-16 17:15:37 +01004405 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004407 };
mohammad16035955c982018-04-26 00:53:03 +03004408
Gilles Peskine449bd832023-01-11 14:50:10 +01004409 status = psa_aead_check_nonce_length(alg, nonce_length);
4410 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004411 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004412 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004413
Ronald Cronde822812021-03-17 16:08:20 +01004414 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004415 &attributes, slot->key.data, slot->key.bytes,
4416 alg,
4417 nonce, nonce_length,
4418 additional_data, additional_data_length,
4419 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004421
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4423 memset(ciphertext, 0, ciphertext_size);
4424 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004425
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004426exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004428
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004430}
4431
Gilles Peskine449bd832023-01-11 14:50:10 +01004432psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4433 psa_algorithm_t alg,
4434 const uint8_t *nonce,
4435 size_t nonce_length,
4436 const uint8_t *additional_data,
4437 size_t additional_data_length,
4438 const uint8_t *ciphertext,
4439 size_t ciphertext_length,
4440 uint8_t *plaintext,
4441 size_t plaintext_size,
4442 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004443{
Ronald Cron215633c2021-03-16 17:15:37 +01004444 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4445 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004446
Gilles Peskineee652a32018-06-01 19:23:52 +02004447 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004448
Gilles Peskine449bd832023-01-11 14:50:10 +01004449 status = psa_aead_check_algorithm(alg);
4450 if (status != PSA_SUCCESS) {
4451 return status;
4452 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004453
Ronald Cron9a986162021-03-26 12:40:07 +01004454 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4456 if (status != PSA_SUCCESS) {
4457 return status;
4458 }
mohammad16035955c982018-04-26 00:53:03 +03004459
Ronald Cron215633c2021-03-16 17:15:37 +01004460 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004462 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004463
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 status = psa_aead_check_nonce_length(alg, nonce_length);
4465 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004466 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004467 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004468
Ronald Cronde822812021-03-17 16:08:20 +01004469 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004470 &attributes, slot->key.data, slot->key.bytes,
4471 alg,
4472 nonce, nonce_length,
4473 additional_data, additional_data_length,
4474 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004475 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004476
Gilles Peskine449bd832023-01-11 14:50:10 +01004477 if (status != PSA_SUCCESS && plaintext_size != 0) {
4478 memset(plaintext, 0, plaintext_size);
4479 }
mohammad160360a64d02018-06-03 17:20:42 +03004480
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004481exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004482 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004483
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 return status;
mohammad16035955c982018-04-26 00:53:03 +03004485}
4486
Gilles Peskine449bd832023-01-11 14:50:10 +01004487static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4488{
4489 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004490
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004492#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004493 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004494 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004495 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4496 return PSA_ERROR_INVALID_ARGUMENT;
4497 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004498 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004499#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004500
Przemek Stekiel86679c72022-10-06 17:06:56 +02004501#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004502 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004503 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4505 return PSA_ERROR_INVALID_ARGUMENT;
4506 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004507 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004508#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004509
Przemek Stekiel86679c72022-10-06 17:06:56 +02004510#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004512 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 if (tag_len != 16) {
4514 return PSA_ERROR_INVALID_ARGUMENT;
4515 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004516 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004517#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004518
4519 default:
4520 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004522 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004524}
4525
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004526/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004527static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4528 int is_encrypt,
4529 mbedtls_svc_key_id_t key,
4530 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004531{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004532 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4533 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4534 psa_key_slot_t *slot = NULL;
4535 psa_key_usage_t key_usage = 0;
4536
Gilles Peskine449bd832023-01-11 14:50:10 +01004537 status = psa_aead_check_algorithm(alg);
4538 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004539 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004540 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004541
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004543 status = PSA_ERROR_BAD_STATE;
4544 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004545 }
4546
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 if (operation->nonce_set || operation->lengths_set ||
4548 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004549 status = PSA_ERROR_BAD_STATE;
4550 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004551 }
4552
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004554 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004556 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004557 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004558
Gilles Peskine449bd832023-01-11 14:50:10 +01004559 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4560 alg);
4561 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004562 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004564
4565 psa_key_attributes_t attributes = {
4566 .core = slot->attr
4567 };
4568
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004570 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004572
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 if (is_encrypt) {
4574 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4575 &attributes,
4576 slot->key.data,
4577 slot->key.bytes,
4578 alg);
4579 } else {
4580 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4581 &attributes,
4582 slot->key.data,
4583 slot->key.bytes,
4584 alg);
4585 }
4586 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004587 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004589
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004591
4592exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004594
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004596 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004598 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 } else {
4600 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004601 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004602
Gilles Peskine449bd832023-01-11 14:50:10 +01004603 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004604}
4605
Paul Elliott302ff6b2021-04-20 18:10:30 +01004606/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004607psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4608 mbedtls_svc_key_id_t key,
4609 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004610{
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004612}
4613
4614/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004615psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4616 mbedtls_svc_key_id_t key,
4617 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004618{
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004620}
4621
4622/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004623psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4624 uint8_t *nonce,
4625 size_t nonce_size,
4626 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004627{
4628 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004629 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004630 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004631
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004632 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004633
Gilles Peskine449bd832023-01-11 14:50:10 +01004634 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004635 status = PSA_ERROR_BAD_STATE;
4636 goto exit;
4637 }
4638
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004640 status = PSA_ERROR_BAD_STATE;
4641 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004642 }
4643
Paul Elliotte193ea82021-10-01 13:00:16 +01004644 /* For CCM, this size may not be correct according to the PSA
4645 * specification. The PSA Crypto 1.0.1 specification states:
4646 *
4647 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4648 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4649 *
4650 * However this restriction that L has to be the smallest integer is not
4651 * applied in practice, and it is not implementable here since the
4652 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4654 operation->alg);
4655 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004656 status = PSA_ERROR_BUFFER_TOO_SMALL;
4657 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004658 }
4659
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 status = psa_generate_random(local_nonce, required_nonce_size);
4661 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004662 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004663 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004664
Gilles Peskine449bd832023-01-11 14:50:10 +01004665 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004666
Paul Elliott39dc6b82021-05-11 19:16:09 +01004667exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 if (status == PSA_SUCCESS) {
4669 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004670 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 } else {
4672 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004673 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004674
Gilles Peskine449bd832023-01-11 14:50:10 +01004675 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004676}
4677
4678/* Set the nonce for a multipart authenticated encryption or decryption
4679 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004680psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4681 const uint8_t *nonce,
4682 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004683{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004684 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4685
Gilles Peskine449bd832023-01-11 14:50:10 +01004686 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004687 status = PSA_ERROR_BAD_STATE;
4688 goto exit;
4689 }
4690
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004692 status = PSA_ERROR_BAD_STATE;
4693 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004694 }
4695
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4697 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004698 status = PSA_ERROR_INVALID_ARGUMENT;
4699 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004700 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004701
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4703 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004704
Paul Elliott39dc6b82021-05-11 19:16:09 +01004705exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004707 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004708 } else {
4709 psa_aead_abort(operation);
4710 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004711
Gilles Peskine449bd832023-01-11 14:50:10 +01004712 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004713}
4714
4715/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004716psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4717 size_t ad_length,
4718 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004719{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004720 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4721
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004723 status = PSA_ERROR_BAD_STATE;
4724 goto exit;
4725 }
4726
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 if (operation->lengths_set || operation->ad_started ||
4728 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004729 status = PSA_ERROR_BAD_STATE;
4730 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004731 }
4732
Gilles Peskine449bd832023-01-11 14:50:10 +01004733 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004734#if defined(PSA_WANT_ALG_GCM)
4735 case PSA_ALG_GCM:
4736 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004737 * bits. Without the guard this code will generate warnings on 32bit
4738 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004739#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 if (((uint64_t) ad_length) >> 61 != 0 ||
4741 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004742 status = PSA_ERROR_INVALID_ARGUMENT;
4743 goto exit;
4744 }
Paul Elliott325d3742021-09-27 17:56:28 +01004745#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004746 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004747#endif /* PSA_WANT_ALG_GCM */
4748#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004749 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004750 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004751 status = PSA_ERROR_INVALID_ARGUMENT;
4752 goto exit;
4753 }
4754 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004755#endif /* PSA_WANT_ALG_CCM */
4756#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004757 case PSA_ALG_CHACHA20_POLY1305:
4758 /* No length restrictions for ChaChaPoly. */
4759 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004760#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004761 default:
4762 break;
4763 }
Paul Elliott325d3742021-09-27 17:56:28 +01004764
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4766 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004767
Paul Elliott39dc6b82021-05-11 19:16:09 +01004768exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004770 operation->ad_remaining = ad_length;
4771 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004772 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004773 } else {
4774 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004775 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004776
Gilles Peskine449bd832023-01-11 14:50:10 +01004777 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004778}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004779
4780/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004781psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4782 const uint8_t *input,
4783 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004784{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004785 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4786
Gilles Peskine449bd832023-01-11 14:50:10 +01004787 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004788 status = PSA_ERROR_BAD_STATE;
4789 goto exit;
4790 }
4791
Gilles Peskine449bd832023-01-11 14:50:10 +01004792 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004793 status = PSA_ERROR_BAD_STATE;
4794 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004795 }
4796
Gilles Peskine449bd832023-01-11 14:50:10 +01004797 if (operation->lengths_set) {
4798 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004799 status = PSA_ERROR_INVALID_ARGUMENT;
4800 goto exit;
4801 }
4802
4803 operation->ad_remaining -= input_length;
4804 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004805#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004806 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004807 status = PSA_ERROR_BAD_STATE;
4808 goto exit;
4809 }
4810#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004811
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 status = psa_driver_wrapper_aead_update_ad(operation, input,
4813 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004814
Paul Elliott39dc6b82021-05-11 19:16:09 +01004815exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004817 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 } else {
4819 psa_aead_abort(operation);
4820 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004821
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004823}
4824
4825/* Encrypt or decrypt a message fragment in an active multipart AEAD
4826 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004827psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4828 const uint8_t *input,
4829 size_t input_length,
4830 uint8_t *output,
4831 size_t output_size,
4832 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004833{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004834 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004835
4836 *output_length = 0;
4837
Gilles Peskine449bd832023-01-11 14:50:10 +01004838 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004839 status = PSA_ERROR_BAD_STATE;
4840 goto exit;
4841 }
4842
Gilles Peskine449bd832023-01-11 14:50:10 +01004843 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004844 status = PSA_ERROR_BAD_STATE;
4845 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004846 }
4847
Gilles Peskine449bd832023-01-11 14:50:10 +01004848 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004849 /* Additional data length was supplied, but not all the additional
4850 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004852 status = PSA_ERROR_INVALID_ARGUMENT;
4853 goto exit;
4854 }
4855
4856 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004857 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004858 status = PSA_ERROR_INVALID_ARGUMENT;
4859 goto exit;
4860 }
4861
4862 operation->body_remaining -= input_length;
4863 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004864#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004866 status = PSA_ERROR_BAD_STATE;
4867 goto exit;
4868 }
4869#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4872 output, output_size,
4873 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004874
Paul Elliott39dc6b82021-05-11 19:16:09 +01004875exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004877 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004878 } else {
4879 psa_aead_abort(operation);
4880 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004881
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004883}
4884
Gilles Peskine449bd832023-01-11 14:50:10 +01004885static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004886{
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 if (operation->id == 0 || !operation->nonce_set) {
4888 return PSA_ERROR_BAD_STATE;
4889 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004890
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4892 operation->body_remaining != 0)) {
4893 return PSA_ERROR_INVALID_ARGUMENT;
4894 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004895
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004897}
4898
Paul Elliott302ff6b2021-04-20 18:10:30 +01004899/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004900psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4901 uint8_t *ciphertext,
4902 size_t ciphertext_size,
4903 size_t *ciphertext_length,
4904 uint8_t *tag,
4905 size_t tag_size,
4906 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004907{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004908 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4909
Paul Elliott302ff6b2021-04-20 18:10:30 +01004910 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004911 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004912
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 status = psa_aead_final_checks(operation);
4914 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004915 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004916 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004917
Gilles Peskine449bd832023-01-11 14:50:10 +01004918 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004919 status = PSA_ERROR_BAD_STATE;
4920 goto exit;
4921 }
4922
Gilles Peskine449bd832023-01-11 14:50:10 +01004923 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4924 ciphertext_size,
4925 ciphertext_length,
4926 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004927
Paul Elliott39dc6b82021-05-11 19:16:09 +01004928exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004929
4930
Paul Elliottf47b0952021-05-21 18:02:33 +01004931 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004932 * the zero tag size, make sure the tag is set to something implausible.
4933 * Even if the operation succeeds, make sure we clear the rest of the
4934 * buffer to prevent potential leakage of anything previously placed in
4935 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004936 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004937
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004939
Gilles Peskine449bd832023-01-11 14:50:10 +01004940 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004941}
4942
4943/* Finish authenticating and decrypting a message in a multipart AEAD
4944 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004945psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4946 uint8_t *plaintext,
4947 size_t plaintext_size,
4948 size_t *plaintext_length,
4949 const uint8_t *tag,
4950 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004951{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004952 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4953
Paul Elliott302ff6b2021-04-20 18:10:30 +01004954 *plaintext_length = 0;
4955
Gilles Peskine449bd832023-01-11 14:50:10 +01004956 status = psa_aead_final_checks(operation);
4957 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004958 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004960
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004962 status = PSA_ERROR_BAD_STATE;
4963 goto exit;
4964 }
4965
Gilles Peskine449bd832023-01-11 14:50:10 +01004966 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4967 plaintext_size,
4968 plaintext_length,
4969 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004970
4971exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004973
Gilles Peskine449bd832023-01-11 14:50:10 +01004974 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004975}
4976
4977/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004978psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004979{
4980 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4981
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004983 /* The object has (apparently) been initialized but it is not (yet)
4984 * in use. It's ok to call abort on such an object, and there's
4985 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004986 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004987 }
4988
Gilles Peskine449bd832023-01-11 14:50:10 +01004989 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004990
Gilles Peskine449bd832023-01-11 14:50:10 +01004991 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004992
Gilles Peskine449bd832023-01-11 14:50:10 +01004993 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004994}
4995
Gilles Peskinee59236f2018-01-27 23:32:46 +01004996/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004997/* Generators */
4998/****************************************************************/
4999
Przemek Stekiel69c46792022-06-10 12:59:51 +02005000#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005001 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005002 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
5003 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08005004#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005005#endif /* At least one builtin KDF */
5006
Przemek Stekiel69c46792022-06-10 12:59:51 +02005007#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005008 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5009 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5010static psa_status_t psa_key_derivation_start_hmac(
5011 psa_mac_operation_t *operation,
5012 psa_algorithm_t hash_alg,
5013 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005014 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005015{
5016 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5017 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005018 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5019 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5020 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005021
Steven Cooreman72f736a2021-05-07 14:14:37 +02005022 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005023 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005024
Gilles Peskine449bd832023-01-11 14:50:10 +01005025 status = psa_driver_wrapper_mac_sign_setup(operation,
5026 &attributes,
5027 hmac_key, hmac_key_length,
5028 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005029
Gilles Peskine449bd832023-01-11 14:50:10 +01005030 psa_reset_key_attributes(&attributes);
5031 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005032}
5033#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005034
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005035#define HKDF_STATE_INIT 0 /* no input yet */
5036#define HKDF_STATE_STARTED 1 /* got salt */
5037#define HKDF_STATE_KEYED 2 /* got key */
5038#define HKDF_STATE_OUTPUT 3 /* output started */
5039
Gilles Peskinecbe66502019-05-16 16:59:18 +02005040static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005041 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005042{
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5044 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5045 } else {
5046 return operation->alg;
5047 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005048}
5049
Gilles Peskine449bd832023-01-11 14:50:10 +01005050psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005051{
5052 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005053 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5054 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005055 /* The object has (apparently) been initialized but it is not
5056 * in use. It's ok to call abort on such an object, and there's
5057 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005058 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005059#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5061 mbedtls_free(operation->ctx.hkdf.info);
5062 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5063 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005064#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005065#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5066 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005067 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5068 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5069 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5070 if (operation->ctx.tls12_prf.secret != NULL) {
5071 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5072 operation->ctx.tls12_prf.secret_length);
5073 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005074 }
5075
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 if (operation->ctx.tls12_prf.seed != NULL) {
5077 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5078 operation->ctx.tls12_prf.seed_length);
5079 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005080 }
5081
Gilles Peskine449bd832023-01-11 14:50:10 +01005082 if (operation->ctx.tls12_prf.label != NULL) {
5083 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5084 operation->ctx.tls12_prf.label_length);
5085 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005086 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005087#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 if (operation->ctx.tls12_prf.other_secret != NULL) {
5089 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5090 operation->ctx.tls12_prf.other_secret_length);
5091 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005092 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005093#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005094 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005095
5096 /* We leave the fields Ai and output_block to be erased safely by the
5097 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005098 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005099#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5100 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005101#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005102 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5103 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5104 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5105 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005106#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005107 {
5108 status = PSA_ERROR_BAD_STATE;
5109 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005110 mbedtls_platform_zeroize(operation, sizeof(*operation));
5111 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005112}
5113
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005114psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005116{
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005118 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005120 }
5121
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005122 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005123 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005124}
5125
Gilles Peskine449bd832023-01-11 14:50:10 +01005126psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5127 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005128{
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 if (operation->alg == 0) {
5130 return PSA_ERROR_BAD_STATE;
5131 }
5132 if (capacity > operation->capacity) {
5133 return PSA_ERROR_INVALID_ARGUMENT;
5134 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005135 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005137}
5138
Przemek Stekiel69c46792022-06-10 12:59:51 +02005139#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005140/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005141static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5142 psa_algorithm_t kdf_alg,
5143 uint8_t *output,
5144 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005145{
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5147 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005148 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005149 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005150#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005152#else
5153 const uint8_t last_block = 0xff;
5154#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005155
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 if (hkdf->state < HKDF_STATE_KEYED ||
5157 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005158#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005160#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 )) {
5162 return PSA_ERROR_BAD_STATE;
5163 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005164 hkdf->state = HKDF_STATE_OUTPUT;
5165
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005167 /* Copy what remains of the current block */
5168 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005170 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 }
5172 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005173 output += n;
5174 output_length -= n;
5175 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005176 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005177 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005179 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005180 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005181 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005182 * object was corrupted or if this function is called directly
5183 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 if (hkdf->block_number == last_block) {
5185 return PSA_ERROR_BAD_STATE;
5186 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005187
5188 /* We need a new block */
5189 ++hkdf->block_number;
5190 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005191
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5193 hash_alg,
5194 hkdf->prk,
5195 hash_length);
5196 if (status != PSA_SUCCESS) {
5197 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005198 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005199
5200 if (hkdf->block_number != 1) {
5201 status = psa_mac_update(&hkdf->hmac,
5202 hkdf->output_block,
5203 hash_length);
5204 if (status != PSA_SUCCESS) {
5205 return status;
5206 }
5207 }
5208 status = psa_mac_update(&hkdf->hmac,
5209 hkdf->info,
5210 hkdf->info_length);
5211 if (status != PSA_SUCCESS) {
5212 return status;
5213 }
5214 status = psa_mac_update(&hkdf->hmac,
5215 &hkdf->block_number, 1);
5216 if (status != PSA_SUCCESS) {
5217 return status;
5218 }
5219 status = psa_mac_sign_finish(&hkdf->hmac,
5220 hkdf->output_block,
5221 sizeof(hkdf->output_block),
5222 &hmac_output_length);
5223 if (status != PSA_SUCCESS) {
5224 return status;
5225 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005226 }
5227
Gilles Peskine449bd832023-01-11 14:50:10 +01005228 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005229}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005230#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005231
John Durkop07cc04a2020-11-16 22:08:34 -08005232#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5233 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005234static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5235 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005237{
Gilles Peskine449bd832023-01-11 14:50:10 +01005238 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5239 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005240 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5241 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005242 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005243
Janos Follath7742fee2019-06-17 12:58:10 +01005244 /* We can't be wanting more output after block 0xff, otherwise
5245 * the capacity check in psa_key_derivation_output_bytes() would have
5246 * prevented this call. It could happen only if the operation
5247 * object was corrupted or if this function is called directly
5248 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005249 if (tls12_prf->block_number == 0xff) {
5250 return PSA_ERROR_CORRUPTION_DETECTED;
5251 }
Janos Follath7742fee2019-06-17 12:58:10 +01005252
5253 /* We need a new block */
5254 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005255 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005256
5257 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5258 *
5259 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5260 *
5261 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5262 * HMAC_hash(secret, A(2) + seed) +
5263 * HMAC_hash(secret, A(3) + seed) + ...
5264 *
5265 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005266 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005267 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005268 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005269 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005270 * is currently extracted as `output_block` and where i is
5271 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005272 */
5273
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 status = psa_key_derivation_start_hmac(&hmac,
5275 hash_alg,
5276 tls12_prf->secret,
5277 tls12_prf->secret_length);
5278 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005279 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005280 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005281
5282 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005284 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5285 * the variable seed and in this instance means it in the context of the
5286 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 status = psa_mac_update(&hmac,
5288 tls12_prf->label,
5289 tls12_prf->label_length);
5290 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005291 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 }
5293 status = psa_mac_update(&hmac,
5294 tls12_prf->seed,
5295 tls12_prf->seed_length);
5296 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005297 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 }
5299 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005300 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5302 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005303 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005305 }
5306
Gilles Peskine449bd832023-01-11 14:50:10 +01005307 status = psa_mac_sign_finish(&hmac,
5308 tls12_prf->Ai, hash_length,
5309 &hmac_output_length);
5310 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005311 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005312 }
5313 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005314 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005316
5317 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 status = psa_key_derivation_start_hmac(&hmac,
5319 hash_alg,
5320 tls12_prf->secret,
5321 tls12_prf->secret_length);
5322 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005323 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 }
5325 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5326 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005327 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 }
5329 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5330 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005331 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005332 }
5333 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5334 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005335 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 }
5337 status = psa_mac_sign_finish(&hmac,
5338 tls12_prf->output_block, hash_length,
5339 &hmac_output_length);
5340 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005341 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005343
Janos Follath7742fee2019-06-17 12:58:10 +01005344
5345cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 cleanup_status = psa_mac_abort(&hmac);
5347 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005348 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005349 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005350
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005352}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005353
Janos Follath844eb0e2019-06-19 12:10:49 +01005354static psa_status_t psa_key_derivation_tls12_prf_read(
5355 psa_tls12_prf_key_derivation_t *tls12_prf,
5356 psa_algorithm_t alg,
5357 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005359{
Gilles Peskine449bd832023-01-11 14:50:10 +01005360 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5361 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005362 psa_status_t status;
5363 uint8_t offset, length;
5364
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005366 case PSA_TLS12_PRF_STATE_LABEL_SET:
5367 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5368 break;
5369 case PSA_TLS12_PRF_STATE_OUTPUT:
5370 break;
5371 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005373 }
5374
Gilles Peskine449bd832023-01-11 14:50:10 +01005375 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005376 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 if (tls12_prf->left_in_block == 0) {
5378 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5379 alg);
5380 if (status != PSA_SUCCESS) {
5381 return status;
5382 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005383
5384 continue;
5385 }
5386
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005388 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005389 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005390 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005392
5393 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005395 output += length;
5396 output_length -= length;
5397 tls12_prf->left_in_block -= length;
5398 }
5399
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005401}
John Durkop07cc04a2020-11-16 22:08:34 -08005402#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5403 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005404
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005405#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5406static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5407 psa_tls12_ecjpake_to_pms_t *ecjpake,
5408 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005409 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005410{
5411 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005412 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005413
Gilles Peskine449bd832023-01-11 14:50:10 +01005414 if (output_length != 32) {
5415 return PSA_ERROR_INVALID_ARGUMENT;
5416 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005417
Gilles Peskine449bd832023-01-11 14:50:10 +01005418 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5419 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5420 &output_size);
5421 if (status != PSA_SUCCESS) {
5422 return status;
5423 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005424
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 if (output_size != output_length) {
5426 return PSA_ERROR_GENERIC_ERROR;
5427 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005428
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005430}
5431#endif
5432
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005433psa_status_t psa_key_derivation_output_bytes(
5434 psa_key_derivation_operation_t *operation,
5435 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005436 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005437{
5438 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005440
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005442 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005444 }
5445
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005447 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005448 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005449 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005450 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005451 goto exit;
5452 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005454 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005455 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005456 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5457 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005458 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005459 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005461 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005462 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005463
Przemek Stekiel69c46792022-06-10 12:59:51 +02005464#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5466 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5467 output, output_length);
5468 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005469#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005470#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5471 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5473 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5474 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5475 kdf_alg, output,
5476 output_length);
5477 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005478#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5479 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005480#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005482 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5484 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005485#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5486
Gilles Peskineeab56e42018-07-12 17:12:33 +02005487 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005488 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005490 }
5491
5492exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005494 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005495 * This allows us to differentiate between exhausted operations and
5496 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5497 * operations. */
5498 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005500 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005502 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005504}
5505
David Brown7807bf72021-02-09 16:28:23 -07005506#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005507static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005508{
Gilles Peskine449bd832023-01-11 14:50:10 +01005509 if (data_size >= 8) {
5510 mbedtls_des_key_set_parity(data);
5511 }
5512 if (data_size >= 16) {
5513 mbedtls_des_key_set_parity(data + 8);
5514 }
5515 if (data_size >= 24) {
5516 mbedtls_des_key_set_parity(data + 16);
5517 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005518}
David Brown7807bf72021-02-09 16:28:23 -07005519#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005520
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005521/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 * ECC keys on a Weierstrass elliptic curve require the generation
5523 * of a private key which is an integer
5524 * in the range [1, N - 1], where N is the boundary of the private key domain:
5525 * N is the prime p for Diffie-Hellman, or the order of the
5526 * curve’s base point for ECC.
5527 *
5528 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5529 * This function generates the private key using the following process:
5530 *
5531 * 1. Draw a byte string of length ceiling(m/8) bytes.
5532 * 2. If m is not a multiple of 8, set the most significant
5533 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5534 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5535 * 4. If k > N - 2, discard the result and return to step 1.
5536 * 5. Output k + 1 as the private key.
5537 *
5538 * This method allows compliance to NIST standards, specifically the methods titled
5539 * Key-Pair Generation by Testing Candidates in the following publications:
5540 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5541 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5542 * Diffie-Hellman keys.
5543 *
5544 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5545 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5546 *
5547 * Note: Function allocates memory for *data buffer, so given *data should be
5548 * always NULL.
5549 */
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005550#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5551 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005552 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5553 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5554 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005555static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005556 psa_key_slot_t *slot,
5557 size_t bits,
5558 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005559 uint8_t **data
5560 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005561{
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005562#if defined(MBEDTLS_ECP_C)
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005563 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005564 mbedtls_mpi k;
5565 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005566 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005567 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005568
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 mbedtls_mpi_init(&k);
5570 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005571
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005572 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005573 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005574 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005576
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005578 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005579 goto cleanup;
5580 }
5581
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005582 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005584
Gilles Peskine449bd832023-01-11 14:50:10 +01005585 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005586
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005587 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005588 /* Let m be the bit size of N. */
5589 size_t m = ecp_group.nbits;
5590
Gilles Peskine449bd832023-01-11 14:50:10 +01005591 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005592
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005593 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005594 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005595
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005596 /* Note: This function is always called with *data == NULL and it
5597 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 *data = mbedtls_calloc(1, m_bytes);
5599 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005600 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005601 goto cleanup;
5602 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005603
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005605 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005607 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005608 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005609
5610 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005612 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005613 * (8 * ceiling(m/8) - m) bits of the first byte in
5614 * the string to zero.
5615 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005616 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005617 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005618 }
5619
5620 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 * big-endian byte string.
5622 */
5623 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005624
5625 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005626 * Result of comparison is returned. When it indicates error
5627 * then this function is called again.
5628 */
5629 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005630 }
5631
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005632 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005633 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5634 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005635cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005636 if (ret != 0) {
5637 status = mbedtls_to_psa_error(ret);
5638 }
5639 if (status != PSA_SUCCESS) {
5640 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005641 *data = NULL;
5642 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005643 mbedtls_mpi_free(&k);
5644 mbedtls_mpi_free(&diff_N_2);
5645 return status;
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005646#else /* MBEDTLS_ECP_C */
5647 (void) slot;
5648 (void) bits;
5649 (void) operation;
5650 (void) data;
5651 return PSA_ERROR_NOT_SUPPORTED;
5652#endif /* MBEDTLS_ECP_C */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005653}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005654
5655/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5656 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5657 *
5658 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5659 * draw a 32-byte string and process it as specified in
5660 * Elliptic Curves for Security [RFC7748] §5.
5661 *
5662 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5663 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5664 *
5665 * Note: Function allocates memory for *data buffer, so given *data should be
5666 * always NULL.
5667 */
5668
5669static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5670 size_t bits,
5671 psa_key_derivation_operation_t *operation,
5672 uint8_t **data
5673 )
5674{
5675 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005676 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005677
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005679 case 255:
5680 output_length = 32;
5681 break;
5682 case 448:
5683 output_length = 56;
5684 break;
5685 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005687 break;
5688 }
5689
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005691
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 if (*data == NULL) {
5693 return PSA_ERROR_INSUFFICIENT_MEMORY;
5694 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005695
Gilles Peskine449bd832023-01-11 14:50:10 +01005696 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005697
Gilles Peskine449bd832023-01-11 14:50:10 +01005698 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005699 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005700 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005701
Gilles Peskine449bd832023-01-11 14:50:10 +01005702 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005703 case 255:
5704 (*data)[0] &= 248;
5705 (*data)[31] &= 127;
5706 (*data)[31] |= 64;
5707 break;
5708 case 448:
5709 (*data)[0] &= 252;
5710 (*data)[55] |= 128;
5711 break;
5712 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005714 break;
5715 }
5716
5717 return status;
5718}
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005719#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5720 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005721 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5722 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5723 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005724
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005725static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005726 psa_key_slot_t *slot,
5727 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005729{
5730 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005731 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305732 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005733 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005734
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5736 return PSA_ERROR_INVALID_ARGUMENT;
5737 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005738
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005739#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5740 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005741 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5742 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5743 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5745 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5746 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005747 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005748 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5749 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005750 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005751 }
5752 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005753 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5755 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005756 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005758 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005759 } else
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005760#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5761 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005762 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5763 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5764 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 if (key_type_is_raw_bytes(slot->attr.type)) {
5766 if (bits % 8 != 0) {
5767 return PSA_ERROR_INVALID_ARGUMENT;
5768 }
5769 data = mbedtls_calloc(1, bytes);
5770 if (data == NULL) {
5771 return PSA_ERROR_INSUFFICIENT_MEMORY;
5772 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005773
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 status = psa_key_derivation_output_bytes(operation, data, bytes);
5775 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005776 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 }
David Brown12ca5032021-02-11 11:02:00 -07005778#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5780 psa_des_set_key_parity(data, bytes);
5781 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005782#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 } else {
5784 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005785 }
Ronald Crondd04d422020-11-28 15:14:42 +01005786
Ronald Cronbf33c932020-11-28 18:06:53 +01005787 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005788 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005790 };
5791
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5793 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5794 &storage_size);
5795 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305796 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005797 }
Archanad8a83dc2021-06-14 10:04:16 +05305798 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 status = psa_allocate_buffer_to_slot(slot, storage_size);
5800 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305801 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 }
Archanad8a83dc2021-06-14 10:04:16 +05305803
Gilles Peskine449bd832023-01-11 14:50:10 +01005804 status = psa_driver_wrapper_import_key(&attributes,
5805 data, bytes,
5806 slot->key.data,
5807 slot->key.bytes,
5808 &slot->key.bytes, &bits);
5809 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005810 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005812
5813exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005814 mbedtls_free(data);
5815 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005816}
5817
Gilles Peskine449bd832023-01-11 14:50:10 +01005818psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5819 psa_key_derivation_operation_t *operation,
5820 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005821{
5822 psa_status_t status;
5823 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005824 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005825
Ronald Cron81709fc2020-11-14 12:10:32 +01005826 *key = MBEDTLS_SVC_KEY_ID_INIT;
5827
Gilles Peskine0f84d622019-09-12 19:03:13 +02005828 /* Reject any attempt to create a zero-length key so that we don't
5829 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005830 if (psa_get_key_bits(attributes) == 0) {
5831 return PSA_ERROR_INVALID_ARGUMENT;
5832 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005833
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 if (operation->alg == PSA_ALG_NONE) {
5835 return PSA_ERROR_BAD_STATE;
5836 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005837
Gilles Peskine449bd832023-01-11 14:50:10 +01005838 if (!operation->can_output_key) {
5839 return PSA_ERROR_NOT_PERMITTED;
5840 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005841
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5843 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005844#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005845 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005846 /* Deriving a key in a secure element is not implemented yet. */
5847 status = PSA_ERROR_NOT_SUPPORTED;
5848 }
5849#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 if (status == PSA_SUCCESS) {
5851 status = psa_generate_derived_key_internal(slot,
5852 attributes->core.bits,
5853 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005854 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005855 if (status == PSA_SUCCESS) {
5856 status = psa_finish_key_creation(slot, driver, key);
5857 }
5858 if (status != PSA_SUCCESS) {
5859 psa_fail_key_creation(slot, driver);
5860 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005861
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005863}
5864
Gilles Peskineeab56e42018-07-12 17:12:33 +02005865
5866
5867/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005868/* Key derivation */
5869/****************************************************************/
5870
Steven Cooreman932ffb72021-02-15 12:14:32 +01005871#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005872static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005873{
5874#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5876 return 1;
5877 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005878#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005879#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5881 return 1;
5882 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005883#endif
5884#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5886 return 1;
5887 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005888#endif
5889#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005890 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5891 return 1;
5892 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005893#endif
5894#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5896 return 1;
5897 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005898#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005899#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005900 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5901 return 1;
5902 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005903#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005904 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005905}
5906
Gilles Peskine449bd832023-01-11 14:50:10 +01005907static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005908{
5909 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005910 psa_status_t status = psa_hash_setup(&operation, alg);
5911 psa_hash_abort(&operation);
5912 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005913}
5914
Gilles Peskine969c5d62019-01-16 15:53:06 +01005915static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005916 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005917 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005918{
Janos Follath5fe19732019-06-20 15:09:30 +01005919 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5920 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005922
Gilles Peskine969c5d62019-01-16 15:53:06 +01005923 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005924 if (!is_kdf_alg_supported(kdf_alg)) {
5925 return PSA_ERROR_NOT_SUPPORTED;
5926 }
John Durkop07cc04a2020-11-16 22:08:34 -08005927
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005928 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005929 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5931 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5932 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5933 if (hash_size == 0) {
5934 return PSA_ERROR_NOT_SUPPORTED;
5935 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005936
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005937 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5938 * we might fail later, which is somewhat unfriendly and potentially
5939 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 psa_status_t status = psa_hash_try_support(hash_alg);
5941 if (status != PSA_SUCCESS) {
5942 return status;
5943 }
5944 } else {
5945 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005946 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005947
Gilles Peskine449bd832023-01-11 14:50:10 +01005948 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5949 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
5950 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
5951 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005952 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005953#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005954 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005955 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
5956 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005957 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005959#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5960 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005961 operation->capacity = 255 * hash_size;
5962 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005963}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005964
Gilles Peskine449bd832023-01-11 14:50:10 +01005965static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005966{
5967#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005968 if (alg == PSA_ALG_ECDH) {
5969 return PSA_SUCCESS;
5970 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005971#endif
5972 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005974}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005975
5976static int psa_key_derivation_allows_free_form_secret_input(
5977 psa_algorithm_t kdf_alg)
5978{
5979#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
5980 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5981 return 0;
5982 }
5983#endif
5984 (void) kdf_alg;
5985 return 1;
5986}
John Durkop07cc04a2020-11-16 22:08:34 -08005987#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005988
Gilles Peskine449bd832023-01-11 14:50:10 +01005989psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
5990 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005991{
5992 psa_status_t status;
5993
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 if (operation->alg != 0) {
5995 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005996 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005997
Gilles Peskine449bd832023-01-11 14:50:10 +01005998 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
5999 return PSA_ERROR_INVALID_ARGUMENT;
6000 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6001#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6002 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6003 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6004 status = psa_key_agreement_try_support(ka_alg);
6005 if (status != PSA_SUCCESS) {
6006 return status;
6007 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006008 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6009 return PSA_ERROR_INVALID_ARGUMENT;
6010 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006011 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6012#else
6013 return PSA_ERROR_NOT_SUPPORTED;
6014#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6015 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6016#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6017 status = psa_key_derivation_setup_kdf(operation, alg);
6018#else
6019 return PSA_ERROR_NOT_SUPPORTED;
6020#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6021 } else {
6022 return PSA_ERROR_INVALID_ARGUMENT;
6023 }
6024
6025 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006026 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006027 }
6028 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006029}
6030
Przemek Stekiel69c46792022-06-10 12:59:51 +02006031#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006032static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6033 psa_algorithm_t kdf_alg,
6034 psa_key_derivation_step_t step,
6035 const uint8_t *data,
6036 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006037{
Gilles Peskine449bd832023-01-11 14:50:10 +01006038 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006039 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006040 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006041 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006042#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6044 return PSA_ERROR_INVALID_ARGUMENT;
6045 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006046#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006047 if (hkdf->state != HKDF_STATE_INIT) {
6048 return PSA_ERROR_BAD_STATE;
6049 } else {
6050 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6051 hash_alg,
6052 data, data_length);
6053 if (status != PSA_SUCCESS) {
6054 return status;
6055 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006056 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006058 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006059 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006060#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006061 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006062 /* We shouldn't be in different state as HKDF_EXPAND only allows
6063 * two inputs: SECRET (this case) and INFO which does not modify
6064 * the state. It could happen only if the hkdf
6065 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 if (hkdf->state != HKDF_STATE_INIT) {
6067 return PSA_ERROR_BAD_STATE;
6068 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006069
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006070 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006071 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6072 return PSA_ERROR_INVALID_ARGUMENT;
6073 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006074
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 memcpy(hkdf->prk, data, data_length);
6076 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006077#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006078 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006079 /* HKDF: If no salt was provided, use an empty salt.
6080 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006081 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +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 PSA_ERROR_BAD_STATE;
6085 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006086#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006087 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6088 hash_alg,
6089 NULL, 0);
6090 if (status != PSA_SUCCESS) {
6091 return status;
6092 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006093 hkdf->state = HKDF_STATE_STARTED;
6094 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 if (hkdf->state != HKDF_STATE_STARTED) {
6096 return PSA_ERROR_BAD_STATE;
6097 }
6098 status = psa_mac_update(&hkdf->hmac,
6099 data, data_length);
6100 if (status != PSA_SUCCESS) {
6101 return status;
6102 }
6103 status = psa_mac_sign_finish(&hkdf->hmac,
6104 hkdf->prk,
6105 sizeof(hkdf->prk),
6106 &data_length);
6107 if (status != PSA_SUCCESS) {
6108 return status;
6109 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006110 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006111
Gilles Peskine2b522db2019-04-12 15:11:49 +02006112 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006113 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006114#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006116 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006118 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006120#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006121 {
6122 /* Block 0 is empty, and the next block will be
6123 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006124 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006125 }
6126
Gilles Peskine449bd832023-01-11 14:50:10 +01006127 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006128 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006129#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6131 return PSA_ERROR_INVALID_ARGUMENT;
6132 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006133#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006134#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6136 hkdf->state == HKDF_STATE_INIT) {
6137 return PSA_ERROR_BAD_STATE;
6138 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006139#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006140 if (hkdf->state == HKDF_STATE_OUTPUT) {
6141 return PSA_ERROR_BAD_STATE;
6142 }
6143 if (hkdf->info_set) {
6144 return PSA_ERROR_BAD_STATE;
6145 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006146 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 if (data_length != 0) {
6148 hkdf->info = mbedtls_calloc(1, data_length);
6149 if (hkdf->info == NULL) {
6150 return PSA_ERROR_INSUFFICIENT_MEMORY;
6151 }
6152 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006153 }
6154 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006156 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006158 }
6159}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006160#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006161
John Durkop07cc04a2020-11-16 22:08:34 -08006162#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6163 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006164static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6165 const uint8_t *data,
6166 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006167{
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6169 return PSA_ERROR_BAD_STATE;
6170 }
Janos Follathf08e2652019-06-13 09:05:41 +01006171
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 if (data_length != 0) {
6173 prf->seed = mbedtls_calloc(1, data_length);
6174 if (prf->seed == NULL) {
6175 return PSA_ERROR_INSUFFICIENT_MEMORY;
6176 }
Janos Follathf08e2652019-06-13 09:05:41 +01006177
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006179 prf->seed_length = data_length;
6180 }
Janos Follathf08e2652019-06-13 09:05:41 +01006181
Gilles Peskine43f958b2020-12-13 14:55:14 +01006182 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006183
Gilles Peskine449bd832023-01-11 14:50:10 +01006184 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006185}
6186
Gilles Peskine449bd832023-01-11 14:50:10 +01006187static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6188 const uint8_t *data,
6189 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006190{
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6192 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6193 return PSA_ERROR_BAD_STATE;
6194 }
Janos Follath81550542019-06-13 14:26:34 +01006195
Gilles Peskine449bd832023-01-11 14:50:10 +01006196 if (data_length != 0) {
6197 prf->secret = mbedtls_calloc(1, data_length);
6198 if (prf->secret == NULL) {
6199 return PSA_ERROR_INSUFFICIENT_MEMORY;
6200 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006201
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006203 prf->secret_length = data_length;
6204 }
Janos Follath81550542019-06-13 14:26:34 +01006205
Gilles Peskine43f958b2020-12-13 14:55:14 +01006206 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006207
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006209}
6210
Gilles Peskine449bd832023-01-11 14:50:10 +01006211static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6212 const uint8_t *data,
6213 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006214{
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6216 return PSA_ERROR_BAD_STATE;
6217 }
Janos Follath63028dd2019-06-13 09:15:47 +01006218
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 if (data_length != 0) {
6220 prf->label = mbedtls_calloc(1, data_length);
6221 if (prf->label == NULL) {
6222 return PSA_ERROR_INSUFFICIENT_MEMORY;
6223 }
Janos Follath63028dd2019-06-13 09:15:47 +01006224
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006226 prf->label_length = data_length;
6227 }
Janos Follath63028dd2019-06-13 09:15:47 +01006228
Gilles Peskine43f958b2020-12-13 14:55:14 +01006229 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006230
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006232}
6233
Gilles Peskine449bd832023-01-11 14:50:10 +01006234static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6235 psa_key_derivation_step_t step,
6236 const uint8_t *data,
6237 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006238{
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006240 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006242 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006243 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006244 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006246 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006248 }
6249}
John Durkop07cc04a2020-11-16 22:08:34 -08006250#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6251 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6252
6253#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6254static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6255 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006256 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006258{
6259 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6261 4 + data_length + prf->other_secret_length :
6262 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006263
Gilles Peskine449bd832023-01-11 14:50:10 +01006264 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6265 return PSA_ERROR_INVALID_ARGUMENT;
6266 }
John Durkop07cc04a2020-11-16 22:08:34 -08006267
Gilles Peskine449bd832023-01-11 14:50:10 +01006268 uint8_t *pms = mbedtls_calloc(1, pms_len);
6269 if (pms == NULL) {
6270 return PSA_ERROR_INSUFFICIENT_MEMORY;
6271 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006272 uint8_t *cur = pms;
6273
6274 /* pure-PSK:
6275 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006276 *
6277 * The premaster secret is formed as follows: if the PSK is N octets
6278 * long, concatenate a uint16 with the value N, N zero octets, a second
6279 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006280 *
6281 * mixed-PSK:
6282 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6283 * follows: concatenate a uint16 with the length of the other secret,
6284 * the other secret itself, uint16 with the length of PSK, and the
6285 * PSK itself.
6286 * For details please check:
6287 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6288 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6289 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006290 */
6291
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6293 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6294 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6295 if (prf->other_secret_length != 0) {
6296 memcpy(cur, prf->other_secret, prf->other_secret_length);
6297 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006298 cur += prf->other_secret_length;
6299 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 } else {
6301 *cur++ = MBEDTLS_BYTE_1(data_length);
6302 *cur++ = MBEDTLS_BYTE_0(data_length);
6303 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006304 cur += data_length;
6305 }
6306
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 *cur++ = MBEDTLS_BYTE_1(data_length);
6308 *cur++ = MBEDTLS_BYTE_0(data_length);
6309 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006310 cur += data_length;
6311
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006313
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 mbedtls_platform_zeroize(pms, pms_len);
6315 mbedtls_free(pms);
6316 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006317}
Janos Follath6660f0e2019-06-17 08:44:03 +01006318
Przemek Stekiele47201b2022-04-19 13:53:28 +02006319static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6320 psa_tls12_prf_key_derivation_t *prf,
6321 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006323{
Gilles Peskine449bd832023-01-11 14:50:10 +01006324 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6325 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006326 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006327
6328 if (data_length != 0) {
6329 prf->other_secret = mbedtls_calloc(1, data_length);
6330 if (prf->other_secret == NULL) {
6331 return PSA_ERROR_INSUFFICIENT_MEMORY;
6332 }
6333
6334 memcpy(prf->other_secret, data, data_length);
6335 prf->other_secret_length = data_length;
6336 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006337 prf->other_secret_length = 0;
6338 }
6339
6340 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6341
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006343}
6344
Janos Follath6660f0e2019-06-17 08:44:03 +01006345static psa_status_t psa_tls12_prf_psk_to_ms_input(
6346 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006347 psa_key_derivation_step_t step,
6348 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006350{
Gilles Peskine449bd832023-01-11 14:50:10 +01006351 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006352 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 return psa_tls12_prf_psk_to_ms_set_key(prf,
6354 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006355 break;
6356 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6358 data,
6359 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006360 break;
6361 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006363 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006364
Przemek Stekiele47201b2022-04-19 13:53:28 +02006365 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006366}
John Durkop07cc04a2020-11-16 22:08:34 -08006367#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006368
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006369#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6370static psa_status_t psa_tls12_ecjpake_to_pms_input(
6371 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006372 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006373 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006374 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006375{
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6377 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6378 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006379 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006380
6381 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 if (data[0] != 0x04) {
6383 return PSA_ERROR_INVALID_ARGUMENT;
6384 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006385
6386 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006388
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006390}
6391#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006392/** Check whether the given key type is acceptable for the given
6393 * input step of a key derivation.
6394 *
6395 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6396 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6397 * Both secret and non-secret inputs can alternatively have the type
6398 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6399 * that the input was passed as a buffer rather than via a key object.
6400 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006401static int psa_key_derivation_check_input_type(
6402 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006404{
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006406 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 if (key_type == PSA_KEY_TYPE_DERIVE) {
6408 return PSA_SUCCESS;
6409 }
6410 if (key_type == PSA_KEY_TYPE_NONE) {
6411 return PSA_SUCCESS;
6412 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006413 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006414 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 if (key_type == PSA_KEY_TYPE_DERIVE) {
6416 return PSA_SUCCESS;
6417 }
6418 if (key_type == PSA_KEY_TYPE_NONE) {
6419 return PSA_SUCCESS;
6420 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006421 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006422 case PSA_KEY_DERIVATION_INPUT_LABEL:
6423 case PSA_KEY_DERIVATION_INPUT_SALT:
6424 case PSA_KEY_DERIVATION_INPUT_INFO:
6425 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6427 return PSA_SUCCESS;
6428 }
6429 if (key_type == PSA_KEY_TYPE_NONE) {
6430 return PSA_SUCCESS;
6431 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006432 break;
6433 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006435}
6436
Janos Follathb80a94e2019-06-12 15:54:46 +01006437static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006438 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006439 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006440 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006441 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006442 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006443{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006444 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006445 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006446
Gilles Peskine449bd832023-01-11 14:50:10 +01006447 status = psa_key_derivation_check_input_type(step, key_type);
6448 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006449 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006451
Przemek Stekiel69c46792022-06-10 12:59:51 +02006452#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006453 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6454 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6455 step, data, data_length);
6456 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006457#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006458#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6460 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6461 step, data, data_length);
6462 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006463#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6464#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6466 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6467 step, data, data_length);
6468 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006469#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006470#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006472 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6474 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006475#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006476 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006477 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006478 (void) data;
6479 (void) data_length;
6480 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006481 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006482 }
6483
Gilles Peskine224b0d62019-09-23 18:13:17 +02006484exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 if (status != PSA_SUCCESS) {
6486 psa_key_derivation_abort(operation);
6487 }
6488 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006489}
6490
Janos Follath51f4a0f2019-06-14 11:35:55 +01006491psa_status_t psa_key_derivation_input_bytes(
6492 psa_key_derivation_operation_t *operation,
6493 psa_key_derivation_step_t step,
6494 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006495 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006496{
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 return psa_key_derivation_input_internal(operation, step,
6498 PSA_KEY_TYPE_NONE,
6499 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006500}
6501
Janos Follath51f4a0f2019-06-14 11:35:55 +01006502psa_status_t psa_key_derivation_input_key(
6503 psa_key_derivation_operation_t *operation,
6504 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006505 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006506{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006507 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006508 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006509 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006510
Ronald Cron5c522922020-11-14 16:35:34 +01006511 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006512 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6513 if (status != PSA_SUCCESS) {
6514 psa_key_derivation_abort(operation);
6515 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006516 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006517
6518 /* Passing a key object as a SECRET input unlocks the permission
6519 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006520 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006521 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006522 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006523
Gilles Peskine449bd832023-01-11 14:50:10 +01006524 status = psa_key_derivation_input_internal(operation,
6525 step, slot->attr.type,
6526 slot->key.data,
6527 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006528
Gilles Peskine449bd832023-01-11 14:50:10 +01006529 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006530
Gilles Peskine449bd832023-01-11 14:50:10 +01006531 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006532}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006533
6534
6535
6536/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006537/* Key agreement */
6538/****************************************************************/
6539
Gilles Peskine449bd832023-01-11 14:50:10 +01006540psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6541 const uint8_t *key_buffer,
6542 size_t key_buffer_size,
6543 psa_algorithm_t alg,
6544 const uint8_t *peer_key,
6545 size_t peer_key_length,
6546 uint8_t *shared_secret,
6547 size_t shared_secret_size,
6548 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006549{
Gilles Peskine449bd832023-01-11 14:50:10 +01006550 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006551#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006552 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006553 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6554 key_buffer_size, alg,
6555 peer_key, peer_key_length,
6556 shared_secret,
6557 shared_secret_size,
6558 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006559#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006560 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006561 (void) attributes;
6562 (void) key_buffer;
6563 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006564 (void) peer_key;
6565 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006566 (void) shared_secret;
6567 (void) shared_secret_size;
6568 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006569 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006570 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006571}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006572
Aditya Deshpande17845b82022-10-13 17:21:01 +01006573/** Internal function for raw key agreement
6574 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006575 * to the driver's implementation if a driver is present.
6576 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006577 * (psa_key_agreement_raw_builtin).
6578 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006579static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6580 psa_key_slot_t *private_key,
6581 const uint8_t *peer_key,
6582 size_t peer_key_length,
6583 uint8_t *shared_secret,
6584 size_t shared_secret_size,
6585 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006586{
Gilles Peskine449bd832023-01-11 14:50:10 +01006587 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6588 return PSA_ERROR_NOT_SUPPORTED;
6589 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006590
6591 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006592 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006593 };
6594
Gilles Peskine449bd832023-01-11 14:50:10 +01006595 return psa_driver_wrapper_key_agreement(&attributes,
6596 private_key->key.data,
6597 private_key->key.bytes, alg,
6598 peer_key, peer_key_length,
6599 shared_secret,
6600 shared_secret_size,
6601 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006602}
6603
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006604/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006605 * to potentially free embedded data structures and wipe confidential data.
6606 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006607static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6608 psa_key_derivation_step_t step,
6609 psa_key_slot_t *private_key,
6610 const uint8_t *peer_key,
6611 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006612{
6613 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006614 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006615 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006616 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006617
6618 /* Step 1: run the secret agreement algorithm to generate the shared
6619 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006620 status = psa_key_agreement_raw_internal(ka_alg,
6621 private_key,
6622 peer_key, peer_key_length,
6623 shared_secret,
6624 sizeof(shared_secret),
6625 &shared_secret_length);
6626 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006627 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006628 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006629
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006630 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006631 * the shared secret. A shared secret is permitted wherever a key
6632 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006633 status = psa_key_derivation_input_internal(operation, step,
6634 PSA_KEY_TYPE_DERIVE,
6635 shared_secret,
6636 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006637exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006638 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6639 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006640}
6641
Gilles Peskine449bd832023-01-11 14:50:10 +01006642psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6643 psa_key_derivation_step_t step,
6644 mbedtls_svc_key_id_t private_key,
6645 const uint8_t *peer_key,
6646 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006647{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006648 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006649 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006650 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006651
Gilles Peskine449bd832023-01-11 14:50:10 +01006652 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6653 return PSA_ERROR_INVALID_ARGUMENT;
6654 }
Ronald Cron5c522922020-11-14 16:35:34 +01006655 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006656 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6657 if (status != PSA_SUCCESS) {
6658 return status;
6659 }
6660 status = psa_key_agreement_internal(operation, step,
6661 slot,
6662 peer_key, peer_key_length);
6663 if (status != PSA_SUCCESS) {
6664 psa_key_derivation_abort(operation);
6665 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006666 /* If a private key has been added as SECRET, we allow the derived
6667 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006668 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006669 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006670 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006671 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006672
Gilles Peskine449bd832023-01-11 14:50:10 +01006673 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006674
Gilles Peskine449bd832023-01-11 14:50:10 +01006675 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006676}
6677
Gilles Peskine449bd832023-01-11 14:50:10 +01006678psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6679 mbedtls_svc_key_id_t private_key,
6680 const uint8_t *peer_key,
6681 size_t peer_key_length,
6682 uint8_t *output,
6683 size_t output_size,
6684 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006685{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006686 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006687 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006688 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006689
Gilles Peskine449bd832023-01-11 14:50:10 +01006690 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006691 status = PSA_ERROR_INVALID_ARGUMENT;
6692 goto exit;
6693 }
Ronald Cron5c522922020-11-14 16:35:34 +01006694 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006695 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6696 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006697 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006698 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006699
Gilles Peskine3e561302022-04-14 00:17:15 +02006700 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6701 * for the output size. The PSA specification only guarantees that this
6702 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6703 * but it might be nice to allow smaller buffers if the output fits.
6704 * At the time of writing this comment, with only ECDH implemented,
6705 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6706 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6707 * be exact for it as well. */
6708 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006709 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6710 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006711 status = PSA_ERROR_BUFFER_TOO_SMALL;
6712 goto exit;
6713 }
6714
Gilles Peskine449bd832023-01-11 14:50:10 +01006715 status = psa_key_agreement_raw_internal(alg, slot,
6716 peer_key, peer_key_length,
6717 output, output_size,
6718 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006719
6720exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006721 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006722 /* If an error happens and is not handled properly, the output
6723 * may be used as a key to protect sensitive data. Arrange for such
6724 * a key to be random, which is likely to result in decryption or
6725 * verification errors. This is better than filling the buffer with
6726 * some constant data such as zeros, which would result in the data
6727 * being protected with a reproducible, easily knowable key.
6728 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006729 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006730 *output_length = output_size;
6731 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006732
Gilles Peskine449bd832023-01-11 14:50:10 +01006733 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006734
Gilles Peskine449bd832023-01-11 14:50:10 +01006735 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006736}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006737
6738
Gilles Peskine30524eb2020-11-13 17:02:26 +01006739
Gilles Peskine86a440b2018-11-12 18:39:40 +01006740/****************************************************************/
6741/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006742/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006743
Gilles Peskine30524eb2020-11-13 17:02:26 +01006744/** Initialize the PSA random generator.
6745 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006746static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006747{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006748#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006749 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006750#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6751
Gilles Peskine30524eb2020-11-13 17:02:26 +01006752 /* Set default configuration if
6753 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006754 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006755 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006756 }
6757 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006758 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006759 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006760
Gilles Peskine449bd832023-01-11 14:50:10 +01006761 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006762#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6763 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6764 /* The PSA entropy injection feature depends on using NV seed as an entropy
6765 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006766 mbedtls_entropy_add_source(&rng->entropy,
6767 mbedtls_nv_seed_poll, NULL,
6768 MBEDTLS_ENTROPY_BLOCK_SIZE,
6769 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006770#endif
6771
Gilles Peskine449bd832023-01-11 14:50:10 +01006772 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006773#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006774}
6775
6776/** Deinitialize the PSA random generator.
6777 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006778static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006779{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006780#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006781 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006782#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01006783 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
6784 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006785#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006786}
6787
6788/** Seed the PSA random generator.
6789 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006790static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006791{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006792#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6793 /* Do nothing: the external RNG seeds itself. */
6794 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006795 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006796#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006797 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01006798 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
6799 drbg_seed, sizeof(drbg_seed) - 1);
6800 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006801#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006802}
6803
Gilles Peskine449bd832023-01-11 14:50:10 +01006804psa_status_t psa_generate_random(uint8_t *output,
6805 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006806{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006807 GUARD_MODULE_INITIALIZED;
6808
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006809#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6810
6811 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006812 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6813 output, output_size,
6814 &output_length);
6815 if (status != PSA_SUCCESS) {
6816 return status;
6817 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006818 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006819 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006820 if (output_length != output_size) {
6821 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6822 }
6823 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006824
6825#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6826
Gilles Peskine449bd832023-01-11 14:50:10 +01006827 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006828 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006829 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6830 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6831 output_size);
6832 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6833 output, request_size);
6834 if (ret != 0) {
6835 return mbedtls_to_psa_error(ret);
6836 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006837 output_size -= request_size;
6838 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006839 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006840 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006841#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006842}
6843
Gilles Peskine8814fc42020-12-14 15:33:44 +01006844/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006845 *
6846 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6847 * `psa_generate_random(...)`. The state parameter is ignored since the
6848 * PSA API doesn't support passing an explicit state.
6849 *
6850 * In the non-external case, psa_generate_random() calls an
6851 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6852 * and semantics as mbedtls_psa_get_random(). As an optimization,
6853 * instead of doing this back-and-forth between the PSA API and the
6854 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6855 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006856 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006857#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6858int mbedtls_psa_get_random(void *p_rng,
6859 unsigned char *output,
6860 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01006861{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006862 /* This function takes a pointer to the RNG state because that's what
6863 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6864 * its own state internally and doesn't let the caller access that state.
6865 * So we just ignore the state parameter, and in practice we'll pass
6866 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006867 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 psa_status_t status = psa_generate_random(output, output_size);
6869 if (status == PSA_SUCCESS) {
6870 return 0;
6871 } else {
6872 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
6873 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01006874}
6875#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6876
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006877#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006878#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006879
Gilles Peskine449bd832023-01-11 14:50:10 +01006880psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
6881 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006882{
Gilles Peskine449bd832023-01-11 14:50:10 +01006883 if (global_data.initialized) {
6884 return PSA_ERROR_NOT_PERMITTED;
6885 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006886
Gilles Peskine449bd832023-01-11 14:50:10 +01006887 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
6888 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
6889 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
6890 return PSA_ERROR_INVALID_ARGUMENT;
6891 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006892
Gilles Peskine449bd832023-01-11 14:50:10 +01006893 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006894}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006895#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006896
Ronald Cron3772afe2021-02-08 16:10:05 +01006897/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006898 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006899 * \param type The key type
6900 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006901 *
6902 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006903 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006904 * \retval #PSA_ERROR_INVALID_ARGUMENT
6905 * The size in bits of the key is not valid.
6906 * \retval #PSA_ERROR_NOT_SUPPORTED
6907 * The type and/or the size in bits of the key or the combination of
6908 * the two is not supported.
6909 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006910static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006911 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006912{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006913 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006914
Gilles Peskine449bd832023-01-11 14:50:10 +01006915 if (key_type_is_raw_bytes(type)) {
6916 status = psa_validate_unstructured_key_bit_size(type, bits);
6917 if (status != PSA_SUCCESS) {
6918 return status;
6919 }
6920 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006921#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006922 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6923 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
6924 return PSA_ERROR_NOT_SUPPORTED;
6925 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006926
Ronald Cron01b2aba2020-10-05 09:42:02 +02006927 /* Accept only byte-aligned keys, for the same reasons as
6928 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006929 if (bits % 8 != 0) {
6930 return PSA_ERROR_NOT_SUPPORTED;
6931 }
6932 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006933#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006934
Ronald Cron5c4d3862020-12-07 11:07:24 +01006935#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006936 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01006937 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006938 return PSA_SUCCESS;
6939 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006940#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006941 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006942 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006943 }
6944
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006946}
6947
Ronald Cron55ed0592020-10-05 10:30:40 +02006948psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006949 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01006950 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006951{
6952 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006953 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006954
Gilles Peskine449bd832023-01-11 14:50:10 +01006955 if ((attributes->domain_parameters == NULL) &&
6956 (attributes->domain_parameters_size != 0)) {
6957 return PSA_ERROR_INVALID_ARGUMENT;
6958 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02006959
Gilles Peskine449bd832023-01-11 14:50:10 +01006960 if (key_type_is_raw_bytes(type)) {
6961 status = psa_generate_random(key_buffer, key_buffer_size);
6962 if (status != PSA_SUCCESS) {
6963 return status;
6964 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006965
David Brown12ca5032021-02-11 11:02:00 -07006966#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006967 if (type == PSA_KEY_TYPE_DES) {
6968 psa_des_set_key_parity(key_buffer, key_buffer_size);
6969 }
David Brown8107e312021-02-12 08:08:46 -07006970#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01006971 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01006972
Jaeden Amero424fa932021-05-14 08:34:32 +01006973#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
6974 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01006975 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
6976 return mbedtls_psa_rsa_generate_key(attributes,
6977 key_buffer,
6978 key_buffer_size,
6979 key_buffer_length);
6980 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01006981#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
6982 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006983
John Durkop9814fa22020-11-04 12:28:15 -08006984#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006985 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6986 return mbedtls_psa_ecp_generate_key(attributes,
6987 key_buffer,
6988 key_buffer_size,
6989 key_buffer_length);
6990 } else
John Durkop9814fa22020-11-04 12:28:15 -08006991#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006992 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006993 (void) key_buffer_length;
6994 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02006995 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006996
Gilles Peskine449bd832023-01-11 14:50:10 +01006997 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006998}
Darryl Green0c6575a2018-11-07 16:05:30 +00006999
Gilles Peskine449bd832023-01-11 14:50:10 +01007000psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7001 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007002{
7003 psa_status_t status;
7004 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007005 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007006 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007007
Ronald Cron81709fc2020-11-14 12:10:32 +01007008 *key = MBEDTLS_SVC_KEY_ID_INIT;
7009
Gilles Peskine0f84d622019-09-12 19:03:13 +02007010 /* Reject any attempt to create a zero-length key so that we don't
7011 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007012 if (psa_get_key_bits(attributes) == 0) {
7013 return PSA_ERROR_INVALID_ARGUMENT;
7014 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007015
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007016 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007017 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7018 return PSA_ERROR_INVALID_ARGUMENT;
7019 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007020
Gilles Peskine449bd832023-01-11 14:50:10 +01007021 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7022 &slot, &driver);
7023 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007024 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007025 }
Gilles Peskine11792082019-08-06 18:36:36 +02007026
Ronald Cron977c2472020-10-13 08:32:21 +02007027 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307028 * storage ( thus not in the case of generating a key in a secure element
7029 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7030 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007031 if (slot->key.data == NULL) {
7032 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7033 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007034 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007035 attributes->core.type, attributes->core.bits);
7036 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007037 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007038 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007039
7040 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007041 attributes->core.type,
7042 attributes->core.bits);
7043 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007044 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007045 attributes, &key_buffer_size);
7046 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007047 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007048 }
Ronald Cron977c2472020-10-13 08:32:21 +02007049 }
Ronald Cron977c2472020-10-13 08:32:21 +02007050
Gilles Peskine449bd832023-01-11 14:50:10 +01007051 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7052 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007053 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007054 }
Ronald Cron977c2472020-10-13 08:32:21 +02007055 }
7056
Gilles Peskine449bd832023-01-11 14:50:10 +01007057 status = psa_driver_wrapper_generate_key(attributes,
7058 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007059
Gilles Peskine449bd832023-01-11 14:50:10 +01007060 if (status != PSA_SUCCESS) {
7061 psa_remove_key_data_from_memory(slot);
7062 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007063
Gilles Peskine11792082019-08-06 18:36:36 +02007064exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007065 if (status == PSA_SUCCESS) {
7066 status = psa_finish_key_creation(slot, driver, key);
7067 }
7068 if (status != PSA_SUCCESS) {
7069 psa_fail_key_creation(slot, driver);
7070 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007071
Gilles Peskine449bd832023-01-11 14:50:10 +01007072 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007073}
7074
Gilles Peskinee59236f2018-01-27 23:32:46 +01007075/****************************************************************/
7076/* Module setup */
7077/****************************************************************/
7078
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007079#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007080psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007081 void (* entropy_init)(mbedtls_entropy_context *ctx),
7082 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007083{
Gilles Peskine449bd832023-01-11 14:50:10 +01007084 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7085 return PSA_ERROR_BAD_STATE;
7086 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007087 global_data.rng.entropy_init = entropy_init;
7088 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007089 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007090}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007091#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007092
Gilles Peskine449bd832023-01-11 14:50:10 +01007093void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007094{
Gilles Peskine449bd832023-01-11 14:50:10 +01007095 psa_wipe_all_key_slots();
7096 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7097 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007098 }
7099 /* Wipe all remaining data, including configuration.
7100 * In particular, this sets all state indicator to the value
7101 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007102 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007103
7104 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007105 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007106}
7107
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007108#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7109/** Recover a transaction that was interrupted by a power failure.
7110 *
7111 * This function is called during initialization, before psa_crypto_init()
7112 * returns. If this function returns a failure status, the initialization
7113 * fails.
7114 */
7115static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007116 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007117{
Gilles Peskine449bd832023-01-11 14:50:10 +01007118 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007119 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7120 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007121 /* TODO - fall through to the failure case until this
7122 * is implemented.
7123 * https://github.com/ARMmbed/mbed-crypto/issues/218
7124 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007125 default:
7126 /* We found an unsupported transaction in the storage.
7127 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007128 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007129 }
7130}
7131#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7132
Gilles Peskine449bd832023-01-11 14:50:10 +01007133psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007134{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007135 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007136
Gilles Peskinec6b69072018-11-20 21:42:52 +01007137 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007138 if (global_data.initialized != 0) {
7139 return PSA_SUCCESS;
7140 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007141
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007142 /* Init drivers */
7143 status = psa_driver_wrapper_init();
7144 if (status != PSA_SUCCESS) {
7145 goto exit;
7146 }
7147 global_data.drivers_initialized = 1;
7148
Gilles Peskine30524eb2020-11-13 17:02:26 +01007149 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007150 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007151 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007152 status = mbedtls_psa_random_seed(&global_data.rng);
7153 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007154 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007155 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007156 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007157
Gilles Peskine449bd832023-01-11 14:50:10 +01007158 status = psa_initialize_key_slots();
7159 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007160 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007161 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007162
Gilles Peskine4b734222019-07-24 15:56:31 +02007163#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007164 status = psa_crypto_load_transaction();
7165 if (status == PSA_SUCCESS) {
7166 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7167 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007168 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007169 }
7170 status = psa_crypto_stop_transaction();
7171 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007172 /* There's no transaction to complete. It's all good. */
7173 status = PSA_SUCCESS;
7174 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007175#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007176
Gilles Peskinec6b69072018-11-20 21:42:52 +01007177 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007178 global_data.initialized = 1;
7179
7180exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007181 if (status != PSA_SUCCESS) {
7182 mbedtls_psa_crypto_free();
7183 }
7184 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007185}
7186
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007187psa_status_t psa_crypto_driver_pake_get_password_len(
7188 const psa_crypto_driver_pake_inputs_t *inputs,
7189 size_t *password_len)
7190{
7191 if (inputs->password_len == 0) {
7192 return PSA_ERROR_BAD_STATE;
7193 }
7194
7195 *password_len = inputs->password_len;
7196
7197 return PSA_SUCCESS;
7198}
7199
7200psa_status_t psa_crypto_driver_pake_get_password(
7201 const psa_crypto_driver_pake_inputs_t *inputs,
7202 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7203{
7204 if (inputs->password_len == 0) {
7205 return PSA_ERROR_BAD_STATE;
7206 }
7207
7208 if (buffer_size < inputs->password_len) {
7209 return PSA_ERROR_BUFFER_TOO_SMALL;
7210 }
7211
7212 memcpy(buffer, inputs->password, inputs->password_len);
7213 *buffer_length = inputs->password_len;
7214
7215 return PSA_SUCCESS;
7216}
7217
7218psa_status_t psa_crypto_driver_pake_get_role(
7219 const psa_crypto_driver_pake_inputs_t *inputs,
7220 psa_pake_role_t *role)
7221{
7222 if (inputs->role == PSA_PAKE_ROLE_NONE) {
7223 return PSA_ERROR_BAD_STATE;
7224 }
7225
7226 *role = inputs->role;
7227
7228 return PSA_SUCCESS;
7229}
7230
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007231psa_status_t psa_crypto_driver_pake_get_user_len(
7232 const psa_crypto_driver_pake_inputs_t *inputs,
7233 size_t *user_len)
7234{
7235 if (inputs->user_len == 0) {
7236 return PSA_ERROR_BAD_STATE;
7237 }
7238
7239 *user_len = inputs->user_len;
7240
7241 return PSA_SUCCESS;
7242}
7243
7244psa_status_t psa_crypto_driver_pake_get_user(
7245 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007246 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007247{
7248 if (inputs->user_len == 0) {
7249 return PSA_ERROR_BAD_STATE;
7250 }
7251
Przemek Stekielc0e62502023-03-14 11:49:36 +01007252 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007253 return PSA_ERROR_BUFFER_TOO_SMALL;
7254 }
7255
Przemek Stekielc0e62502023-03-14 11:49:36 +01007256 memcpy(user_id, inputs->user, inputs->user_len);
7257 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007258
7259 return PSA_SUCCESS;
7260}
7261
7262psa_status_t psa_crypto_driver_pake_get_peer_len(
7263 const psa_crypto_driver_pake_inputs_t *inputs,
7264 size_t *peer_len)
7265{
7266 if (inputs->peer_len == 0) {
7267 return PSA_ERROR_BAD_STATE;
7268 }
7269
7270 *peer_len = inputs->peer_len;
7271
7272 return PSA_SUCCESS;
7273}
7274
7275psa_status_t psa_crypto_driver_pake_get_peer(
7276 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007277 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007278{
7279 if (inputs->peer_len == 0) {
7280 return PSA_ERROR_BAD_STATE;
7281 }
7282
Przemek Stekielc0e62502023-03-14 11:49:36 +01007283 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007284 return PSA_ERROR_BUFFER_TOO_SMALL;
7285 }
7286
Przemek Stekielc0e62502023-03-14 11:49:36 +01007287 memcpy(peer_id, inputs->peer, inputs->peer_len);
7288 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007289
7290 return PSA_SUCCESS;
7291}
7292
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007293psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7294 const psa_crypto_driver_pake_inputs_t *inputs,
7295 psa_pake_cipher_suite_t *cipher_suite)
7296{
7297 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7298 return PSA_ERROR_BAD_STATE;
7299 }
7300
7301 *cipher_suite = inputs->cipher_suite;
7302
7303 return PSA_SUCCESS;
7304}
7305
Neil Armstronga7d08c32022-06-01 18:21:20 +02007306psa_status_t psa_pake_setup(
7307 psa_pake_operation_t *operation,
7308 const psa_pake_cipher_suite_t *cipher_suite)
7309{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007310 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007311
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007312 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007313 status = PSA_ERROR_BAD_STATE;
7314 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007315 }
7316
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007317 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007318 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007319 status = PSA_ERROR_INVALID_ARGUMENT;
7320 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007321 }
7322
Przemek Stekiel51eac532022-12-07 11:04:51 +01007323 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7324
Przemek Stekiele12ed362022-12-21 12:54:46 +01007325 operation->alg = cipher_suite->algorithm;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007326 operation->data.inputs.cipher_suite = *cipher_suite;
7327
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007328#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007329 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007330 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007331 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007332
Przemek Stekiele12ed362022-12-21 12:54:46 +01007333 computation_stage->state = PSA_PAKE_STATE_SETUP;
7334 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7335 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7336 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007337 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007338#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007339 {
7340 status = PSA_ERROR_NOT_SUPPORTED;
7341 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007342 }
7343
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007344 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7345
Przemek Stekiel51eac532022-12-07 11:04:51 +01007346 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007347exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007348 psa_pake_abort(operation);
7349 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007350}
7351
7352psa_status_t psa_pake_set_password_key(
7353 psa_pake_operation_t *operation,
7354 mbedtls_svc_key_id_t password)
7355{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007356 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007357 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7358 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007359
Przemek Stekiel51eac532022-12-07 11:04:51 +01007360 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007361 status = PSA_ERROR_BAD_STATE;
7362 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007363 }
7364
Przemek Stekiel6c764412022-11-22 14:05:12 +01007365 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7366 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007367 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007368 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007369 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007370 }
7371
Przemek Stekiel6c764412022-11-22 14:05:12 +01007372 psa_key_attributes_t attributes = {
7373 .core = slot->attr
7374 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007375
Przemek Stekiel51eac532022-12-07 11:04:51 +01007376 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007377
Przemek Stekiel51eac532022-12-07 11:04:51 +01007378 if (type != PSA_KEY_TYPE_PASSWORD &&
7379 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7380 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007381 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007382 }
7383
Przemek Stekiel51eac532022-12-07 11:04:51 +01007384 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7385 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007386 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007387 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007388 }
7389
7390 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7391 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007392 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007393exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007394 if (status != PSA_SUCCESS) {
7395 psa_pake_abort(operation);
7396 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007397 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007398 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007399}
7400
7401psa_status_t psa_pake_set_user(
7402 psa_pake_operation_t *operation,
7403 const uint8_t *user_id,
7404 size_t user_id_len)
7405{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007406 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007407
7408 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007409 status = PSA_ERROR_BAD_STATE;
7410 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007411 }
7412
Przemek Stekiel51eac532022-12-07 11:04:51 +01007413 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007414 status = PSA_ERROR_INVALID_ARGUMENT;
7415 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007416 }
7417
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007418 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007419 status = PSA_ERROR_BAD_STATE;
7420 goto exit;
7421 }
7422
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007423 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007424 if ((user_id_len != sizeof(jpake_server_id) ||
7425 memcmp(user_id, jpake_server_id, user_id_len) != 0) &&
7426 (user_id_len != sizeof(jpake_client_id) ||
7427 memcmp(user_id, jpake_client_id, user_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007428 status = PSA_ERROR_NOT_SUPPORTED;
7429 goto exit;
7430 }
7431
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007432 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7433 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007434 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7435 goto exit;
7436 }
7437
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007438 memcpy(operation->data.inputs.user, user_id, user_id_len);
7439 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007440
7441 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007442exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007443 psa_pake_abort(operation);
7444 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007445}
7446
7447psa_status_t psa_pake_set_peer(
7448 psa_pake_operation_t *operation,
7449 const uint8_t *peer_id,
7450 size_t peer_id_len)
7451{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007452 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007453
7454 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007455 status = PSA_ERROR_BAD_STATE;
7456 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007457 }
7458
Przemek Stekiel51eac532022-12-07 11:04:51 +01007459 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007460 status = PSA_ERROR_INVALID_ARGUMENT;
7461 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007462 }
7463
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007464 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007465 status = PSA_ERROR_BAD_STATE;
7466 goto exit;
7467 }
7468
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007469 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007470 if ((peer_id_len != sizeof(jpake_server_id) ||
7471 memcmp(peer_id, jpake_server_id, peer_id_len) != 0) &&
7472 (peer_id_len != sizeof(jpake_client_id) ||
7473 memcmp(peer_id, jpake_client_id, peer_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007474 status = PSA_ERROR_NOT_SUPPORTED;
7475 goto exit;
7476 }
7477
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007478 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7479 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007480 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7481 goto exit;
7482 }
7483
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007484 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7485 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007486
7487 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007488exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007489 psa_pake_abort(operation);
7490 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007491}
7492
7493psa_status_t psa_pake_set_role(
7494 psa_pake_operation_t *operation,
7495 psa_pake_role_t role)
7496{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007497 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007498
Przemek Stekiel51eac532022-12-07 11:04:51 +01007499 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007500 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007501 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007502 }
7503
Przemek Stekiel09104b82023-03-06 14:11:51 +01007504 switch (operation->alg) {
7505#if defined(PSA_WANT_ALG_JPAKE)
7506 case PSA_ALG_JPAKE:
7507 if (role == PSA_PAKE_ROLE_NONE) {
7508 return PSA_SUCCESS;
7509 }
7510 status = PSA_ERROR_INVALID_ARGUMENT;
7511 break;
7512#endif
7513 default:
7514 (void) role;
7515 status = PSA_ERROR_NOT_SUPPORTED;
7516 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007517 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007518exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007519 psa_pake_abort(operation);
7520 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007521}
7522
Przemek Stekielb09c4872023-01-17 12:05:38 +01007523/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007524#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007525static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007526 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007527{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007528 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007529 case PSA_PAKE_OUTPUT_X1_X2:
7530 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007531 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007532 case PSA_PAKE_X1_STEP_KEY_SHARE:
7533 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007534 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7535 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007536 case PSA_PAKE_X1_STEP_ZK_PROOF:
7537 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007538 case PSA_PAKE_X2_STEP_KEY_SHARE:
7539 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007540 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7541 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007542 case PSA_PAKE_X2_STEP_ZK_PROOF:
7543 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007544 default:
7545 return PSA_JPAKE_STEP_INVALID;
7546 }
7547 break;
7548 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007549 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007550 case PSA_PAKE_X1_STEP_KEY_SHARE:
7551 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007552 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7553 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007554 case PSA_PAKE_X1_STEP_ZK_PROOF:
7555 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007556 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007557 return PSA_JPAKE_STEP_INVALID;
7558 }
7559 break;
7560 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007561 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007562 case PSA_PAKE_X1_STEP_KEY_SHARE:
7563 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007564 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7565 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007566 case PSA_PAKE_X1_STEP_ZK_PROOF:
7567 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007568 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007569 return PSA_JPAKE_STEP_INVALID;
7570 }
7571 break;
7572 default:
7573 return PSA_JPAKE_STEP_INVALID;
7574 }
7575 return PSA_JPAKE_STEP_INVALID;
7576}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007577#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007578
Przemek Stekiel2797d372022-12-22 11:19:22 +01007579static psa_status_t psa_pake_complete_inputs(
7580 psa_pake_operation_t *operation)
7581{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007582 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007583 /* Create copy of the inputs on stack as inputs share memory
7584 with the driver context which will be setup by the driver. */
7585 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007586
Przemek Stekielfde11282023-03-13 16:06:09 +01007587 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007588 return PSA_ERROR_BAD_STATE;
7589 }
7590
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007591 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007592 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7593 return PSA_ERROR_BAD_STATE;
7594 }
7595 if (memcmp(inputs.user, jpake_client_id, inputs.user_len) == 0 &&
7596 memcmp(inputs.peer, jpake_server_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007597 inputs.role = PSA_PAKE_ROLE_CLIENT;
7598 } else
Przemek Stekielfde11282023-03-13 16:06:09 +01007599 if (memcmp(inputs.user, jpake_server_id, inputs.user_len) == 0 &&
7600 memcmp(inputs.peer, jpake_client_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007601 inputs.role = PSA_PAKE_ROLE_SERVER;
7602 }
7603
7604 if (inputs.role != PSA_PAKE_ROLE_CLIENT &&
7605 inputs.role != PSA_PAKE_ROLE_SERVER) {
7606 return PSA_ERROR_NOT_SUPPORTED;
7607 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007608 }
7609
Przemek Stekiel18620a32023-01-17 16:34:52 +01007610 /* Clear driver context */
7611 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7612
7613 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007614
7615 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007616 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7617 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007618
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007619 /* User and peer are translated to role. */
7620 mbedtls_free(inputs.user);
7621 mbedtls_free(inputs.peer);
7622
Przemek Stekiel2797d372022-12-22 11:19:22 +01007623 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007624#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007625 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007626 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007627 psa_jpake_computation_stage_t *computation_stage =
7628 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007629 computation_stage->state = PSA_PAKE_STATE_READY;
7630 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7631 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7632 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007633 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007634#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007635 {
7636 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007637 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007638 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007639 return status;
7640}
7641
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007642#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007643static psa_status_t psa_jpake_output_prologue(
7644 psa_pake_operation_t *operation,
7645 psa_pake_step_t step)
7646{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007647 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7648 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7649 step != PSA_PAKE_STEP_ZK_PROOF) {
7650 return PSA_ERROR_INVALID_ARGUMENT;
7651 }
7652
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007653 psa_jpake_computation_stage_t *computation_stage =
7654 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007655
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007656 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7657 return PSA_ERROR_BAD_STATE;
7658 }
7659
7660 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7661 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
7662 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
7663 return PSA_ERROR_BAD_STATE;
7664 }
7665
7666 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7667 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007668 return PSA_ERROR_BAD_STATE;
7669 }
7670
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007671 switch (computation_stage->output_step) {
7672 case PSA_PAKE_STEP_X1_X2:
7673 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007674 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007675 case PSA_PAKE_STEP_X2S:
7676 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007677 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007678 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007679 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007680 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007681
7682 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7683 }
7684
7685 /* Check if step matches current sequence */
7686 switch (computation_stage->sequence) {
7687 case PSA_PAKE_X1_STEP_KEY_SHARE:
7688 case PSA_PAKE_X2_STEP_KEY_SHARE:
7689 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7690 return PSA_ERROR_BAD_STATE;
7691 }
7692 break;
7693
7694 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7695 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7696 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7697 return PSA_ERROR_BAD_STATE;
7698 }
7699 break;
7700
7701 case PSA_PAKE_X1_STEP_ZK_PROOF:
7702 case PSA_PAKE_X2_STEP_ZK_PROOF:
7703 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7704 return PSA_ERROR_BAD_STATE;
7705 }
7706 break;
7707
7708 default:
7709 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007710 }
7711
7712 return PSA_SUCCESS;
7713}
7714
7715static psa_status_t psa_jpake_output_epilogue(
7716 psa_pake_operation_t *operation)
7717{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007718 psa_jpake_computation_stage_t *computation_stage =
7719 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007720
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007721 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007722 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007723 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007724 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007725 computation_stage->state = PSA_PAKE_STATE_READY;
7726 computation_stage->output_step++;
7727 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7728 } else {
7729 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007730 }
7731
7732 return PSA_SUCCESS;
7733}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007734#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007735
Neil Armstronga7d08c32022-06-01 18:21:20 +02007736psa_status_t psa_pake_output(
7737 psa_pake_operation_t *operation,
7738 psa_pake_step_t step,
7739 uint8_t *output,
7740 size_t output_size,
7741 size_t *output_length)
7742{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007743 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007744 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007745 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007746
7747 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007748 status = psa_pake_complete_inputs(operation);
7749 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007750 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007751 }
7752 }
7753
7754 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007755 status = PSA_ERROR_BAD_STATE;
7756 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007757 }
7758
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007759 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007760 status = PSA_ERROR_INVALID_ARGUMENT;
7761 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007762 }
7763
Przemek Stekiele12ed362022-12-21 12:54:46 +01007764 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007765#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007766 case PSA_ALG_JPAKE:
7767 status = psa_jpake_output_prologue(operation, step);
7768 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007769 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007770 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007771 driver_step = convert_jpake_computation_stage_to_driver_step(
7772 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007773 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007774#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007775 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007776 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007777 status = PSA_ERROR_NOT_SUPPORTED;
7778 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007779 }
7780
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007781 status = psa_driver_wrapper_pake_output(operation, driver_step,
7782 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007783
7784 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007785 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007786 }
7787
7788 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007789#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007790 case PSA_ALG_JPAKE:
7791 status = psa_jpake_output_epilogue(operation);
7792 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007793 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007794 }
7795 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007796#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007797 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007798 status = PSA_ERROR_NOT_SUPPORTED;
7799 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007800 }
7801
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007802 return PSA_SUCCESS;
7803exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007804 psa_pake_abort(operation);
7805 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007806}
7807
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007808#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007809static psa_status_t psa_jpake_input_prologue(
7810 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007811 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007812{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007813 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7814 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7815 step != PSA_PAKE_STEP_ZK_PROOF) {
7816 return PSA_ERROR_INVALID_ARGUMENT;
7817 }
7818
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007819 psa_jpake_computation_stage_t *computation_stage =
7820 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007821
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007822 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7823 return PSA_ERROR_BAD_STATE;
7824 }
7825
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007826 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7827 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
7828 computation_stage->state != PSA_PAKE_INPUT_X4S) {
7829 return PSA_ERROR_BAD_STATE;
7830 }
7831
7832 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7833 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007834 return PSA_ERROR_BAD_STATE;
7835 }
7836
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007837 switch (computation_stage->input_step) {
7838 case PSA_PAKE_STEP_X1_X2:
7839 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007840 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007841 case PSA_PAKE_STEP_X2S:
7842 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007843 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007844 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007845 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007846 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007847
7848 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7849 }
7850
7851 /* Check if step matches current sequence */
7852 switch (computation_stage->sequence) {
7853 case PSA_PAKE_X1_STEP_KEY_SHARE:
7854 case PSA_PAKE_X2_STEP_KEY_SHARE:
7855 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7856 return PSA_ERROR_BAD_STATE;
7857 }
7858 break;
7859
7860 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7861 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7862 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7863 return PSA_ERROR_BAD_STATE;
7864 }
7865 break;
7866
7867 case PSA_PAKE_X1_STEP_ZK_PROOF:
7868 case PSA_PAKE_X2_STEP_ZK_PROOF:
7869 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7870 return PSA_ERROR_BAD_STATE;
7871 }
7872 break;
7873
7874 default:
7875 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007876 }
7877
7878 return PSA_SUCCESS;
7879}
7880
Przemek Stekiele12ed362022-12-21 12:54:46 +01007881static psa_status_t psa_jpake_input_epilogue(
7882 psa_pake_operation_t *operation)
7883{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007884 psa_jpake_computation_stage_t *computation_stage =
7885 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007886
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007887 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007888 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007889 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007890 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007891 computation_stage->state = PSA_PAKE_STATE_READY;
7892 computation_stage->input_step++;
7893 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7894 } else {
7895 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007896 }
7897
7898 return PSA_SUCCESS;
7899}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007900#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007901
Neil Armstronga7d08c32022-06-01 18:21:20 +02007902psa_status_t psa_pake_input(
7903 psa_pake_operation_t *operation,
7904 psa_pake_step_t step,
7905 const uint8_t *input,
7906 size_t input_length)
7907{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007908 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007909 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007910
7911 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007912 status = psa_pake_complete_inputs(operation);
7913 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007914 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007915 }
7916 }
7917
7918 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007919 status = PSA_ERROR_BAD_STATE;
7920 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007921 }
7922
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007923 if (input_length == 0 || input_length > PSA_PAKE_INPUT_MAX_SIZE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007924 status = PSA_ERROR_INVALID_ARGUMENT;
7925 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007926 }
7927
Przemek Stekiele12ed362022-12-21 12:54:46 +01007928 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007929#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007930 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007931 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007932 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007933 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007934 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007935 driver_step = convert_jpake_computation_stage_to_driver_step(
7936 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007937 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007938#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007939 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007940 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007941 status = PSA_ERROR_NOT_SUPPORTED;
7942 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007943 }
7944
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007945 status = psa_driver_wrapper_pake_input(operation, driver_step,
7946 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007947
7948 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007949 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007950 }
7951
7952 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007953#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007954 case PSA_ALG_JPAKE:
7955 status = psa_jpake_input_epilogue(operation);
7956 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007957 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007958 }
7959 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007960#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007961 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007962 status = PSA_ERROR_NOT_SUPPORTED;
7963 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007964 }
7965
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007966 return PSA_SUCCESS;
7967exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007968 psa_pake_abort(operation);
7969 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007970}
7971
7972psa_status_t psa_pake_get_implicit_key(
7973 psa_pake_operation_t *operation,
7974 psa_key_derivation_operation_t *output)
7975{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007976 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007977 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007978 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01007979 size_t shared_key_len = 0;
7980
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007981 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007982 status = PSA_ERROR_BAD_STATE;
7983 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007984 }
7985
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007986#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007987 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007988 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01007989 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007990 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
7991 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007992 status = PSA_ERROR_BAD_STATE;
7993 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007994 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01007995 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007996#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007997 {
7998 status = PSA_ERROR_NOT_SUPPORTED;
7999 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008000 }
8001
Przemek Stekiel0c781802022-11-29 14:53:13 +01008002 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8003 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008004 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008005 &shared_key_len);
8006
8007 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008008 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008009 }
8010
8011 status = psa_key_derivation_input_bytes(output,
8012 PSA_KEY_DERIVATION_INPUT_SECRET,
8013 shared_key,
8014 shared_key_len);
8015
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008016 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008017exit:
8018 abort_status = psa_pake_abort(operation);
8019 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008020}
8021
8022psa_status_t psa_pake_abort(
8023 psa_pake_operation_t *operation)
8024{
Przemek Stekield69dca92023-01-31 19:59:20 +01008025 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008026
Przemek Stekield69dca92023-01-31 19:59:20 +01008027 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008028 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008029 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008030
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008031 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8032 if (operation->data.inputs.password != NULL) {
8033 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008034 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008035 mbedtls_free(operation->data.inputs.password);
8036 }
8037 if (operation->data.inputs.user != NULL) {
8038 mbedtls_free(operation->data.inputs.user);
8039 }
8040 if (operation->data.inputs.peer != NULL) {
8041 mbedtls_free(operation->data.inputs.peer);
8042 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008043 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008044 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008045
Przemek Stekield69dca92023-01-31 19:59:20 +01008046 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008047}
Neil Armstronga7d08c32022-06-01 18:21:20 +02008048
Gilles Peskinee59236f2018-01-27 23:32:46 +01008049#endif /* MBEDTLS_PSA_CRYPTO_C */