blob: 2d53bf698db90a6ca95586c34bcafb544234dae6 [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
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010093/****************************************************************/
94/* Global data, support functions and library management */
95/****************************************************************/
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020098{
Gilles Peskine449bd832023-01-11 14:50:10 +010099 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200100}
101
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100102/* Values for psa_global_data_t::rng_state */
103#define RNG_NOT_INITIALIZED 0
104#define RNG_INITIALIZED 1
105#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100108 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100109 unsigned rng_state : 2;
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100110 unsigned drivers_initialized : 1;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100111 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100112} psa_global_data_t;
113
114static psa_global_data_t global_data;
115
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100116#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
117mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
118 &global_data.rng.drbg;
119#endif
120
itayzafrir0adf0fc2018-09-06 16:24:41 +0300121#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 if (global_data.initialized == 0) \
123 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300124
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100125int psa_can_do_hash(psa_algorithm_t hash_alg)
126{
127 (void) hash_alg;
128 return global_data.drivers_initialized;
129}
130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100132{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100133 /* Mbed TLS error codes can combine a high-level error code and a
134 * low-level error code. The low-level error usually reflects the
135 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 int low_level_ret = -(-ret & 0x007f);
137 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100138 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100140
141 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
142 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200144 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
145 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
146 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
147 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
148 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200150 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200152 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200154
Jaeden Amero93e21112019-02-20 13:57:28 +0000155#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000156 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000157#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100158 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100160
161 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100163 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100165
Gilles Peskine26869f22019-05-06 15:25:00 +0200166 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200168
169 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200171 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200173
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100178 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100180 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100182 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100184 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100186 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
190 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100191 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
192 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
196 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100198 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100200#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100201
202 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100204
Gilles Peskinee59236f2018-01-27 23:32:46 +0100205 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
206 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
207 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100209
210 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200212 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100214 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100216
Gilles Peskine82e57d12020-11-13 21:31:17 +0100217#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100219 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
220 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100221 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100223 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
224 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100226 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100228#endif
229
Gilles Peskinea5905292018-02-07 20:59:33 +0100230 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100232 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100234 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100236 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100238
Gilles Peskinef76aa772018-10-29 19:24:33 +0100239 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100241 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100243 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100245 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100247 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
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_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100251 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
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_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100255
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100256 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100258 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
259 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100261 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100263 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
264 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100266 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100268 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
269 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 case MBEDTLS_ERR_PK_INVALID_ALG:
274 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
275 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100277 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200279 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100281
Gilles Peskineff2d2002019-05-06 15:26:23 +0200282 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200284 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200286
Gilles Peskinea5905292018-02-07 20:59:33 +0100287 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100289 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100291 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100293 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100295 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
296 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100298 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100300 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100302 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200304
itayzafrir5c753392018-05-08 11:18:38 +0300305 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300306 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300308 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300310 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300312 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
313 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300315 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100317 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100319
Paul Elliott588f8ed2022-12-02 18:10:26 +0000320 case MBEDTLS_ERR_ECP_IN_PROGRESS:
321 return PSA_OPERATION_INCOMPLETE;
322
Janos Follatha13b9052019-11-22 12:48:59 +0000323 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300325
Gilles Peskinee59236f2018-01-27 23:32:46 +0100326 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100328 }
329}
330
Paul Elliottdc42ca82023-02-24 18:11:59 +0000331/**
332 * \brief For output buffers which contain "tags"
333 * (outputs that may be checked for validity like
334 * hashes, MACs and signatures), fill the unused
335 * part of the output buffer (the whole buffer on
336 * error, the trailing part on success) with
337 * something that isn't a valid tag (barring an
338 * attack on the tag and deliberately-crafted
339 * input), in case the caller doesn't check the
340 * return status properly.
341 *
342 * \param output_buffer Pointer to buffer to wipe. May not be NULL
343 * unless \p output_buffer_size is zero.
344 * \param status Status of function called to generate
345 * output_buffer originally
346 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
347 * could be NULL.
348 * \param output_buffer_length Length of data written to output_buffer, must be
349 * less than \p output_buffer_size
350 */
351static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000352 size_t output_buffer_size, size_t output_buffer_length)
353{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000354 size_t offset = 0;
355
356 if (output_buffer_size == 0) {
357 /* If output_buffer_size is 0 then we have nothing to do. We must not
358 call memset because output_buffer may be NULL in this case */
359 return;
360 }
361
362 if (status == PSA_SUCCESS) {
363 offset = output_buffer_length;
364 }
365
366 memset(output_buffer + offset, '!', output_buffer_size - offset);
367}
368
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200369
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200370
371
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100372/****************************************************************/
373/* Key management */
374/****************************************************************/
375
Joakim Anderssonbb576fe2023-03-01 11:23:02 +0100376#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
377 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cronf467d632021-11-19 18:02:34 +0100378 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
379 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
380 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100381mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
382 size_t bits,
383 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200384{
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100386 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100388#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100389 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100391#endif
392#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100393 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100395#endif
396#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100397 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100399#endif
400#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100401 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100403#endif
404#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100405 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100407 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if (bits_is_sloppy) {
409 return MBEDTLS_ECP_DP_SECP521R1;
410 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100411 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100412#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100413 }
414 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100415
Paul Elliott8ff510a2020-06-02 17:19:28 +0100416 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100418#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100419 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100421#endif
422#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100423 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100425#endif
426#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100427 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100429#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100430 }
431 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100432
Paul Elliott8ff510a2020-06-02 17:19:28 +0100433 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100435#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100436 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100438 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if (bits_is_sloppy) {
440 return MBEDTLS_ECP_DP_CURVE25519;
441 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100442 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100443#endif
444#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100445 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100447#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100448 }
449 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100450
Paul Elliott8ff510a2020-06-02 17:19:28 +0100451 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100453#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100454 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100456#endif
457#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100458 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100460#endif
461#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100462 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100464#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100465 }
466 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200467 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100468
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100469 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200471}
Joakim Anderssonbb576fe2023-03-01 11:23:02 +0100472#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
473 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Ronald Cronf467d632021-11-19 18:02:34 +0100474 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
475 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
476 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
479 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200480{
481 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200483 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200484 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200485 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200486 case PSA_KEY_TYPE_PASSWORD:
487 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200488 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100489#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 if (bits != 128 && bits != 192 && bits != 256) {
492 return PSA_ERROR_INVALID_ARGUMENT;
493 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200494 break;
495#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200496#if defined(PSA_WANT_KEY_TYPE_ARIA)
497 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (bits != 128 && bits != 192 && bits != 256) {
499 return PSA_ERROR_INVALID_ARGUMENT;
500 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200501 break;
502#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100503#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200504 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 if (bits != 128 && bits != 192 && bits != 256) {
506 return PSA_ERROR_INVALID_ARGUMENT;
507 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200508 break;
509#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100510#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200511 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 if (bits != 64 && bits != 128 && bits != 192) {
513 return PSA_ERROR_INVALID_ARGUMENT;
514 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200515 break;
516#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100517#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200518 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 if (bits != 256) {
520 return PSA_ERROR_INVALID_ARGUMENT;
521 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200522 break;
523#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200524 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200526 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 if (bits % 8 != 0) {
528 return PSA_ERROR_INVALID_ARGUMENT;
529 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200532}
533
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100534/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100535 *
Steven Cooremancd640932021-03-08 12:00:27 +0100536 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
537 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100538 *
539 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100540 * \param[in] key_type The key type of the key to be used with the
541 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100542 *
543 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100544 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100545 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100546 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100547 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100548MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100549 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100551{
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 if (PSA_ALG_IS_HMAC(algorithm)) {
553 if (key_type == PSA_KEY_TYPE_HMAC) {
554 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100555 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100556 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
559 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
560 * key. */
561 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
562 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
563 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
564 * the block length (larger than 1) for block ciphers. */
565 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
566 return PSA_SUCCESS;
567 }
568 }
569 }
570
571 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100572}
573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
575 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200576{
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 if (slot->key.data != NULL) {
578 return PSA_ERROR_ALREADY_EXISTS;
579 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 slot->key.data = mbedtls_calloc(1, buffer_length);
582 if (slot->key.data == NULL) {
583 return PSA_ERROR_INSUFFICIENT_MEMORY;
584 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200585
Ronald Cronea0f8a62020-11-25 17:52:23 +0100586 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200588}
589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
591 const uint8_t *data,
592 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200593{
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 psa_status_t status = psa_allocate_buffer_to_slot(slot,
595 data_length);
596 if (status != PSA_SUCCESS) {
597 return status;
598 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 memcpy(slot->key.data, data, data_length);
601 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200602}
603
Ronald Crona0fe59f2020-11-29 11:14:53 +0100604psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100605 const psa_key_attributes_t *attributes,
606 const uint8_t *data, size_t data_length,
607 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100609{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100610 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
611 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100612
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200613 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 if (data_length == 0) {
615 return PSA_ERROR_NOT_SUPPORTED;
616 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 if (key_type_is_raw_bytes(type)) {
619 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200620
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
622 *bits);
623 if (status != PSA_SUCCESS) {
624 return status;
625 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200626
Ronald Crondd04d422020-11-28 15:14:42 +0100627 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100629 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 return PSA_SUCCESS;
633 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800634#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
636 if (PSA_KEY_TYPE_IS_ECC(type)) {
637 return mbedtls_psa_ecp_import_key(attributes,
638 data, data_length,
639 key_buffer, key_buffer_size,
640 key_buffer_length,
641 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100642 }
John Durkop9814fa22020-11-04 12:28:15 -0800643#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
644 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700645#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
647 if (PSA_KEY_TYPE_IS_RSA(type)) {
648 return mbedtls_psa_rsa_import_key(attributes,
649 data, data_length,
650 key_buffer, key_buffer_size,
651 key_buffer_length,
652 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200653 }
John Durkop9814fa22020-11-04 12:28:15 -0800654#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
655 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100656 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100659}
660
Gilles Peskinef603c712019-01-19 13:40:11 +0100661/** Calculate the intersection of two algorithm usage policies.
662 *
663 * Return 0 (which allows no operation) on incompatibility.
664 */
665static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100666 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100667 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100669{
Gilles Peskine549ea862019-05-22 11:45:59 +0200670 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 if (alg1 == alg2) {
672 return alg1;
673 }
Steven Cooreman03488022021-02-18 12:07:20 +0100674 /* If the policies are from the same hash-and-sign family, check
675 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
677 PSA_ALG_IS_SIGN_HASH(alg2) &&
678 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
679 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
680 return alg2;
681 }
682 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
683 return alg1;
684 }
Steven Cooreman03488022021-02-18 12:07:20 +0100685 }
686 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100687 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100688 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
690 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
691 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
692 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
693 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100694 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100695
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100696 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
698 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
699 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
700 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100701 }
702 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
704 (alg1_len <= alg2_len)) {
705 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100706 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
708 (alg2_len <= alg1_len)) {
709 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100710 }
711 }
712 /* If the policies are from the same MAC family, check whether one
713 * of them is a minimum-MAC-length policy. Calculate the most
714 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
716 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
717 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100718 /* Validate the combination of key type and algorithm. Since the base
719 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
721 return 0;
722 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100723
Steven Cooreman31a876d2021-03-03 20:47:40 +0100724 /* Get the (exact or at-least) output lengths for both sides of the
725 * requested intersection. None of the currently supported algorithms
726 * have an output length dependent on the actual key size, so setting it
727 * to a bogus value of 0 is currently OK.
728 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100729 * Note that for at-least-this-length wildcard algorithms, the output
730 * length is set to the shortest allowed length, which allows us to
731 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
733 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100734 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100735
Steven Cooremana1d83222021-02-25 10:20:29 +0100736 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
738 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
739 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100740 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100741
742 /* If only one is an at-least-this-length policy, the intersection would
743 * be the other (fixed-length) policy as long as said fixed length is
744 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
746 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100747 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
749 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100750 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100751
Steven Cooremancd640932021-03-08 12:00:27 +0100752 /* If none of them are wildcards, check whether they define the same tag
753 * length. This is still possible here when one is default-length and
754 * the other specific-length. Ensure to always return the
755 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 if (alg1_len == alg2_len) {
757 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
758 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100759 }
760 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100762}
763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764static int psa_key_algorithm_permits(psa_key_type_t key_type,
765 psa_algorithm_t policy_alg,
766 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200767{
Gilles Peskine549ea862019-05-22 11:45:59 +0200768 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if (requested_alg == policy_alg) {
770 return 1;
771 }
Steven Cooreman03488022021-02-18 12:07:20 +0100772 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
773 * and requested_alg is the same hash-and-sign family with any hash,
774 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
776 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
777 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
778 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100779 }
780 /* If policy_alg is a wildcard AEAD algorithm of the same base as
781 * the requested algorithm, check the requested tag length to be
782 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 if (PSA_ALG_IS_AEAD(policy_alg) &&
784 PSA_ALG_IS_AEAD(requested_alg) &&
785 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
786 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
787 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
788 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
789 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100790 }
Steven Cooremancd640932021-03-08 12:00:27 +0100791 /* If policy_alg is a MAC algorithm of the same base as the requested
792 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if (PSA_ALG_IS_MAC(policy_alg) &&
794 PSA_ALG_IS_MAC(requested_alg) &&
795 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
796 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100797 /* Validate the combination of key type and algorithm. Since the policy
798 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
800 return 0;
801 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100802
Steven Cooreman31a876d2021-03-03 20:47:40 +0100803 /* Get both the requested output length for the algorithm which is to be
804 * verified, and the default output length for the base algorithm.
805 * Note that none of the currently supported algorithms have an output
806 * length dependent on actual key size, so setting it to a bogus value
807 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100808 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100810 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 key_type, 0,
812 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100813
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100814 /* If the policy is default-length, only allow an algorithm with
815 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
817 return requested_output_length == default_output_length;
818 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100819
820 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100821 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
823 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
824 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100825 }
826
Steven Cooremancd640932021-03-08 12:00:27 +0100827 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
828 * check for the requested MAC length to be equal to or longer than the
829 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
831 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
832 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100833 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200834 }
Steven Cooremance48e852020-10-05 16:02:45 +0200835 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200836 * a key derivation with that key agreement should also be allowed. This
837 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
839 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
840 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
841 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200842 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100843 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200845}
846
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100847/** Test whether a policy permits an algorithm.
848 *
849 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100850 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100851 * \note This function requires providing the key type for which the policy is
852 * being validated, since some algorithm policy definitions (e.g. MAC)
853 * have different properties depending on what kind of cipher it is
854 * combined with.
855 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100856 * \retval PSA_SUCCESS When \p alg is a specific algorithm
857 * allowed by the \p policy.
858 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
859 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
860 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100861 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
863 psa_key_type_t key_type,
864 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100865{
Steven Cooremand788fab2021-02-25 11:29:17 +0100866 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 if (alg == 0) {
868 return PSA_ERROR_INVALID_ARGUMENT;
869 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100870
Steven Cooreman7e39f052021-02-23 14:18:32 +0100871 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 if (PSA_ALG_IS_WILDCARD(alg)) {
873 return PSA_ERROR_INVALID_ARGUMENT;
874 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100875
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
877 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
878 return PSA_SUCCESS;
879 } else {
880 return PSA_ERROR_NOT_PERMITTED;
881 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100882}
883
Gilles Peskinef603c712019-01-19 13:40:11 +0100884/** Restrict a key policy based on a constraint.
885 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100886 * \note This function requires providing the key type for which the policy is
887 * being restricted, since some algorithm policy definitions (e.g. MAC)
888 * have different properties depending on what kind of cipher it is
889 * combined with.
890 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100891 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100892 * \param[in,out] policy The policy to restrict.
893 * \param[in] constraint The policy constraint to apply.
894 *
895 * \retval #PSA_SUCCESS
896 * \c *policy contains the intersection of the original value of
897 * \c *policy and \c *constraint.
898 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100899 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100900 * \c *policy is unchanged.
901 */
902static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100903 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100904 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100906{
907 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 psa_key_policy_algorithm_intersection(key_type, policy->alg,
909 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200910 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
912 constraint->alg2);
913 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
914 return PSA_ERROR_INVALID_ARGUMENT;
915 }
916 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
917 return PSA_ERROR_INVALID_ARGUMENT;
918 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100919 policy->usage &= constraint->usage;
920 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200921 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100923}
924
Przemek Stekiel061f6942022-11-30 10:51:35 +0100925/** Get the description of a key given its identifier and policy constraints
926 * and lock it.
927 *
928 * The key must have allow all the usage flags set in \p usage. If \p alg is
929 * nonzero, the key must allow operations with this algorithm. If \p alg is
930 * zero, the algorithm is not checked.
931 *
932 * In case of a persistent key, the function loads the description of the key
933 * into a key slot if not already done.
934 *
935 * On success, the returned key slot is locked. It is the responsibility of
936 * the caller to unlock the key slot when it does not access it anymore.
937 */
938static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100939 mbedtls_svc_key_id_t key,
940 psa_key_slot_t **p_slot,
941 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100943{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200944 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100945 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 status = psa_get_and_lock_key_slot(key, p_slot);
948 if (status != PSA_SUCCESS) {
949 return status;
950 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200951 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100952
953 /* Enforce that usage policy for the key slot contains all the flags
954 * required by the usage parameter. There is one exception: public
955 * keys can always be exported, so we treat public key objects as
956 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100958 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200960
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100962 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200963 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100964 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100965
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800966 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 if (alg != 0) {
968 status = psa_key_policy_permits(&slot->attr.policy,
969 slot->attr.type,
970 alg);
971 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100972 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100974 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100975
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200977
978error:
979 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200981
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100983}
Darryl Green940d72c2018-07-13 13:18:51 +0100984
Ronald Cron5c522922020-11-14 16:35:34 +0100985/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200986 *
987 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200988 * available, as opposed to a key in a secure element and/or to be used
989 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200990 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200991 * This is a temporary function that may be used instead of
992 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
993 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200994 *
Ronald Cron5c522922020-11-14 16:35:34 +0100995 * On success, the returned key slot is locked. It is the responsibility of the
996 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200997 */
Ronald Cron5c522922020-11-14 16:35:34 +0100998static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
999 mbedtls_svc_key_id_t key,
1000 psa_key_slot_t **p_slot,
1001 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001003{
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1005 usage, alg);
1006 if (status != PSA_SUCCESS) {
1007 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001008 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001009
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1011 psa_unlock_key_slot(*p_slot);
1012 *p_slot = NULL;
1013 return PSA_ERROR_NOT_SUPPORTED;
1014 }
1015
1016 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001017}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001018
Gilles Peskine449bd832023-01-11 14:50:10 +01001019psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001020{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001021 /* Data pointer will always be either a valid pointer or NULL in an
1022 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 if (slot->key.data != NULL) {
1024 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1025 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001028 slot->key.data = NULL;
1029 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001032}
1033
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001034/** Completely wipe a slot in memory, including its policy.
1035 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001036psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001037{
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001039
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 /*
1041 * As the return error code may not be handled in case of multiple errors,
1042 * do our best to report an unexpected lock counter. Assert with
1043 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1044 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1045 * function is called as part of the execution of a test suite, the
1046 * execution of the test suite is stopped in error if the assertion fails.
1047 */
1048 if (slot->lock_count != 1) {
1049 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001050 status = PSA_ERROR_CORRUPTION_DETECTED;
1051 }
1052
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001053 /* Multipart operations may still be using the key. This is safe
1054 * because all multipart operation objects are independent from
1055 * the key slot: if they need to access the key after the setup
1056 * phase, they have a copy of the key. Note that this means that
1057 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001058 /* At this point, key material and other type-specific content has
1059 * been wiped. Clear remaining metadata. We can call memset and not
1060 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 memset(slot, 0, sizeof(*slot));
1062 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001063}
1064
Gilles Peskine449bd832023-01-11 14:50:10 +01001065psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001066{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001067 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001068 psa_status_t status; /* status of the last operation */
1069 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001070#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1071 psa_se_drv_table_entry_t *driver;
1072#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 if (mbedtls_svc_key_id_is_null(key)) {
1075 return PSA_SUCCESS;
1076 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001077
Ronald Cronf2911112020-10-29 17:51:10 +01001078 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001079 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001080 * key, this will load the key description from persistent memory if not
1081 * done yet. We cannot avoid this loading as without it we don't know if
1082 * the key is operated by an SE or not and this information is needed by
1083 * the current implementation.
1084 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 status = psa_get_and_lock_key_slot(key, &slot);
1086 if (status != PSA_SUCCESS) {
1087 return status;
1088 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001089
Ronald Cronf2911112020-10-29 17:51:10 +01001090 /*
1091 * If the key slot containing the key description is under access by the
1092 * library (apart from the present access), the key cannot be destroyed
1093 * yet. For the time being, just return in error. Eventually (to be
1094 * implemented), the key should be destroyed when all accesses have
1095 * stopped.
1096 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 if (slot->lock_count > 1) {
1098 psa_unlock_key_slot(slot);
1099 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001100 }
1101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001103 /* Refuse the destruction of a read-only key (which may or may not work
1104 * if we attempt it, depending on whether the key is merely read-only
1105 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001106 * Just do the best we can, which is to wipe the copy in memory
1107 * (done in this function's cleanup code). */
1108 overall_status = PSA_ERROR_NOT_PERMITTED;
1109 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001110 }
1111
Gilles Peskine354f7672019-07-12 23:46:38 +02001112#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1114 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001115 /* For a key in a secure element, we need to do three things:
1116 * remove the key file in internal storage, destroy the
1117 * key inside the secure element, and update the driver's
1118 * persistent data. Start a transaction that will encompass these
1119 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001121 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001123 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 status = psa_crypto_save_transaction();
1125 if (status != PSA_SUCCESS) {
1126 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001127 /* We should still try to destroy the key in the secure
1128 * element and the key metadata in storage. This is especially
1129 * important if the error is that the storage is full.
1130 * But how to do it exactly without risking an inconsistent
1131 * state after a reset?
1132 * https://github.com/ARMmbed/mbed-crypto/issues/215
1133 */
1134 overall_status = status;
1135 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001136 }
1137
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 status = psa_destroy_se_key(driver,
1139 psa_key_slot_get_slot_number(slot));
1140 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001141 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001143 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001144#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1145
Darryl Greend49a4992018-06-18 17:27:26 +01001146#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1148 status = psa_destroy_persistent_key(slot->attr.id);
1149 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001150 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001152
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001153 /* TODO: other slots may have a copy of the same key. We should
1154 * invalidate them.
1155 * https://github.com/ARMmbed/mbed-crypto/issues/214
1156 */
Darryl Greend49a4992018-06-18 17:27:26 +01001157 }
1158#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001159
Gilles Peskinefc762652019-07-22 19:30:34 +02001160#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 if (driver != NULL) {
1162 status = psa_save_se_persistent_data(driver);
1163 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001164 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 }
1166 status = psa_crypto_stop_transaction();
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 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001170 }
1171#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1172
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001173exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001175 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001177 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 }
1179 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001180}
1181
John Durkop07cc04a2020-11-16 22:08:34 -08001182#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001183 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001184static psa_status_t psa_get_rsa_public_exponent(
1185 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001187{
1188 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001189 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001190 uint8_t *buffer = NULL;
1191 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001193
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1195 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001196 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 }
1198 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001199 /* It's the default value, which is reported as an empty string,
1200 * so there's nothing to do. */
1201 goto exit;
1202 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001203
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 buflen = mbedtls_mpi_size(&mpi);
1205 buffer = mbedtls_calloc(1, buflen);
1206 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001207 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1208 goto exit;
1209 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1211 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001212 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001214 attributes->domain_parameters = buffer;
1215 attributes->domain_parameters_size = buflen;
1216
1217exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 mbedtls_mpi_free(&mpi);
1219 if (ret != 0) {
1220 mbedtls_free(buffer);
1221 }
1222 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001223}
John Durkop07cc04a2020-11-16 22:08:34 -08001224#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001225 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001226
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001227/** Retrieve all the publicly-accessible attributes of a key.
1228 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001229psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1230 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001231{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001232 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001233 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001234 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001235
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001237
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1239 if (status != PSA_SUCCESS) {
1240 return status;
1241 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001242
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001243 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1245 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001246
1247#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1249 psa_set_key_slot_number(attributes,
1250 psa_key_slot_get_slot_number(slot));
1251 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001252#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001255#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001256 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001257 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001258 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001259 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001260 * is not yet implemented.
1261 * https://github.com/ARMmbed/mbed-crypto/issues/216
1262 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001264 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001265
Ronald Cron90857082020-11-25 14:58:33 +01001266 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 slot->attr.type,
1268 slot->key.data,
1269 slot->key.bytes,
1270 &rsa);
1271 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001272 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001274
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 status = psa_get_rsa_public_exponent(rsa,
1276 attributes);
1277 mbedtls_rsa_free(rsa);
1278 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001279 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001280 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001281#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001282 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001283 default:
1284 /* Nothing else to do. */
1285 break;
1286 }
1287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 if (status != PSA_SUCCESS) {
1289 psa_reset_key_attributes(attributes);
1290 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001295}
1296
Gilles Peskinec8000c02019-08-02 20:15:51 +02001297#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1298psa_status_t psa_get_key_slot_number(
1299 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001301{
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001303 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 return PSA_SUCCESS;
1305 } else {
1306 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001307 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001308}
1309#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1312 size_t key_buffer_size,
1313 uint8_t *data,
1314 size_t data_size,
1315 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001316{
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 if (key_buffer_size > data_size) {
1318 return PSA_ERROR_BUFFER_TOO_SMALL;
1319 }
1320 memcpy(data, key_buffer, key_buffer_size);
1321 memset(data + key_buffer_size, 0,
1322 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001323 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001325}
1326
Ronald Cron7285cda2020-11-26 14:46:37 +01001327psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001328 const psa_key_attributes_t *attributes,
1329 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001331{
Ronald Crond18b5f82020-11-25 16:46:05 +01001332 psa_key_type_t type = attributes->core.type;
1333
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 if (key_type_is_raw_bytes(type) ||
1335 PSA_KEY_TYPE_IS_RSA(type) ||
1336 PSA_KEY_TYPE_IS_ECC(type)) {
1337 return psa_export_key_buffer_internal(
1338 key_buffer, key_buffer_size,
1339 data, data_size, data_length);
1340 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001341 /* This shouldn't happen in the reference implementation, but
1342 it is valid for a special-purpose implementation to omit
1343 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001345 }
1346}
1347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1349 uint8_t *data,
1350 size_t data_size,
1351 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001352{
1353 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1354 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1355 psa_key_slot_t *slot;
1356
Ronald Crone907e552021-01-18 13:22:38 +01001357 /* Reject a zero-length output buffer now, since this can never be a
1358 * valid key representation. This way we know that data must be a valid
1359 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 if (data_size == 0) {
1361 return PSA_ERROR_BUFFER_TOO_SMALL;
1362 }
Ronald Crone907e552021-01-18 13:22:38 +01001363
Ronald Cron9486f9d2020-11-26 13:36:23 +01001364 /* Set the key to empty now, so that even when there are errors, we always
1365 * set data_length to a value between 0 and data_size. On error, setting
1366 * the key to empty is a good choice because an empty key representation is
1367 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001368 *data_length = 0;
1369
Ronald Cron9486f9d2020-11-26 13:36:23 +01001370 /* Export requires the EXPORT flag. There is an exception for public keys,
1371 * which don't require any flag, but
1372 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1373 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1375 PSA_KEY_USAGE_EXPORT, 0);
1376 if (status != PSA_SUCCESS) {
1377 return status;
1378 }
mohammad160306e79202018-03-28 13:17:44 +03001379
Ronald Crond18b5f82020-11-25 16:46:05 +01001380 psa_key_attributes_t attributes = {
1381 .core = slot->attr
1382 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 status = psa_driver_wrapper_export_key(&attributes,
1384 slot->key.data, slot->key.bytes,
1385 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001386
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001388
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001390}
1391
Ronald Cron7285cda2020-11-26 14:46:37 +01001392psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001393 const psa_key_attributes_t *attributes,
1394 const uint8_t *key_buffer,
1395 size_t key_buffer_size,
1396 uint8_t *data,
1397 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001399{
Ronald Crond18b5f82020-11-25 16:46:05 +01001400 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001401
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1403 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001404 /* Exporting public -> public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 return psa_export_key_buffer_internal(
1406 key_buffer, key_buffer_size,
1407 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001408 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001409
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001411#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1413 return mbedtls_psa_rsa_export_public_key(attributes,
1414 key_buffer,
1415 key_buffer_size,
1416 data,
1417 data_size,
1418 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001419#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001420 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001422#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1423 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001425#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1427 return mbedtls_psa_ecp_export_public_key(attributes,
1428 key_buffer,
1429 key_buffer_size,
1430 data,
1431 data_size,
1432 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001433#else
1434 /* We don't know how to convert a private ECC key to public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001436#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1437 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001438 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001440 /* This shouldn't happen in the reference implementation, but
1441 it is valid for a special-purpose implementation to omit
1442 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001444 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001445}
Ronald Crond18b5f82020-11-25 16:46:05 +01001446
Gilles Peskine449bd832023-01-11 14:50:10 +01001447psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1448 uint8_t *data,
1449 size_t data_size,
1450 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001451{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001452 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001453 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001454 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001455
Ronald Crone907e552021-01-18 13:22:38 +01001456 /* Reject a zero-length output buffer now, since this can never be a
1457 * valid key representation. This way we know that data must be a valid
1458 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 if (data_size == 0) {
1460 return PSA_ERROR_BUFFER_TOO_SMALL;
1461 }
Ronald Crone907e552021-01-18 13:22:38 +01001462
Darryl Greendd8fb772018-11-07 16:00:44 +00001463 /* Set the key to empty now, so that even when there are errors, we always
1464 * set data_length to a value between 0 and data_size. On error, setting
1465 * the key to empty is a good choice because an empty key representation is
1466 * unlikely to be accepted anywhere. */
1467 *data_length = 0;
1468
1469 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1471 if (status != PSA_SUCCESS) {
1472 return status;
1473 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1476 status = PSA_ERROR_INVALID_ARGUMENT;
1477 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001478 }
1479
Ronald Crond18b5f82020-11-25 16:46:05 +01001480 psa_key_attributes_t attributes = {
1481 .core = slot->attr
1482 };
Ronald Cron67227982020-11-26 15:16:05 +01001483 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001484 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001486
1487exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001491}
1492
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001493MBEDTLS_STATIC_ASSERT(
1494 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1495 "One or more key attribute flag is listed as both external-only and dual-use")
1496MBEDTLS_STATIC_ASSERT(
1497 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1498 "One or more key attribute flag is listed as both internal-only and dual-use")
1499MBEDTLS_STATIC_ASSERT(
1500 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1501 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001502
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001503/** Validate that a key policy is internally well-formed.
1504 *
1505 * This function only rejects invalid policies. It does not validate the
1506 * consistency of the policy with respect to other attributes of the key
1507 * such as the key type.
1508 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001509static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001510{
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1512 PSA_KEY_USAGE_COPY |
1513 PSA_KEY_USAGE_ENCRYPT |
1514 PSA_KEY_USAGE_DECRYPT |
1515 PSA_KEY_USAGE_SIGN_MESSAGE |
1516 PSA_KEY_USAGE_VERIFY_MESSAGE |
1517 PSA_KEY_USAGE_SIGN_HASH |
1518 PSA_KEY_USAGE_VERIFY_HASH |
1519 PSA_KEY_USAGE_VERIFY_DERIVATION |
1520 PSA_KEY_USAGE_DERIVE)) != 0) {
1521 return PSA_ERROR_INVALID_ARGUMENT;
1522 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001525}
1526
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001527/** Validate the internal consistency of key attributes.
1528 *
1529 * This function only rejects invalid attribute values. If does not
1530 * validate the consistency of the attributes with any key data that may
1531 * be involved in the creation of the key.
1532 *
1533 * Call this function early in the key creation process.
1534 *
1535 * \param[in] attributes Key attributes for the new key.
1536 * \param[out] p_drv On any return, the driver for the key, if any.
1537 * NULL for a transparent key.
1538 *
1539 */
1540static psa_status_t psa_validate_key_attributes(
1541 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001543{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001544 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1546 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 status = psa_validate_key_location(lifetime, p_drv);
1549 if (status != PSA_SUCCESS) {
1550 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001551 }
1552
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 status = psa_validate_key_persistence(lifetime);
1554 if (status != PSA_SUCCESS) {
1555 return status;
1556 }
1557
1558 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1559 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1560 return PSA_ERROR_INVALID_ARGUMENT;
1561 }
1562 } else {
1563 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1564 return PSA_ERROR_INVALID_ARGUMENT;
1565 }
1566 }
1567
1568 status = psa_validate_key_policy(&attributes->core.policy);
1569 if (status != PSA_SUCCESS) {
1570 return status;
1571 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001572
1573 /* Refuse to create overly large keys.
1574 * Note that this doesn't trigger on import if the attributes don't
1575 * explicitly specify a size (so psa_get_key_bits returns 0), so
1576 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1578 return PSA_ERROR_NOT_SUPPORTED;
1579 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001580
Gilles Peskine91e8c332019-08-02 19:19:39 +02001581 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1583 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1584 return PSA_ERROR_INVALID_ARGUMENT;
1585 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001586
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001588}
1589
Gilles Peskine4747d192019-04-17 15:05:45 +02001590/** Prepare a key slot to receive key material.
1591 *
1592 * This function allocates a key slot and sets its metadata.
1593 *
1594 * If this function fails, call psa_fail_key_creation().
1595 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001596 * This function is intended to be used as follows:
1597 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001598 * it with the specified attributes, and in case of a volatile key assign it
1599 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001600 * -# Populate the slot with the key material.
1601 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1602 * In case of failure at any step, stop the sequence and call
1603 * psa_fail_key_creation().
1604 *
Ronald Cron5c522922020-11-14 16:35:34 +01001605 * On success, the key slot is locked. It is the responsibility of the caller
1606 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001607 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001608 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001609 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001610 * \param[out] p_slot On success, a pointer to the prepared slot.
1611 * \param[out] p_drv On any return, the driver for the key, if any.
1612 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001613 *
1614 * \retval #PSA_SUCCESS
1615 * The key slot is ready to receive key material.
1616 * \return If this function fails, the key slot is an invalid state.
1617 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001618 */
1619static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001620 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001621 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001622 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001624{
1625 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001626 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001627 psa_key_slot_t *slot;
1628
Gilles Peskinedf179142019-07-15 22:02:14 +02001629 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001630 *p_drv = NULL;
1631
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 status = psa_validate_key_attributes(attributes, p_drv);
1633 if (status != PSA_SUCCESS) {
1634 return status;
1635 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001636
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1638 if (status != PSA_SUCCESS) {
1639 return status;
1640 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001641 slot = *p_slot;
1642
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001643 /* We're storing the declared bit-size of the key. It's up to each
1644 * creation mechanism to verify that this information is correct.
1645 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001646 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001647 * is optional (import, copy). In case of a volatile key, assign it the
1648 * volatile key identifier associated to the slot returned to contain its
1649 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001650
1651 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001653#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1654 slot->attr.id = volatile_key_id;
1655#else
1656 slot->attr.id.key_id = volatile_key_id;
1657#endif
1658 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001659
Gilles Peskine91e8c332019-08-02 19:19:39 +02001660 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001661 * external-only flags, query `attributes`. Thanks to the check
1662 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001663 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001664 * may have set. */
1665 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001666
Gilles Peskinecbaff462019-07-12 23:46:04 +02001667#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001668 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001669 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001670 * create the key file in internal storage, create the
1671 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001672 * persistent data. This is done by starting a transaction that will
1673 * encompass these three actions.
1674 * For registering a volatile key, we just need to find an appropriate
1675 * slot number inside the SE. Since the key is designated volatile, creating
1676 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001677 /* The first thing to do is to find a slot number for the new key.
1678 * We save the slot number in persistent storage as part of the
1679 * transaction data. It will be needed to recover if the power
1680 * fails during the key creation process, to clean up on the secure
1681 * element side after restarting. Obtaining a slot number from the
1682 * secure element driver updates its persistent state, but we do not yet
1683 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001684 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001686 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1688 &slot_number);
1689 if (status != PSA_SUCCESS) {
1690 return status;
1691 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001692
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1694 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001695 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001696 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001697 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 status = psa_crypto_save_transaction();
1699 if (status != PSA_SUCCESS) {
1700 (void) psa_crypto_stop_transaction();
1701 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001702 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001703 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001704
1705 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001707 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001708
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001710 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001712 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001713#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1714
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001716}
Gilles Peskine4747d192019-04-17 15:05:45 +02001717
1718/** Finalize the creation of a key once its key material has been set.
1719 *
1720 * This entails writing the key to persistent storage.
1721 *
1722 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001723 * See the documentation of psa_start_key_creation() for the intended use
1724 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001725 *
Ronald Cron5c522922020-11-14 16:35:34 +01001726 * If the finalization succeeds, the function unlocks the key slot (it was
1727 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1728 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001729 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001730 * \param[in,out] slot Pointer to the slot with key material.
1731 * \param[in] driver The secure element driver for the key,
1732 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001733 * \param[out] key On success, identifier of the key. Note that the
1734 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001735 *
1736 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001737 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001738 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1739 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1740 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1741 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1742 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1743 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001744 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001745 * \return If this function fails, the key slot is an invalid state.
1746 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001747 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001748static psa_status_t psa_finish_key_creation(
1749 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001750 psa_se_drv_table_entry_t *driver,
1751 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001752{
1753 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001754 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001755 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001756
1757#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001759#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001761 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001762 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001764
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001765 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1766 sizeof(data.slot_number),
1767 "Slot number size does not match psa_se_key_data_storage_t");
1768
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1770 status = psa_save_persistent_key(&slot->attr,
1771 (uint8_t *) &data,
1772 sizeof(data));
1773 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001774#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1775 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001776 /* Key material is saved in export representation in the slot, so
1777 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 status = psa_save_persistent_key(&slot->attr,
1779 slot->key.data,
1780 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001781 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001782 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001783#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1784
Gilles Peskinecbaff462019-07-12 23:46:04 +02001785#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001786 /* Finish the transaction for a key creation. This does not
1787 * happen when registering an existing key. Detect this case
1788 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001789 * creation of a persistent key in a secure element requires a transaction,
1790 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 if (driver != NULL &&
1792 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1793 status = psa_save_se_persistent_data(driver);
1794 if (status != PSA_SUCCESS) {
1795 psa_destroy_persistent_key(slot->attr.id);
1796 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001797 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001799 }
1800#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001803 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 status = psa_unlock_key_slot(slot);
1805 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001806 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001808 }
Ronald Cron50972942020-11-14 11:28:25 +01001809
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001811}
1812
1813/** Abort the creation of a key.
1814 *
1815 * You may call this function after calling psa_start_key_creation(),
1816 * or after psa_finish_key_creation() fails. In other circumstances, this
1817 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001818 * See the documentation of psa_start_key_creation() for the intended use
1819 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001820 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001821 * \param[in,out] slot Pointer to the slot with key material.
1822 * \param[in] driver The secure element driver for the key,
1823 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001824 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001825static void psa_fail_key_creation(psa_key_slot_t *slot,
1826 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001827{
Gilles Peskine011e4282019-06-26 18:34:38 +02001828 (void) driver;
1829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001831 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001833
1834#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001835 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001836 * element, and the failure happened later (when saving metadata
1837 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001838 * element.
1839 * https://github.com/ARMmbed/mbed-crypto/issues/217
1840 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001841
Gilles Peskined7729582019-08-05 15:55:54 +02001842 /* Abort the ongoing transaction if any (there may not be one if
1843 * the creation process failed before starting one, or if the
1844 * key creation is a registration of a key in a secure element).
1845 * Earlier functions must already have done what it takes to undo any
1846 * partial creation. All that's left is to update the transaction data
1847 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001849#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1850
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001852}
1853
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001854/** Validate optional attributes during key creation.
1855 *
1856 * Some key attributes are optional during key creation. If they are
1857 * specified in the attributes structure, check that they are consistent
1858 * with the data in the slot.
1859 *
1860 * This function should be called near the end of key creation, after
1861 * the slot in memory is fully populated but before saving persistent data.
1862 */
1863static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001864 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001866{
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 if (attributes->core.type != 0) {
1868 if (attributes->core.type != slot->attr.type) {
1869 return PSA_ERROR_INVALID_ARGUMENT;
1870 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001871 }
1872
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001874#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1876 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001877 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001878 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001879 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001880
Ronald Cron90857082020-11-25 14:58:33 +01001881 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 slot->attr.type,
1883 slot->key.data,
1884 slot->key.bytes,
1885 &rsa);
1886 if (status != PSA_SUCCESS) {
1887 return status;
1888 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001889
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 mbedtls_mpi_init(&actual);
1891 mbedtls_mpi_init(&required);
1892 ret = mbedtls_rsa_export(rsa,
1893 NULL, NULL, NULL, NULL, &actual);
1894 mbedtls_rsa_free(rsa);
1895 mbedtls_free(rsa);
1896 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001897 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 }
1899 ret = mbedtls_mpi_read_binary(&required,
1900 attributes->domain_parameters,
1901 attributes->domain_parameters_size);
1902 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001903 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 }
1905 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001906 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 }
1908rsa_exit:
1909 mbedtls_mpi_free(&actual);
1910 mbedtls_mpi_free(&required);
1911 if (ret != 0) {
1912 return mbedtls_to_psa_error(ret);
1913 }
1914 } else
John Durkop9814fa22020-11-04 12:28:15 -08001915#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1916 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001917 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001919 }
1920 }
1921
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 if (attributes->core.bits != 0) {
1923 if (attributes->core.bits != slot->attr.bits) {
1924 return PSA_ERROR_INVALID_ARGUMENT;
1925 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001926 }
1927
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001929}
1930
Gilles Peskine449bd832023-01-11 14:50:10 +01001931psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1932 const uint8_t *data,
1933 size_t data_length,
1934 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001935{
1936 psa_status_t status;
1937 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001938 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001939 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301940 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001941
Ronald Cron81709fc2020-11-14 12:10:32 +01001942 *key = MBEDTLS_SVC_KEY_ID_INIT;
1943
Gilles Peskine0f84d622019-09-12 19:03:13 +02001944 /* Reject zero-length symmetric keys (including raw data key objects).
1945 * This also rejects any key which might be encoded as an empty string,
1946 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 if (data_length == 0) {
1948 return PSA_ERROR_INVALID_ARGUMENT;
1949 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001950
Archana449608b2021-09-08 15:36:05 +05301951 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 if (data_length > SIZE_MAX / 8) {
1953 return PSA_ERROR_NOT_SUPPORTED;
1954 }
Archana6ed4bda2021-08-04 10:47:15 +05301955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1957 &slot, &driver);
1958 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001959 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001961
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001962 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301963 * storage ( thus not in the case of importing a key in a secure element
1964 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1965 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 if (slot->key.data == NULL) {
1967 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301968 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 attributes, data, data_length, &storage_size);
1970 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301971 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 }
Archanad8a83dc2021-06-14 10:04:16 +05301973 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 status = psa_allocate_buffer_to_slot(slot, storage_size);
1975 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001976 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001978 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001979
1980 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 status = psa_driver_wrapper_import_key(attributes,
1982 data, data_length,
1983 slot->key.data,
1984 slot->key.bytes,
1985 &slot->key.bytes, &bits);
1986 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001987 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001989
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001991 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001993 status = PSA_ERROR_INVALID_ARGUMENT;
1994 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001995 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001996
Archana6ed4bda2021-08-04 10:47:15 +05301997 /* Enforce a size limit, and in particular ensure that the bit
1998 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302000 status = PSA_ERROR_NOT_SUPPORTED;
2001 goto exit;
2002 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 status = psa_validate_optional_attributes(slot, attributes);
2004 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002005 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002007
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002009exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 if (status != PSA_SUCCESS) {
2011 psa_fail_key_creation(slot, driver);
2012 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002013
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002015}
2016
Gilles Peskined7729582019-08-05 15:55:54 +02002017#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2018psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002020{
2021 psa_status_t status;
2022 psa_key_slot_t *slot = NULL;
2023 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002024 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002025
2026 /* Leaving attributes unspecified is not currently supported.
2027 * It could make sense to query the key type and size from the
2028 * secure element, but not all secure elements support this
2029 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2031 return PSA_ERROR_NOT_SUPPORTED;
2032 }
2033 if (psa_get_key_bits(attributes) == 0) {
2034 return PSA_ERROR_NOT_SUPPORTED;
2035 }
Gilles Peskined7729582019-08-05 15:55:54 +02002036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2038 &slot, &driver);
2039 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002040 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 }
Gilles Peskined7729582019-08-05 15:55:54 +02002042
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002044
2045exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 if (status != PSA_SUCCESS) {
2047 psa_fail_key_creation(slot, driver);
2048 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002049
Gilles Peskined7729582019-08-05 15:55:54 +02002050 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 psa_close_key(key);
2052 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002053}
2054#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2055
Gilles Peskine449bd832023-01-11 14:50:10 +01002056psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2057 const psa_key_attributes_t *specified_attributes,
2058 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002059{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002060 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002061 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002062 psa_key_slot_t *source_slot = NULL;
2063 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002064 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002065 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302066 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002067
Ronald Cron81709fc2020-11-14 12:10:32 +01002068 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2069
Archana8a180362021-07-05 02:18:48 +05302070 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2072 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002073 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002075
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 status = psa_validate_optional_attributes(source_slot,
2077 specified_attributes);
2078 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002079 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002081
Archana9d17bf42021-09-10 06:22:44 +05302082 /* The target key type and number of bits have been validated by
2083 * psa_validate_optional_attributes() to be either equal to zero or
2084 * equal to the ones of the source key. So it is safe to inherit
2085 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302086 * */
Archana9d17bf42021-09-10 06:22:44 +05302087 actual_attributes.core.bits = source_slot->attr.bits;
2088 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302089
2090
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 status = psa_restrict_key_policy(source_slot->attr.type,
2092 &actual_attributes.core.policy,
2093 &source_slot->attr.policy);
2094 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002095 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002097
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2099 &target_slot, &driver);
2100 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002101 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 }
2103 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2104 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302105 /*
Archana9d17bf42021-09-10 06:22:44 +05302106 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302107 * the source key would need to be exported as plaintext and re-imported
2108 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302109 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302110 * appropriate API invocations from the application, if needed.
2111 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002112 status = PSA_ERROR_NOT_SUPPORTED;
2113 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002114 }
Archana8a180362021-07-05 02:18:48 +05302115 /*
2116 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302117 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302118 * - For opaque keys this translates to an invocation of the drivers'
2119 * copy_key entry point through the dispatch layer.
2120 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2122 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2123 &storage_size);
2124 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302125 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 }
Archana374fe5b2021-09-08 15:50:28 +05302127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2129 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302130 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 }
Archana374fe5b2021-09-08 15:50:28 +05302132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 status = psa_driver_wrapper_copy_key(&actual_attributes,
2134 source_slot->key.data,
2135 source_slot->key.bytes,
2136 target_slot->key.data,
2137 target_slot->key.bytes,
2138 &target_slot->key.bytes);
2139 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302140 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 }
2142 } else {
2143 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302144 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 source_slot->key.bytes);
2146 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302147 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 }
Archana8a180362021-07-05 02:18:48 +05302149 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002151exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 if (status != PSA_SUCCESS) {
2153 psa_fail_key_creation(target_slot, driver);
2154 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002155
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002157
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002159}
2160
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002161
2162
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002163/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002164/* Message digests */
2165/****************************************************************/
2166
Gilles Peskine449bd832023-01-11 14:50:10 +01002167psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002168{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002169 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 if (operation->id == 0) {
2171 return PSA_SUCCESS;
2172 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002173
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002175 operation->id = 0;
2176
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002178}
2179
Gilles Peskine449bd832023-01-11 14:50:10 +01002180psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2181 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002182{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002183 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002184
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002185 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002187 status = PSA_ERROR_BAD_STATE;
2188 goto exit;
2189 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002192 status = PSA_ERROR_INVALID_ARGUMENT;
2193 goto exit;
2194 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002195
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002196 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2197 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002199
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002201
2202exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 if (status != PSA_SUCCESS) {
2204 psa_hash_abort(operation);
2205 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002206
2207 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002208}
2209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2211 const uint8_t *input,
2212 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002213{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002214 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002217 status = PSA_ERROR_BAD_STATE;
2218 goto exit;
2219 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002220
Steven Cooremanc8288352021-03-04 14:02:19 +01002221 /* Don't require hash implementations to behave correctly on a
2222 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 if (input_length == 0) {
2224 return PSA_SUCCESS;
2225 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002226
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002228
2229exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 if (status != PSA_SUCCESS) {
2231 psa_hash_abort(operation);
2232 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002235}
2236
Gilles Peskine449bd832023-01-11 14:50:10 +01002237psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2238 uint8_t *hash,
2239 size_t hash_size,
2240 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002241{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002242 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 if (operation->id == 0) {
2244 return PSA_ERROR_BAD_STATE;
2245 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002246
Steven Cooreman1e582352021-02-18 17:24:37 +01002247 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 operation, hash, hash_size, hash_length);
2249 psa_hash_abort(operation);
2250 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002251}
2252
Gilles Peskine449bd832023-01-11 14:50:10 +01002253psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2254 const uint8_t *hash,
2255 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002256{
Ronald Cron69a63422021-10-18 09:47:58 +02002257 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002258 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002259 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 operation,
2261 actual_hash, sizeof(actual_hash),
2262 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002263
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002265 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002267
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002269 status = PSA_ERROR_INVALID_SIGNATURE;
2270 goto exit;
2271 }
2272
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002274 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002276
2277exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2279 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002280 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002284}
2285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286psa_status_t psa_hash_compute(psa_algorithm_t alg,
2287 const uint8_t *input, size_t input_length,
2288 uint8_t *hash, size_t hash_size,
2289 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002290{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002291 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 if (!PSA_ALG_IS_HASH(alg)) {
2293 return PSA_ERROR_INVALID_ARGUMENT;
2294 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002295
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2297 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002298}
2299
Gilles Peskine449bd832023-01-11 14:50:10 +01002300psa_status_t psa_hash_compare(psa_algorithm_t alg,
2301 const uint8_t *input, size_t input_length,
2302 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002303{
Ronald Cron69a63422021-10-18 09:47:58 +02002304 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002305 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 if (!PSA_ALG_IS_HASH(alg)) {
2308 return PSA_ERROR_INVALID_ARGUMENT;
2309 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002310
Steven Cooreman1e582352021-02-18 17:24:37 +01002311 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 alg, input, input_length,
2313 actual_hash, sizeof(actual_hash),
2314 &actual_hash_length);
2315 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002316 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 }
2318 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002319 status = PSA_ERROR_INVALID_SIGNATURE;
2320 goto exit;
2321 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002323 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002325
2326exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2328 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002329}
2330
Gilles Peskine449bd832023-01-11 14:50:10 +01002331psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2332 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002333{
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 if (source_operation->id == 0 ||
2335 target_operation->id != 0) {
2336 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002337 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2340 target_operation);
2341 if (status != PSA_SUCCESS) {
2342 psa_hash_abort(target_operation);
2343 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002344
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002346}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002347
2348
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002349/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002350/* MAC */
2351/****************************************************************/
2352
Gilles Peskine449bd832023-01-11 14:50:10 +01002353psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002354{
Steven Cooremane6804192021-03-19 18:28:56 +01002355 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 if (operation->id == 0) {
2357 return PSA_SUCCESS;
2358 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002359
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002361 operation->mac_size = 0;
2362 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002363 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002364
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002366}
2367
Ronald Cron2dff3b22021-06-17 16:33:22 +02002368static psa_status_t psa_mac_finalize_alg_and_key_validation(
2369 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002370 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002372{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002373 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 psa_key_type_t key_type = psa_get_key_type(attributes);
2375 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002376
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 if (!PSA_ALG_IS_MAC(alg)) {
2378 return PSA_ERROR_INVALID_ARGUMENT;
2379 }
Steven Cooremane6804192021-03-19 18:28:56 +01002380
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002381 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 status = psa_mac_key_can_do(alg, key_type);
2383 if (status != PSA_SUCCESS) {
2384 return status;
2385 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002386
Steven Cooremand1a68f12021-05-10 11:13:41 +02002387 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002391 /* A very short MAC is too short for security since it can be
2392 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2393 * so we make this our minimum, even though 32 bits is still
2394 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002396 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2399 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002400 /* It's impossible to "truncate" to a larger length than the full length
2401 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002403 }
2404
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002406 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2407 * that is disabled in the compile-time configuration. The result can
2408 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2409 * configuration into account. In this case, force a return of
2410 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2411 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2412 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2413 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2414 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002416 }
2417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002419}
2420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2422 mbedtls_svc_key_id_t key,
2423 psa_algorithm_t alg,
2424 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002425{
2426 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2427 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002428 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002429
2430 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002432 status = PSA_ERROR_BAD_STATE;
2433 goto exit;
2434 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002435
2436 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 key,
2438 &slot,
2439 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2440 alg);
2441 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002442 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002444
2445 psa_key_attributes_t attributes = {
2446 .core = slot->attr
2447 };
2448
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2450 &operation->mac_size);
2451 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002452 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002454
2455 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002456 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 if (is_sign) {
2458 status = psa_driver_wrapper_mac_sign_setup(operation,
2459 &attributes,
2460 slot->key.data,
2461 slot->key.bytes,
2462 alg);
2463 } else {
2464 status = psa_driver_wrapper_mac_verify_setup(operation,
2465 &attributes,
2466 slot->key.data,
2467 slot->key.bytes,
2468 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002469 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002470
Gilles Peskinefbfac682018-07-08 20:51:54 +02002471exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 if (status != PSA_SUCCESS) {
2473 psa_mac_abort(operation);
2474 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002475
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002479}
2480
Gilles Peskine449bd832023-01-11 14:50:10 +01002481psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2482 mbedtls_svc_key_id_t key,
2483 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002484{
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002486}
2487
Gilles Peskine449bd832023-01-11 14:50:10 +01002488psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2489 mbedtls_svc_key_id_t key,
2490 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002491{
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002493}
2494
Gilles Peskine449bd832023-01-11 14:50:10 +01002495psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2496 const uint8_t *input,
2497 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002498{
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 if (operation->id == 0) {
2500 return PSA_ERROR_BAD_STATE;
2501 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002502
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002503 /* Don't require hash implementations to behave correctly on a
2504 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 if (input_length == 0) {
2506 return PSA_SUCCESS;
2507 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002508
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2510 input, input_length);
2511 if (status != PSA_SUCCESS) {
2512 psa_mac_abort(operation);
2513 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002516}
2517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2519 uint8_t *mac,
2520 size_t mac_size,
2521 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002522{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002523 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2524 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002525
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002527 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002528 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002529 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002530
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002532 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002533 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002534 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002535
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002536 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2537 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002539 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002540 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002541 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002542
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002544 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002545 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002546 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002547
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 status = psa_driver_wrapper_mac_sign_finish(operation,
2549 mac, operation->mac_size,
2550 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002551
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002552exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002553 /* In case of success, set the potential excess room in the output buffer
2554 * to an invalid value, to avoid potentially leaking a longer MAC.
2555 * In case of error, set the output length and content to a safe default,
2556 * such that in case the caller misses an error check, the output would be
2557 * an unachievable MAC.
2558 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002560 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002561 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002562 }
mohammad16036df908f2018-04-02 08:34:15 -07002563
Paul Elliottdc42ca82023-02-24 18:11:59 +00002564 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002565
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002569}
2570
Gilles Peskine449bd832023-01-11 14:50:10 +01002571psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2572 const uint8_t *mac,
2573 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002574{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002575 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2576 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002577
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002579 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002580 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002581 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002582
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002584 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002585 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002586 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002587
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002589 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002590 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002591 }
2592
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 status = psa_driver_wrapper_mac_verify_finish(operation,
2594 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002595
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002596exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002598
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002600}
2601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2603 psa_algorithm_t alg,
2604 const uint8_t *input,
2605 size_t input_length,
2606 uint8_t *mac,
2607 size_t mac_size,
2608 size_t *mac_length,
2609 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002610{
2611 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002612 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2613 psa_key_slot_t *slot;
2614 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002615
Ronald Cron51131b52021-06-17 17:17:20 +02002616 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 key,
2618 &slot,
2619 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2620 alg);
2621 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002622 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002624
Ronald Cron51131b52021-06-17 17:17:20 +02002625 psa_key_attributes_t attributes = {
2626 .core = slot->attr
2627 };
2628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2630 &operation_mac_size);
2631 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002632 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002634
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002636 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002637 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002638 }
2639
2640 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 &attributes,
2642 slot->key.data, slot->key.bytes,
2643 alg,
2644 input, input_length,
2645 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002646
2647exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002648 /* In case of success, set the potential excess room in the output buffer
2649 * to an invalid value, to avoid potentially leaking a longer MAC.
2650 * In case of error, set the output length and content to a safe default,
2651 * such that in case the caller misses an error check, the output would be
2652 * an unachievable MAC.
2653 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002655 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002656 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002657 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002658
2659 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002660
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002662
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002664}
2665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002667 psa_algorithm_t alg,
2668 const uint8_t *input,
2669 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 uint8_t *mac,
2671 size_t mac_size,
2672 size_t *mac_length)
2673{
2674 return psa_mac_compute_internal(key, alg,
2675 input, input_length,
2676 mac, mac_size, mac_length, 1);
2677}
2678
2679psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2680 psa_algorithm_t alg,
2681 const uint8_t *input,
2682 size_t input_length,
2683 const uint8_t *mac,
2684 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002685{
2686 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002687 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2688 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002689
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 status = psa_mac_compute_internal(key, alg,
2691 input, input_length,
2692 actual_mac, sizeof(actual_mac),
2693 &actual_mac_length, 0);
2694 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002695 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002699 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002700 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002701 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
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 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002706
2707exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002709
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002711}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002712
Gilles Peskinee59236f2018-01-27 23:32:46 +01002713/****************************************************************/
2714/* Asymmetric cryptography */
2715/****************************************************************/
2716
Gilles Peskine449bd832023-01-11 14:50:10 +01002717static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2718 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002719{
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 if (input_is_message) {
2721 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2722 return PSA_ERROR_INVALID_ARGUMENT;
2723 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002724
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2726 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2727 return PSA_ERROR_INVALID_ARGUMENT;
2728 }
2729 }
2730 } else {
2731 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2732 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002733 }
2734 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002735
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002737}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002738
Gilles Peskine449bd832023-01-11 14:50:10 +01002739static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2740 int input_is_message,
2741 psa_algorithm_t alg,
2742 const uint8_t *input,
2743 size_t input_length,
2744 uint8_t *signature,
2745 size_t signature_size,
2746 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002747{
2748 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2749 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2750 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002751
2752 *signature_length = 0;
2753
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 status = psa_sign_verify_check_alg(input_is_message, alg);
2755 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002756 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002758
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002759 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002760 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002761 * buffer can in principle be empty since it doesn't actually have
2762 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 if (signature_size == 0) {
2764 return PSA_ERROR_BUFFER_TOO_SMALL;
2765 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002766
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002767 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 key, &slot,
2769 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2770 PSA_KEY_USAGE_SIGN_HASH,
2771 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002772
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002774 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002776
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002778 status = PSA_ERROR_INVALID_ARGUMENT;
2779 goto exit;
2780 }
2781
2782 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002784 };
2785
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002787 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002788 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002789 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 signature, signature_size, signature_length);
2791 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002792
2793 status = psa_driver_wrapper_sign_hash(
2794 &attributes, slot->key.data, slot->key.bytes,
2795 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002797 }
2798
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002799
2800exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002801 psa_wipe_tag_output_buffer(signature, status, signature_size,
2802 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002803
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002805
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002807}
2808
Gilles Peskine449bd832023-01-11 14:50:10 +01002809static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2810 int input_is_message,
2811 psa_algorithm_t alg,
2812 const uint8_t *input,
2813 size_t input_length,
2814 const uint8_t *signature,
2815 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002816{
2817 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2818 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2819 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002820
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 status = psa_sign_verify_check_alg(input_is_message, alg);
2822 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002823 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002825
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002826 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 key, &slot,
2828 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2829 PSA_KEY_USAGE_VERIFY_HASH,
2830 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002831
Gilles Peskine449bd832023-01-11 14:50:10 +01002832 if (status != PSA_SUCCESS) {
2833 return status;
2834 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002835
2836 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002838 };
2839
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002841 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002842 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002843 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 signature, signature_length);
2845 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002846 status = psa_driver_wrapper_verify_hash(
2847 &attributes, slot->key.data, slot->key.bytes,
2848 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002850 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002851
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002853
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002855
2856}
2857
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002858psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002859 const psa_key_attributes_t *attributes,
2860 const uint8_t *key_buffer,
2861 size_t key_buffer_size,
2862 psa_algorithm_t alg,
2863 const uint8_t *input,
2864 size_t input_length,
2865 uint8_t *signature,
2866 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002868{
2869 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002870
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002872 size_t hash_length;
2873 uint8_t hash[PSA_HASH_MAX_SIZE];
2874
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002875 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 PSA_ALG_SIGN_GET_HASH(alg),
2877 input, input_length,
2878 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002879
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002881 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002883
2884 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 attributes, key_buffer, key_buffer_size,
2886 alg, hash, hash_length,
2887 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002888 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002889
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002891}
2892
Gilles Peskine449bd832023-01-11 14:50:10 +01002893psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2894 psa_algorithm_t alg,
2895 const uint8_t *input,
2896 size_t input_length,
2897 uint8_t *signature,
2898 size_t signature_size,
2899 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002900{
2901 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002902 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002904}
2905
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002906psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002907 const psa_key_attributes_t *attributes,
2908 const uint8_t *key_buffer,
2909 size_t key_buffer_size,
2910 psa_algorithm_t alg,
2911 const uint8_t *input,
2912 size_t input_length,
2913 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002915{
2916 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002917
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002919 size_t hash_length;
2920 uint8_t hash[PSA_HASH_MAX_SIZE];
2921
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002922 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 PSA_ALG_SIGN_GET_HASH(alg),
2924 input, input_length,
2925 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002926
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002928 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002930
2931 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002932 attributes, key_buffer, key_buffer_size,
2933 alg, hash, hash_length,
2934 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002935 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002936
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002938}
2939
Gilles Peskine449bd832023-01-11 14:50:10 +01002940psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2941 psa_algorithm_t alg,
2942 const uint8_t *input,
2943 size_t input_length,
2944 const uint8_t *signature,
2945 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002946{
2947 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002948 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002950}
2951
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002952psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002953 const psa_key_attributes_t *attributes,
2954 const uint8_t *key_buffer, size_t key_buffer_size,
2955 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002957{
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2959 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2960 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002961#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2963 return mbedtls_psa_rsa_sign_hash(
2964 attributes,
2965 key_buffer, key_buffer_size,
2966 alg, hash, hash_length,
2967 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002968#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2969 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 } else {
2971 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002972 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2974 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002975#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2977 return mbedtls_psa_ecdsa_sign_hash(
2978 attributes,
2979 key_buffer, key_buffer_size,
2980 alg, hash, hash_length,
2981 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002982#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2983 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002984 } else {
2985 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002986 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002987 }
Ronald Cron4501c982021-03-19 20:09:32 +01002988
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 (void) key_buffer;
2990 (void) key_buffer_size;
2991 (void) hash;
2992 (void) hash_length;
2993 (void) signature;
2994 (void) signature_size;
2995 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002996
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002998}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002999
Gilles Peskine449bd832023-01-11 14:50:10 +01003000psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3001 psa_algorithm_t alg,
3002 const uint8_t *hash,
3003 size_t hash_length,
3004 uint8_t *signature,
3005 size_t signature_size,
3006 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003007{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003008 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003009 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003011}
3012
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003013psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003014 const psa_key_attributes_t *attributes,
3015 const uint8_t *key_buffer, size_t key_buffer_size,
3016 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003018{
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3020 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3021 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003022#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3024 return mbedtls_psa_rsa_verify_hash(
3025 attributes,
3026 key_buffer, key_buffer_size,
3027 alg, hash, hash_length,
3028 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003029#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3030 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 } else {
3032 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003033 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3035 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003036#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003037 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3038 return mbedtls_psa_ecdsa_verify_hash(
3039 attributes,
3040 key_buffer, key_buffer_size,
3041 alg, hash, hash_length,
3042 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003043#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3044 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 } else {
3046 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003047 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003048 }
Ronald Cron4501c982021-03-19 20:09:32 +01003049
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 (void) key_buffer;
3051 (void) key_buffer_size;
3052 (void) hash;
3053 (void) hash_length;
3054 (void) signature;
3055 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003056
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003058}
3059
Gilles Peskine449bd832023-01-11 14:50:10 +01003060psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3061 psa_algorithm_t alg,
3062 const uint8_t *hash,
3063 size_t hash_length,
3064 const uint8_t *signature,
3065 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003066{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003067 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003068 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003070}
3071
Gilles Peskine449bd832023-01-11 14:50:10 +01003072psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3073 psa_algorithm_t alg,
3074 const uint8_t *input,
3075 size_t input_length,
3076 const uint8_t *salt,
3077 size_t salt_length,
3078 uint8_t *output,
3079 size_t output_size,
3080 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003081{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003082 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003083 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003084 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003085
Darryl Green5cc689a2018-07-24 15:34:10 +01003086 (void) input;
3087 (void) input_length;
3088 (void) salt;
3089 (void) output;
3090 (void) output_size;
3091
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003092 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003093
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3095 return PSA_ERROR_INVALID_ARGUMENT;
3096 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003097
Ronald Cron5c522922020-11-14 16:35:34 +01003098 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3100 if (status != PSA_SUCCESS) {
3101 return status;
3102 }
3103 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3104 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003105 status = PSA_ERROR_INVALID_ARGUMENT;
3106 goto exit;
3107 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003108
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003109 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003111 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003112
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003113 status = psa_driver_wrapper_asymmetric_encrypt(
3114 &attributes, slot->key.data, slot->key.bytes,
3115 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003117exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003119
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003121}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003122
Gilles Peskine449bd832023-01-11 14:50:10 +01003123psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3124 psa_algorithm_t alg,
3125 const uint8_t *input,
3126 size_t input_length,
3127 const uint8_t *salt,
3128 size_t salt_length,
3129 uint8_t *output,
3130 size_t output_size,
3131 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003132{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003133 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003134 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003135 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003136
Darryl Green5cc689a2018-07-24 15:34:10 +01003137 (void) input;
3138 (void) input_length;
3139 (void) salt;
3140 (void) output;
3141 (void) output_size;
3142
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003143 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003144
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3146 return PSA_ERROR_INVALID_ARGUMENT;
3147 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003148
Ronald Cron5c522922020-11-14 16:35:34 +01003149 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003150 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3151 if (status != PSA_SUCCESS) {
3152 return status;
3153 }
3154 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003155 status = PSA_ERROR_INVALID_ARGUMENT;
3156 goto exit;
3157 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003158
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003159 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003161 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003162
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003163 status = psa_driver_wrapper_asymmetric_decrypt(
3164 &attributes, slot->key.data, slot->key.bytes,
3165 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003167
3168exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003169 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003170
Gilles Peskine449bd832023-01-11 14:50:10 +01003171 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003172}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003173
Paul Elliott9fe12f62022-11-30 19:16:02 +00003174/****************************************************************/
3175/* Asymmetric interruptible cryptography */
3176/****************************************************************/
3177
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003178static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3179
Paul Elliott9fe12f62022-11-30 19:16:02 +00003180void psa_interruptible_set_max_ops(uint32_t max_ops)
3181{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003182 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003183}
3184
3185uint32_t psa_interruptible_get_max_ops(void)
3186{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003187 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003188}
3189
Paul Elliott9fe12f62022-11-30 19:16:02 +00003190uint32_t psa_sign_hash_get_num_ops(
3191 const psa_sign_hash_interruptible_operation_t *operation)
3192{
Paul Elliott296ede92022-12-15 17:00:30 +00003193 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003194}
3195
3196uint32_t psa_verify_hash_get_num_ops(
3197 const psa_verify_hash_interruptible_operation_t *operation)
3198{
Paul Elliott296ede92022-12-15 17:00:30 +00003199 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003200}
3201
Paul Elliott59ad9452022-12-18 15:09:02 +00003202static psa_status_t psa_sign_hash_abort_internal(
3203 psa_sign_hash_interruptible_operation_t *operation)
3204{
3205 if (operation->id == 0) {
3206 /* The object has (apparently) been initialized but it is not (yet)
3207 * in use. It's ok to call abort on such an object, and there's
3208 * nothing to do. */
3209 return PSA_SUCCESS;
3210 }
3211
3212 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3213
3214 status = psa_driver_wrapper_sign_hash_abort(operation);
3215
3216 operation->id = 0;
3217
Paul Elliotte6145dc2023-02-07 12:51:21 +00003218 /* Do not clear either the error_occurred or num_ops elements here as they
3219 * only want to be cleared by the application calling abort, not by abort
3220 * being called at completion of an operation. */
3221
Paul Elliott59ad9452022-12-18 15:09:02 +00003222 return status;
3223}
3224
Paul Elliott9fe12f62022-11-30 19:16:02 +00003225psa_status_t psa_sign_hash_start(
3226 psa_sign_hash_interruptible_operation_t *operation,
3227 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3228 const uint8_t *hash, size_t hash_length)
3229{
3230 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3231 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3232 psa_key_slot_t *slot;
3233
Paul Elliottc9774412023-02-06 15:14:07 +00003234 /* Check that start has not been previously called, or operation has not
3235 * previously errored. */
3236 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003237 return PSA_ERROR_BAD_STATE;
3238 }
3239
Paul Elliott9fe12f62022-11-30 19:16:02 +00003240 status = psa_sign_verify_check_alg(0, alg);
3241 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003242 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003243 return status;
3244 }
3245
3246 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3247 PSA_KEY_USAGE_SIGN_HASH,
3248 alg);
3249
3250 if (status != PSA_SUCCESS) {
3251 goto exit;
3252 }
3253
3254 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3255 status = PSA_ERROR_INVALID_ARGUMENT;
3256 goto exit;
3257 }
3258
3259 psa_key_attributes_t attributes = {
3260 .core = slot->attr
3261 };
3262
Paul Elliott296ede92022-12-15 17:00:30 +00003263 /* Ensure ops count gets reset, in case of operation re-use. */
3264 operation->num_ops = 0;
3265
Paul Elliott9fe12f62022-11-30 19:16:02 +00003266 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3267 slot->key.data,
3268 slot->key.bytes, alg,
3269 hash, hash_length);
3270exit:
3271
3272 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003273 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003274 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003275 }
3276
3277 unlock_status = psa_unlock_key_slot(slot);
3278
Paul Elliottc9774412023-02-06 15:14:07 +00003279 if (unlock_status != PSA_SUCCESS) {
3280 operation->error_occurred = 1;
3281 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003282
Paul Elliottc9774412023-02-06 15:14:07 +00003283 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003284}
3285
3286
3287psa_status_t psa_sign_hash_complete(
3288 psa_sign_hash_interruptible_operation_t *operation,
3289 uint8_t *signature, size_t signature_size,
3290 size_t *signature_length)
3291{
3292 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3293
3294 *signature_length = 0;
3295
Paul Elliottc9774412023-02-06 15:14:07 +00003296 /* Check that start has been called first, and that operation has not
3297 * previously errored. */
3298 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003299 status = PSA_ERROR_BAD_STATE;
3300 goto exit;
3301 }
3302
Paul Elliott096abc42023-02-03 18:33:23 +00003303 /* Immediately reject a zero-length signature buffer. This guarantees that
3304 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003305 if (signature_size == 0) {
3306 status = PSA_ERROR_BUFFER_TOO_SMALL;
3307 goto exit;
3308 }
3309
3310 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3311 signature_size,
3312 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003313
Paul Elliott296ede92022-12-15 17:00:30 +00003314 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003315 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003316
Paul Elliottebe225c2023-02-10 14:32:53 +00003317exit:
3318
Paul Elliott59200a22023-02-21 15:07:40 +00003319 psa_wipe_tag_output_buffer(signature, status, signature_size,
3320 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003321
Paul Elliott53bb3122023-02-10 14:22:22 +00003322 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003323 if (status != PSA_SUCCESS) {
3324 operation->error_occurred = 1;
3325 }
3326
Paul Elliott59ad9452022-12-18 15:09:02 +00003327 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003328 }
3329
3330 return status;
3331}
3332
3333psa_status_t psa_sign_hash_abort(
3334 psa_sign_hash_interruptible_operation_t *operation)
3335{
Paul Elliott59ad9452022-12-18 15:09:02 +00003336 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3337
3338 status = psa_sign_hash_abort_internal(operation);
3339
3340 /* We clear the number of ops done here, so that it is not cleared when
3341 * the operation fails or succeeds, only on manual abort. */
3342 operation->num_ops = 0;
3343
Paul Elliottc9774412023-02-06 15:14:07 +00003344 /* Likewise, failure state. */
3345 operation->error_occurred = 0;
3346
Paul Elliott59ad9452022-12-18 15:09:02 +00003347 return status;
3348}
3349
3350static psa_status_t psa_verify_hash_abort_internal(
3351 psa_verify_hash_interruptible_operation_t *operation)
3352{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003353 if (operation->id == 0) {
3354 /* The object has (apparently) been initialized but it is not (yet)
3355 * in use. It's ok to call abort on such an object, and there's
3356 * nothing to do. */
3357 return PSA_SUCCESS;
3358 }
3359
Paul Elliott59ad9452022-12-18 15:09:02 +00003360 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3361
3362 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003363
3364 operation->id = 0;
3365
Paul Elliotte6145dc2023-02-07 12:51:21 +00003366 /* Do not clear either the error_occurred or num_ops elements here as they
3367 * only want to be cleared by the application calling abort, not by abort
3368 * being called at completion of an operation. */
3369
Paul Elliott59ad9452022-12-18 15:09:02 +00003370 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003371}
3372
3373psa_status_t psa_verify_hash_start(
3374 psa_verify_hash_interruptible_operation_t *operation,
3375 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3376 const uint8_t *hash, size_t hash_length,
3377 const uint8_t *signature, size_t signature_length)
3378{
3379 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3380 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3381 psa_key_slot_t *slot;
3382
Paul Elliottc9774412023-02-06 15:14:07 +00003383 /* Check that start has not been previously called, or operation has not
3384 * previously errored. */
3385 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003386 return PSA_ERROR_BAD_STATE;
3387 }
3388
3389 status = psa_sign_verify_check_alg(0, alg);
3390 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003391 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003392 return status;
3393 }
3394
3395 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3396 PSA_KEY_USAGE_VERIFY_HASH,
3397 alg);
3398
3399 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003400 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003401 return status;
3402 }
3403
3404 psa_key_attributes_t attributes = {
3405 .core = slot->attr
3406 };
3407
Paul Elliott296ede92022-12-15 17:00:30 +00003408 /* Ensure ops count gets reset, in case of operation re-use. */
3409 operation->num_ops = 0;
3410
Paul Elliott9fe12f62022-11-30 19:16:02 +00003411 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3412 slot->key.data,
3413 slot->key.bytes,
3414 alg, hash, hash_length,
3415 signature, signature_length);
3416
3417 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003418 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003419 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003420 }
3421
3422 unlock_status = psa_unlock_key_slot(slot);
3423
Paul Elliottc9774412023-02-06 15:14:07 +00003424 if (unlock_status != PSA_SUCCESS) {
3425 operation->error_occurred = 1;
3426 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003427
Paul Elliottc9774412023-02-06 15:14:07 +00003428 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003429}
3430
3431psa_status_t psa_verify_hash_complete(
3432 psa_verify_hash_interruptible_operation_t *operation)
3433{
3434 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3435
Paul Elliottc9774412023-02-06 15:14:07 +00003436 /* Check that start has been called first, and that operation has not
3437 * previously errored. */
3438 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003439 status = PSA_ERROR_BAD_STATE;
3440 goto exit;
3441 }
3442
3443 status = psa_driver_wrapper_verify_hash_complete(operation);
3444
Paul Elliott296ede92022-12-15 17:00:30 +00003445 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003446 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003447 operation);
3448
Paul Elliottebe225c2023-02-10 14:32:53 +00003449exit:
3450
Paul Elliott9fe12f62022-11-30 19:16:02 +00003451 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003452 if (status != PSA_SUCCESS) {
3453 operation->error_occurred = 1;
3454 }
3455
Paul Elliott59ad9452022-12-18 15:09:02 +00003456 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003457 }
3458
3459 return status;
3460}
3461
3462psa_status_t psa_verify_hash_abort(
3463 psa_verify_hash_interruptible_operation_t *operation)
3464{
Paul Elliott59ad9452022-12-18 15:09:02 +00003465 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003466
Paul Elliott59ad9452022-12-18 15:09:02 +00003467 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003468
Paul Elliott59ad9452022-12-18 15:09:02 +00003469 /* We clear the number of ops done here, so that it is not cleared when
3470 * the operation fails or succeeds, only on manual abort. */
3471 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003472
Paul Elliottc9774412023-02-06 15:14:07 +00003473 /* Likewise, failure state. */
3474 operation->error_occurred = 0;
3475
Paul Elliott59ad9452022-12-18 15:09:02 +00003476 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003477}
3478
Paul Elliott588f8ed2022-12-02 18:10:26 +00003479/****************************************************************/
3480/* Asymmetric interruptible cryptography internal */
3481/* implementations */
3482/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003483
Paul Elliott588f8ed2022-12-02 18:10:26 +00003484void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3485{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003486
Paul Elliott588f8ed2022-12-02 18:10:26 +00003487#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3488 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3489 defined(MBEDTLS_ECP_RESTARTABLE)
3490
3491 /* Internal implementation uses zero to indicate infinite number max ops,
3492 * therefore avoid this value, and set to minimum possible. */
3493 if (max_ops == 0) {
3494 max_ops = 1;
3495 }
3496
Paul Elliott588f8ed2022-12-02 18:10:26 +00003497 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003498#else
3499 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003500#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3501 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3502 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3503}
3504
Paul Elliott588f8ed2022-12-02 18:10:26 +00003505uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003506 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003507{
3508#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3509 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3510 defined(MBEDTLS_ECP_RESTARTABLE)
3511
Paul Elliott93d9ca82023-02-15 18:14:21 +00003512 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003513#else
3514 (void) operation;
3515 return 0;
3516#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3517 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3518 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3519}
3520
3521uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003522 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003523{
3524 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3525 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3526 defined(MBEDTLS_ECP_RESTARTABLE)
3527
Paul Elliott93d9ca82023-02-15 18:14:21 +00003528 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003529#else
3530 (void) operation;
3531 return 0;
3532#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3533 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3534 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3535}
3536
3537psa_status_t mbedtls_psa_sign_hash_start(
3538 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3539 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3540 size_t key_buffer_size, psa_algorithm_t alg,
3541 const uint8_t *hash, size_t hash_length)
3542{
3543 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003544 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003545
Paul Elliott068fe072023-01-16 13:59:15 +00003546 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3547 return PSA_ERROR_NOT_SUPPORTED;
3548 }
3549
3550 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003551 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003552 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003553
3554#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003555 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3556 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003557
Paul Elliotta1c94092023-02-15 16:38:04 +00003558 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3559
Paul Elliott93d9ca82023-02-15 18:14:21 +00003560 /* Ensure num_ops is zero'ed in case of context re-use. */
3561 operation->num_ops = 0;
3562
Paul Elliott068fe072023-01-16 13:59:15 +00003563 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3564 attributes->core.bits,
3565 key_buffer,
3566 key_buffer_size,
3567 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003568
Paul Elliott068fe072023-01-16 13:59:15 +00003569 if (status != PSA_SUCCESS) {
3570 return status;
3571 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003572
Paul Elliott1bc59df2023-02-05 13:41:57 +00003573 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003574 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003575
Paul Elliott068fe072023-01-16 13:59:15 +00003576 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3577 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3578 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003579
Paul Elliott0290a762023-02-09 14:30:24 +00003580 /* We only need to store the same length of hash as the private key size
3581 * here, it would be truncated by the internal implementation anyway. */
3582 required_hash_length = (hash_length < operation->coordinate_bytes ?
3583 hash_length : operation->coordinate_bytes);
3584
Paul Elliottba70ad42023-02-15 18:23:53 +00003585 if (required_hash_length > sizeof(operation->hash)) {
3586 /* Shouldn't happen, but better safe than sorry. */
3587 return PSA_ERROR_CORRUPTION_DETECTED;
3588 }
3589
Paul Elliott0290a762023-02-09 14:30:24 +00003590 memcpy(operation->hash, hash, required_hash_length);
3591 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003592
3593 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003594
3595#else
Paul Elliott068fe072023-01-16 13:59:15 +00003596 (void) operation;
3597 (void) key_buffer;
3598 (void) key_buffer_size;
3599 (void) alg;
3600 (void) hash;
3601 (void) hash_length;
3602 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003603 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003604
Paul Elliott068fe072023-01-16 13:59:15 +00003605 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003606#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3607 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3608 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003609}
3610
3611psa_status_t mbedtls_psa_sign_hash_complete(
3612 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3613 uint8_t *signature, size_t signature_size,
3614 size_t *signature_length)
3615{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003616#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3617 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3618 defined(MBEDTLS_ECP_RESTARTABLE)
3619
Paul Elliotta1c94092023-02-15 16:38:04 +00003620 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003621 mbedtls_mpi r;
3622 mbedtls_mpi s;
3623
Paul Elliott46845252023-02-03 14:59:11 +00003624 mbedtls_mpi_init(&r);
3625 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003626
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003627 /* Ensure max_ops is set to the current value (or default). */
3628 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3629
Paul Elliotta1c94092023-02-15 16:38:04 +00003630 if (signature_size < 2 * operation->coordinate_bytes) {
3631 status = PSA_ERROR_BUFFER_TOO_SMALL;
3632 goto exit;
3633 }
3634
Paul Elliott588f8ed2022-12-02 18:10:26 +00003635 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003636
Paul Elliott588f8ed2022-12-02 18:10:26 +00003637#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3638 status = mbedtls_to_psa_error(
3639 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003640 &r,
3641 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003642 &operation->ctx->d,
3643 operation->hash,
3644 operation->hash_length,
3645 operation->md_alg,
3646 mbedtls_psa_get_random,
3647 MBEDTLS_PSA_RANDOM_STATE,
3648 &operation->restart_ctx));
3649#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003650 status = PSA_ERROR_NOT_SUPPORTED;
3651 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003652#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3653 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003654 status = mbedtls_to_psa_error(
3655 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003656 &r,
3657 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003658 &operation->ctx->d,
3659 operation->hash,
3660 operation->hash_length,
3661 mbedtls_psa_get_random,
3662 MBEDTLS_PSA_RANDOM_STATE,
3663 mbedtls_psa_get_random,
3664 MBEDTLS_PSA_RANDOM_STATE,
3665 &operation->restart_ctx));
3666 }
3667
Paul Elliottf8e5b562023-02-19 18:43:45 +00003668 /* Hide the fact that the restart context only holds a delta of number of
3669 * ops done during the last operation, not an absolute value. */
3670 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3671
Paul Elliott724bd252023-02-08 12:35:08 +00003672 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003673 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003674 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003675 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003676 operation->coordinate_bytes)
3677 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003678
3679 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003680 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003681 }
3682
3683 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003684 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003685 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003686 operation->coordinate_bytes,
3687 operation->coordinate_bytes)
3688 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003689
3690 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003691 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003692 }
3693
Paul Elliott1bc59df2023-02-05 13:41:57 +00003694 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003695
Paul Elliott724bd252023-02-08 12:35:08 +00003696 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003697 }
Paul Elliott724bd252023-02-08 12:35:08 +00003698
3699exit:
3700
3701 mbedtls_mpi_free(&r);
3702 mbedtls_mpi_free(&s);
3703 return status;
3704
Paul Elliott588f8ed2022-12-02 18:10:26 +00003705 #else
3706
3707 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003708 (void) signature;
3709 (void) signature_size;
3710 (void) signature_length;
3711
3712 return PSA_ERROR_NOT_SUPPORTED;
3713
3714#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3715 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3716 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3717}
3718
3719psa_status_t mbedtls_psa_sign_hash_abort(
3720 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3721{
3722
3723#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3724 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3725 defined(MBEDTLS_ECP_RESTARTABLE)
3726
3727 if (operation->ctx) {
3728 mbedtls_ecdsa_free(operation->ctx);
3729 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003730 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003731 }
3732
3733 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3734
Paul Elliott93d9ca82023-02-15 18:14:21 +00003735 operation->num_ops = 0;
3736
Paul Elliott588f8ed2022-12-02 18:10:26 +00003737 return PSA_SUCCESS;
3738
3739#else
3740
3741 (void) operation;
3742
3743 return PSA_ERROR_NOT_SUPPORTED;
3744
3745#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3746 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3747 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3748}
3749
3750psa_status_t mbedtls_psa_verify_hash_start(
3751 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3752 const psa_key_attributes_t *attributes,
3753 const uint8_t *key_buffer, size_t key_buffer_size,
3754 psa_algorithm_t alg,
3755 const uint8_t *hash, size_t hash_length,
3756 const uint8_t *signature, size_t signature_length)
3757{
3758 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003759 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003760 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003761
Paul Elliott068fe072023-01-16 13:59:15 +00003762 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3763 return PSA_ERROR_NOT_SUPPORTED;
3764 }
3765
3766 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003767 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003768 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003769
3770#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003771 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3772 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003773
Paul Elliotta1c94092023-02-15 16:38:04 +00003774 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3775 mbedtls_mpi_init(&operation->r);
3776 mbedtls_mpi_init(&operation->s);
3777
Paul Elliott93d9ca82023-02-15 18:14:21 +00003778 /* Ensure num_ops is zero'ed in case of context re-use. */
3779 operation->num_ops = 0;
3780
Paul Elliott068fe072023-01-16 13:59:15 +00003781 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3782 attributes->core.bits,
3783 key_buffer,
3784 key_buffer_size,
3785 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003786
Paul Elliott068fe072023-01-16 13:59:15 +00003787 if (status != PSA_SUCCESS) {
3788 return status;
3789 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003790
Paul Elliottc569fc22023-02-10 13:02:54 +00003791 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003792
Paul Elliott1bc59df2023-02-05 13:41:57 +00003793 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003794 return PSA_ERROR_INVALID_SIGNATURE;
3795 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003796
Paul Elliott068fe072023-01-16 13:59:15 +00003797 status = mbedtls_to_psa_error(
3798 mbedtls_mpi_read_binary(&operation->r,
3799 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003800 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003801
Paul Elliott068fe072023-01-16 13:59:15 +00003802 if (status != PSA_SUCCESS) {
3803 return status;
3804 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003805
Paul Elliott068fe072023-01-16 13:59:15 +00003806 status = mbedtls_to_psa_error(
3807 mbedtls_mpi_read_binary(&operation->s,
3808 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003809 coordinate_bytes,
3810 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003811
Paul Elliott068fe072023-01-16 13:59:15 +00003812 if (status != PSA_SUCCESS) {
3813 return status;
3814 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003815
Paul Elliott2c9843f2023-02-15 17:32:42 +00003816 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003817
Paul Elliott2c9843f2023-02-15 17:32:42 +00003818 if (status != PSA_SUCCESS) {
3819 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003820 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003821
Paul Elliott0290a762023-02-09 14:30:24 +00003822 /* We only need to store the same length of hash as the private key size
3823 * here, it would be truncated by the internal implementation anyway. */
3824 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3825 coordinate_bytes);
3826
Paul Elliottba70ad42023-02-15 18:23:53 +00003827 if (required_hash_length > sizeof(operation->hash)) {
3828 /* Shouldn't happen, but better safe than sorry. */
3829 return PSA_ERROR_CORRUPTION_DETECTED;
3830 }
3831
Paul Elliott0290a762023-02-09 14:30:24 +00003832 memcpy(operation->hash, hash, required_hash_length);
3833 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003834
3835 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003836#else
Paul Elliott068fe072023-01-16 13:59:15 +00003837 (void) operation;
3838 (void) key_buffer;
3839 (void) key_buffer_size;
3840 (void) alg;
3841 (void) hash;
3842 (void) hash_length;
3843 (void) signature;
3844 (void) signature_length;
3845 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003846 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003847 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003848
Paul Elliott068fe072023-01-16 13:59:15 +00003849 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003850#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3851 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3852 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003853}
3854
3855psa_status_t mbedtls_psa_verify_hash_complete(
3856 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3857{
3858
3859#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3860 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3861 defined(MBEDTLS_ECP_RESTARTABLE)
3862
Paul Elliottf8e5b562023-02-19 18:43:45 +00003863 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3864
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003865 /* Ensure max_ops is set to the current value (or default). */
3866 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3867
Paul Elliottf8e5b562023-02-19 18:43:45 +00003868 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003869 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3870 operation->hash,
3871 operation->hash_length,
3872 &operation->ctx->Q,
3873 &operation->r,
3874 &operation->s,
3875 &operation->restart_ctx));
3876
Paul Elliottf8e5b562023-02-19 18:43:45 +00003877 /* Hide the fact that the restart context only holds a delta of number of
3878 * ops done during the last operation, not an absolute value. */
3879 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3880
3881 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003882#else
3883 (void) operation;
3884
3885 return PSA_ERROR_NOT_SUPPORTED;
3886
3887#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3888 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3889 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3890}
3891
3892psa_status_t mbedtls_psa_verify_hash_abort(
3893 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3894{
3895
3896#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3897 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3898 defined(MBEDTLS_ECP_RESTARTABLE)
3899
3900 if (operation->ctx) {
3901 mbedtls_ecdsa_free(operation->ctx);
3902 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003903 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003904 }
3905
3906 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3907
Paul Elliott93d9ca82023-02-15 18:14:21 +00003908 operation->num_ops = 0;
3909
Paul Elliott588f8ed2022-12-02 18:10:26 +00003910 mbedtls_mpi_free(&operation->r);
3911 mbedtls_mpi_free(&operation->s);
3912
3913 return PSA_SUCCESS;
3914
3915#else
3916 (void) operation;
3917
3918 return PSA_ERROR_NOT_SUPPORTED;
3919
3920#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3921 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3922 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3923}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003924
mohammad1603503973b2018-03-12 15:59:30 +02003925/****************************************************************/
3926/* Symmetric cryptography */
3927/****************************************************************/
3928
Gilles Peskine449bd832023-01-11 14:50:10 +01003929static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3930 mbedtls_svc_key_id_t key,
3931 psa_algorithm_t alg,
3932 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003933{
3934 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3935 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003936 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003937 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3938 PSA_KEY_USAGE_ENCRYPT :
3939 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003940
3941 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003942 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003943 status = PSA_ERROR_BAD_STATE;
3944 goto exit;
3945 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003946
Gilles Peskine449bd832023-01-11 14:50:10 +01003947 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003948 status = PSA_ERROR_INVALID_ARGUMENT;
3949 goto exit;
3950 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003951
Gilles Peskine449bd832023-01-11 14:50:10 +01003952 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3953 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003954 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003955 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003956
Ronald Cron49fafa92021-03-10 08:34:23 +01003957 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003958 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003959 * so we only set it (in the driver wrapper) after resources have been
3960 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003961 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003962 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003963 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003964 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003965 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003966 }
3967 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003968
Ronald Crona4af55f2020-12-14 14:36:06 +01003969 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003970 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003971 };
3972
Ronald Cronab99ac22020-10-01 16:38:28 +02003973 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003974 if (cipher_operation == MBEDTLS_ENCRYPT) {
3975 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3976 &attributes,
3977 slot->key.data,
3978 slot->key.bytes,
3979 alg);
3980 } else {
3981 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3982 &attributes,
3983 slot->key.data,
3984 slot->key.bytes,
3985 alg);
3986 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003987
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003988exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003989 if (status != PSA_SUCCESS) {
3990 psa_cipher_abort(operation);
3991 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003992
Gilles Peskine449bd832023-01-11 14:50:10 +01003993 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003994
Gilles Peskine449bd832023-01-11 14:50:10 +01003995 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003996}
3997
Gilles Peskine449bd832023-01-11 14:50:10 +01003998psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3999 mbedtls_svc_key_id_t key,
4000 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004001{
Gilles Peskine449bd832023-01-11 14:50:10 +01004002 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004003}
4004
Gilles Peskine449bd832023-01-11 14:50:10 +01004005psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4006 mbedtls_svc_key_id_t key,
4007 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004008{
Gilles Peskine449bd832023-01-11 14:50:10 +01004009 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004010}
4011
Gilles Peskine449bd832023-01-11 14:50:10 +01004012psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4013 uint8_t *iv,
4014 size_t iv_size,
4015 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004016{
Ronald Cron6d051732020-10-01 14:10:20 +02004017 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004018 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4019 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004020
Gilles Peskine449bd832023-01-11 14:50:10 +01004021 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004022 status = PSA_ERROR_BAD_STATE;
4023 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004024 }
4025
Gilles Peskine449bd832023-01-11 14:50:10 +01004026 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004027 status = PSA_ERROR_BAD_STATE;
4028 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004029 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004030
Ronald Cron2fb90522021-07-05 12:31:44 +02004031 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004033 status = PSA_ERROR_BUFFER_TOO_SMALL;
4034 goto exit;
4035 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004036
Gilles Peskine449bd832023-01-11 14:50:10 +01004037 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004038 status = PSA_ERROR_GENERIC_ERROR;
4039 goto exit;
4040 }
4041
Gilles Peskine449bd832023-01-11 14:50:10 +01004042 status = psa_generate_random(local_iv, default_iv_length);
4043 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004044 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004045 }
Ronald Cron5618a392021-03-26 09:52:26 +01004046
Gilles Peskine449bd832023-01-11 14:50:10 +01004047 status = psa_driver_wrapper_cipher_set_iv(operation,
4048 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004049
4050exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004051 if (status == PSA_SUCCESS) {
4052 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004053 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004054 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004055 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004056 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004058 }
Ronald Cron6d051732020-10-01 14:10:20 +02004059
Gilles Peskine449bd832023-01-11 14:50:10 +01004060 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004061}
4062
Gilles Peskine449bd832023-01-11 14:50:10 +01004063psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4064 const uint8_t *iv,
4065 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004066{
Ronald Cron6d051732020-10-01 14:10:20 +02004067 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004068
Gilles Peskine449bd832023-01-11 14:50:10 +01004069 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004070 status = PSA_ERROR_BAD_STATE;
4071 goto exit;
4072 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004073
Gilles Peskine449bd832023-01-11 14:50:10 +01004074 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004075 status = PSA_ERROR_BAD_STATE;
4076 goto exit;
4077 }
Ronald Crona0d68172021-03-26 10:15:08 +01004078
Gilles Peskine449bd832023-01-11 14:50:10 +01004079 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004080 status = PSA_ERROR_INVALID_ARGUMENT;
4081 goto exit;
4082 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004083
Gilles Peskine449bd832023-01-11 14:50:10 +01004084 status = psa_driver_wrapper_cipher_set_iv(operation,
4085 iv,
4086 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004087
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004088exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004089 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004090 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 } else {
4092 psa_cipher_abort(operation);
4093 }
4094 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004095}
4096
Gilles Peskine449bd832023-01-11 14:50:10 +01004097psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4098 const uint8_t *input,
4099 size_t input_length,
4100 uint8_t *output,
4101 size_t output_size,
4102 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004103{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004104 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004105
Gilles Peskine449bd832023-01-11 14:50:10 +01004106 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004107 status = PSA_ERROR_BAD_STATE;
4108 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004109 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004110
Gilles Peskine449bd832023-01-11 14:50:10 +01004111 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004112 status = PSA_ERROR_BAD_STATE;
4113 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004114 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004115
Gilles Peskine449bd832023-01-11 14:50:10 +01004116 status = psa_driver_wrapper_cipher_update(operation,
4117 input,
4118 input_length,
4119 output,
4120 output_size,
4121 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004122
4123exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 if (status != PSA_SUCCESS) {
4125 psa_cipher_abort(operation);
4126 }
Ronald Cron6d051732020-10-01 14:10:20 +02004127
Gilles Peskine449bd832023-01-11 14:50:10 +01004128 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004129}
4130
Gilles Peskine449bd832023-01-11 14:50:10 +01004131psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4132 uint8_t *output,
4133 size_t output_size,
4134 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004135{
David Saadab4ecc272019-02-14 13:48:10 +02004136 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004137
Gilles Peskine449bd832023-01-11 14:50:10 +01004138 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004139 status = PSA_ERROR_BAD_STATE;
4140 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004141 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004142
Gilles Peskine449bd832023-01-11 14:50:10 +01004143 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004144 status = PSA_ERROR_BAD_STATE;
4145 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004146 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004147
Gilles Peskine449bd832023-01-11 14:50:10 +01004148 status = psa_driver_wrapper_cipher_finish(operation,
4149 output,
4150 output_size,
4151 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004152
4153exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 if (status == PSA_SUCCESS) {
4155 return psa_cipher_abort(operation);
4156 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004157 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004158 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004159
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004161 }
mohammad1603503973b2018-03-12 15:59:30 +02004162}
4163
Gilles Peskine449bd832023-01-11 14:50:10 +01004164psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004165{
Gilles Peskine449bd832023-01-11 14:50:10 +01004166 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004167 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004168 * in use. It's ok to call abort on such an object, and there's
4169 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004171 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004172
Gilles Peskine449bd832023-01-11 14:50:10 +01004173 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004174
Ronald Cron49fafa92021-03-10 08:34:23 +01004175 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004176 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004177 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004178
Gilles Peskine449bd832023-01-11 14:50:10 +01004179 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004180}
4181
Gilles Peskine449bd832023-01-11 14:50:10 +01004182psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4183 psa_algorithm_t alg,
4184 const uint8_t *input,
4185 size_t input_length,
4186 uint8_t *output,
4187 size_t output_size,
4188 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004189{
4190 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004191 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004192 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004193 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4194 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004195
Gilles Peskine449bd832023-01-11 14:50:10 +01004196 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004197 status = PSA_ERROR_INVALID_ARGUMENT;
4198 goto exit;
4199 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004200
Gilles Peskine449bd832023-01-11 14:50:10 +01004201 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4202 PSA_KEY_USAGE_ENCRYPT,
4203 alg);
4204 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004205 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004206 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004207
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004208 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004210 };
4211
Gilles Peskine449bd832023-01-11 14:50:10 +01004212 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4213 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004214 status = PSA_ERROR_GENERIC_ERROR;
4215 goto exit;
4216 }
4217
Gilles Peskine449bd832023-01-11 14:50:10 +01004218 if (default_iv_length > 0) {
4219 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004220 status = PSA_ERROR_BUFFER_TOO_SMALL;
4221 goto exit;
4222 }
4223
Gilles Peskine449bd832023-01-11 14:50:10 +01004224 status = psa_generate_random(local_iv, default_iv_length);
4225 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004226 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004227 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004228 }
4229
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004230 status = psa_driver_wrapper_cipher_encrypt(
4231 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004232 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004233 mbedtls_buffer_offset(output, default_iv_length),
4234 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004235
4236exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 unlock_status = psa_unlock_key_slot(slot);
4238 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004239 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004240 }
Ronald Cron23919522021-07-08 19:00:07 +02004241
Gilles Peskine449bd832023-01-11 14:50:10 +01004242 if (status == PSA_SUCCESS) {
4243 if (default_iv_length > 0) {
4244 memcpy(output, local_iv, default_iv_length);
4245 }
4246 *output_length += default_iv_length;
4247 } else {
4248 *output_length = 0;
4249 }
4250
4251 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004252}
4253
Gilles Peskine449bd832023-01-11 14:50:10 +01004254psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4255 psa_algorithm_t alg,
4256 const uint8_t *input,
4257 size_t input_length,
4258 uint8_t *output,
4259 size_t output_size,
4260 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004261{
4262 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004263 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004264 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004265
Gilles Peskine449bd832023-01-11 14:50:10 +01004266 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004267 status = PSA_ERROR_INVALID_ARGUMENT;
4268 goto exit;
4269 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004270
Gilles Peskine449bd832023-01-11 14:50:10 +01004271 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4272 PSA_KEY_USAGE_DECRYPT,
4273 alg);
4274 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004275 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004277
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004278 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004279 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004280 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004281
Gilles Peskine449bd832023-01-11 14:50:10 +01004282 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4283 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004284 status = PSA_ERROR_INVALID_ARGUMENT;
4285 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004287 status = PSA_ERROR_INVALID_ARGUMENT;
4288 goto exit;
4289 }
4290
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004291 status = psa_driver_wrapper_cipher_decrypt(
4292 &attributes, slot->key.data, slot->key.bytes,
4293 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004294 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004295
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004296exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 unlock_status = psa_unlock_key_slot(slot);
4298 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004299 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004301
Gilles Peskine449bd832023-01-11 14:50:10 +01004302 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004303 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004304 }
Ronald Cron23919522021-07-08 19:00:07 +02004305
Gilles Peskine449bd832023-01-11 14:50:10 +01004306 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004307}
4308
4309
mohammad16035955c982018-04-26 00:53:03 +03004310/****************************************************************/
4311/* AEAD */
4312/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004313
Paul Elliottbaff51c2021-09-28 17:44:45 +01004314/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004315static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004316{
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004318}
4319
4320/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004321static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4322 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004323{
Gilles Peskine449bd832023-01-11 14:50:10 +01004324 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004325
Gilles Peskine449bd832023-01-11 14:50:10 +01004326 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004327#if defined(PSA_WANT_ALG_GCM)
4328 case PSA_ALG_GCM:
4329 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004330 * arbitrarily large nonces. Please note that we do not generally
4331 * recommend the usage of nonces of greater length than
4332 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4333 * size, which can then lead to collisions if you encrypt a very
4334 * large number of messages.*/
4335 if (nonce_length != 0) {
4336 return PSA_SUCCESS;
4337 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004338 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004339#endif /* PSA_WANT_ALG_GCM */
4340#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004341 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004342 if (nonce_length >= 7 && nonce_length <= 13) {
4343 return PSA_SUCCESS;
4344 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004345 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004346#endif /* PSA_WANT_ALG_CCM */
4347#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004348 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 if (nonce_length == 12) {
4350 return PSA_SUCCESS;
4351 } else if (nonce_length == 8) {
4352 return PSA_ERROR_NOT_SUPPORTED;
4353 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004354 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004355#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004356 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004357 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004359 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004360
Gilles Peskine449bd832023-01-11 14:50:10 +01004361 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004362}
4363
Gilles Peskine449bd832023-01-11 14:50:10 +01004364static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004365{
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4367 return PSA_ERROR_INVALID_ARGUMENT;
4368 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004369
Gilles Peskine449bd832023-01-11 14:50:10 +01004370 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004371}
4372
Gilles Peskine449bd832023-01-11 14:50:10 +01004373psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4374 psa_algorithm_t alg,
4375 const uint8_t *nonce,
4376 size_t nonce_length,
4377 const uint8_t *additional_data,
4378 size_t additional_data_length,
4379 const uint8_t *plaintext,
4380 size_t plaintext_length,
4381 uint8_t *ciphertext,
4382 size_t ciphertext_size,
4383 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004384{
Ronald Cron215633c2021-03-16 17:15:37 +01004385 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4386 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004387
mohammad1603f08a5502018-06-03 15:05:47 +03004388 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004389
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 status = psa_aead_check_algorithm(alg);
4391 if (status != PSA_SUCCESS) {
4392 return status;
4393 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004394
Ronald Cron9a986162021-03-26 12:40:07 +01004395 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004396 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4397 if (status != PSA_SUCCESS) {
4398 return status;
4399 }
mohammad16035955c982018-04-26 00:53:03 +03004400
Ronald Cron215633c2021-03-16 17:15:37 +01004401 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004403 };
mohammad16035955c982018-04-26 00:53:03 +03004404
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 status = psa_aead_check_nonce_length(alg, nonce_length);
4406 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004407 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004408 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004409
Ronald Cronde822812021-03-17 16:08:20 +01004410 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004411 &attributes, slot->key.data, slot->key.bytes,
4412 alg,
4413 nonce, nonce_length,
4414 additional_data, additional_data_length,
4415 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004416 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004417
Gilles Peskine449bd832023-01-11 14:50:10 +01004418 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4419 memset(ciphertext, 0, ciphertext_size);
4420 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004421
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004422exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004423 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004424
Gilles Peskine449bd832023-01-11 14:50:10 +01004425 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004426}
4427
Gilles Peskine449bd832023-01-11 14:50:10 +01004428psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4429 psa_algorithm_t alg,
4430 const uint8_t *nonce,
4431 size_t nonce_length,
4432 const uint8_t *additional_data,
4433 size_t additional_data_length,
4434 const uint8_t *ciphertext,
4435 size_t ciphertext_length,
4436 uint8_t *plaintext,
4437 size_t plaintext_size,
4438 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004439{
Ronald Cron215633c2021-03-16 17:15:37 +01004440 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4441 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004442
Gilles Peskineee652a32018-06-01 19:23:52 +02004443 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004444
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 status = psa_aead_check_algorithm(alg);
4446 if (status != PSA_SUCCESS) {
4447 return status;
4448 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004449
Ronald Cron9a986162021-03-26 12:40:07 +01004450 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4452 if (status != PSA_SUCCESS) {
4453 return status;
4454 }
mohammad16035955c982018-04-26 00:53:03 +03004455
Ronald Cron215633c2021-03-16 17:15:37 +01004456 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004458 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004459
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 status = psa_aead_check_nonce_length(alg, nonce_length);
4461 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004462 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004463 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004464
Ronald Cronde822812021-03-17 16:08:20 +01004465 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004466 &attributes, slot->key.data, slot->key.bytes,
4467 alg,
4468 nonce, nonce_length,
4469 additional_data, additional_data_length,
4470 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004472
Gilles Peskine449bd832023-01-11 14:50:10 +01004473 if (status != PSA_SUCCESS && plaintext_size != 0) {
4474 memset(plaintext, 0, plaintext_size);
4475 }
mohammad160360a64d02018-06-03 17:20:42 +03004476
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004477exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004479
Gilles Peskine449bd832023-01-11 14:50:10 +01004480 return status;
mohammad16035955c982018-04-26 00:53:03 +03004481}
4482
Gilles Peskine449bd832023-01-11 14:50:10 +01004483static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4484{
4485 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004486
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004488#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004489 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004490 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4492 return PSA_ERROR_INVALID_ARGUMENT;
4493 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004494 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004495#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004496
Przemek Stekiel86679c72022-10-06 17:06:56 +02004497#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004498 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004499 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4501 return PSA_ERROR_INVALID_ARGUMENT;
4502 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004503 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004504#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004505
Przemek Stekiel86679c72022-10-06 17:06:56 +02004506#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004508 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 if (tag_len != 16) {
4510 return PSA_ERROR_INVALID_ARGUMENT;
4511 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004512 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004513#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004514
4515 default:
4516 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004518 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004519 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004520}
4521
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004522/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004523static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4524 int is_encrypt,
4525 mbedtls_svc_key_id_t key,
4526 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004527{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004528 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4529 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4530 psa_key_slot_t *slot = NULL;
4531 psa_key_usage_t key_usage = 0;
4532
Gilles Peskine449bd832023-01-11 14:50:10 +01004533 status = psa_aead_check_algorithm(alg);
4534 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004535 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004536 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004537
Gilles Peskine449bd832023-01-11 14:50:10 +01004538 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004539 status = PSA_ERROR_BAD_STATE;
4540 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004541 }
4542
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 if (operation->nonce_set || operation->lengths_set ||
4544 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004545 status = PSA_ERROR_BAD_STATE;
4546 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004547 }
4548
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004550 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004551 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004552 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004554
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4556 alg);
4557 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004558 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004559 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004560
4561 psa_key_attributes_t attributes = {
4562 .core = slot->attr
4563 };
4564
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004566 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004568
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 if (is_encrypt) {
4570 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4571 &attributes,
4572 slot->key.data,
4573 slot->key.bytes,
4574 alg);
4575 } else {
4576 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4577 &attributes,
4578 slot->key.data,
4579 slot->key.bytes,
4580 alg);
4581 }
4582 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004583 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004585
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004587
4588exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004590
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004592 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004594 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 } else {
4596 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004597 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004598
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004600}
4601
Paul Elliott302ff6b2021-04-20 18:10:30 +01004602/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004603psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4604 mbedtls_svc_key_id_t key,
4605 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004606{
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004608}
4609
4610/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004611psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4612 mbedtls_svc_key_id_t key,
4613 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004614{
Gilles Peskine449bd832023-01-11 14:50:10 +01004615 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004616}
4617
4618/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004619psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4620 uint8_t *nonce,
4621 size_t nonce_size,
4622 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004623{
4624 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004625 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004626 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004627
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004628 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004629
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004631 status = PSA_ERROR_BAD_STATE;
4632 goto exit;
4633 }
4634
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004636 status = PSA_ERROR_BAD_STATE;
4637 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004638 }
4639
Paul Elliotte193ea82021-10-01 13:00:16 +01004640 /* For CCM, this size may not be correct according to the PSA
4641 * specification. The PSA Crypto 1.0.1 specification states:
4642 *
4643 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4644 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4645 *
4646 * However this restriction that L has to be the smallest integer is not
4647 * applied in practice, and it is not implementable here since the
4648 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4650 operation->alg);
4651 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004652 status = PSA_ERROR_BUFFER_TOO_SMALL;
4653 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004654 }
4655
Gilles Peskine449bd832023-01-11 14:50:10 +01004656 status = psa_generate_random(local_nonce, required_nonce_size);
4657 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004658 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004659 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004660
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004662
Paul Elliott39dc6b82021-05-11 19:16:09 +01004663exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 if (status == PSA_SUCCESS) {
4665 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004666 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 } else {
4668 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004669 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004670
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004672}
4673
4674/* Set the nonce for a multipart authenticated encryption or decryption
4675 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004676psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4677 const uint8_t *nonce,
4678 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004679{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004680 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4681
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004683 status = PSA_ERROR_BAD_STATE;
4684 goto exit;
4685 }
4686
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004688 status = PSA_ERROR_BAD_STATE;
4689 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004690 }
4691
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4693 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004694 status = PSA_ERROR_INVALID_ARGUMENT;
4695 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004696 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004697
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4699 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004700
Paul Elliott39dc6b82021-05-11 19:16:09 +01004701exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004703 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 } else {
4705 psa_aead_abort(operation);
4706 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004707
Gilles Peskine449bd832023-01-11 14:50:10 +01004708 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004709}
4710
4711/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004712psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4713 size_t ad_length,
4714 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004715{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004716 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4717
Gilles Peskine449bd832023-01-11 14:50:10 +01004718 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004719 status = PSA_ERROR_BAD_STATE;
4720 goto exit;
4721 }
4722
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 if (operation->lengths_set || operation->ad_started ||
4724 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004725 status = PSA_ERROR_BAD_STATE;
4726 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004727 }
4728
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004730#if defined(PSA_WANT_ALG_GCM)
4731 case PSA_ALG_GCM:
4732 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004733 * bits. Without the guard this code will generate warnings on 32bit
4734 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004735#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 if (((uint64_t) ad_length) >> 61 != 0 ||
4737 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004738 status = PSA_ERROR_INVALID_ARGUMENT;
4739 goto exit;
4740 }
Paul Elliott325d3742021-09-27 17:56:28 +01004741#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004742 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004743#endif /* PSA_WANT_ALG_GCM */
4744#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004745 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004746 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004747 status = PSA_ERROR_INVALID_ARGUMENT;
4748 goto exit;
4749 }
4750 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004751#endif /* PSA_WANT_ALG_CCM */
4752#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004753 case PSA_ALG_CHACHA20_POLY1305:
4754 /* No length restrictions for ChaChaPoly. */
4755 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004756#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004757 default:
4758 break;
4759 }
Paul Elliott325d3742021-09-27 17:56:28 +01004760
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4762 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004763
Paul Elliott39dc6b82021-05-11 19:16:09 +01004764exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004766 operation->ad_remaining = ad_length;
4767 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004768 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 } else {
4770 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004771 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004772
Gilles Peskine449bd832023-01-11 14:50:10 +01004773 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004774}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004775
4776/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004777psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4778 const uint8_t *input,
4779 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004780{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004781 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4782
Gilles Peskine449bd832023-01-11 14:50:10 +01004783 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004784 status = PSA_ERROR_BAD_STATE;
4785 goto exit;
4786 }
4787
Gilles Peskine449bd832023-01-11 14:50:10 +01004788 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004789 status = PSA_ERROR_BAD_STATE;
4790 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004791 }
4792
Gilles Peskine449bd832023-01-11 14:50:10 +01004793 if (operation->lengths_set) {
4794 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004795 status = PSA_ERROR_INVALID_ARGUMENT;
4796 goto exit;
4797 }
4798
4799 operation->ad_remaining -= input_length;
4800 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004801#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004802 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004803 status = PSA_ERROR_BAD_STATE;
4804 goto exit;
4805 }
4806#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004807
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 status = psa_driver_wrapper_aead_update_ad(operation, input,
4809 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004810
Paul Elliott39dc6b82021-05-11 19:16:09 +01004811exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004813 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 } else {
4815 psa_aead_abort(operation);
4816 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004817
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004819}
4820
4821/* Encrypt or decrypt a message fragment in an active multipart AEAD
4822 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004823psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4824 const uint8_t *input,
4825 size_t input_length,
4826 uint8_t *output,
4827 size_t output_size,
4828 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004829{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004830 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004831
4832 *output_length = 0;
4833
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004835 status = PSA_ERROR_BAD_STATE;
4836 goto exit;
4837 }
4838
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004840 status = PSA_ERROR_BAD_STATE;
4841 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004842 }
4843
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004845 /* Additional data length was supplied, but not all the additional
4846 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004847 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004848 status = PSA_ERROR_INVALID_ARGUMENT;
4849 goto exit;
4850 }
4851
4852 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004853 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004854 status = PSA_ERROR_INVALID_ARGUMENT;
4855 goto exit;
4856 }
4857
4858 operation->body_remaining -= input_length;
4859 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004860#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004861 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004862 status = PSA_ERROR_BAD_STATE;
4863 goto exit;
4864 }
4865#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004866
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4868 output, output_size,
4869 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004870
Paul Elliott39dc6b82021-05-11 19:16:09 +01004871exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004873 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 } else {
4875 psa_aead_abort(operation);
4876 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004877
Gilles Peskine449bd832023-01-11 14:50:10 +01004878 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004879}
4880
Gilles Peskine449bd832023-01-11 14:50:10 +01004881static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004882{
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 if (operation->id == 0 || !operation->nonce_set) {
4884 return PSA_ERROR_BAD_STATE;
4885 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004886
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4888 operation->body_remaining != 0)) {
4889 return PSA_ERROR_INVALID_ARGUMENT;
4890 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004891
Gilles Peskine449bd832023-01-11 14:50:10 +01004892 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004893}
4894
Paul Elliott302ff6b2021-04-20 18:10:30 +01004895/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004896psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4897 uint8_t *ciphertext,
4898 size_t ciphertext_size,
4899 size_t *ciphertext_length,
4900 uint8_t *tag,
4901 size_t tag_size,
4902 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004903{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004904 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4905
Paul Elliott302ff6b2021-04-20 18:10:30 +01004906 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004907 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004908
Gilles Peskine449bd832023-01-11 14:50:10 +01004909 status = psa_aead_final_checks(operation);
4910 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004911 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004913
Gilles Peskine449bd832023-01-11 14:50:10 +01004914 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004915 status = PSA_ERROR_BAD_STATE;
4916 goto exit;
4917 }
4918
Gilles Peskine449bd832023-01-11 14:50:10 +01004919 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4920 ciphertext_size,
4921 ciphertext_length,
4922 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004923
Paul Elliott39dc6b82021-05-11 19:16:09 +01004924exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004925
4926
Paul Elliottf47b0952021-05-21 18:02:33 +01004927 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004928 * the zero tag size, make sure the tag is set to something implausible.
4929 * Even if the operation succeeds, make sure we clear the rest of the
4930 * buffer to prevent potential leakage of anything previously placed in
4931 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004932 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004933
Gilles Peskine449bd832023-01-11 14:50:10 +01004934 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004935
Gilles Peskine449bd832023-01-11 14:50:10 +01004936 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004937}
4938
4939/* Finish authenticating and decrypting a message in a multipart AEAD
4940 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004941psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4942 uint8_t *plaintext,
4943 size_t plaintext_size,
4944 size_t *plaintext_length,
4945 const uint8_t *tag,
4946 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004947{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004948 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4949
Paul Elliott302ff6b2021-04-20 18:10:30 +01004950 *plaintext_length = 0;
4951
Gilles Peskine449bd832023-01-11 14:50:10 +01004952 status = psa_aead_final_checks(operation);
4953 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004954 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004955 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004956
Gilles Peskine449bd832023-01-11 14:50:10 +01004957 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004958 status = PSA_ERROR_BAD_STATE;
4959 goto exit;
4960 }
4961
Gilles Peskine449bd832023-01-11 14:50:10 +01004962 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4963 plaintext_size,
4964 plaintext_length,
4965 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004966
4967exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004969
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004971}
4972
4973/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004974psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004975{
4976 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4977
Gilles Peskine449bd832023-01-11 14:50:10 +01004978 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004979 /* The object has (apparently) been initialized but it is not (yet)
4980 * in use. It's ok to call abort on such an object, and there's
4981 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004983 }
4984
Gilles Peskine449bd832023-01-11 14:50:10 +01004985 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004986
Gilles Peskine449bd832023-01-11 14:50:10 +01004987 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004988
Gilles Peskine449bd832023-01-11 14:50:10 +01004989 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004990}
4991
Gilles Peskinee59236f2018-01-27 23:32:46 +01004992/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004993/* Generators */
4994/****************************************************************/
4995
Przemek Stekiel69c46792022-06-10 12:59:51 +02004996#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004997 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004998 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
4999 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08005000#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005001#endif /* At least one builtin KDF */
5002
Przemek Stekiel69c46792022-06-10 12:59:51 +02005003#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005004 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5005 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5006static psa_status_t psa_key_derivation_start_hmac(
5007 psa_mac_operation_t *operation,
5008 psa_algorithm_t hash_alg,
5009 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005010 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005011{
5012 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5013 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005014 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5015 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5016 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005017
Steven Cooreman72f736a2021-05-07 14:14:37 +02005018 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005020
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 status = psa_driver_wrapper_mac_sign_setup(operation,
5022 &attributes,
5023 hmac_key, hmac_key_length,
5024 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005025
Gilles Peskine449bd832023-01-11 14:50:10 +01005026 psa_reset_key_attributes(&attributes);
5027 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005028}
5029#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005030
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005031#define HKDF_STATE_INIT 0 /* no input yet */
5032#define HKDF_STATE_STARTED 1 /* got salt */
5033#define HKDF_STATE_KEYED 2 /* got key */
5034#define HKDF_STATE_OUTPUT 3 /* output started */
5035
Gilles Peskinecbe66502019-05-16 16:59:18 +02005036static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005038{
Gilles Peskine449bd832023-01-11 14:50:10 +01005039 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5040 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5041 } else {
5042 return operation->alg;
5043 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005044}
5045
Gilles Peskine449bd832023-01-11 14:50:10 +01005046psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005047{
5048 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005049 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5050 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005051 /* The object has (apparently) been initialized but it is not
5052 * in use. It's ok to call abort on such an object, and there's
5053 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005054 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005055#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005056 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5057 mbedtls_free(operation->ctx.hkdf.info);
5058 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5059 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005060#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005061#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5062 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005063 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5064 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5065 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5066 if (operation->ctx.tls12_prf.secret != NULL) {
5067 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5068 operation->ctx.tls12_prf.secret_length);
5069 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005070 }
5071
Gilles Peskine449bd832023-01-11 14:50:10 +01005072 if (operation->ctx.tls12_prf.seed != NULL) {
5073 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5074 operation->ctx.tls12_prf.seed_length);
5075 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005076 }
5077
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 if (operation->ctx.tls12_prf.label != NULL) {
5079 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5080 operation->ctx.tls12_prf.label_length);
5081 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005082 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005083#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 if (operation->ctx.tls12_prf.other_secret != NULL) {
5085 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5086 operation->ctx.tls12_prf.other_secret_length);
5087 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005088 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005089#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005090 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005091
5092 /* We leave the fields Ai and output_block to be erased safely by the
5093 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005094 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005095#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5096 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005097#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005098 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5099 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5100 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5101 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005102#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005103 {
5104 status = PSA_ERROR_BAD_STATE;
5105 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 mbedtls_platform_zeroize(operation, sizeof(*operation));
5107 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005108}
5109
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005110psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005112{
Gilles Peskine449bd832023-01-11 14:50:10 +01005113 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005114 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005116 }
5117
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005118 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005120}
5121
Gilles Peskine449bd832023-01-11 14:50:10 +01005122psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5123 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005124{
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 if (operation->alg == 0) {
5126 return PSA_ERROR_BAD_STATE;
5127 }
5128 if (capacity > operation->capacity) {
5129 return PSA_ERROR_INVALID_ARGUMENT;
5130 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005131 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005132 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005133}
5134
Przemek Stekiel69c46792022-06-10 12:59:51 +02005135#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005136/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005137static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5138 psa_algorithm_t kdf_alg,
5139 uint8_t *output,
5140 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005141{
Gilles Peskine449bd832023-01-11 14:50:10 +01005142 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5143 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005144 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005145 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005146#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005148#else
5149 const uint8_t last_block = 0xff;
5150#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005151
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 if (hkdf->state < HKDF_STATE_KEYED ||
5153 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005154#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005156#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 )) {
5158 return PSA_ERROR_BAD_STATE;
5159 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005160 hkdf->state = HKDF_STATE_OUTPUT;
5161
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005163 /* Copy what remains of the current block */
5164 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005165 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005166 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 }
5168 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005169 output += n;
5170 output_length -= n;
5171 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005173 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005175 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005176 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005177 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005178 * object was corrupted or if this function is called directly
5179 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 if (hkdf->block_number == last_block) {
5181 return PSA_ERROR_BAD_STATE;
5182 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005183
5184 /* We need a new block */
5185 ++hkdf->block_number;
5186 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005187
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5189 hash_alg,
5190 hkdf->prk,
5191 hash_length);
5192 if (status != PSA_SUCCESS) {
5193 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005194 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005195
5196 if (hkdf->block_number != 1) {
5197 status = psa_mac_update(&hkdf->hmac,
5198 hkdf->output_block,
5199 hash_length);
5200 if (status != PSA_SUCCESS) {
5201 return status;
5202 }
5203 }
5204 status = psa_mac_update(&hkdf->hmac,
5205 hkdf->info,
5206 hkdf->info_length);
5207 if (status != PSA_SUCCESS) {
5208 return status;
5209 }
5210 status = psa_mac_update(&hkdf->hmac,
5211 &hkdf->block_number, 1);
5212 if (status != PSA_SUCCESS) {
5213 return status;
5214 }
5215 status = psa_mac_sign_finish(&hkdf->hmac,
5216 hkdf->output_block,
5217 sizeof(hkdf->output_block),
5218 &hmac_output_length);
5219 if (status != PSA_SUCCESS) {
5220 return status;
5221 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005222 }
5223
Gilles Peskine449bd832023-01-11 14:50:10 +01005224 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005225}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005226#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005227
John Durkop07cc04a2020-11-16 22:08:34 -08005228#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5229 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005230static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5231 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005233{
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5235 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005236 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5237 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005238 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005239
Janos Follath7742fee2019-06-17 12:58:10 +01005240 /* We can't be wanting more output after block 0xff, otherwise
5241 * the capacity check in psa_key_derivation_output_bytes() would have
5242 * prevented this call. It could happen only if the operation
5243 * object was corrupted or if this function is called directly
5244 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 if (tls12_prf->block_number == 0xff) {
5246 return PSA_ERROR_CORRUPTION_DETECTED;
5247 }
Janos Follath7742fee2019-06-17 12:58:10 +01005248
5249 /* We need a new block */
5250 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005251 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005252
5253 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5254 *
5255 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5256 *
5257 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5258 * HMAC_hash(secret, A(2) + seed) +
5259 * HMAC_hash(secret, A(3) + seed) + ...
5260 *
5261 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005262 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005263 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005264 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005265 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005266 * is currently extracted as `output_block` and where i is
5267 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005268 */
5269
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 status = psa_key_derivation_start_hmac(&hmac,
5271 hash_alg,
5272 tls12_prf->secret,
5273 tls12_prf->secret_length);
5274 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005275 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005277
5278 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005280 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5281 * the variable seed and in this instance means it in the context of the
5282 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 status = psa_mac_update(&hmac,
5284 tls12_prf->label,
5285 tls12_prf->label_length);
5286 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005287 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005288 }
5289 status = psa_mac_update(&hmac,
5290 tls12_prf->seed,
5291 tls12_prf->seed_length);
5292 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005293 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 }
5295 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005296 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5298 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005299 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005301 }
5302
Gilles Peskine449bd832023-01-11 14:50:10 +01005303 status = psa_mac_sign_finish(&hmac,
5304 tls12_prf->Ai, hash_length,
5305 &hmac_output_length);
5306 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005307 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 }
5309 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005310 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005312
5313 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005314 status = psa_key_derivation_start_hmac(&hmac,
5315 hash_alg,
5316 tls12_prf->secret,
5317 tls12_prf->secret_length);
5318 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005319 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 }
5321 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_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->label, tls12_prf->label_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->seed, tls12_prf->seed_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_sign_finish(&hmac,
5334 tls12_prf->output_block, hash_length,
5335 &hmac_output_length);
5336 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005337 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005338 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005339
Janos Follath7742fee2019-06-17 12:58:10 +01005340
5341cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 cleanup_status = psa_mac_abort(&hmac);
5343 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005344 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005346
Gilles Peskine449bd832023-01-11 14:50:10 +01005347 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005348}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005349
Janos Follath844eb0e2019-06-19 12:10:49 +01005350static psa_status_t psa_key_derivation_tls12_prf_read(
5351 psa_tls12_prf_key_derivation_t *tls12_prf,
5352 psa_algorithm_t alg,
5353 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005355{
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5357 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005358 psa_status_t status;
5359 uint8_t offset, length;
5360
Gilles Peskine449bd832023-01-11 14:50:10 +01005361 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005362 case PSA_TLS12_PRF_STATE_LABEL_SET:
5363 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5364 break;
5365 case PSA_TLS12_PRF_STATE_OUTPUT:
5366 break;
5367 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005368 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005369 }
5370
Gilles Peskine449bd832023-01-11 14:50:10 +01005371 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005372 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 if (tls12_prf->left_in_block == 0) {
5374 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5375 alg);
5376 if (status != PSA_SUCCESS) {
5377 return status;
5378 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005379
5380 continue;
5381 }
5382
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005384 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005386 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005388
5389 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005391 output += length;
5392 output_length -= length;
5393 tls12_prf->left_in_block -= length;
5394 }
5395
Gilles Peskine449bd832023-01-11 14:50:10 +01005396 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005397}
John Durkop07cc04a2020-11-16 22:08:34 -08005398#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5399 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005400
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005401#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5402static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5403 psa_tls12_ecjpake_to_pms_t *ecjpake,
5404 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005406{
5407 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005408 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005409
Gilles Peskine449bd832023-01-11 14:50:10 +01005410 if (output_length != 32) {
5411 return PSA_ERROR_INVALID_ARGUMENT;
5412 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005413
Gilles Peskine449bd832023-01-11 14:50:10 +01005414 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5415 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5416 &output_size);
5417 if (status != PSA_SUCCESS) {
5418 return status;
5419 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005420
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 if (output_size != output_length) {
5422 return PSA_ERROR_GENERIC_ERROR;
5423 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005424
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005426}
5427#endif
5428
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005429psa_status_t psa_key_derivation_output_bytes(
5430 psa_key_derivation_operation_t *operation,
5431 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005433{
5434 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005436
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005438 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005440 }
5441
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005443 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005444 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005445 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005446 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005447 goto exit;
5448 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005450 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005451 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005452 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5453 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005454 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005455 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005457 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005458 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005459
Przemek Stekiel69c46792022-06-10 12:59:51 +02005460#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005461 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5462 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5463 output, output_length);
5464 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005465#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005466#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5467 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005468 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5469 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5470 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5471 kdf_alg, output,
5472 output_length);
5473 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005474#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5475 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005476#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005478 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5480 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005481#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5482
Gilles Peskineeab56e42018-07-12 17:12:33 +02005483 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005484 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005485 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005486 }
5487
5488exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005490 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005491 * This allows us to differentiate between exhausted operations and
5492 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5493 * operations. */
5494 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005496 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005498 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005500}
5501
David Brown7807bf72021-02-09 16:28:23 -07005502#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005503static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005504{
Gilles Peskine449bd832023-01-11 14:50:10 +01005505 if (data_size >= 8) {
5506 mbedtls_des_key_set_parity(data);
5507 }
5508 if (data_size >= 16) {
5509 mbedtls_des_key_set_parity(data + 8);
5510 }
5511 if (data_size >= 24) {
5512 mbedtls_des_key_set_parity(data + 16);
5513 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005514}
David Brown7807bf72021-02-09 16:28:23 -07005515#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005516
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005517/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005518 * ECC keys on a Weierstrass elliptic curve require the generation
5519 * of a private key which is an integer
5520 * in the range [1, N - 1], where N is the boundary of the private key domain:
5521 * N is the prime p for Diffie-Hellman, or the order of the
5522 * curve’s base point for ECC.
5523 *
5524 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5525 * This function generates the private key using the following process:
5526 *
5527 * 1. Draw a byte string of length ceiling(m/8) bytes.
5528 * 2. If m is not a multiple of 8, set the most significant
5529 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5530 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5531 * 4. If k > N - 2, discard the result and return to step 1.
5532 * 5. Output k + 1 as the private key.
5533 *
5534 * This method allows compliance to NIST standards, specifically the methods titled
5535 * Key-Pair Generation by Testing Candidates in the following publications:
5536 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5537 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5538 * Diffie-Hellman keys.
5539 *
5540 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5541 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5542 *
5543 * Note: Function allocates memory for *data buffer, so given *data should be
5544 * always NULL.
5545 */
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005546#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5547 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005548 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5549 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5550 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005551static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005552 psa_key_slot_t *slot,
5553 size_t bits,
5554 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005555 uint8_t **data
5556 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005557{
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005558#if defined(MBEDTLS_ECP_C)
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005559 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005560 mbedtls_mpi k;
5561 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005562 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005563 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005564
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 mbedtls_mpi_init(&k);
5566 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005567
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005568 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005570 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005572
Gilles Peskine449bd832023-01-11 14:50:10 +01005573 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005574 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005575 goto cleanup;
5576 }
5577
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005578 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005580
Gilles Peskine449bd832023-01-11 14:50:10 +01005581 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005582
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005583 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005584 /* Let m be the bit size of N. */
5585 size_t m = ecp_group.nbits;
5586
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005588
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005589 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005590 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005591
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005592 /* Note: This function is always called with *data == NULL and it
5593 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005594 *data = mbedtls_calloc(1, m_bytes);
5595 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005596 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005597 goto cleanup;
5598 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005599
Gilles Peskine449bd832023-01-11 14:50:10 +01005600 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005601 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005602 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005603 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005605
5606 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005608 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005609 * (8 * ceiling(m/8) - m) bits of the first byte in
5610 * the string to zero.
5611 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005612 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005613 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005614 }
5615
5616 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005617 * big-endian byte string.
5618 */
5619 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005620
5621 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005622 * Result of comparison is returned. When it indicates error
5623 * then this function is called again.
5624 */
5625 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005626 }
5627
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005628 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5630 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005631cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 if (ret != 0) {
5633 status = mbedtls_to_psa_error(ret);
5634 }
5635 if (status != PSA_SUCCESS) {
5636 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005637 *data = NULL;
5638 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 mbedtls_mpi_free(&k);
5640 mbedtls_mpi_free(&diff_N_2);
5641 return status;
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005642#else /* MBEDTLS_ECP_C */
5643 (void) slot;
5644 (void) bits;
5645 (void) operation;
5646 (void) data;
5647 return PSA_ERROR_NOT_SUPPORTED;
5648#endif /* MBEDTLS_ECP_C */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005649}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005650
5651/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5652 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5653 *
5654 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5655 * draw a 32-byte string and process it as specified in
5656 * Elliptic Curves for Security [RFC7748] §5.
5657 *
5658 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5659 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5660 *
5661 * Note: Function allocates memory for *data buffer, so given *data should be
5662 * always NULL.
5663 */
5664
5665static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5666 size_t bits,
5667 psa_key_derivation_operation_t *operation,
5668 uint8_t **data
5669 )
5670{
5671 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005672 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005673
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005675 case 255:
5676 output_length = 32;
5677 break;
5678 case 448:
5679 output_length = 56;
5680 break;
5681 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005682 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005683 break;
5684 }
5685
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005687
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 if (*data == NULL) {
5689 return PSA_ERROR_INSUFFICIENT_MEMORY;
5690 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005691
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005693
Gilles Peskine449bd832023-01-11 14:50:10 +01005694 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005695 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005696 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005697
Gilles Peskine449bd832023-01-11 14:50:10 +01005698 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005699 case 255:
5700 (*data)[0] &= 248;
5701 (*data)[31] &= 127;
5702 (*data)[31] |= 64;
5703 break;
5704 case 448:
5705 (*data)[0] &= 252;
5706 (*data)[55] |= 128;
5707 break;
5708 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005709 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005710 break;
5711 }
5712
5713 return status;
5714}
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005715#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5716 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005717 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5718 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5719 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005720
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005721static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005722 psa_key_slot_t *slot,
5723 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005725{
5726 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005727 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305728 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005729 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005730
Gilles Peskine449bd832023-01-11 14:50:10 +01005731 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5732 return PSA_ERROR_INVALID_ARGUMENT;
5733 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005734
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005735#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5736 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005737 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5738 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5739 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005740 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5741 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5742 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005743 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5745 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005746 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 }
5748 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005749 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005750 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5751 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005752 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005754 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005755 } else
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005756#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5757 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005758 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5759 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5760 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 if (key_type_is_raw_bytes(slot->attr.type)) {
5762 if (bits % 8 != 0) {
5763 return PSA_ERROR_INVALID_ARGUMENT;
5764 }
5765 data = mbedtls_calloc(1, bytes);
5766 if (data == NULL) {
5767 return PSA_ERROR_INSUFFICIENT_MEMORY;
5768 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005769
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 status = psa_key_derivation_output_bytes(operation, data, bytes);
5771 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005772 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 }
David Brown12ca5032021-02-11 11:02:00 -07005774#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5776 psa_des_set_key_parity(data, bytes);
5777 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005778#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 } else {
5780 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005781 }
Ronald Crondd04d422020-11-28 15:14:42 +01005782
Ronald Cronbf33c932020-11-28 18:06:53 +01005783 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005784 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005786 };
5787
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5789 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5790 &storage_size);
5791 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305792 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005793 }
Archanad8a83dc2021-06-14 10:04:16 +05305794 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005795 status = psa_allocate_buffer_to_slot(slot, storage_size);
5796 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305797 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 }
Archanad8a83dc2021-06-14 10:04:16 +05305799
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 status = psa_driver_wrapper_import_key(&attributes,
5801 data, bytes,
5802 slot->key.data,
5803 slot->key.bytes,
5804 &slot->key.bytes, &bits);
5805 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005806 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005808
5809exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005810 mbedtls_free(data);
5811 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005812}
5813
Gilles Peskine449bd832023-01-11 14:50:10 +01005814psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5815 psa_key_derivation_operation_t *operation,
5816 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005817{
5818 psa_status_t status;
5819 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005820 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005821
Ronald Cron81709fc2020-11-14 12:10:32 +01005822 *key = MBEDTLS_SVC_KEY_ID_INIT;
5823
Gilles Peskine0f84d622019-09-12 19:03:13 +02005824 /* Reject any attempt to create a zero-length key so that we don't
5825 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005826 if (psa_get_key_bits(attributes) == 0) {
5827 return PSA_ERROR_INVALID_ARGUMENT;
5828 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005829
Gilles Peskine449bd832023-01-11 14:50:10 +01005830 if (operation->alg == PSA_ALG_NONE) {
5831 return PSA_ERROR_BAD_STATE;
5832 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005833
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 if (!operation->can_output_key) {
5835 return PSA_ERROR_NOT_PERMITTED;
5836 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005837
Gilles Peskine449bd832023-01-11 14:50:10 +01005838 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5839 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005840#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005842 /* Deriving a key in a secure element is not implemented yet. */
5843 status = PSA_ERROR_NOT_SUPPORTED;
5844 }
5845#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005846 if (status == PSA_SUCCESS) {
5847 status = psa_generate_derived_key_internal(slot,
5848 attributes->core.bits,
5849 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005850 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005851 if (status == PSA_SUCCESS) {
5852 status = psa_finish_key_creation(slot, driver, key);
5853 }
5854 if (status != PSA_SUCCESS) {
5855 psa_fail_key_creation(slot, driver);
5856 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005857
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005859}
5860
Gilles Peskineeab56e42018-07-12 17:12:33 +02005861
5862
5863/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005864/* Key derivation */
5865/****************************************************************/
5866
Steven Cooreman932ffb72021-02-15 12:14:32 +01005867#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005868static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005869{
5870#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5872 return 1;
5873 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005874#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005875#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5877 return 1;
5878 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005879#endif
5880#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5882 return 1;
5883 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005884#endif
5885#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5887 return 1;
5888 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005889#endif
5890#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005891 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5892 return 1;
5893 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005894#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005895#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5897 return 1;
5898 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005899#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005900 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005901}
5902
Gilles Peskine449bd832023-01-11 14:50:10 +01005903static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005904{
5905 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 psa_status_t status = psa_hash_setup(&operation, alg);
5907 psa_hash_abort(&operation);
5908 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005909}
5910
Gilles Peskine969c5d62019-01-16 15:53:06 +01005911static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005912 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005914{
Janos Follath5fe19732019-06-20 15:09:30 +01005915 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5916 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005917 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005918
Gilles Peskine969c5d62019-01-16 15:53:06 +01005919 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 if (!is_kdf_alg_supported(kdf_alg)) {
5921 return PSA_ERROR_NOT_SUPPORTED;
5922 }
John Durkop07cc04a2020-11-16 22:08:34 -08005923
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005924 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005925 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5927 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5928 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5929 if (hash_size == 0) {
5930 return PSA_ERROR_NOT_SUPPORTED;
5931 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005932
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005933 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5934 * we might fail later, which is somewhat unfriendly and potentially
5935 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 psa_status_t status = psa_hash_try_support(hash_alg);
5937 if (status != PSA_SUCCESS) {
5938 return status;
5939 }
5940 } else {
5941 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005942 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005943
Gilles Peskine449bd832023-01-11 14:50:10 +01005944 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5945 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
5946 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
5947 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005948 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005949#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005950 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
5952 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005953 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005955#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5956 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005957 operation->capacity = 255 * hash_size;
5958 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005959}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005960
Gilles Peskine449bd832023-01-11 14:50:10 +01005961static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005962{
5963#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005964 if (alg == PSA_ALG_ECDH) {
5965 return PSA_SUCCESS;
5966 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005967#endif
5968 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005969 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005970}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005971
5972static int psa_key_derivation_allows_free_form_secret_input(
5973 psa_algorithm_t kdf_alg)
5974{
5975#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
5976 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5977 return 0;
5978 }
5979#endif
5980 (void) kdf_alg;
5981 return 1;
5982}
John Durkop07cc04a2020-11-16 22:08:34 -08005983#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005984
Gilles Peskine449bd832023-01-11 14:50:10 +01005985psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
5986 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005987{
5988 psa_status_t status;
5989
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 if (operation->alg != 0) {
5991 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005992 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005993
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
5995 return PSA_ERROR_INVALID_ARGUMENT;
5996 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
5997#if defined(AT_LEAST_ONE_BUILTIN_KDF)
5998 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
5999 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6000 status = psa_key_agreement_try_support(ka_alg);
6001 if (status != PSA_SUCCESS) {
6002 return status;
6003 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006004 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6005 return PSA_ERROR_INVALID_ARGUMENT;
6006 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6008#else
6009 return PSA_ERROR_NOT_SUPPORTED;
6010#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6011 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6012#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6013 status = psa_key_derivation_setup_kdf(operation, alg);
6014#else
6015 return PSA_ERROR_NOT_SUPPORTED;
6016#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6017 } else {
6018 return PSA_ERROR_INVALID_ARGUMENT;
6019 }
6020
6021 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006022 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006023 }
6024 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006025}
6026
Przemek Stekiel69c46792022-06-10 12:59:51 +02006027#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006028static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6029 psa_algorithm_t kdf_alg,
6030 psa_key_derivation_step_t step,
6031 const uint8_t *data,
6032 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006033{
Gilles Peskine449bd832023-01-11 14:50:10 +01006034 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006035 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006037 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006038#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006039 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6040 return PSA_ERROR_INVALID_ARGUMENT;
6041 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006042#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 if (hkdf->state != HKDF_STATE_INIT) {
6044 return PSA_ERROR_BAD_STATE;
6045 } else {
6046 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6047 hash_alg,
6048 data, data_length);
6049 if (status != PSA_SUCCESS) {
6050 return status;
6051 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006052 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006054 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006055 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006056#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006058 /* We shouldn't be in different state as HKDF_EXPAND only allows
6059 * two inputs: SECRET (this case) and INFO which does not modify
6060 * the state. It could happen only if the hkdf
6061 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 if (hkdf->state != HKDF_STATE_INIT) {
6063 return PSA_ERROR_BAD_STATE;
6064 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006065
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006066 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6068 return PSA_ERROR_INVALID_ARGUMENT;
6069 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006070
Gilles Peskine449bd832023-01-11 14:50:10 +01006071 memcpy(hkdf->prk, data, data_length);
6072 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006073#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006074 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006075 /* HKDF: If no salt was provided, use an empty salt.
6076 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006078#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6080 return PSA_ERROR_BAD_STATE;
6081 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006082#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6084 hash_alg,
6085 NULL, 0);
6086 if (status != PSA_SUCCESS) {
6087 return status;
6088 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006089 hkdf->state = HKDF_STATE_STARTED;
6090 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 if (hkdf->state != HKDF_STATE_STARTED) {
6092 return PSA_ERROR_BAD_STATE;
6093 }
6094 status = psa_mac_update(&hkdf->hmac,
6095 data, data_length);
6096 if (status != PSA_SUCCESS) {
6097 return status;
6098 }
6099 status = psa_mac_sign_finish(&hkdf->hmac,
6100 hkdf->prk,
6101 sizeof(hkdf->prk),
6102 &data_length);
6103 if (status != PSA_SUCCESS) {
6104 return status;
6105 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006106 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006107
Gilles Peskine2b522db2019-04-12 15:11:49 +02006108 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006109 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006110#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006112 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006114 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006116#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006117 {
6118 /* Block 0 is empty, and the next block will be
6119 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006121 }
6122
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006124 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006125#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6127 return PSA_ERROR_INVALID_ARGUMENT;
6128 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006129#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006130#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006131 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6132 hkdf->state == HKDF_STATE_INIT) {
6133 return PSA_ERROR_BAD_STATE;
6134 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006135#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 if (hkdf->state == HKDF_STATE_OUTPUT) {
6137 return PSA_ERROR_BAD_STATE;
6138 }
6139 if (hkdf->info_set) {
6140 return PSA_ERROR_BAD_STATE;
6141 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006142 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 if (data_length != 0) {
6144 hkdf->info = mbedtls_calloc(1, data_length);
6145 if (hkdf->info == NULL) {
6146 return PSA_ERROR_INSUFFICIENT_MEMORY;
6147 }
6148 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006149 }
6150 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006152 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006154 }
6155}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006156#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006157
John Durkop07cc04a2020-11-16 22:08:34 -08006158#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6159 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006160static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6161 const uint8_t *data,
6162 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006163{
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6165 return PSA_ERROR_BAD_STATE;
6166 }
Janos Follathf08e2652019-06-13 09:05:41 +01006167
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 if (data_length != 0) {
6169 prf->seed = mbedtls_calloc(1, data_length);
6170 if (prf->seed == NULL) {
6171 return PSA_ERROR_INSUFFICIENT_MEMORY;
6172 }
Janos Follathf08e2652019-06-13 09:05:41 +01006173
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006175 prf->seed_length = data_length;
6176 }
Janos Follathf08e2652019-06-13 09:05:41 +01006177
Gilles Peskine43f958b2020-12-13 14:55:14 +01006178 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006179
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006181}
6182
Gilles Peskine449bd832023-01-11 14:50:10 +01006183static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6184 const uint8_t *data,
6185 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006186{
Gilles Peskine449bd832023-01-11 14:50:10 +01006187 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6188 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6189 return PSA_ERROR_BAD_STATE;
6190 }
Janos Follath81550542019-06-13 14:26:34 +01006191
Gilles Peskine449bd832023-01-11 14:50:10 +01006192 if (data_length != 0) {
6193 prf->secret = mbedtls_calloc(1, data_length);
6194 if (prf->secret == NULL) {
6195 return PSA_ERROR_INSUFFICIENT_MEMORY;
6196 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006197
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006199 prf->secret_length = data_length;
6200 }
Janos Follath81550542019-06-13 14:26:34 +01006201
Gilles Peskine43f958b2020-12-13 14:55:14 +01006202 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006203
Gilles Peskine449bd832023-01-11 14:50:10 +01006204 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006205}
6206
Gilles Peskine449bd832023-01-11 14:50:10 +01006207static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6208 const uint8_t *data,
6209 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006210{
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6212 return PSA_ERROR_BAD_STATE;
6213 }
Janos Follath63028dd2019-06-13 09:15:47 +01006214
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 if (data_length != 0) {
6216 prf->label = mbedtls_calloc(1, data_length);
6217 if (prf->label == NULL) {
6218 return PSA_ERROR_INSUFFICIENT_MEMORY;
6219 }
Janos Follath63028dd2019-06-13 09:15:47 +01006220
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006222 prf->label_length = data_length;
6223 }
Janos Follath63028dd2019-06-13 09:15:47 +01006224
Gilles Peskine43f958b2020-12-13 14:55:14 +01006225 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006226
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006228}
6229
Gilles Peskine449bd832023-01-11 14:50:10 +01006230static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6231 psa_key_derivation_step_t step,
6232 const uint8_t *data,
6233 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006234{
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006236 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006238 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006240 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006242 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006243 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006244 }
6245}
John Durkop07cc04a2020-11-16 22:08:34 -08006246#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6247 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6248
6249#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6250static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6251 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006252 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006254{
6255 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6257 4 + data_length + prf->other_secret_length :
6258 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6261 return PSA_ERROR_INVALID_ARGUMENT;
6262 }
John Durkop07cc04a2020-11-16 22:08:34 -08006263
Gilles Peskine449bd832023-01-11 14:50:10 +01006264 uint8_t *pms = mbedtls_calloc(1, pms_len);
6265 if (pms == NULL) {
6266 return PSA_ERROR_INSUFFICIENT_MEMORY;
6267 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006268 uint8_t *cur = pms;
6269
6270 /* pure-PSK:
6271 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006272 *
6273 * The premaster secret is formed as follows: if the PSK is N octets
6274 * long, concatenate a uint16 with the value N, N zero octets, a second
6275 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006276 *
6277 * mixed-PSK:
6278 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6279 * follows: concatenate a uint16 with the length of the other secret,
6280 * the other secret itself, uint16 with the length of PSK, and the
6281 * PSK itself.
6282 * For details please check:
6283 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6284 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6285 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006286 */
6287
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6289 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6290 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6291 if (prf->other_secret_length != 0) {
6292 memcpy(cur, prf->other_secret, prf->other_secret_length);
6293 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006294 cur += prf->other_secret_length;
6295 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006296 } else {
6297 *cur++ = MBEDTLS_BYTE_1(data_length);
6298 *cur++ = MBEDTLS_BYTE_0(data_length);
6299 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006300 cur += data_length;
6301 }
6302
Gilles Peskine449bd832023-01-11 14:50:10 +01006303 *cur++ = MBEDTLS_BYTE_1(data_length);
6304 *cur++ = MBEDTLS_BYTE_0(data_length);
6305 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006306 cur += data_length;
6307
Gilles Peskine449bd832023-01-11 14:50:10 +01006308 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006309
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 mbedtls_platform_zeroize(pms, pms_len);
6311 mbedtls_free(pms);
6312 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006313}
Janos Follath6660f0e2019-06-17 08:44:03 +01006314
Przemek Stekiele47201b2022-04-19 13:53:28 +02006315static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6316 psa_tls12_prf_key_derivation_t *prf,
6317 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006319{
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6321 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006322 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006323
6324 if (data_length != 0) {
6325 prf->other_secret = mbedtls_calloc(1, data_length);
6326 if (prf->other_secret == NULL) {
6327 return PSA_ERROR_INSUFFICIENT_MEMORY;
6328 }
6329
6330 memcpy(prf->other_secret, data, data_length);
6331 prf->other_secret_length = data_length;
6332 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006333 prf->other_secret_length = 0;
6334 }
6335
6336 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6337
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006339}
6340
Janos Follath6660f0e2019-06-17 08:44:03 +01006341static psa_status_t psa_tls12_prf_psk_to_ms_input(
6342 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006343 psa_key_derivation_step_t step,
6344 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006346{
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006348 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 return psa_tls12_prf_psk_to_ms_set_key(prf,
6350 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006351 break;
6352 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6354 data,
6355 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006356 break;
6357 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006358 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006359 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006360
Przemek Stekiele47201b2022-04-19 13:53:28 +02006361 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006362}
John Durkop07cc04a2020-11-16 22:08:34 -08006363#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006364
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006365#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6366static psa_status_t psa_tls12_ecjpake_to_pms_input(
6367 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006368 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006369 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006371{
Gilles Peskine449bd832023-01-11 14:50:10 +01006372 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6373 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6374 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006375 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006376
6377 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006378 if (data[0] != 0x04) {
6379 return PSA_ERROR_INVALID_ARGUMENT;
6380 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006381
6382 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006384
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006386}
6387#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006388/** Check whether the given key type is acceptable for the given
6389 * input step of a key derivation.
6390 *
6391 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6392 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6393 * Both secret and non-secret inputs can alternatively have the type
6394 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6395 * that the input was passed as a buffer rather than via a key object.
6396 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006397static int psa_key_derivation_check_input_type(
6398 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006400{
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006402 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 if (key_type == PSA_KEY_TYPE_DERIVE) {
6404 return PSA_SUCCESS;
6405 }
6406 if (key_type == PSA_KEY_TYPE_NONE) {
6407 return PSA_SUCCESS;
6408 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006409 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006410 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 if (key_type == PSA_KEY_TYPE_DERIVE) {
6412 return PSA_SUCCESS;
6413 }
6414 if (key_type == PSA_KEY_TYPE_NONE) {
6415 return PSA_SUCCESS;
6416 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006417 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006418 case PSA_KEY_DERIVATION_INPUT_LABEL:
6419 case PSA_KEY_DERIVATION_INPUT_SALT:
6420 case PSA_KEY_DERIVATION_INPUT_INFO:
6421 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6423 return PSA_SUCCESS;
6424 }
6425 if (key_type == PSA_KEY_TYPE_NONE) {
6426 return PSA_SUCCESS;
6427 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006428 break;
6429 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006431}
6432
Janos Follathb80a94e2019-06-12 15:54:46 +01006433static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006434 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006435 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006436 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006437 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006439{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006440 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006442
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 status = psa_key_derivation_check_input_type(step, key_type);
6444 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006445 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006447
Przemek Stekiel69c46792022-06-10 12:59:51 +02006448#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6450 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6451 step, data, data_length);
6452 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006453#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006454#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006455 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6456 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6457 step, data, data_length);
6458 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006459#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6460#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006461 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6462 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6463 step, data, data_length);
6464 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006465#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006466#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006467 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006468 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6470 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006471#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006472 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006473 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006474 (void) data;
6475 (void) data_length;
6476 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006478 }
6479
Gilles Peskine224b0d62019-09-23 18:13:17 +02006480exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006481 if (status != PSA_SUCCESS) {
6482 psa_key_derivation_abort(operation);
6483 }
6484 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006485}
6486
Janos Follath51f4a0f2019-06-14 11:35:55 +01006487psa_status_t psa_key_derivation_input_bytes(
6488 psa_key_derivation_operation_t *operation,
6489 psa_key_derivation_step_t step,
6490 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006492{
Gilles Peskine449bd832023-01-11 14:50:10 +01006493 return psa_key_derivation_input_internal(operation, step,
6494 PSA_KEY_TYPE_NONE,
6495 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006496}
6497
Janos Follath51f4a0f2019-06-14 11:35:55 +01006498psa_status_t psa_key_derivation_input_key(
6499 psa_key_derivation_operation_t *operation,
6500 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006501 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006502{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006503 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006504 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006505 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006506
Ronald Cron5c522922020-11-14 16:35:34 +01006507 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6509 if (status != PSA_SUCCESS) {
6510 psa_key_derivation_abort(operation);
6511 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006512 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006513
6514 /* Passing a key object as a SECRET input unlocks the permission
6515 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006516 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006517 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006518 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006519
Gilles Peskine449bd832023-01-11 14:50:10 +01006520 status = psa_key_derivation_input_internal(operation,
6521 step, slot->attr.type,
6522 slot->key.data,
6523 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006524
Gilles Peskine449bd832023-01-11 14:50:10 +01006525 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006526
Gilles Peskine449bd832023-01-11 14:50:10 +01006527 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006528}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006529
6530
6531
6532/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006533/* Key agreement */
6534/****************************************************************/
6535
Gilles Peskine449bd832023-01-11 14:50:10 +01006536psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6537 const uint8_t *key_buffer,
6538 size_t key_buffer_size,
6539 psa_algorithm_t alg,
6540 const uint8_t *peer_key,
6541 size_t peer_key_length,
6542 uint8_t *shared_secret,
6543 size_t shared_secret_size,
6544 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006545{
Gilles Peskine449bd832023-01-11 14:50:10 +01006546 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006547#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006548 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006549 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6550 key_buffer_size, alg,
6551 peer_key, peer_key_length,
6552 shared_secret,
6553 shared_secret_size,
6554 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006555#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006556 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006557 (void) attributes;
6558 (void) key_buffer;
6559 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006560 (void) peer_key;
6561 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006562 (void) shared_secret;
6563 (void) shared_secret_size;
6564 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006565 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006566 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006567}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006568
Aditya Deshpande17845b82022-10-13 17:21:01 +01006569/** Internal function for raw key agreement
6570 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006571 * to the driver's implementation if a driver is present.
6572 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006573 * (psa_key_agreement_raw_builtin).
6574 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006575static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6576 psa_key_slot_t *private_key,
6577 const uint8_t *peer_key,
6578 size_t peer_key_length,
6579 uint8_t *shared_secret,
6580 size_t shared_secret_size,
6581 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006582{
Gilles Peskine449bd832023-01-11 14:50:10 +01006583 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6584 return PSA_ERROR_NOT_SUPPORTED;
6585 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006586
6587 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006589 };
6590
Gilles Peskine449bd832023-01-11 14:50:10 +01006591 return psa_driver_wrapper_key_agreement(&attributes,
6592 private_key->key.data,
6593 private_key->key.bytes, alg,
6594 peer_key, peer_key_length,
6595 shared_secret,
6596 shared_secret_size,
6597 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006598}
6599
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006600/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006601 * to potentially free embedded data structures and wipe confidential data.
6602 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006603static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6604 psa_key_derivation_step_t step,
6605 psa_key_slot_t *private_key,
6606 const uint8_t *peer_key,
6607 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006608{
6609 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006610 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006611 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006612 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006613
6614 /* Step 1: run the secret agreement algorithm to generate the shared
6615 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006616 status = psa_key_agreement_raw_internal(ka_alg,
6617 private_key,
6618 peer_key, peer_key_length,
6619 shared_secret,
6620 sizeof(shared_secret),
6621 &shared_secret_length);
6622 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006623 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006624 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006625
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006626 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006627 * the shared secret. A shared secret is permitted wherever a key
6628 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006629 status = psa_key_derivation_input_internal(operation, step,
6630 PSA_KEY_TYPE_DERIVE,
6631 shared_secret,
6632 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006633exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006634 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6635 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006636}
6637
Gilles Peskine449bd832023-01-11 14:50:10 +01006638psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6639 psa_key_derivation_step_t step,
6640 mbedtls_svc_key_id_t private_key,
6641 const uint8_t *peer_key,
6642 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006643{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006644 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006645 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006646 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006647
Gilles Peskine449bd832023-01-11 14:50:10 +01006648 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6649 return PSA_ERROR_INVALID_ARGUMENT;
6650 }
Ronald Cron5c522922020-11-14 16:35:34 +01006651 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006652 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6653 if (status != PSA_SUCCESS) {
6654 return status;
6655 }
6656 status = psa_key_agreement_internal(operation, step,
6657 slot,
6658 peer_key, peer_key_length);
6659 if (status != PSA_SUCCESS) {
6660 psa_key_derivation_abort(operation);
6661 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006662 /* If a private key has been added as SECRET, we allow the derived
6663 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006664 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006665 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006666 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006667 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006668
Gilles Peskine449bd832023-01-11 14:50:10 +01006669 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006670
Gilles Peskine449bd832023-01-11 14:50:10 +01006671 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006672}
6673
Gilles Peskine449bd832023-01-11 14:50:10 +01006674psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6675 mbedtls_svc_key_id_t private_key,
6676 const uint8_t *peer_key,
6677 size_t peer_key_length,
6678 uint8_t *output,
6679 size_t output_size,
6680 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006681{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006682 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006683 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006684 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006685
Gilles Peskine449bd832023-01-11 14:50:10 +01006686 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006687 status = PSA_ERROR_INVALID_ARGUMENT;
6688 goto exit;
6689 }
Ronald Cron5c522922020-11-14 16:35:34 +01006690 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6692 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006693 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006694 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006695
Gilles Peskine3e561302022-04-14 00:17:15 +02006696 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6697 * for the output size. The PSA specification only guarantees that this
6698 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6699 * but it might be nice to allow smaller buffers if the output fits.
6700 * At the time of writing this comment, with only ECDH implemented,
6701 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6702 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6703 * be exact for it as well. */
6704 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006705 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6706 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006707 status = PSA_ERROR_BUFFER_TOO_SMALL;
6708 goto exit;
6709 }
6710
Gilles Peskine449bd832023-01-11 14:50:10 +01006711 status = psa_key_agreement_raw_internal(alg, slot,
6712 peer_key, peer_key_length,
6713 output, output_size,
6714 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006715
6716exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006717 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006718 /* If an error happens and is not handled properly, the output
6719 * may be used as a key to protect sensitive data. Arrange for such
6720 * a key to be random, which is likely to result in decryption or
6721 * verification errors. This is better than filling the buffer with
6722 * some constant data such as zeros, which would result in the data
6723 * being protected with a reproducible, easily knowable key.
6724 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006725 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006726 *output_length = output_size;
6727 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006728
Gilles Peskine449bd832023-01-11 14:50:10 +01006729 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006730
Gilles Peskine449bd832023-01-11 14:50:10 +01006731 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006732}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006733
6734
Gilles Peskine30524eb2020-11-13 17:02:26 +01006735
Gilles Peskine86a440b2018-11-12 18:39:40 +01006736/****************************************************************/
6737/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006738/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006739
Gilles Peskine30524eb2020-11-13 17:02:26 +01006740/** Initialize the PSA random generator.
6741 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006742static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006743{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006744#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006745 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006746#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6747
Gilles Peskine30524eb2020-11-13 17:02:26 +01006748 /* Set default configuration if
6749 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006750 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006751 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006752 }
6753 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006754 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006755 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006756
Gilles Peskine449bd832023-01-11 14:50:10 +01006757 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006758#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6759 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6760 /* The PSA entropy injection feature depends on using NV seed as an entropy
6761 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006762 mbedtls_entropy_add_source(&rng->entropy,
6763 mbedtls_nv_seed_poll, NULL,
6764 MBEDTLS_ENTROPY_BLOCK_SIZE,
6765 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006766#endif
6767
Gilles Peskine449bd832023-01-11 14:50:10 +01006768 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006769#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006770}
6771
6772/** Deinitialize the PSA random generator.
6773 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006774static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006775{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006776#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006777 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006778#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01006779 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
6780 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006781#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006782}
6783
6784/** Seed the PSA random generator.
6785 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006786static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006787{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006788#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6789 /* Do nothing: the external RNG seeds itself. */
6790 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006791 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006792#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006793 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01006794 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
6795 drbg_seed, sizeof(drbg_seed) - 1);
6796 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006797#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006798}
6799
Gilles Peskine449bd832023-01-11 14:50:10 +01006800psa_status_t psa_generate_random(uint8_t *output,
6801 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006802{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006803 GUARD_MODULE_INITIALIZED;
6804
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006805#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6806
6807 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006808 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6809 output, output_size,
6810 &output_length);
6811 if (status != PSA_SUCCESS) {
6812 return status;
6813 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006814 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006815 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006816 if (output_length != output_size) {
6817 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6818 }
6819 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006820
6821#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6822
Gilles Peskine449bd832023-01-11 14:50:10 +01006823 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006824 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006825 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6826 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6827 output_size);
6828 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6829 output, request_size);
6830 if (ret != 0) {
6831 return mbedtls_to_psa_error(ret);
6832 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006833 output_size -= request_size;
6834 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006835 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006836 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006837#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006838}
6839
Gilles Peskine8814fc42020-12-14 15:33:44 +01006840/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006841 *
6842 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6843 * `psa_generate_random(...)`. The state parameter is ignored since the
6844 * PSA API doesn't support passing an explicit state.
6845 *
6846 * In the non-external case, psa_generate_random() calls an
6847 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6848 * and semantics as mbedtls_psa_get_random(). As an optimization,
6849 * instead of doing this back-and-forth between the PSA API and the
6850 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6851 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006852 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006853#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6854int mbedtls_psa_get_random(void *p_rng,
6855 unsigned char *output,
6856 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01006857{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006858 /* This function takes a pointer to the RNG state because that's what
6859 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6860 * its own state internally and doesn't let the caller access that state.
6861 * So we just ignore the state parameter, and in practice we'll pass
6862 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006863 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006864 psa_status_t status = psa_generate_random(output, output_size);
6865 if (status == PSA_SUCCESS) {
6866 return 0;
6867 } else {
6868 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
6869 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01006870}
6871#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6872
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006873#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006874#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006875
Gilles Peskine449bd832023-01-11 14:50:10 +01006876psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
6877 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006878{
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 if (global_data.initialized) {
6880 return PSA_ERROR_NOT_PERMITTED;
6881 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006882
Gilles Peskine449bd832023-01-11 14:50:10 +01006883 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
6884 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
6885 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
6886 return PSA_ERROR_INVALID_ARGUMENT;
6887 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006888
Gilles Peskine449bd832023-01-11 14:50:10 +01006889 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006890}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006891#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006892
Ronald Cron3772afe2021-02-08 16:10:05 +01006893/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006894 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006895 * \param type The key type
6896 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006897 *
6898 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006899 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006900 * \retval #PSA_ERROR_INVALID_ARGUMENT
6901 * The size in bits of the key is not valid.
6902 * \retval #PSA_ERROR_NOT_SUPPORTED
6903 * The type and/or the size in bits of the key or the combination of
6904 * the two is not supported.
6905 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006906static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006907 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006908{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006909 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006910
Gilles Peskine449bd832023-01-11 14:50:10 +01006911 if (key_type_is_raw_bytes(type)) {
6912 status = psa_validate_unstructured_key_bit_size(type, bits);
6913 if (status != PSA_SUCCESS) {
6914 return status;
6915 }
6916 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006917#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006918 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6919 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
6920 return PSA_ERROR_NOT_SUPPORTED;
6921 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006922
Ronald Cron01b2aba2020-10-05 09:42:02 +02006923 /* Accept only byte-aligned keys, for the same reasons as
6924 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006925 if (bits % 8 != 0) {
6926 return PSA_ERROR_NOT_SUPPORTED;
6927 }
6928 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006929#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006930
Ronald Cron5c4d3862020-12-07 11:07:24 +01006931#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006932 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01006933 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006934 return PSA_SUCCESS;
6935 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006936#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006937 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006938 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006939 }
6940
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006942}
6943
Ronald Cron55ed0592020-10-05 10:30:40 +02006944psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006945 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01006946 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006947{
6948 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006949 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006950
Gilles Peskine449bd832023-01-11 14:50:10 +01006951 if ((attributes->domain_parameters == NULL) &&
6952 (attributes->domain_parameters_size != 0)) {
6953 return PSA_ERROR_INVALID_ARGUMENT;
6954 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02006955
Gilles Peskine449bd832023-01-11 14:50:10 +01006956 if (key_type_is_raw_bytes(type)) {
6957 status = psa_generate_random(key_buffer, key_buffer_size);
6958 if (status != PSA_SUCCESS) {
6959 return status;
6960 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006961
David Brown12ca5032021-02-11 11:02:00 -07006962#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006963 if (type == PSA_KEY_TYPE_DES) {
6964 psa_des_set_key_parity(key_buffer, key_buffer_size);
6965 }
David Brown8107e312021-02-12 08:08:46 -07006966#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01006967 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01006968
Jaeden Amero424fa932021-05-14 08:34:32 +01006969#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
6970 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01006971 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
6972 return mbedtls_psa_rsa_generate_key(attributes,
6973 key_buffer,
6974 key_buffer_size,
6975 key_buffer_length);
6976 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01006977#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
6978 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006979
John Durkop9814fa22020-11-04 12:28:15 -08006980#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006981 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6982 return mbedtls_psa_ecp_generate_key(attributes,
6983 key_buffer,
6984 key_buffer_size,
6985 key_buffer_length);
6986 } else
John Durkop9814fa22020-11-04 12:28:15 -08006987#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006988 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006989 (void) key_buffer_length;
6990 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02006991 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006992
Gilles Peskine449bd832023-01-11 14:50:10 +01006993 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006994}
Darryl Green0c6575a2018-11-07 16:05:30 +00006995
Gilles Peskine449bd832023-01-11 14:50:10 +01006996psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
6997 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006998{
6999 psa_status_t status;
7000 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007001 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007002 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007003
Ronald Cron81709fc2020-11-14 12:10:32 +01007004 *key = MBEDTLS_SVC_KEY_ID_INIT;
7005
Gilles Peskine0f84d622019-09-12 19:03:13 +02007006 /* Reject any attempt to create a zero-length key so that we don't
7007 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007008 if (psa_get_key_bits(attributes) == 0) {
7009 return PSA_ERROR_INVALID_ARGUMENT;
7010 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007011
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007012 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007013 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7014 return PSA_ERROR_INVALID_ARGUMENT;
7015 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007016
Gilles Peskine449bd832023-01-11 14:50:10 +01007017 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7018 &slot, &driver);
7019 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007020 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007021 }
Gilles Peskine11792082019-08-06 18:36:36 +02007022
Ronald Cron977c2472020-10-13 08:32:21 +02007023 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307024 * storage ( thus not in the case of generating a key in a secure element
7025 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7026 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007027 if (slot->key.data == NULL) {
7028 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7029 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007030 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007031 attributes->core.type, attributes->core.bits);
7032 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007033 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007035
7036 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007037 attributes->core.type,
7038 attributes->core.bits);
7039 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007040 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007041 attributes, &key_buffer_size);
7042 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007043 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007044 }
Ronald Cron977c2472020-10-13 08:32:21 +02007045 }
Ronald Cron977c2472020-10-13 08:32:21 +02007046
Gilles Peskine449bd832023-01-11 14:50:10 +01007047 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7048 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007049 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007050 }
Ronald Cron977c2472020-10-13 08:32:21 +02007051 }
7052
Gilles Peskine449bd832023-01-11 14:50:10 +01007053 status = psa_driver_wrapper_generate_key(attributes,
7054 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007055
Gilles Peskine449bd832023-01-11 14:50:10 +01007056 if (status != PSA_SUCCESS) {
7057 psa_remove_key_data_from_memory(slot);
7058 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007059
Gilles Peskine11792082019-08-06 18:36:36 +02007060exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007061 if (status == PSA_SUCCESS) {
7062 status = psa_finish_key_creation(slot, driver, key);
7063 }
7064 if (status != PSA_SUCCESS) {
7065 psa_fail_key_creation(slot, driver);
7066 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007067
Gilles Peskine449bd832023-01-11 14:50:10 +01007068 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007069}
7070
Gilles Peskinee59236f2018-01-27 23:32:46 +01007071/****************************************************************/
7072/* Module setup */
7073/****************************************************************/
7074
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007075#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007076psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007077 void (* entropy_init)(mbedtls_entropy_context *ctx),
7078 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007079{
Gilles Peskine449bd832023-01-11 14:50:10 +01007080 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7081 return PSA_ERROR_BAD_STATE;
7082 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007083 global_data.rng.entropy_init = entropy_init;
7084 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007085 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007086}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007087#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007088
Gilles Peskine449bd832023-01-11 14:50:10 +01007089void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007090{
Gilles Peskine449bd832023-01-11 14:50:10 +01007091 psa_wipe_all_key_slots();
7092 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7093 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007094 }
7095 /* Wipe all remaining data, including configuration.
7096 * In particular, this sets all state indicator to the value
7097 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007099
7100 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007101 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007102}
7103
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007104#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7105/** Recover a transaction that was interrupted by a power failure.
7106 *
7107 * This function is called during initialization, before psa_crypto_init()
7108 * returns. If this function returns a failure status, the initialization
7109 * fails.
7110 */
7111static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007112 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007113{
Gilles Peskine449bd832023-01-11 14:50:10 +01007114 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007115 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7116 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007117 /* TODO - fall through to the failure case until this
7118 * is implemented.
7119 * https://github.com/ARMmbed/mbed-crypto/issues/218
7120 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007121 default:
7122 /* We found an unsupported transaction in the storage.
7123 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007124 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007125 }
7126}
7127#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7128
Gilles Peskine449bd832023-01-11 14:50:10 +01007129psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007130{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007131 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007132
Gilles Peskinec6b69072018-11-20 21:42:52 +01007133 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007134 if (global_data.initialized != 0) {
7135 return PSA_SUCCESS;
7136 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007137
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007138 /* Init drivers */
7139 status = psa_driver_wrapper_init();
7140 if (status != PSA_SUCCESS) {
7141 goto exit;
7142 }
7143 global_data.drivers_initialized = 1;
7144
Gilles Peskine30524eb2020-11-13 17:02:26 +01007145 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007146 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007147 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007148 status = mbedtls_psa_random_seed(&global_data.rng);
7149 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007150 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007151 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007152 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007153
Gilles Peskine449bd832023-01-11 14:50:10 +01007154 status = psa_initialize_key_slots();
7155 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007156 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007157 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007158
Gilles Peskine4b734222019-07-24 15:56:31 +02007159#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007160 status = psa_crypto_load_transaction();
7161 if (status == PSA_SUCCESS) {
7162 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7163 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007164 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007165 }
7166 status = psa_crypto_stop_transaction();
7167 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007168 /* There's no transaction to complete. It's all good. */
7169 status = PSA_SUCCESS;
7170 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007171#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007172
Gilles Peskinec6b69072018-11-20 21:42:52 +01007173 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007174 global_data.initialized = 1;
7175
7176exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007177 if (status != PSA_SUCCESS) {
7178 mbedtls_psa_crypto_free();
7179 }
7180 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007181}
7182
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007183psa_status_t psa_crypto_driver_pake_get_password_len(
7184 const psa_crypto_driver_pake_inputs_t *inputs,
7185 size_t *password_len)
7186{
7187 if (inputs->password_len == 0) {
7188 return PSA_ERROR_BAD_STATE;
7189 }
7190
7191 *password_len = inputs->password_len;
7192
7193 return PSA_SUCCESS;
7194}
7195
7196psa_status_t psa_crypto_driver_pake_get_password(
7197 const psa_crypto_driver_pake_inputs_t *inputs,
7198 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7199{
7200 if (inputs->password_len == 0) {
7201 return PSA_ERROR_BAD_STATE;
7202 }
7203
7204 if (buffer_size < inputs->password_len) {
7205 return PSA_ERROR_BUFFER_TOO_SMALL;
7206 }
7207
7208 memcpy(buffer, inputs->password, inputs->password_len);
7209 *buffer_length = inputs->password_len;
7210
7211 return PSA_SUCCESS;
7212}
7213
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007214psa_status_t psa_crypto_driver_pake_get_user_len(
7215 const psa_crypto_driver_pake_inputs_t *inputs,
7216 size_t *user_len)
7217{
7218 if (inputs->user_len == 0) {
7219 return PSA_ERROR_BAD_STATE;
7220 }
7221
7222 *user_len = inputs->user_len;
7223
7224 return PSA_SUCCESS;
7225}
7226
7227psa_status_t psa_crypto_driver_pake_get_user(
7228 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007229 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007230{
7231 if (inputs->user_len == 0) {
7232 return PSA_ERROR_BAD_STATE;
7233 }
7234
Przemek Stekielc0e62502023-03-14 11:49:36 +01007235 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007236 return PSA_ERROR_BUFFER_TOO_SMALL;
7237 }
7238
Przemek Stekielc0e62502023-03-14 11:49:36 +01007239 memcpy(user_id, inputs->user, inputs->user_len);
7240 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007241
7242 return PSA_SUCCESS;
7243}
7244
7245psa_status_t psa_crypto_driver_pake_get_peer_len(
7246 const psa_crypto_driver_pake_inputs_t *inputs,
7247 size_t *peer_len)
7248{
7249 if (inputs->peer_len == 0) {
7250 return PSA_ERROR_BAD_STATE;
7251 }
7252
7253 *peer_len = inputs->peer_len;
7254
7255 return PSA_SUCCESS;
7256}
7257
7258psa_status_t psa_crypto_driver_pake_get_peer(
7259 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007260 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007261{
7262 if (inputs->peer_len == 0) {
7263 return PSA_ERROR_BAD_STATE;
7264 }
7265
Przemek Stekielc0e62502023-03-14 11:49:36 +01007266 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007267 return PSA_ERROR_BUFFER_TOO_SMALL;
7268 }
7269
Przemek Stekielc0e62502023-03-14 11:49:36 +01007270 memcpy(peer_id, inputs->peer, inputs->peer_len);
7271 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007272
7273 return PSA_SUCCESS;
7274}
7275
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007276psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7277 const psa_crypto_driver_pake_inputs_t *inputs,
7278 psa_pake_cipher_suite_t *cipher_suite)
7279{
7280 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7281 return PSA_ERROR_BAD_STATE;
7282 }
7283
7284 *cipher_suite = inputs->cipher_suite;
7285
7286 return PSA_SUCCESS;
7287}
7288
Neil Armstronga7d08c32022-06-01 18:21:20 +02007289psa_status_t psa_pake_setup(
7290 psa_pake_operation_t *operation,
7291 const psa_pake_cipher_suite_t *cipher_suite)
7292{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007293 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007294
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007295 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007296 status = PSA_ERROR_BAD_STATE;
7297 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007298 }
7299
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007300 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007301 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007302 status = PSA_ERROR_INVALID_ARGUMENT;
7303 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007304 }
7305
Przemek Stekiel51eac532022-12-07 11:04:51 +01007306 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7307
Przemek Stekiele12ed362022-12-21 12:54:46 +01007308 operation->alg = cipher_suite->algorithm;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007309 operation->data.inputs.cipher_suite = *cipher_suite;
7310
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007311#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007312 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007313 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007314 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007315
Przemek Stekiele12ed362022-12-21 12:54:46 +01007316 computation_stage->state = PSA_PAKE_STATE_SETUP;
7317 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7318 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7319 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007320 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007321#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007322 {
7323 status = PSA_ERROR_NOT_SUPPORTED;
7324 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007325 }
7326
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007327 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7328
Przemek Stekiel51eac532022-12-07 11:04:51 +01007329 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007330exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007331 psa_pake_abort(operation);
7332 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007333}
7334
7335psa_status_t psa_pake_set_password_key(
7336 psa_pake_operation_t *operation,
7337 mbedtls_svc_key_id_t password)
7338{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007339 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007340 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7341 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007342
Przemek Stekiel51eac532022-12-07 11:04:51 +01007343 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007344 status = PSA_ERROR_BAD_STATE;
7345 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007346 }
7347
Przemek Stekiel6c764412022-11-22 14:05:12 +01007348 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7349 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007350 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007351 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007352 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007353 }
7354
Przemek Stekiel6c764412022-11-22 14:05:12 +01007355 psa_key_attributes_t attributes = {
7356 .core = slot->attr
7357 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007358
Przemek Stekiel51eac532022-12-07 11:04:51 +01007359 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007360
Przemek Stekiel51eac532022-12-07 11:04:51 +01007361 if (type != PSA_KEY_TYPE_PASSWORD &&
7362 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7363 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007364 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007365 }
7366
Przemek Stekiel51eac532022-12-07 11:04:51 +01007367 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7368 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007369 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007370 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007371 }
7372
7373 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7374 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007375 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007376exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007377 if (status != PSA_SUCCESS) {
7378 psa_pake_abort(operation);
7379 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007380 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007381 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007382}
7383
7384psa_status_t psa_pake_set_user(
7385 psa_pake_operation_t *operation,
7386 const uint8_t *user_id,
7387 size_t user_id_len)
7388{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007389 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007390
7391 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007392 status = PSA_ERROR_BAD_STATE;
7393 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007394 }
7395
Przemek Stekiel51eac532022-12-07 11:04:51 +01007396 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007397 status = PSA_ERROR_INVALID_ARGUMENT;
7398 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007399 }
7400
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007401 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007402 status = PSA_ERROR_BAD_STATE;
7403 goto exit;
7404 }
7405
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007406 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7407 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007408 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7409 goto exit;
7410 }
7411
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007412 memcpy(operation->data.inputs.user, user_id, user_id_len);
7413 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007414
7415 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007416exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007417 psa_pake_abort(operation);
7418 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007419}
7420
7421psa_status_t psa_pake_set_peer(
7422 psa_pake_operation_t *operation,
7423 const uint8_t *peer_id,
7424 size_t peer_id_len)
7425{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007426 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007427
7428 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007429 status = PSA_ERROR_BAD_STATE;
7430 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007431 }
7432
Przemek Stekiel51eac532022-12-07 11:04:51 +01007433 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007434 status = PSA_ERROR_INVALID_ARGUMENT;
7435 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007436 }
7437
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007438 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007439 status = PSA_ERROR_BAD_STATE;
7440 goto exit;
7441 }
7442
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007443 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7444 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007445 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7446 goto exit;
7447 }
7448
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007449 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7450 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007451
7452 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007453exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007454 psa_pake_abort(operation);
7455 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007456}
7457
7458psa_status_t psa_pake_set_role(
7459 psa_pake_operation_t *operation,
7460 psa_pake_role_t role)
7461{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007462 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007463
Przemek Stekiel51eac532022-12-07 11:04:51 +01007464 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007465 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007466 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007467 }
7468
Przemek Stekiel09104b82023-03-06 14:11:51 +01007469 switch (operation->alg) {
7470#if defined(PSA_WANT_ALG_JPAKE)
7471 case PSA_ALG_JPAKE:
7472 if (role == PSA_PAKE_ROLE_NONE) {
7473 return PSA_SUCCESS;
7474 }
7475 status = PSA_ERROR_INVALID_ARGUMENT;
7476 break;
7477#endif
7478 default:
7479 (void) role;
7480 status = PSA_ERROR_NOT_SUPPORTED;
7481 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007482 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007483exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007484 psa_pake_abort(operation);
7485 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007486}
7487
Przemek Stekielb09c4872023-01-17 12:05:38 +01007488/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007489#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007490static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007491 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007492{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007493 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007494 case PSA_PAKE_OUTPUT_X1_X2:
7495 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007496 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007497 case PSA_PAKE_X1_STEP_KEY_SHARE:
7498 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007499 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7500 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007501 case PSA_PAKE_X1_STEP_ZK_PROOF:
7502 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007503 case PSA_PAKE_X2_STEP_KEY_SHARE:
7504 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007505 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7506 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007507 case PSA_PAKE_X2_STEP_ZK_PROOF:
7508 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007509 default:
7510 return PSA_JPAKE_STEP_INVALID;
7511 }
7512 break;
7513 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007514 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007515 case PSA_PAKE_X1_STEP_KEY_SHARE:
7516 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007517 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7518 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007519 case PSA_PAKE_X1_STEP_ZK_PROOF:
7520 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007521 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007522 return PSA_JPAKE_STEP_INVALID;
7523 }
7524 break;
7525 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007526 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007527 case PSA_PAKE_X1_STEP_KEY_SHARE:
7528 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007529 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7530 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007531 case PSA_PAKE_X1_STEP_ZK_PROOF:
7532 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007533 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007534 return PSA_JPAKE_STEP_INVALID;
7535 }
7536 break;
7537 default:
7538 return PSA_JPAKE_STEP_INVALID;
7539 }
7540 return PSA_JPAKE_STEP_INVALID;
7541}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007542#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007543
Przemek Stekiel2797d372022-12-22 11:19:22 +01007544static psa_status_t psa_pake_complete_inputs(
7545 psa_pake_operation_t *operation)
7546{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007547 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007548 /* Create copy of the inputs on stack as inputs share memory
7549 with the driver context which will be setup by the driver. */
7550 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007551
Przemek Stekielfde11282023-03-13 16:06:09 +01007552 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007553 return PSA_ERROR_BAD_STATE;
7554 }
7555
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007556 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007557 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7558 return PSA_ERROR_BAD_STATE;
7559 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007560 }
7561
Przemek Stekiel18620a32023-01-17 16:34:52 +01007562 /* Clear driver context */
7563 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7564
7565 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007566
7567 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007568 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7569 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007570
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007571 /* User and peer are translated to role. */
7572 mbedtls_free(inputs.user);
7573 mbedtls_free(inputs.peer);
7574
Przemek Stekiel2797d372022-12-22 11:19:22 +01007575 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007576#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007577 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007578 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007579 psa_jpake_computation_stage_t *computation_stage =
7580 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007581 computation_stage->state = PSA_PAKE_STATE_READY;
7582 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7583 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7584 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007585 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007586#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007587 {
7588 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007589 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007590 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007591 return status;
7592}
7593
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007594#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007595static psa_status_t psa_jpake_output_prologue(
7596 psa_pake_operation_t *operation,
7597 psa_pake_step_t step)
7598{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007599 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7600 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7601 step != PSA_PAKE_STEP_ZK_PROOF) {
7602 return PSA_ERROR_INVALID_ARGUMENT;
7603 }
7604
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007605 psa_jpake_computation_stage_t *computation_stage =
7606 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007607
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007608 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7609 return PSA_ERROR_BAD_STATE;
7610 }
7611
7612 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7613 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
7614 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
7615 return PSA_ERROR_BAD_STATE;
7616 }
7617
7618 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7619 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007620 return PSA_ERROR_BAD_STATE;
7621 }
7622
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007623 switch (computation_stage->output_step) {
7624 case PSA_PAKE_STEP_X1_X2:
7625 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007626 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007627 case PSA_PAKE_STEP_X2S:
7628 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007629 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007630 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007631 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007632 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007633
7634 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7635 }
7636
7637 /* Check if step matches current sequence */
7638 switch (computation_stage->sequence) {
7639 case PSA_PAKE_X1_STEP_KEY_SHARE:
7640 case PSA_PAKE_X2_STEP_KEY_SHARE:
7641 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7642 return PSA_ERROR_BAD_STATE;
7643 }
7644 break;
7645
7646 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7647 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7648 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7649 return PSA_ERROR_BAD_STATE;
7650 }
7651 break;
7652
7653 case PSA_PAKE_X1_STEP_ZK_PROOF:
7654 case PSA_PAKE_X2_STEP_ZK_PROOF:
7655 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7656 return PSA_ERROR_BAD_STATE;
7657 }
7658 break;
7659
7660 default:
7661 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007662 }
7663
7664 return PSA_SUCCESS;
7665}
7666
7667static psa_status_t psa_jpake_output_epilogue(
7668 psa_pake_operation_t *operation)
7669{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007670 psa_jpake_computation_stage_t *computation_stage =
7671 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007672
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007673 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007674 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007675 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007676 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007677 computation_stage->state = PSA_PAKE_STATE_READY;
7678 computation_stage->output_step++;
7679 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7680 } else {
7681 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007682 }
7683
7684 return PSA_SUCCESS;
7685}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007686#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007687
Neil Armstronga7d08c32022-06-01 18:21:20 +02007688psa_status_t psa_pake_output(
7689 psa_pake_operation_t *operation,
7690 psa_pake_step_t step,
7691 uint8_t *output,
7692 size_t output_size,
7693 size_t *output_length)
7694{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007695 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007696 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007697 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007698
7699 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007700 status = psa_pake_complete_inputs(operation);
7701 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007702 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007703 }
7704 }
7705
7706 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007707 status = PSA_ERROR_BAD_STATE;
7708 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007709 }
7710
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007711 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007712 status = PSA_ERROR_INVALID_ARGUMENT;
7713 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007714 }
7715
Przemek Stekiele12ed362022-12-21 12:54:46 +01007716 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007717#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007718 case PSA_ALG_JPAKE:
7719 status = psa_jpake_output_prologue(operation, step);
7720 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007721 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007722 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007723 driver_step = convert_jpake_computation_stage_to_driver_step(
7724 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007725 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007726#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007727 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007728 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007729 status = PSA_ERROR_NOT_SUPPORTED;
7730 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007731 }
7732
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007733 status = psa_driver_wrapper_pake_output(operation, driver_step,
7734 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007735
7736 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007737 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007738 }
7739
7740 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007741#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007742 case PSA_ALG_JPAKE:
7743 status = psa_jpake_output_epilogue(operation);
7744 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007745 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007746 }
7747 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007748#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007749 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007750 status = PSA_ERROR_NOT_SUPPORTED;
7751 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007752 }
7753
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007754 return PSA_SUCCESS;
7755exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007756 psa_pake_abort(operation);
7757 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007758}
7759
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007760#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007761static psa_status_t psa_jpake_input_prologue(
7762 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007763 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007764{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007765 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7766 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7767 step != PSA_PAKE_STEP_ZK_PROOF) {
7768 return PSA_ERROR_INVALID_ARGUMENT;
7769 }
7770
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007771 psa_jpake_computation_stage_t *computation_stage =
7772 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007773
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007774 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7775 return PSA_ERROR_BAD_STATE;
7776 }
7777
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007778 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7779 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
7780 computation_stage->state != PSA_PAKE_INPUT_X4S) {
7781 return PSA_ERROR_BAD_STATE;
7782 }
7783
7784 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7785 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007786 return PSA_ERROR_BAD_STATE;
7787 }
7788
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007789 switch (computation_stage->input_step) {
7790 case PSA_PAKE_STEP_X1_X2:
7791 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007792 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007793 case PSA_PAKE_STEP_X2S:
7794 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007795 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007796 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007797 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007798 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007799
7800 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7801 }
7802
7803 /* Check if step matches current sequence */
7804 switch (computation_stage->sequence) {
7805 case PSA_PAKE_X1_STEP_KEY_SHARE:
7806 case PSA_PAKE_X2_STEP_KEY_SHARE:
7807 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7808 return PSA_ERROR_BAD_STATE;
7809 }
7810 break;
7811
7812 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7813 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7814 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7815 return PSA_ERROR_BAD_STATE;
7816 }
7817 break;
7818
7819 case PSA_PAKE_X1_STEP_ZK_PROOF:
7820 case PSA_PAKE_X2_STEP_ZK_PROOF:
7821 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7822 return PSA_ERROR_BAD_STATE;
7823 }
7824 break;
7825
7826 default:
7827 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007828 }
7829
7830 return PSA_SUCCESS;
7831}
7832
Przemek Stekiele12ed362022-12-21 12:54:46 +01007833static psa_status_t psa_jpake_input_epilogue(
7834 psa_pake_operation_t *operation)
7835{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007836 psa_jpake_computation_stage_t *computation_stage =
7837 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007838
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007839 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007840 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007841 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007842 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007843 computation_stage->state = PSA_PAKE_STATE_READY;
7844 computation_stage->input_step++;
7845 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7846 } else {
7847 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007848 }
7849
7850 return PSA_SUCCESS;
7851}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007852#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007853
Neil Armstronga7d08c32022-06-01 18:21:20 +02007854psa_status_t psa_pake_input(
7855 psa_pake_operation_t *operation,
7856 psa_pake_step_t step,
7857 const uint8_t *input,
7858 size_t input_length)
7859{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007860 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007861 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007862
7863 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007864 status = psa_pake_complete_inputs(operation);
7865 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007866 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007867 }
7868 }
7869
7870 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007871 status = PSA_ERROR_BAD_STATE;
7872 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007873 }
7874
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007875 if (input_length == 0 || input_length > PSA_PAKE_INPUT_MAX_SIZE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007876 status = PSA_ERROR_INVALID_ARGUMENT;
7877 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007878 }
7879
Przemek Stekiele12ed362022-12-21 12:54:46 +01007880 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007881#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007882 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007883 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007884 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007885 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007886 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007887 driver_step = convert_jpake_computation_stage_to_driver_step(
7888 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007889 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007890#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007891 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007892 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007893 status = PSA_ERROR_NOT_SUPPORTED;
7894 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007895 }
7896
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007897 status = psa_driver_wrapper_pake_input(operation, driver_step,
7898 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007899
7900 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007901 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007902 }
7903
7904 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007905#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007906 case PSA_ALG_JPAKE:
7907 status = psa_jpake_input_epilogue(operation);
7908 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007909 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007910 }
7911 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007912#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007913 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007914 status = PSA_ERROR_NOT_SUPPORTED;
7915 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007916 }
7917
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007918 return PSA_SUCCESS;
7919exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007920 psa_pake_abort(operation);
7921 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007922}
7923
7924psa_status_t psa_pake_get_implicit_key(
7925 psa_pake_operation_t *operation,
7926 psa_key_derivation_operation_t *output)
7927{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007928 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007929 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007930 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01007931 size_t shared_key_len = 0;
7932
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007933 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007934 status = PSA_ERROR_BAD_STATE;
7935 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007936 }
7937
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007938#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007939 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007940 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01007941 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007942 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
7943 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007944 status = PSA_ERROR_BAD_STATE;
7945 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007946 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01007947 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007948#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007949 {
7950 status = PSA_ERROR_NOT_SUPPORTED;
7951 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007952 }
7953
Przemek Stekiel0c781802022-11-29 14:53:13 +01007954 status = psa_driver_wrapper_pake_get_implicit_key(operation,
7955 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01007956 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01007957 &shared_key_len);
7958
7959 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007960 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01007961 }
7962
7963 status = psa_key_derivation_input_bytes(output,
7964 PSA_KEY_DERIVATION_INPUT_SECRET,
7965 shared_key,
7966 shared_key_len);
7967
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007968 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007969exit:
7970 abort_status = psa_pake_abort(operation);
7971 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007972}
7973
7974psa_status_t psa_pake_abort(
7975 psa_pake_operation_t *operation)
7976{
Przemek Stekield69dca92023-01-31 19:59:20 +01007977 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007978
Przemek Stekield69dca92023-01-31 19:59:20 +01007979 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007980 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007981 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007982
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007983 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
7984 if (operation->data.inputs.password != NULL) {
7985 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01007986 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007987 mbedtls_free(operation->data.inputs.password);
7988 }
7989 if (operation->data.inputs.user != NULL) {
7990 mbedtls_free(operation->data.inputs.user);
7991 }
7992 if (operation->data.inputs.peer != NULL) {
7993 mbedtls_free(operation->data.inputs.peer);
7994 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007995 }
Przemek Stekield69dca92023-01-31 19:59:20 +01007996 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007997
Przemek Stekield69dca92023-01-31 19:59:20 +01007998 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007999}
Neil Armstronga7d08c32022-06-01 18:21:20 +02008000
Gilles Peskinee59236f2018-01-27 23:32:46 +01008001#endif /* MBEDTLS_PSA_CRYPTO_C */