blob: 1609c74dfbd0137f8013d2a8ae5d7199c5a0652f [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
John Durkop07cc04a2020-11-16 22:08:34 -080025#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
26#include "check_crypto_config.h"
27#endif
28
Gilles Peskinee59236f2018-01-27 23:32:46 +010029#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010030#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031
Ronald Crond6d28882020-12-14 14:56:02 +010032#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020035#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010036#include "psa_crypto_ecp.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010037#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010038#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010039#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010040#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020041#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020042#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020043#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010044#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010045/* Include internal declarations that are useful for implementing persistently
46 * stored keys. */
47#include "psa_crypto_storage.h"
48
Gilles Peskineb2b64d32020-12-14 16:43:58 +010049#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010050
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010051#include <stdlib.h>
52#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010054
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010055#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020056#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000057#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020058#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020060#include "mbedtls/chacha20.h"
61#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/cipher.h"
63#include "mbedtls/ccm.h"
64#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010065#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020066#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010067#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010068#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010069#include "mbedtls/error.h"
70#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010072#include "mbedtls/md.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000073#include "md_wrap.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010074#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000075#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010076#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000077#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010079#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/sha1.h"
81#include "mbedtls/sha256.h"
82#include "mbedtls/sha512.h"
Paul Elliott588f8ed2022-12-02 18:10:26 +000083#include "hash_info.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010084
Gilles Peskine449bd832023-01-11 14:50:10 +010085#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020086
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020087#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
88 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
89 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020090#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020091#endif
92
Przemek Stekielfde11282023-03-13 16:06:09 +010093/* The only two JPAKE user/peer identifiers supported for the time being. */
94static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
95static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
Przemek Stekiel18cd6c92023-03-06 15:40:35 +010096
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010097/****************************************************************/
98/* Global data, support functions and library management */
99/****************************************************************/
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200102{
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200104}
105
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100106/* Values for psa_global_data_t::rng_state */
107#define RNG_NOT_INITIALIZED 0
108#define RNG_INITIALIZED 1
109#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100112 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100113 unsigned rng_state : 2;
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100114 unsigned drivers_initialized : 1;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100115 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100116} psa_global_data_t;
117
118static psa_global_data_t global_data;
119
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100120#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
121mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
122 &global_data.rng.drbg;
123#endif
124
itayzafrir0adf0fc2018-09-06 16:24:41 +0300125#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 if (global_data.initialized == 0) \
127 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300128
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100129int psa_can_do_hash(psa_algorithm_t hash_alg)
130{
131 (void) hash_alg;
132 return global_data.drivers_initialized;
133}
134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100136{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100137 /* Mbed TLS error codes can combine a high-level error code and a
138 * low-level error code. The low-level error usually reflects the
139 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 int low_level_ret = -(-ret & 0x007f);
141 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100142 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100144
145 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
146 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200148 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
149 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
150 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
151 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
152 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200154 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200156 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200158
Jaeden Amero93e21112019-02-20 13:57:28 +0000159#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000160 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100162 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100164
165 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100167 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100169
Gilles Peskine26869f22019-05-06 15:25:00 +0200170 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200172
173 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200175 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200177
Gilles Peskinea5905292018-02-07 20:59:33 +0100178 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100180 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100182 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100184 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100186 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100188 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100190 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
194 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100195 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
196 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100199 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
200 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100202 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100204#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100205
206 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100208
Gilles Peskinee59236f2018-01-27 23:32:46 +0100209 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
210 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
211 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100213
214 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200216 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100218 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100220
Gilles Peskine82e57d12020-11-13 21:31:17 +0100221#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100223 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
224 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100225 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100227 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
228 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100230 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100232#endif
233
Gilles Peskinea5905292018-02-07 20:59:33 +0100234 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100236 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100238 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100240 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100242
Gilles Peskinef76aa772018-10-29 19:24:33 +0100243 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100245 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100247 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100249 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100251 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100253 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100255 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100257 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100259
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100260 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100262 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
263 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100265 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100267 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
268 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100270 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100272 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
273 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100275 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100277 case MBEDTLS_ERR_PK_INVALID_ALG:
278 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
279 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200283 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100285
Gilles Peskineff2d2002019-05-06 15:26:23 +0200286 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200288 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200290
Gilles Peskinea5905292018-02-07 20:59:33 +0100291 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100293 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100295 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100297 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100299 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
300 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100302 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200308
itayzafrir5c753392018-05-08 11:18:38 +0300309 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300310 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300312 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300314 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300316 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
317 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300319 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100321 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100323
Paul Elliott588f8ed2022-12-02 18:10:26 +0000324 case MBEDTLS_ERR_ECP_IN_PROGRESS:
325 return PSA_OPERATION_INCOMPLETE;
326
Janos Follatha13b9052019-11-22 12:48:59 +0000327 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300329
Gilles Peskinee59236f2018-01-27 23:32:46 +0100330 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100332 }
333}
334
Paul Elliottdc42ca82023-02-24 18:11:59 +0000335/**
336 * \brief For output buffers which contain "tags"
337 * (outputs that may be checked for validity like
338 * hashes, MACs and signatures), fill the unused
339 * part of the output buffer (the whole buffer on
340 * error, the trailing part on success) with
341 * something that isn't a valid tag (barring an
342 * attack on the tag and deliberately-crafted
343 * input), in case the caller doesn't check the
344 * return status properly.
345 *
346 * \param output_buffer Pointer to buffer to wipe. May not be NULL
347 * unless \p output_buffer_size is zero.
348 * \param status Status of function called to generate
349 * output_buffer originally
350 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
351 * could be NULL.
352 * \param output_buffer_length Length of data written to output_buffer, must be
353 * less than \p output_buffer_size
354 */
355static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000356 size_t output_buffer_size, size_t output_buffer_length)
357{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000358 size_t offset = 0;
359
360 if (output_buffer_size == 0) {
361 /* If output_buffer_size is 0 then we have nothing to do. We must not
362 call memset because output_buffer may be NULL in this case */
363 return;
364 }
365
366 if (status == PSA_SUCCESS) {
367 offset = output_buffer_length;
368 }
369
370 memset(output_buffer + offset, '!', output_buffer_size - offset);
371}
372
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200373
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200374
375
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100376/****************************************************************/
377/* Key management */
378/****************************************************************/
379
Valerio Setti9affb732023-03-20 17:07:57 +0100380#if defined(MBEDTLS_ECP_C)
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}
Valerio Setti9affb732023-03-20 17:07:57 +0100472#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
475 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200476{
477 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200479 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200480 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200481 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200482 case PSA_KEY_TYPE_PASSWORD:
483 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200484 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100485#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200486 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 if (bits != 128 && bits != 192 && bits != 256) {
488 return PSA_ERROR_INVALID_ARGUMENT;
489 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490 break;
491#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200492#if defined(PSA_WANT_KEY_TYPE_ARIA)
493 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (bits != 128 && bits != 192 && bits != 256) {
495 return PSA_ERROR_INVALID_ARGUMENT;
496 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200497 break;
498#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100499#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200500 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 if (bits != 128 && bits != 192 && bits != 256) {
502 return PSA_ERROR_INVALID_ARGUMENT;
503 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200504 break;
505#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100506#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200507 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 if (bits != 64 && bits != 128 && bits != 192) {
509 return PSA_ERROR_INVALID_ARGUMENT;
510 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200511 break;
512#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100513#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200514 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 if (bits != 256) {
516 return PSA_ERROR_INVALID_ARGUMENT;
517 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200518 break;
519#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200520 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200522 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 if (bits % 8 != 0) {
524 return PSA_ERROR_INVALID_ARGUMENT;
525 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200528}
529
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100530/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100531 *
Steven Cooremancd640932021-03-08 12:00:27 +0100532 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
533 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100534 *
535 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100536 * \param[in] key_type The key type of the key to be used with the
537 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100538 *
539 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100540 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100541 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100542 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100543 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100544MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100545 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100547{
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 if (PSA_ALG_IS_HMAC(algorithm)) {
549 if (key_type == PSA_KEY_TYPE_HMAC) {
550 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100551 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100552 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
555 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
556 * key. */
557 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
558 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
559 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
560 * the block length (larger than 1) for block ciphers. */
561 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
562 return PSA_SUCCESS;
563 }
564 }
565 }
566
567 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100568}
569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
571 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200572{
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 if (slot->key.data != NULL) {
574 return PSA_ERROR_ALREADY_EXISTS;
575 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 slot->key.data = mbedtls_calloc(1, buffer_length);
578 if (slot->key.data == NULL) {
579 return PSA_ERROR_INSUFFICIENT_MEMORY;
580 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200581
Ronald Cronea0f8a62020-11-25 17:52:23 +0100582 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200584}
585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
587 const uint8_t *data,
588 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200589{
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 psa_status_t status = psa_allocate_buffer_to_slot(slot,
591 data_length);
592 if (status != PSA_SUCCESS) {
593 return status;
594 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 memcpy(slot->key.data, data, data_length);
597 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200598}
599
Ronald Crona0fe59f2020-11-29 11:14:53 +0100600psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100601 const psa_key_attributes_t *attributes,
602 const uint8_t *data, size_t data_length,
603 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100605{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100606 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
607 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100608
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200609 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 if (data_length == 0) {
611 return PSA_ERROR_NOT_SUPPORTED;
612 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 if (key_type_is_raw_bytes(type)) {
615 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200616
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
618 *bits);
619 if (status != PSA_SUCCESS) {
620 return status;
621 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200622
Ronald Crondd04d422020-11-28 15:14:42 +0100623 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100625 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 return PSA_SUCCESS;
629 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800630#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
632 if (PSA_KEY_TYPE_IS_ECC(type)) {
633 return mbedtls_psa_ecp_import_key(attributes,
634 data, data_length,
635 key_buffer, key_buffer_size,
636 key_buffer_length,
637 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100638 }
John Durkop9814fa22020-11-04 12:28:15 -0800639#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
640 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700641#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
643 if (PSA_KEY_TYPE_IS_RSA(type)) {
644 return mbedtls_psa_rsa_import_key(attributes,
645 data, data_length,
646 key_buffer, key_buffer_size,
647 key_buffer_length,
648 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200649 }
John Durkop9814fa22020-11-04 12:28:15 -0800650#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
651 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100652 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100655}
656
Gilles Peskinef603c712019-01-19 13:40:11 +0100657/** Calculate the intersection of two algorithm usage policies.
658 *
659 * Return 0 (which allows no operation) on incompatibility.
660 */
661static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100662 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100663 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100665{
Gilles Peskine549ea862019-05-22 11:45:59 +0200666 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 if (alg1 == alg2) {
668 return alg1;
669 }
Steven Cooreman03488022021-02-18 12:07:20 +0100670 /* If the policies are from the same hash-and-sign family, check
671 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
673 PSA_ALG_IS_SIGN_HASH(alg2) &&
674 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
675 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
676 return alg2;
677 }
678 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
679 return alg1;
680 }
Steven Cooreman03488022021-02-18 12:07:20 +0100681 }
682 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100683 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100684 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
686 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
687 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
688 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
689 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100690 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100691
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100692 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
694 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
695 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
696 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100697 }
698 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
700 (alg1_len <= alg2_len)) {
701 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100702 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
704 (alg2_len <= alg1_len)) {
705 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100706 }
707 }
708 /* If the policies are from the same MAC family, check whether one
709 * of them is a minimum-MAC-length policy. Calculate the most
710 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
712 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
713 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100714 /* Validate the combination of key type and algorithm. Since the base
715 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
717 return 0;
718 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100719
Steven Cooreman31a876d2021-03-03 20:47:40 +0100720 /* Get the (exact or at-least) output lengths for both sides of the
721 * requested intersection. None of the currently supported algorithms
722 * have an output length dependent on the actual key size, so setting it
723 * to a bogus value of 0 is currently OK.
724 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100725 * Note that for at-least-this-length wildcard algorithms, the output
726 * length is set to the shortest allowed length, which allows us to
727 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
729 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100730 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100731
Steven Cooremana1d83222021-02-25 10:20:29 +0100732 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
734 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
735 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100736 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100737
738 /* If only one is an at-least-this-length policy, the intersection would
739 * be the other (fixed-length) policy as long as said fixed length is
740 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
742 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100743 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
745 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100746 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100747
Steven Cooremancd640932021-03-08 12:00:27 +0100748 /* If none of them are wildcards, check whether they define the same tag
749 * length. This is still possible here when one is default-length and
750 * the other specific-length. Ensure to always return the
751 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 if (alg1_len == alg2_len) {
753 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
754 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100755 }
756 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100758}
759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760static int psa_key_algorithm_permits(psa_key_type_t key_type,
761 psa_algorithm_t policy_alg,
762 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200763{
Gilles Peskine549ea862019-05-22 11:45:59 +0200764 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 if (requested_alg == policy_alg) {
766 return 1;
767 }
Steven Cooreman03488022021-02-18 12:07:20 +0100768 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
769 * and requested_alg is the same hash-and-sign family with any hash,
770 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
772 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
773 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
774 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100775 }
776 /* If policy_alg is a wildcard AEAD algorithm of the same base as
777 * the requested algorithm, check the requested tag length to be
778 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 if (PSA_ALG_IS_AEAD(policy_alg) &&
780 PSA_ALG_IS_AEAD(requested_alg) &&
781 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
782 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
783 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
784 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
785 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100786 }
Steven Cooremancd640932021-03-08 12:00:27 +0100787 /* If policy_alg is a MAC algorithm of the same base as the requested
788 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 if (PSA_ALG_IS_MAC(policy_alg) &&
790 PSA_ALG_IS_MAC(requested_alg) &&
791 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
792 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100793 /* Validate the combination of key type and algorithm. Since the policy
794 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
796 return 0;
797 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100798
Steven Cooreman31a876d2021-03-03 20:47:40 +0100799 /* Get both the requested output length for the algorithm which is to be
800 * verified, and the default output length for the base algorithm.
801 * Note that none of the currently supported algorithms have an output
802 * length dependent on actual key size, so setting it to a bogus value
803 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100804 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100806 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 key_type, 0,
808 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100809
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100810 /* If the policy is default-length, only allow an algorithm with
811 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
813 return requested_output_length == default_output_length;
814 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100815
816 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100817 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
819 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
820 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100821 }
822
Steven Cooremancd640932021-03-08 12:00:27 +0100823 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
824 * check for the requested MAC length to be equal to or longer than the
825 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
827 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
828 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100829 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200830 }
Steven Cooremance48e852020-10-05 16:02:45 +0200831 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200832 * a key derivation with that key agreement should also be allowed. This
833 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
835 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
836 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
837 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200838 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100839 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200841}
842
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100843/** Test whether a policy permits an algorithm.
844 *
845 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100846 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100847 * \note This function requires providing the key type for which the policy is
848 * being validated, since some algorithm policy definitions (e.g. MAC)
849 * have different properties depending on what kind of cipher it is
850 * combined with.
851 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100852 * \retval PSA_SUCCESS When \p alg is a specific algorithm
853 * allowed by the \p policy.
854 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
855 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
856 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100857 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100858static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
859 psa_key_type_t key_type,
860 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100861{
Steven Cooremand788fab2021-02-25 11:29:17 +0100862 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 if (alg == 0) {
864 return PSA_ERROR_INVALID_ARGUMENT;
865 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100866
Steven Cooreman7e39f052021-02-23 14:18:32 +0100867 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 if (PSA_ALG_IS_WILDCARD(alg)) {
869 return PSA_ERROR_INVALID_ARGUMENT;
870 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
873 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
874 return PSA_SUCCESS;
875 } else {
876 return PSA_ERROR_NOT_PERMITTED;
877 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100878}
879
Gilles Peskinef603c712019-01-19 13:40:11 +0100880/** Restrict a key policy based on a constraint.
881 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100882 * \note This function requires providing the key type for which the policy is
883 * being restricted, since some algorithm policy definitions (e.g. MAC)
884 * have different properties depending on what kind of cipher it is
885 * combined with.
886 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100887 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100888 * \param[in,out] policy The policy to restrict.
889 * \param[in] constraint The policy constraint to apply.
890 *
891 * \retval #PSA_SUCCESS
892 * \c *policy contains the intersection of the original value of
893 * \c *policy and \c *constraint.
894 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100895 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100896 * \c *policy is unchanged.
897 */
898static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100899 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100900 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100902{
903 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 psa_key_policy_algorithm_intersection(key_type, policy->alg,
905 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200906 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
908 constraint->alg2);
909 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
910 return PSA_ERROR_INVALID_ARGUMENT;
911 }
912 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
913 return PSA_ERROR_INVALID_ARGUMENT;
914 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100915 policy->usage &= constraint->usage;
916 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200917 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100919}
920
Przemek Stekiel061f6942022-11-30 10:51:35 +0100921/** Get the description of a key given its identifier and policy constraints
922 * and lock it.
923 *
924 * The key must have allow all the usage flags set in \p usage. If \p alg is
925 * nonzero, the key must allow operations with this algorithm. If \p alg is
926 * zero, the algorithm is not checked.
927 *
928 * In case of a persistent key, the function loads the description of the key
929 * into a key slot if not already done.
930 *
931 * On success, the returned key slot is locked. It is the responsibility of
932 * the caller to unlock the key slot when it does not access it anymore.
933 */
934static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100935 mbedtls_svc_key_id_t key,
936 psa_key_slot_t **p_slot,
937 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100939{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200940 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100941 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 status = psa_get_and_lock_key_slot(key, p_slot);
944 if (status != PSA_SUCCESS) {
945 return status;
946 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200947 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100948
949 /* Enforce that usage policy for the key slot contains all the flags
950 * required by the usage parameter. There is one exception: public
951 * keys can always be exported, so we treat public key objects as
952 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100954 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200956
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100958 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200959 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100960 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100961
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800962 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 if (alg != 0) {
964 status = psa_key_policy_permits(&slot->attr.policy,
965 slot->attr.type,
966 alg);
967 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100968 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100970 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100971
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200973
974error:
975 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100979}
Darryl Green940d72c2018-07-13 13:18:51 +0100980
Ronald Cron5c522922020-11-14 16:35:34 +0100981/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200982 *
983 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200984 * available, as opposed to a key in a secure element and/or to be used
985 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200986 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200987 * This is a temporary function that may be used instead of
988 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
989 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200990 *
Ronald Cron5c522922020-11-14 16:35:34 +0100991 * On success, the returned key slot is locked. It is the responsibility of the
992 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200993 */
Ronald Cron5c522922020-11-14 16:35:34 +0100994static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
995 mbedtls_svc_key_id_t key,
996 psa_key_slot_t **p_slot,
997 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +0200999{
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1001 usage, alg);
1002 if (status != PSA_SUCCESS) {
1003 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001004 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001005
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1007 psa_unlock_key_slot(*p_slot);
1008 *p_slot = NULL;
1009 return PSA_ERROR_NOT_SUPPORTED;
1010 }
1011
1012 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001013}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001014
Gilles Peskine449bd832023-01-11 14:50:10 +01001015psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001016{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001017 /* Data pointer will always be either a valid pointer or NULL in an
1018 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 if (slot->key.data != NULL) {
1020 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1021 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001024 slot->key.data = NULL;
1025 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001028}
1029
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001030/** Completely wipe a slot in memory, including its policy.
1031 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001032psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001033{
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001035
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 /*
1037 * As the return error code may not be handled in case of multiple errors,
1038 * do our best to report an unexpected lock counter. Assert with
1039 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1040 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1041 * function is called as part of the execution of a test suite, the
1042 * execution of the test suite is stopped in error if the assertion fails.
1043 */
1044 if (slot->lock_count != 1) {
1045 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001046 status = PSA_ERROR_CORRUPTION_DETECTED;
1047 }
1048
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001049 /* Multipart operations may still be using the key. This is safe
1050 * because all multipart operation objects are independent from
1051 * the key slot: if they need to access the key after the setup
1052 * phase, they have a copy of the key. Note that this means that
1053 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001054 /* At this point, key material and other type-specific content has
1055 * been wiped. Clear remaining metadata. We can call memset and not
1056 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 memset(slot, 0, sizeof(*slot));
1058 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001059}
1060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001062{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001063 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001064 psa_status_t status; /* status of the last operation */
1065 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001066#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1067 psa_se_drv_table_entry_t *driver;
1068#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 if (mbedtls_svc_key_id_is_null(key)) {
1071 return PSA_SUCCESS;
1072 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001073
Ronald Cronf2911112020-10-29 17:51:10 +01001074 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001075 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001076 * key, this will load the key description from persistent memory if not
1077 * done yet. We cannot avoid this loading as without it we don't know if
1078 * the key is operated by an SE or not and this information is needed by
1079 * the current implementation.
1080 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 status = psa_get_and_lock_key_slot(key, &slot);
1082 if (status != PSA_SUCCESS) {
1083 return status;
1084 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001085
Ronald Cronf2911112020-10-29 17:51:10 +01001086 /*
1087 * If the key slot containing the key description is under access by the
1088 * library (apart from the present access), the key cannot be destroyed
1089 * yet. For the time being, just return in error. Eventually (to be
1090 * implemented), the key should be destroyed when all accesses have
1091 * stopped.
1092 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 if (slot->lock_count > 1) {
1094 psa_unlock_key_slot(slot);
1095 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001096 }
1097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001099 /* Refuse the destruction of a read-only key (which may or may not work
1100 * if we attempt it, depending on whether the key is merely read-only
1101 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001102 * Just do the best we can, which is to wipe the copy in memory
1103 * (done in this function's cleanup code). */
1104 overall_status = PSA_ERROR_NOT_PERMITTED;
1105 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001106 }
1107
Gilles Peskine354f7672019-07-12 23:46:38 +02001108#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1110 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001111 /* For a key in a secure element, we need to do three things:
1112 * remove the key file in internal storage, destroy the
1113 * key inside the secure element, and update the driver's
1114 * persistent data. Start a transaction that will encompass these
1115 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001117 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001119 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 status = psa_crypto_save_transaction();
1121 if (status != PSA_SUCCESS) {
1122 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001123 /* We should still try to destroy the key in the secure
1124 * element and the key metadata in storage. This is especially
1125 * important if the error is that the storage is full.
1126 * But how to do it exactly without risking an inconsistent
1127 * state after a reset?
1128 * https://github.com/ARMmbed/mbed-crypto/issues/215
1129 */
1130 overall_status = status;
1131 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001132 }
1133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 status = psa_destroy_se_key(driver,
1135 psa_key_slot_get_slot_number(slot));
1136 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001137 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001139 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001140#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1141
Darryl Greend49a4992018-06-18 17:27:26 +01001142#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1144 status = psa_destroy_persistent_key(slot->attr.id);
1145 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001146 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001148
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001149 /* TODO: other slots may have a copy of the same key. We should
1150 * invalidate them.
1151 * https://github.com/ARMmbed/mbed-crypto/issues/214
1152 */
Darryl Greend49a4992018-06-18 17:27:26 +01001153 }
1154#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001155
Gilles Peskinefc762652019-07-22 19:30:34 +02001156#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 if (driver != NULL) {
1158 status = psa_save_se_persistent_data(driver);
1159 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001160 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 }
1162 status = psa_crypto_stop_transaction();
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 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001166 }
1167#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1168
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001169exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001171 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001173 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 }
1175 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001176}
1177
John Durkop07cc04a2020-11-16 22:08:34 -08001178#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001179 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001180static psa_status_t psa_get_rsa_public_exponent(
1181 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001183{
1184 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001185 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001186 uint8_t *buffer = NULL;
1187 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001189
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1191 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001192 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 }
1194 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001195 /* It's the default value, which is reported as an empty string,
1196 * so there's nothing to do. */
1197 goto exit;
1198 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001199
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 buflen = mbedtls_mpi_size(&mpi);
1201 buffer = mbedtls_calloc(1, buflen);
1202 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001203 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1204 goto exit;
1205 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1207 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001208 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001210 attributes->domain_parameters = buffer;
1211 attributes->domain_parameters_size = buflen;
1212
1213exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 mbedtls_mpi_free(&mpi);
1215 if (ret != 0) {
1216 mbedtls_free(buffer);
1217 }
1218 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001219}
John Durkop07cc04a2020-11-16 22:08:34 -08001220#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001221 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001222
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001223/** Retrieve all the publicly-accessible attributes of a key.
1224 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001225psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1226 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001227{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001228 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001229 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001230 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001233
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1235 if (status != PSA_SUCCESS) {
1236 return status;
1237 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001238
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001239 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1241 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001242
1243#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1245 psa_set_key_slot_number(attributes,
1246 psa_key_slot_get_slot_number(slot));
1247 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001248#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001249
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001251#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001252 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001253 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001254 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001255 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001256 * is not yet implemented.
1257 * https://github.com/ARMmbed/mbed-crypto/issues/216
1258 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001260 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001261
Ronald Cron90857082020-11-25 14:58:33 +01001262 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 slot->attr.type,
1264 slot->key.data,
1265 slot->key.bytes,
1266 &rsa);
1267 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001268 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 status = psa_get_rsa_public_exponent(rsa,
1272 attributes);
1273 mbedtls_rsa_free(rsa);
1274 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001275 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001276 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001277#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001278 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001279 default:
1280 /* Nothing else to do. */
1281 break;
1282 }
1283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 if (status != PSA_SUCCESS) {
1285 psa_reset_key_attributes(attributes);
1286 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001289
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001291}
1292
Gilles Peskinec8000c02019-08-02 20:15:51 +02001293#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1294psa_status_t psa_get_key_slot_number(
1295 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001297{
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001299 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 return PSA_SUCCESS;
1301 } else {
1302 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001303 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001304}
1305#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1308 size_t key_buffer_size,
1309 uint8_t *data,
1310 size_t data_size,
1311 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001312{
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 if (key_buffer_size > data_size) {
1314 return PSA_ERROR_BUFFER_TOO_SMALL;
1315 }
1316 memcpy(data, key_buffer, key_buffer_size);
1317 memset(data + key_buffer_size, 0,
1318 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001319 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001321}
1322
Ronald Cron7285cda2020-11-26 14:46:37 +01001323psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001324 const psa_key_attributes_t *attributes,
1325 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001327{
Ronald Crond18b5f82020-11-25 16:46:05 +01001328 psa_key_type_t type = attributes->core.type;
1329
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 if (key_type_is_raw_bytes(type) ||
1331 PSA_KEY_TYPE_IS_RSA(type) ||
1332 PSA_KEY_TYPE_IS_ECC(type)) {
1333 return psa_export_key_buffer_internal(
1334 key_buffer, key_buffer_size,
1335 data, data_size, data_length);
1336 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001337 /* This shouldn't happen in the reference implementation, but
1338 it is valid for a special-purpose implementation to omit
1339 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001341 }
1342}
1343
Gilles Peskine449bd832023-01-11 14:50:10 +01001344psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1345 uint8_t *data,
1346 size_t data_size,
1347 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001348{
1349 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1350 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1351 psa_key_slot_t *slot;
1352
Ronald Crone907e552021-01-18 13:22:38 +01001353 /* Reject a zero-length output buffer now, since this can never be a
1354 * valid key representation. This way we know that data must be a valid
1355 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 if (data_size == 0) {
1357 return PSA_ERROR_BUFFER_TOO_SMALL;
1358 }
Ronald Crone907e552021-01-18 13:22:38 +01001359
Ronald Cron9486f9d2020-11-26 13:36:23 +01001360 /* Set the key to empty now, so that even when there are errors, we always
1361 * set data_length to a value between 0 and data_size. On error, setting
1362 * the key to empty is a good choice because an empty key representation is
1363 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001364 *data_length = 0;
1365
Ronald Cron9486f9d2020-11-26 13:36:23 +01001366 /* Export requires the EXPORT flag. There is an exception for public keys,
1367 * which don't require any flag, but
1368 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1369 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1371 PSA_KEY_USAGE_EXPORT, 0);
1372 if (status != PSA_SUCCESS) {
1373 return status;
1374 }
mohammad160306e79202018-03-28 13:17:44 +03001375
Ronald Crond18b5f82020-11-25 16:46:05 +01001376 psa_key_attributes_t attributes = {
1377 .core = slot->attr
1378 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 status = psa_driver_wrapper_export_key(&attributes,
1380 slot->key.data, slot->key.bytes,
1381 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001386}
1387
Ronald Cron7285cda2020-11-26 14:46:37 +01001388psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001389 const psa_key_attributes_t *attributes,
1390 const uint8_t *key_buffer,
1391 size_t key_buffer_size,
1392 uint8_t *data,
1393 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001395{
Ronald Crond18b5f82020-11-25 16:46:05 +01001396 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001397
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1399 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001400 /* Exporting public -> public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 return psa_export_key_buffer_internal(
1402 key_buffer, key_buffer_size,
1403 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001404 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001407#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1409 return mbedtls_psa_rsa_export_public_key(attributes,
1410 key_buffer,
1411 key_buffer_size,
1412 data,
1413 data_size,
1414 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001415#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001416 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001418#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1419 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001421#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1423 return mbedtls_psa_ecp_export_public_key(attributes,
1424 key_buffer,
1425 key_buffer_size,
1426 data,
1427 data_size,
1428 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001429#else
1430 /* We don't know how to convert a private ECC key to public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001432#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1433 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001434 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001436 /* This shouldn't happen in the reference implementation, but
1437 it is valid for a special-purpose implementation to omit
1438 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001440 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001441}
Ronald Crond18b5f82020-11-25 16:46:05 +01001442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1444 uint8_t *data,
1445 size_t data_size,
1446 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001447{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001448 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001449 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001450 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001451
Ronald Crone907e552021-01-18 13:22:38 +01001452 /* Reject a zero-length output buffer now, since this can never be a
1453 * valid key representation. This way we know that data must be a valid
1454 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 if (data_size == 0) {
1456 return PSA_ERROR_BUFFER_TOO_SMALL;
1457 }
Ronald Crone907e552021-01-18 13:22:38 +01001458
Darryl Greendd8fb772018-11-07 16:00:44 +00001459 /* Set the key to empty now, so that even when there are errors, we always
1460 * set data_length to a value between 0 and data_size. On error, setting
1461 * the key to empty is a good choice because an empty key representation is
1462 * unlikely to be accepted anywhere. */
1463 *data_length = 0;
1464
1465 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1467 if (status != PSA_SUCCESS) {
1468 return status;
1469 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1472 status = PSA_ERROR_INVALID_ARGUMENT;
1473 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001474 }
1475
Ronald Crond18b5f82020-11-25 16:46:05 +01001476 psa_key_attributes_t attributes = {
1477 .core = slot->attr
1478 };
Ronald Cron67227982020-11-26 15:16:05 +01001479 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001480 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001482
1483exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001487}
1488
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001489MBEDTLS_STATIC_ASSERT(
1490 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1491 "One or more key attribute flag is listed as both external-only and dual-use")
1492MBEDTLS_STATIC_ASSERT(
1493 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1494 "One or more key attribute flag is listed as both internal-only and dual-use")
1495MBEDTLS_STATIC_ASSERT(
1496 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1497 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001498
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001499/** Validate that a key policy is internally well-formed.
1500 *
1501 * This function only rejects invalid policies. It does not validate the
1502 * consistency of the policy with respect to other attributes of the key
1503 * such as the key type.
1504 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001505static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001506{
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1508 PSA_KEY_USAGE_COPY |
1509 PSA_KEY_USAGE_ENCRYPT |
1510 PSA_KEY_USAGE_DECRYPT |
1511 PSA_KEY_USAGE_SIGN_MESSAGE |
1512 PSA_KEY_USAGE_VERIFY_MESSAGE |
1513 PSA_KEY_USAGE_SIGN_HASH |
1514 PSA_KEY_USAGE_VERIFY_HASH |
1515 PSA_KEY_USAGE_VERIFY_DERIVATION |
1516 PSA_KEY_USAGE_DERIVE)) != 0) {
1517 return PSA_ERROR_INVALID_ARGUMENT;
1518 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001519
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001521}
1522
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001523/** Validate the internal consistency of key attributes.
1524 *
1525 * This function only rejects invalid attribute values. If does not
1526 * validate the consistency of the attributes with any key data that may
1527 * be involved in the creation of the key.
1528 *
1529 * Call this function early in the key creation process.
1530 *
1531 * \param[in] attributes Key attributes for the new key.
1532 * \param[out] p_drv On any return, the driver for the key, if any.
1533 * NULL for a transparent key.
1534 *
1535 */
1536static psa_status_t psa_validate_key_attributes(
1537 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001539{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001540 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1542 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001543
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 status = psa_validate_key_location(lifetime, p_drv);
1545 if (status != PSA_SUCCESS) {
1546 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001547 }
1548
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 status = psa_validate_key_persistence(lifetime);
1550 if (status != PSA_SUCCESS) {
1551 return status;
1552 }
1553
1554 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1555 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1556 return PSA_ERROR_INVALID_ARGUMENT;
1557 }
1558 } else {
1559 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1560 return PSA_ERROR_INVALID_ARGUMENT;
1561 }
1562 }
1563
1564 status = psa_validate_key_policy(&attributes->core.policy);
1565 if (status != PSA_SUCCESS) {
1566 return status;
1567 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001568
1569 /* Refuse to create overly large keys.
1570 * Note that this doesn't trigger on import if the attributes don't
1571 * explicitly specify a size (so psa_get_key_bits returns 0), so
1572 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1574 return PSA_ERROR_NOT_SUPPORTED;
1575 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001576
Gilles Peskine91e8c332019-08-02 19:19:39 +02001577 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1579 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1580 return PSA_ERROR_INVALID_ARGUMENT;
1581 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001582
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001584}
1585
Gilles Peskine4747d192019-04-17 15:05:45 +02001586/** Prepare a key slot to receive key material.
1587 *
1588 * This function allocates a key slot and sets its metadata.
1589 *
1590 * If this function fails, call psa_fail_key_creation().
1591 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001592 * This function is intended to be used as follows:
1593 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001594 * it with the specified attributes, and in case of a volatile key assign it
1595 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001596 * -# Populate the slot with the key material.
1597 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1598 * In case of failure at any step, stop the sequence and call
1599 * psa_fail_key_creation().
1600 *
Ronald Cron5c522922020-11-14 16:35:34 +01001601 * On success, the key slot is locked. It is the responsibility of the caller
1602 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001603 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001604 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001605 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001606 * \param[out] p_slot On success, a pointer to the prepared slot.
1607 * \param[out] p_drv On any return, the driver for the key, if any.
1608 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001609 *
1610 * \retval #PSA_SUCCESS
1611 * The key slot is ready to receive key material.
1612 * \return If this function fails, the key slot is an invalid state.
1613 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001614 */
1615static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001616 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001617 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001618 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001620{
1621 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001622 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001623 psa_key_slot_t *slot;
1624
Gilles Peskinedf179142019-07-15 22:02:14 +02001625 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001626 *p_drv = NULL;
1627
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 status = psa_validate_key_attributes(attributes, p_drv);
1629 if (status != PSA_SUCCESS) {
1630 return status;
1631 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1634 if (status != PSA_SUCCESS) {
1635 return status;
1636 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001637 slot = *p_slot;
1638
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001639 /* We're storing the declared bit-size of the key. It's up to each
1640 * creation mechanism to verify that this information is correct.
1641 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001642 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001643 * is optional (import, copy). In case of a volatile key, assign it the
1644 * volatile key identifier associated to the slot returned to contain its
1645 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001646
1647 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001649#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1650 slot->attr.id = volatile_key_id;
1651#else
1652 slot->attr.id.key_id = volatile_key_id;
1653#endif
1654 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001655
Gilles Peskine91e8c332019-08-02 19:19:39 +02001656 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001657 * external-only flags, query `attributes`. Thanks to the check
1658 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001659 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001660 * may have set. */
1661 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001662
Gilles Peskinecbaff462019-07-12 23:46:04 +02001663#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001664 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001665 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001666 * create the key file in internal storage, create the
1667 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001668 * persistent data. This is done by starting a transaction that will
1669 * encompass these three actions.
1670 * For registering a volatile key, we just need to find an appropriate
1671 * slot number inside the SE. Since the key is designated volatile, creating
1672 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001673 /* The first thing to do is to find a slot number for the new key.
1674 * We save the slot number in persistent storage as part of the
1675 * transaction data. It will be needed to recover if the power
1676 * fails during the key creation process, to clean up on the secure
1677 * element side after restarting. Obtaining a slot number from the
1678 * secure element driver updates its persistent state, but we do not yet
1679 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001680 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001682 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1684 &slot_number);
1685 if (status != PSA_SUCCESS) {
1686 return status;
1687 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001688
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1690 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001691 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001692 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001693 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 status = psa_crypto_save_transaction();
1695 if (status != PSA_SUCCESS) {
1696 (void) psa_crypto_stop_transaction();
1697 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001698 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001699 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001700
1701 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001703 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001704
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001706 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001708 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001709#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1710
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001712}
Gilles Peskine4747d192019-04-17 15:05:45 +02001713
1714/** Finalize the creation of a key once its key material has been set.
1715 *
1716 * This entails writing the key to persistent storage.
1717 *
1718 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001719 * See the documentation of psa_start_key_creation() for the intended use
1720 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001721 *
Ronald Cron5c522922020-11-14 16:35:34 +01001722 * If the finalization succeeds, the function unlocks the key slot (it was
1723 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1724 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001725 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001726 * \param[in,out] slot Pointer to the slot with key material.
1727 * \param[in] driver The secure element driver for the key,
1728 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001729 * \param[out] key On success, identifier of the key. Note that the
1730 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001731 *
1732 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001733 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001734 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1735 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1736 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1737 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1738 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1739 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001740 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001741 * \return If this function fails, the key slot is an invalid state.
1742 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001743 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001744static psa_status_t psa_finish_key_creation(
1745 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001746 psa_se_drv_table_entry_t *driver,
1747 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001748{
1749 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001750 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001751 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001752
1753#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001755#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001757 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001758 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001760
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001761 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1762 sizeof(data.slot_number),
1763 "Slot number size does not match psa_se_key_data_storage_t");
1764
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1766 status = psa_save_persistent_key(&slot->attr,
1767 (uint8_t *) &data,
1768 sizeof(data));
1769 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001770#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1771 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001772 /* Key material is saved in export representation in the slot, so
1773 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 status = psa_save_persistent_key(&slot->attr,
1775 slot->key.data,
1776 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001777 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001778 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001779#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1780
Gilles Peskinecbaff462019-07-12 23:46:04 +02001781#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001782 /* Finish the transaction for a key creation. This does not
1783 * happen when registering an existing key. Detect this case
1784 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001785 * creation of a persistent key in a secure element requires a transaction,
1786 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 if (driver != NULL &&
1788 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1789 status = psa_save_se_persistent_data(driver);
1790 if (status != PSA_SUCCESS) {
1791 psa_destroy_persistent_key(slot->attr.id);
1792 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001793 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001795 }
1796#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1797
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001799 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 status = psa_unlock_key_slot(slot);
1801 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001802 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001804 }
Ronald Cron50972942020-11-14 11:28:25 +01001805
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001807}
1808
1809/** Abort the creation of a key.
1810 *
1811 * You may call this function after calling psa_start_key_creation(),
1812 * or after psa_finish_key_creation() fails. In other circumstances, this
1813 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001814 * See the documentation of psa_start_key_creation() for the intended use
1815 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001816 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001817 * \param[in,out] slot Pointer to the slot with key material.
1818 * \param[in] driver The secure element driver for the key,
1819 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001820 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001821static void psa_fail_key_creation(psa_key_slot_t *slot,
1822 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001823{
Gilles Peskine011e4282019-06-26 18:34:38 +02001824 (void) driver;
1825
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001827 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001829
1830#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001831 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001832 * element, and the failure happened later (when saving metadata
1833 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001834 * element.
1835 * https://github.com/ARMmbed/mbed-crypto/issues/217
1836 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001837
Gilles Peskined7729582019-08-05 15:55:54 +02001838 /* Abort the ongoing transaction if any (there may not be one if
1839 * the creation process failed before starting one, or if the
1840 * key creation is a registration of a key in a secure element).
1841 * Earlier functions must already have done what it takes to undo any
1842 * partial creation. All that's left is to update the transaction data
1843 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001845#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001848}
1849
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001850/** Validate optional attributes during key creation.
1851 *
1852 * Some key attributes are optional during key creation. If they are
1853 * specified in the attributes structure, check that they are consistent
1854 * with the data in the slot.
1855 *
1856 * This function should be called near the end of key creation, after
1857 * the slot in memory is fully populated but before saving persistent data.
1858 */
1859static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001860 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001862{
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 if (attributes->core.type != 0) {
1864 if (attributes->core.type != slot->attr.type) {
1865 return PSA_ERROR_INVALID_ARGUMENT;
1866 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001867 }
1868
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001870#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1872 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001873 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001874 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001875 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001876
Ronald Cron90857082020-11-25 14:58:33 +01001877 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 slot->attr.type,
1879 slot->key.data,
1880 slot->key.bytes,
1881 &rsa);
1882 if (status != PSA_SUCCESS) {
1883 return status;
1884 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001885
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 mbedtls_mpi_init(&actual);
1887 mbedtls_mpi_init(&required);
1888 ret = mbedtls_rsa_export(rsa,
1889 NULL, NULL, NULL, NULL, &actual);
1890 mbedtls_rsa_free(rsa);
1891 mbedtls_free(rsa);
1892 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001893 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 }
1895 ret = mbedtls_mpi_read_binary(&required,
1896 attributes->domain_parameters,
1897 attributes->domain_parameters_size);
1898 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001899 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 }
1901 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001902 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 }
1904rsa_exit:
1905 mbedtls_mpi_free(&actual);
1906 mbedtls_mpi_free(&required);
1907 if (ret != 0) {
1908 return mbedtls_to_psa_error(ret);
1909 }
1910 } else
John Durkop9814fa22020-11-04 12:28:15 -08001911#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1912 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001913 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001915 }
1916 }
1917
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 if (attributes->core.bits != 0) {
1919 if (attributes->core.bits != slot->attr.bits) {
1920 return PSA_ERROR_INVALID_ARGUMENT;
1921 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001922 }
1923
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001925}
1926
Gilles Peskine449bd832023-01-11 14:50:10 +01001927psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1928 const uint8_t *data,
1929 size_t data_length,
1930 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001931{
1932 psa_status_t status;
1933 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001934 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001935 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301936 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001937
Ronald Cron81709fc2020-11-14 12:10:32 +01001938 *key = MBEDTLS_SVC_KEY_ID_INIT;
1939
Gilles Peskine0f84d622019-09-12 19:03:13 +02001940 /* Reject zero-length symmetric keys (including raw data key objects).
1941 * This also rejects any key which might be encoded as an empty string,
1942 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 if (data_length == 0) {
1944 return PSA_ERROR_INVALID_ARGUMENT;
1945 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001946
Archana449608b2021-09-08 15:36:05 +05301947 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 if (data_length > SIZE_MAX / 8) {
1949 return PSA_ERROR_NOT_SUPPORTED;
1950 }
Archana6ed4bda2021-08-04 10:47:15 +05301951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1953 &slot, &driver);
1954 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001955 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001957
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001958 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301959 * storage ( thus not in the case of importing a key in a secure element
1960 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1961 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 if (slot->key.data == NULL) {
1963 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301964 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 attributes, data, data_length, &storage_size);
1966 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301967 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 }
Archanad8a83dc2021-06-14 10:04:16 +05301969 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 status = psa_allocate_buffer_to_slot(slot, storage_size);
1971 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001972 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001974 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001975
1976 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 status = psa_driver_wrapper_import_key(attributes,
1978 data, data_length,
1979 slot->key.data,
1980 slot->key.bytes,
1981 &slot->key.bytes, &bits);
1982 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001983 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001985
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001987 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001989 status = PSA_ERROR_INVALID_ARGUMENT;
1990 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001991 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001992
Archana6ed4bda2021-08-04 10:47:15 +05301993 /* Enforce a size limit, and in particular ensure that the bit
1994 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05301996 status = PSA_ERROR_NOT_SUPPORTED;
1997 goto exit;
1998 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 status = psa_validate_optional_attributes(slot, attributes);
2000 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002001 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002003
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002005exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 if (status != PSA_SUCCESS) {
2007 psa_fail_key_creation(slot, driver);
2008 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002009
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002011}
2012
Gilles Peskined7729582019-08-05 15:55:54 +02002013#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2014psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002016{
2017 psa_status_t status;
2018 psa_key_slot_t *slot = NULL;
2019 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002020 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002021
2022 /* Leaving attributes unspecified is not currently supported.
2023 * It could make sense to query the key type and size from the
2024 * secure element, but not all secure elements support this
2025 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2027 return PSA_ERROR_NOT_SUPPORTED;
2028 }
2029 if (psa_get_key_bits(attributes) == 0) {
2030 return PSA_ERROR_NOT_SUPPORTED;
2031 }
Gilles Peskined7729582019-08-05 15:55:54 +02002032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2034 &slot, &driver);
2035 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002036 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 }
Gilles Peskined7729582019-08-05 15:55:54 +02002038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002040
2041exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 if (status != PSA_SUCCESS) {
2043 psa_fail_key_creation(slot, driver);
2044 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002045
Gilles Peskined7729582019-08-05 15:55:54 +02002046 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 psa_close_key(key);
2048 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002049}
2050#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2051
Gilles Peskine449bd832023-01-11 14:50:10 +01002052psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2053 const psa_key_attributes_t *specified_attributes,
2054 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002055{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002056 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002057 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002058 psa_key_slot_t *source_slot = NULL;
2059 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002060 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002061 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302062 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002063
Ronald Cron81709fc2020-11-14 12:10:32 +01002064 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2065
Archana8a180362021-07-05 02:18:48 +05302066 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2068 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002069 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002071
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 status = psa_validate_optional_attributes(source_slot,
2073 specified_attributes);
2074 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002075 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002077
Archana9d17bf42021-09-10 06:22:44 +05302078 /* The target key type and number of bits have been validated by
2079 * psa_validate_optional_attributes() to be either equal to zero or
2080 * equal to the ones of the source key. So it is safe to inherit
2081 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302082 * */
Archana9d17bf42021-09-10 06:22:44 +05302083 actual_attributes.core.bits = source_slot->attr.bits;
2084 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302085
2086
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 status = psa_restrict_key_policy(source_slot->attr.type,
2088 &actual_attributes.core.policy,
2089 &source_slot->attr.policy);
2090 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002091 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002093
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2095 &target_slot, &driver);
2096 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002097 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 }
2099 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2100 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302101 /*
Archana9d17bf42021-09-10 06:22:44 +05302102 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302103 * the source key would need to be exported as plaintext and re-imported
2104 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302105 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302106 * appropriate API invocations from the application, if needed.
2107 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002108 status = PSA_ERROR_NOT_SUPPORTED;
2109 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002110 }
Archana8a180362021-07-05 02:18:48 +05302111 /*
2112 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302113 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302114 * - For opaque keys this translates to an invocation of the drivers'
2115 * copy_key entry point through the dispatch layer.
2116 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2118 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2119 &storage_size);
2120 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302121 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 }
Archana374fe5b2021-09-08 15:50:28 +05302123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2125 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302126 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 }
Archana374fe5b2021-09-08 15:50:28 +05302128
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 status = psa_driver_wrapper_copy_key(&actual_attributes,
2130 source_slot->key.data,
2131 source_slot->key.bytes,
2132 target_slot->key.data,
2133 target_slot->key.bytes,
2134 &target_slot->key.bytes);
2135 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302136 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 }
2138 } else {
2139 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302140 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 source_slot->key.bytes);
2142 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302143 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 }
Archana8a180362021-07-05 02:18:48 +05302145 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002147exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 if (status != PSA_SUCCESS) {
2149 psa_fail_key_creation(target_slot, driver);
2150 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002151
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002155}
2156
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002157
2158
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002159/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002160/* Message digests */
2161/****************************************************************/
2162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002164{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002165 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if (operation->id == 0) {
2167 return PSA_SUCCESS;
2168 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002169
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002171 operation->id = 0;
2172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002174}
2175
Gilles Peskine449bd832023-01-11 14:50:10 +01002176psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2177 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002178{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002179 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002180
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002181 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002183 status = PSA_ERROR_BAD_STATE;
2184 goto exit;
2185 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002186
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002188 status = PSA_ERROR_INVALID_ARGUMENT;
2189 goto exit;
2190 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002191
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002192 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2193 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002197
2198exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 if (status != PSA_SUCCESS) {
2200 psa_hash_abort(operation);
2201 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002202
2203 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002204}
2205
Gilles Peskine449bd832023-01-11 14:50:10 +01002206psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2207 const uint8_t *input,
2208 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002209{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002210 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2211
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002213 status = PSA_ERROR_BAD_STATE;
2214 goto exit;
2215 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002216
Steven Cooremanc8288352021-03-04 14:02:19 +01002217 /* Don't require hash implementations to behave correctly on a
2218 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 if (input_length == 0) {
2220 return PSA_SUCCESS;
2221 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002222
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002224
2225exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 if (status != PSA_SUCCESS) {
2227 psa_hash_abort(operation);
2228 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002231}
2232
Gilles Peskine449bd832023-01-11 14:50:10 +01002233psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2234 uint8_t *hash,
2235 size_t hash_size,
2236 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002237{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002238 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 if (operation->id == 0) {
2240 return PSA_ERROR_BAD_STATE;
2241 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002242
Steven Cooreman1e582352021-02-18 17:24:37 +01002243 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 operation, hash, hash_size, hash_length);
2245 psa_hash_abort(operation);
2246 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002247}
2248
Gilles Peskine449bd832023-01-11 14:50:10 +01002249psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2250 const uint8_t *hash,
2251 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002252{
Ronald Cron69a63422021-10-18 09:47:58 +02002253 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002254 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002255 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 operation,
2257 actual_hash, sizeof(actual_hash),
2258 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002259
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002261 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002263
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002265 status = PSA_ERROR_INVALID_SIGNATURE;
2266 goto exit;
2267 }
2268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002270 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002272
2273exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2275 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002276 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002278
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002280}
2281
Gilles Peskine449bd832023-01-11 14:50:10 +01002282psa_status_t psa_hash_compute(psa_algorithm_t alg,
2283 const uint8_t *input, size_t input_length,
2284 uint8_t *hash, size_t hash_size,
2285 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002286{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002287 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 if (!PSA_ALG_IS_HASH(alg)) {
2289 return PSA_ERROR_INVALID_ARGUMENT;
2290 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2293 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002294}
2295
Gilles Peskine449bd832023-01-11 14:50:10 +01002296psa_status_t psa_hash_compare(psa_algorithm_t alg,
2297 const uint8_t *input, size_t input_length,
2298 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002299{
Ronald Cron69a63422021-10-18 09:47:58 +02002300 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002301 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 if (!PSA_ALG_IS_HASH(alg)) {
2304 return PSA_ERROR_INVALID_ARGUMENT;
2305 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002306
Steven Cooreman1e582352021-02-18 17:24:37 +01002307 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 alg, input, input_length,
2309 actual_hash, sizeof(actual_hash),
2310 &actual_hash_length);
2311 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002312 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 }
2314 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002315 status = PSA_ERROR_INVALID_SIGNATURE;
2316 goto exit;
2317 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002319 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002321
2322exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2324 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002325}
2326
Gilles Peskine449bd832023-01-11 14:50:10 +01002327psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2328 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002329{
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 if (source_operation->id == 0 ||
2331 target_operation->id != 0) {
2332 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002333 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2336 target_operation);
2337 if (status != PSA_SUCCESS) {
2338 psa_hash_abort(target_operation);
2339 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002342}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002343
2344
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002345/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002346/* MAC */
2347/****************************************************************/
2348
Gilles Peskine449bd832023-01-11 14:50:10 +01002349psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002350{
Steven Cooremane6804192021-03-19 18:28:56 +01002351 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 if (operation->id == 0) {
2353 return PSA_SUCCESS;
2354 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002355
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002357 operation->mac_size = 0;
2358 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002359 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002360
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002362}
2363
Ronald Cron2dff3b22021-06-17 16:33:22 +02002364static psa_status_t psa_mac_finalize_alg_and_key_validation(
2365 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002366 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002368{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002369 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 psa_key_type_t key_type = psa_get_key_type(attributes);
2371 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002372
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 if (!PSA_ALG_IS_MAC(alg)) {
2374 return PSA_ERROR_INVALID_ARGUMENT;
2375 }
Steven Cooremane6804192021-03-19 18:28:56 +01002376
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002377 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 status = psa_mac_key_can_do(alg, key_type);
2379 if (status != PSA_SUCCESS) {
2380 return status;
2381 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002382
Steven Cooremand1a68f12021-05-10 11:13:41 +02002383 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002387 /* A very short MAC is too short for security since it can be
2388 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2389 * so we make this our minimum, even though 32 bits is still
2390 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002392 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002393
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2395 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002396 /* It's impossible to "truncate" to a larger length than the full length
2397 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002399 }
2400
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002402 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2403 * that is disabled in the compile-time configuration. The result can
2404 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2405 * configuration into account. In this case, force a return of
2406 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2407 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2408 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2409 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2410 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002412 }
2413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002415}
2416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2418 mbedtls_svc_key_id_t key,
2419 psa_algorithm_t alg,
2420 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002421{
2422 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2423 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002424 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002425
2426 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002428 status = PSA_ERROR_BAD_STATE;
2429 goto exit;
2430 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002431
2432 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 key,
2434 &slot,
2435 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2436 alg);
2437 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002438 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002440
2441 psa_key_attributes_t attributes = {
2442 .core = slot->attr
2443 };
2444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2446 &operation->mac_size);
2447 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002448 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002450
2451 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002452 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 if (is_sign) {
2454 status = psa_driver_wrapper_mac_sign_setup(operation,
2455 &attributes,
2456 slot->key.data,
2457 slot->key.bytes,
2458 alg);
2459 } else {
2460 status = psa_driver_wrapper_mac_verify_setup(operation,
2461 &attributes,
2462 slot->key.data,
2463 slot->key.bytes,
2464 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002465 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002466
Gilles Peskinefbfac682018-07-08 20:51:54 +02002467exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 if (status != PSA_SUCCESS) {
2469 psa_mac_abort(operation);
2470 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002471
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002473
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002475}
2476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2478 mbedtls_svc_key_id_t key,
2479 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002480{
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002482}
2483
Gilles Peskine449bd832023-01-11 14:50:10 +01002484psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2485 mbedtls_svc_key_id_t key,
2486 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002487{
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002489}
2490
Gilles Peskine449bd832023-01-11 14:50:10 +01002491psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2492 const uint8_t *input,
2493 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002494{
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 if (operation->id == 0) {
2496 return PSA_ERROR_BAD_STATE;
2497 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002498
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002499 /* Don't require hash implementations to behave correctly on a
2500 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 if (input_length == 0) {
2502 return PSA_SUCCESS;
2503 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002504
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2506 input, input_length);
2507 if (status != PSA_SUCCESS) {
2508 psa_mac_abort(operation);
2509 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002510
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002512}
2513
Gilles Peskine449bd832023-01-11 14:50:10 +01002514psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2515 uint8_t *mac,
2516 size_t mac_size,
2517 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002518{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002519 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2520 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002521
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002523 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002524 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002525 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002526
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002528 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002529 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002530 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002531
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002532 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2533 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002535 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002536 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002537 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002538
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002540 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002541 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002542 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002543
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 status = psa_driver_wrapper_mac_sign_finish(operation,
2545 mac, operation->mac_size,
2546 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002547
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002548exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002549 /* In case of success, set the potential excess room in the output buffer
2550 * to an invalid value, to avoid potentially leaking a longer MAC.
2551 * In case of error, set the output length and content to a safe default,
2552 * such that in case the caller misses an error check, the output would be
2553 * an unachievable MAC.
2554 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002556 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002557 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002558 }
mohammad16036df908f2018-04-02 08:34:15 -07002559
Paul Elliottdc42ca82023-02-24 18:11:59 +00002560 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002561
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002565}
2566
Gilles Peskine449bd832023-01-11 14:50:10 +01002567psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2568 const uint8_t *mac,
2569 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002570{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002571 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2572 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002573
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002575 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002576 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002577 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002578
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002580 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002581 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002582 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002583
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002585 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002586 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002587 }
2588
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 status = psa_driver_wrapper_mac_verify_finish(operation,
2590 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002591
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002592exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002594
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002596}
2597
Gilles Peskine449bd832023-01-11 14:50:10 +01002598static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2599 psa_algorithm_t alg,
2600 const uint8_t *input,
2601 size_t input_length,
2602 uint8_t *mac,
2603 size_t mac_size,
2604 size_t *mac_length,
2605 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002606{
2607 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002608 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2609 psa_key_slot_t *slot;
2610 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002611
Ronald Cron51131b52021-06-17 17:17:20 +02002612 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 key,
2614 &slot,
2615 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2616 alg);
2617 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002618 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002620
Ronald Cron51131b52021-06-17 17:17:20 +02002621 psa_key_attributes_t attributes = {
2622 .core = slot->attr
2623 };
2624
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2626 &operation_mac_size);
2627 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002628 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002630
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002632 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002633 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002634 }
2635
2636 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 &attributes,
2638 slot->key.data, slot->key.bytes,
2639 alg,
2640 input, input_length,
2641 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002642
2643exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002644 /* In case of success, set the potential excess room in the output buffer
2645 * to an invalid value, to avoid potentially leaking a longer MAC.
2646 * In case of error, set the output length and content to a safe default,
2647 * such that in case the caller misses an error check, the output would be
2648 * an unachievable MAC.
2649 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002651 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002652 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002653 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002654
2655 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002656
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002658
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002660}
2661
Gilles Peskine449bd832023-01-11 14:50:10 +01002662psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002663 psa_algorithm_t alg,
2664 const uint8_t *input,
2665 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 uint8_t *mac,
2667 size_t mac_size,
2668 size_t *mac_length)
2669{
2670 return psa_mac_compute_internal(key, alg,
2671 input, input_length,
2672 mac, mac_size, mac_length, 1);
2673}
2674
2675psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2676 psa_algorithm_t alg,
2677 const uint8_t *input,
2678 size_t input_length,
2679 const uint8_t *mac,
2680 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002681{
2682 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002683 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2684 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002685
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 status = psa_mac_compute_internal(key, alg,
2687 input, input_length,
2688 actual_mac, sizeof(actual_mac),
2689 &actual_mac_length, 0);
2690 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002691 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002693
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002695 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002696 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002697 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
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 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002702
2703exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002705
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002707}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002708
Gilles Peskinee59236f2018-01-27 23:32:46 +01002709/****************************************************************/
2710/* Asymmetric cryptography */
2711/****************************************************************/
2712
Gilles Peskine449bd832023-01-11 14:50:10 +01002713static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2714 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002715{
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 if (input_is_message) {
2717 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2718 return PSA_ERROR_INVALID_ARGUMENT;
2719 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002720
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2722 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2723 return PSA_ERROR_INVALID_ARGUMENT;
2724 }
2725 }
2726 } else {
2727 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2728 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002729 }
2730 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002731
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002733}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2736 int input_is_message,
2737 psa_algorithm_t alg,
2738 const uint8_t *input,
2739 size_t input_length,
2740 uint8_t *signature,
2741 size_t signature_size,
2742 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002743{
2744 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2745 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2746 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002747
2748 *signature_length = 0;
2749
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 status = psa_sign_verify_check_alg(input_is_message, alg);
2751 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002752 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002754
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002755 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002756 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002757 * buffer can in principle be empty since it doesn't actually have
2758 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 if (signature_size == 0) {
2760 return PSA_ERROR_BUFFER_TOO_SMALL;
2761 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002762
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002763 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 key, &slot,
2765 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2766 PSA_KEY_USAGE_SIGN_HASH,
2767 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002768
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002770 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002772
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002774 status = PSA_ERROR_INVALID_ARGUMENT;
2775 goto exit;
2776 }
2777
2778 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002780 };
2781
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002783 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002784 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002785 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 signature, signature_size, signature_length);
2787 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002788
2789 status = psa_driver_wrapper_sign_hash(
2790 &attributes, slot->key.data, slot->key.bytes,
2791 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002792 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002793 }
2794
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002795
2796exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002797 psa_wipe_tag_output_buffer(signature, status, signature_size,
2798 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002799
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002801
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002803}
2804
Gilles Peskine449bd832023-01-11 14:50:10 +01002805static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2806 int input_is_message,
2807 psa_algorithm_t alg,
2808 const uint8_t *input,
2809 size_t input_length,
2810 const uint8_t *signature,
2811 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002812{
2813 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2814 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2815 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002816
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 status = psa_sign_verify_check_alg(input_is_message, alg);
2818 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002819 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002821
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002822 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002823 key, &slot,
2824 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2825 PSA_KEY_USAGE_VERIFY_HASH,
2826 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002827
Gilles Peskine449bd832023-01-11 14:50:10 +01002828 if (status != PSA_SUCCESS) {
2829 return status;
2830 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002831
2832 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002834 };
2835
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002837 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002838 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002839 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 signature, signature_length);
2841 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002842 status = psa_driver_wrapper_verify_hash(
2843 &attributes, slot->key.data, slot->key.bytes,
2844 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002846 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002847
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002849
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002851
2852}
2853
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002854psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002855 const psa_key_attributes_t *attributes,
2856 const uint8_t *key_buffer,
2857 size_t key_buffer_size,
2858 psa_algorithm_t alg,
2859 const uint8_t *input,
2860 size_t input_length,
2861 uint8_t *signature,
2862 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002864{
2865 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002866
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002868 size_t hash_length;
2869 uint8_t hash[PSA_HASH_MAX_SIZE];
2870
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002871 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 PSA_ALG_SIGN_GET_HASH(alg),
2873 input, input_length,
2874 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002875
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002877 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002879
2880 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002881 attributes, key_buffer, key_buffer_size,
2882 alg, hash, hash_length,
2883 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002884 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002885
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002887}
2888
Gilles Peskine449bd832023-01-11 14:50:10 +01002889psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2890 psa_algorithm_t alg,
2891 const uint8_t *input,
2892 size_t input_length,
2893 uint8_t *signature,
2894 size_t signature_size,
2895 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002896{
2897 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002898 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002900}
2901
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002902psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002903 const psa_key_attributes_t *attributes,
2904 const uint8_t *key_buffer,
2905 size_t key_buffer_size,
2906 psa_algorithm_t alg,
2907 const uint8_t *input,
2908 size_t input_length,
2909 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002911{
2912 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002913
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002915 size_t hash_length;
2916 uint8_t hash[PSA_HASH_MAX_SIZE];
2917
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002918 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 PSA_ALG_SIGN_GET_HASH(alg),
2920 input, input_length,
2921 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002922
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002924 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002926
2927 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002928 attributes, key_buffer, key_buffer_size,
2929 alg, hash, hash_length,
2930 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002931 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002932
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002934}
2935
Gilles Peskine449bd832023-01-11 14:50:10 +01002936psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2937 psa_algorithm_t alg,
2938 const uint8_t *input,
2939 size_t input_length,
2940 const uint8_t *signature,
2941 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002942{
2943 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002944 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002946}
2947
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002948psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002949 const psa_key_attributes_t *attributes,
2950 const uint8_t *key_buffer, size_t key_buffer_size,
2951 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002953{
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2955 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2956 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002957#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2959 return mbedtls_psa_rsa_sign_hash(
2960 attributes,
2961 key_buffer, key_buffer_size,
2962 alg, hash, hash_length,
2963 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002964#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2965 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 } else {
2967 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002968 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2970 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002971#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2973 return mbedtls_psa_ecdsa_sign_hash(
2974 attributes,
2975 key_buffer, key_buffer_size,
2976 alg, hash, hash_length,
2977 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002978#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2979 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 } else {
2981 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002982 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002983 }
Ronald Cron4501c982021-03-19 20:09:32 +01002984
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 (void) key_buffer;
2986 (void) key_buffer_size;
2987 (void) hash;
2988 (void) hash_length;
2989 (void) signature;
2990 (void) signature_size;
2991 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002992
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002994}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002995
Gilles Peskine449bd832023-01-11 14:50:10 +01002996psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2997 psa_algorithm_t alg,
2998 const uint8_t *hash,
2999 size_t hash_length,
3000 uint8_t *signature,
3001 size_t signature_size,
3002 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003003{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003004 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003005 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003007}
3008
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003009psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003010 const psa_key_attributes_t *attributes,
3011 const uint8_t *key_buffer, size_t key_buffer_size,
3012 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003014{
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3016 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3017 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003018#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3020 return mbedtls_psa_rsa_verify_hash(
3021 attributes,
3022 key_buffer, key_buffer_size,
3023 alg, hash, hash_length,
3024 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003025#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3026 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 } else {
3028 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003029 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3031 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003032#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3034 return mbedtls_psa_ecdsa_verify_hash(
3035 attributes,
3036 key_buffer, key_buffer_size,
3037 alg, hash, hash_length,
3038 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003039#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3040 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 } else {
3042 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003043 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003044 }
Ronald Cron4501c982021-03-19 20:09:32 +01003045
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 (void) key_buffer;
3047 (void) key_buffer_size;
3048 (void) hash;
3049 (void) hash_length;
3050 (void) signature;
3051 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003052
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003054}
3055
Gilles Peskine449bd832023-01-11 14:50:10 +01003056psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3057 psa_algorithm_t alg,
3058 const uint8_t *hash,
3059 size_t hash_length,
3060 const uint8_t *signature,
3061 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003062{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003063 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003064 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003066}
3067
Gilles Peskine449bd832023-01-11 14:50:10 +01003068psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3069 psa_algorithm_t alg,
3070 const uint8_t *input,
3071 size_t input_length,
3072 const uint8_t *salt,
3073 size_t salt_length,
3074 uint8_t *output,
3075 size_t output_size,
3076 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003077{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003078 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003079 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003080 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003081
Darryl Green5cc689a2018-07-24 15:34:10 +01003082 (void) input;
3083 (void) input_length;
3084 (void) salt;
3085 (void) output;
3086 (void) output_size;
3087
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003088 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003089
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3091 return PSA_ERROR_INVALID_ARGUMENT;
3092 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003093
Ronald Cron5c522922020-11-14 16:35:34 +01003094 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003095 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3096 if (status != PSA_SUCCESS) {
3097 return status;
3098 }
3099 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3100 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003101 status = PSA_ERROR_INVALID_ARGUMENT;
3102 goto exit;
3103 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003104
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003105 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003107 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003108
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003109 status = psa_driver_wrapper_asymmetric_encrypt(
3110 &attributes, slot->key.data, slot->key.bytes,
3111 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003113exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003115
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003117}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003118
Gilles Peskine449bd832023-01-11 14:50:10 +01003119psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3120 psa_algorithm_t alg,
3121 const uint8_t *input,
3122 size_t input_length,
3123 const uint8_t *salt,
3124 size_t salt_length,
3125 uint8_t *output,
3126 size_t output_size,
3127 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003128{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003129 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003130 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003131 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003132
Darryl Green5cc689a2018-07-24 15:34:10 +01003133 (void) input;
3134 (void) input_length;
3135 (void) salt;
3136 (void) output;
3137 (void) output_size;
3138
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003139 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003140
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3142 return PSA_ERROR_INVALID_ARGUMENT;
3143 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003144
Ronald Cron5c522922020-11-14 16:35:34 +01003145 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3147 if (status != PSA_SUCCESS) {
3148 return status;
3149 }
3150 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003151 status = PSA_ERROR_INVALID_ARGUMENT;
3152 goto exit;
3153 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003154
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003155 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003156 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003157 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003158
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003159 status = psa_driver_wrapper_asymmetric_decrypt(
3160 &attributes, slot->key.data, slot->key.bytes,
3161 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003163
3164exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003166
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003168}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003169
Paul Elliott9fe12f62022-11-30 19:16:02 +00003170/****************************************************************/
3171/* Asymmetric interruptible cryptography */
3172/****************************************************************/
3173
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003174static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3175
Paul Elliott9fe12f62022-11-30 19:16:02 +00003176void psa_interruptible_set_max_ops(uint32_t max_ops)
3177{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003178 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003179}
3180
3181uint32_t psa_interruptible_get_max_ops(void)
3182{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003183 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003184}
3185
Paul Elliott9fe12f62022-11-30 19:16:02 +00003186uint32_t psa_sign_hash_get_num_ops(
3187 const psa_sign_hash_interruptible_operation_t *operation)
3188{
Paul Elliott296ede92022-12-15 17:00:30 +00003189 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003190}
3191
3192uint32_t psa_verify_hash_get_num_ops(
3193 const psa_verify_hash_interruptible_operation_t *operation)
3194{
Paul Elliott296ede92022-12-15 17:00:30 +00003195 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003196}
3197
Paul Elliott59ad9452022-12-18 15:09:02 +00003198static psa_status_t psa_sign_hash_abort_internal(
3199 psa_sign_hash_interruptible_operation_t *operation)
3200{
3201 if (operation->id == 0) {
3202 /* The object has (apparently) been initialized but it is not (yet)
3203 * in use. It's ok to call abort on such an object, and there's
3204 * nothing to do. */
3205 return PSA_SUCCESS;
3206 }
3207
3208 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3209
3210 status = psa_driver_wrapper_sign_hash_abort(operation);
3211
3212 operation->id = 0;
3213
Paul Elliotte6145dc2023-02-07 12:51:21 +00003214 /* Do not clear either the error_occurred or num_ops elements here as they
3215 * only want to be cleared by the application calling abort, not by abort
3216 * being called at completion of an operation. */
3217
Paul Elliott59ad9452022-12-18 15:09:02 +00003218 return status;
3219}
3220
Paul Elliott9fe12f62022-11-30 19:16:02 +00003221psa_status_t psa_sign_hash_start(
3222 psa_sign_hash_interruptible_operation_t *operation,
3223 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3224 const uint8_t *hash, size_t hash_length)
3225{
3226 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3227 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3228 psa_key_slot_t *slot;
3229
Paul Elliottc9774412023-02-06 15:14:07 +00003230 /* Check that start has not been previously called, or operation has not
3231 * previously errored. */
3232 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003233 return PSA_ERROR_BAD_STATE;
3234 }
3235
Paul Elliott9fe12f62022-11-30 19:16:02 +00003236 status = psa_sign_verify_check_alg(0, alg);
3237 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003238 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003239 return status;
3240 }
3241
3242 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3243 PSA_KEY_USAGE_SIGN_HASH,
3244 alg);
3245
3246 if (status != PSA_SUCCESS) {
3247 goto exit;
3248 }
3249
3250 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3251 status = PSA_ERROR_INVALID_ARGUMENT;
3252 goto exit;
3253 }
3254
3255 psa_key_attributes_t attributes = {
3256 .core = slot->attr
3257 };
3258
Paul Elliott296ede92022-12-15 17:00:30 +00003259 /* Ensure ops count gets reset, in case of operation re-use. */
3260 operation->num_ops = 0;
3261
Paul Elliott9fe12f62022-11-30 19:16:02 +00003262 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3263 slot->key.data,
3264 slot->key.bytes, alg,
3265 hash, hash_length);
3266exit:
3267
3268 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003269 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003270 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003271 }
3272
3273 unlock_status = psa_unlock_key_slot(slot);
3274
Paul Elliottc9774412023-02-06 15:14:07 +00003275 if (unlock_status != PSA_SUCCESS) {
3276 operation->error_occurred = 1;
3277 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003278
Paul Elliottc9774412023-02-06 15:14:07 +00003279 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003280}
3281
3282
3283psa_status_t psa_sign_hash_complete(
3284 psa_sign_hash_interruptible_operation_t *operation,
3285 uint8_t *signature, size_t signature_size,
3286 size_t *signature_length)
3287{
3288 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3289
3290 *signature_length = 0;
3291
Paul Elliottc9774412023-02-06 15:14:07 +00003292 /* Check that start has been called first, and that operation has not
3293 * previously errored. */
3294 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003295 status = PSA_ERROR_BAD_STATE;
3296 goto exit;
3297 }
3298
Paul Elliott096abc42023-02-03 18:33:23 +00003299 /* Immediately reject a zero-length signature buffer. This guarantees that
3300 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003301 if (signature_size == 0) {
3302 status = PSA_ERROR_BUFFER_TOO_SMALL;
3303 goto exit;
3304 }
3305
3306 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3307 signature_size,
3308 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003309
Paul Elliott296ede92022-12-15 17:00:30 +00003310 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003311 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003312
Paul Elliottebe225c2023-02-10 14:32:53 +00003313exit:
3314
Paul Elliott59200a22023-02-21 15:07:40 +00003315 psa_wipe_tag_output_buffer(signature, status, signature_size,
3316 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003317
Paul Elliott53bb3122023-02-10 14:22:22 +00003318 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003319 if (status != PSA_SUCCESS) {
3320 operation->error_occurred = 1;
3321 }
3322
Paul Elliott59ad9452022-12-18 15:09:02 +00003323 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003324 }
3325
3326 return status;
3327}
3328
3329psa_status_t psa_sign_hash_abort(
3330 psa_sign_hash_interruptible_operation_t *operation)
3331{
Paul Elliott59ad9452022-12-18 15:09:02 +00003332 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3333
3334 status = psa_sign_hash_abort_internal(operation);
3335
3336 /* We clear the number of ops done here, so that it is not cleared when
3337 * the operation fails or succeeds, only on manual abort. */
3338 operation->num_ops = 0;
3339
Paul Elliottc9774412023-02-06 15:14:07 +00003340 /* Likewise, failure state. */
3341 operation->error_occurred = 0;
3342
Paul Elliott59ad9452022-12-18 15:09:02 +00003343 return status;
3344}
3345
3346static psa_status_t psa_verify_hash_abort_internal(
3347 psa_verify_hash_interruptible_operation_t *operation)
3348{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003349 if (operation->id == 0) {
3350 /* The object has (apparently) been initialized but it is not (yet)
3351 * in use. It's ok to call abort on such an object, and there's
3352 * nothing to do. */
3353 return PSA_SUCCESS;
3354 }
3355
Paul Elliott59ad9452022-12-18 15:09:02 +00003356 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3357
3358 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003359
3360 operation->id = 0;
3361
Paul Elliotte6145dc2023-02-07 12:51:21 +00003362 /* Do not clear either the error_occurred or num_ops elements here as they
3363 * only want to be cleared by the application calling abort, not by abort
3364 * being called at completion of an operation. */
3365
Paul Elliott59ad9452022-12-18 15:09:02 +00003366 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003367}
3368
3369psa_status_t psa_verify_hash_start(
3370 psa_verify_hash_interruptible_operation_t *operation,
3371 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3372 const uint8_t *hash, size_t hash_length,
3373 const uint8_t *signature, size_t signature_length)
3374{
3375 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3376 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3377 psa_key_slot_t *slot;
3378
Paul Elliottc9774412023-02-06 15:14:07 +00003379 /* Check that start has not been previously called, or operation has not
3380 * previously errored. */
3381 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003382 return PSA_ERROR_BAD_STATE;
3383 }
3384
3385 status = psa_sign_verify_check_alg(0, alg);
3386 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003387 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003388 return status;
3389 }
3390
3391 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3392 PSA_KEY_USAGE_VERIFY_HASH,
3393 alg);
3394
3395 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003396 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003397 return status;
3398 }
3399
3400 psa_key_attributes_t attributes = {
3401 .core = slot->attr
3402 };
3403
Paul Elliott296ede92022-12-15 17:00:30 +00003404 /* Ensure ops count gets reset, in case of operation re-use. */
3405 operation->num_ops = 0;
3406
Paul Elliott9fe12f62022-11-30 19:16:02 +00003407 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3408 slot->key.data,
3409 slot->key.bytes,
3410 alg, hash, hash_length,
3411 signature, signature_length);
3412
3413 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003414 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003415 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003416 }
3417
3418 unlock_status = psa_unlock_key_slot(slot);
3419
Paul Elliottc9774412023-02-06 15:14:07 +00003420 if (unlock_status != PSA_SUCCESS) {
3421 operation->error_occurred = 1;
3422 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003423
Paul Elliottc9774412023-02-06 15:14:07 +00003424 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003425}
3426
3427psa_status_t psa_verify_hash_complete(
3428 psa_verify_hash_interruptible_operation_t *operation)
3429{
3430 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3431
Paul Elliottc9774412023-02-06 15:14:07 +00003432 /* Check that start has been called first, and that operation has not
3433 * previously errored. */
3434 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003435 status = PSA_ERROR_BAD_STATE;
3436 goto exit;
3437 }
3438
3439 status = psa_driver_wrapper_verify_hash_complete(operation);
3440
Paul Elliott296ede92022-12-15 17:00:30 +00003441 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003442 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003443 operation);
3444
Paul Elliottebe225c2023-02-10 14:32:53 +00003445exit:
3446
Paul Elliott9fe12f62022-11-30 19:16:02 +00003447 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003448 if (status != PSA_SUCCESS) {
3449 operation->error_occurred = 1;
3450 }
3451
Paul Elliott59ad9452022-12-18 15:09:02 +00003452 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003453 }
3454
3455 return status;
3456}
3457
3458psa_status_t psa_verify_hash_abort(
3459 psa_verify_hash_interruptible_operation_t *operation)
3460{
Paul Elliott59ad9452022-12-18 15:09:02 +00003461 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003462
Paul Elliott59ad9452022-12-18 15:09:02 +00003463 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003464
Paul Elliott59ad9452022-12-18 15:09:02 +00003465 /* We clear the number of ops done here, so that it is not cleared when
3466 * the operation fails or succeeds, only on manual abort. */
3467 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003468
Paul Elliottc9774412023-02-06 15:14:07 +00003469 /* Likewise, failure state. */
3470 operation->error_occurred = 0;
3471
Paul Elliott59ad9452022-12-18 15:09:02 +00003472 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003473}
3474
Paul Elliott588f8ed2022-12-02 18:10:26 +00003475/****************************************************************/
3476/* Asymmetric interruptible cryptography internal */
3477/* implementations */
3478/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003479
Paul Elliott588f8ed2022-12-02 18:10:26 +00003480void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3481{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003482
Paul Elliott588f8ed2022-12-02 18:10:26 +00003483#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3484 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3485 defined(MBEDTLS_ECP_RESTARTABLE)
3486
3487 /* Internal implementation uses zero to indicate infinite number max ops,
3488 * therefore avoid this value, and set to minimum possible. */
3489 if (max_ops == 0) {
3490 max_ops = 1;
3491 }
3492
Paul Elliott588f8ed2022-12-02 18:10:26 +00003493 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003494#else
3495 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003496#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3497 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3498 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3499}
3500
Paul Elliott588f8ed2022-12-02 18:10:26 +00003501uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003502 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003503{
3504#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3505 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3506 defined(MBEDTLS_ECP_RESTARTABLE)
3507
Paul Elliott93d9ca82023-02-15 18:14:21 +00003508 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003509#else
3510 (void) operation;
3511 return 0;
3512#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3513 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3514 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3515}
3516
3517uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003518 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003519{
3520 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3521 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3522 defined(MBEDTLS_ECP_RESTARTABLE)
3523
Paul Elliott93d9ca82023-02-15 18:14:21 +00003524 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003525#else
3526 (void) operation;
3527 return 0;
3528#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3529 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3530 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3531}
3532
3533psa_status_t mbedtls_psa_sign_hash_start(
3534 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3535 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3536 size_t key_buffer_size, psa_algorithm_t alg,
3537 const uint8_t *hash, size_t hash_length)
3538{
3539 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003540 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003541
Paul Elliott068fe072023-01-16 13:59:15 +00003542 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3543 return PSA_ERROR_NOT_SUPPORTED;
3544 }
3545
3546 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003547 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003548 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003549
3550#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003551 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3552 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003553
Paul Elliotta1c94092023-02-15 16:38:04 +00003554 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3555
Paul Elliott93d9ca82023-02-15 18:14:21 +00003556 /* Ensure num_ops is zero'ed in case of context re-use. */
3557 operation->num_ops = 0;
3558
Paul Elliott068fe072023-01-16 13:59:15 +00003559 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3560 attributes->core.bits,
3561 key_buffer,
3562 key_buffer_size,
3563 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003564
Paul Elliott068fe072023-01-16 13:59:15 +00003565 if (status != PSA_SUCCESS) {
3566 return status;
3567 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003568
Paul Elliott1bc59df2023-02-05 13:41:57 +00003569 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003570 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003571
Paul Elliott068fe072023-01-16 13:59:15 +00003572 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3573 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3574 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003575
Paul Elliott0290a762023-02-09 14:30:24 +00003576 /* We only need to store the same length of hash as the private key size
3577 * here, it would be truncated by the internal implementation anyway. */
3578 required_hash_length = (hash_length < operation->coordinate_bytes ?
3579 hash_length : operation->coordinate_bytes);
3580
Paul Elliottba70ad42023-02-15 18:23:53 +00003581 if (required_hash_length > sizeof(operation->hash)) {
3582 /* Shouldn't happen, but better safe than sorry. */
3583 return PSA_ERROR_CORRUPTION_DETECTED;
3584 }
3585
Paul Elliott0290a762023-02-09 14:30:24 +00003586 memcpy(operation->hash, hash, required_hash_length);
3587 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003588
3589 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003590
3591#else
Paul Elliott068fe072023-01-16 13:59:15 +00003592 (void) operation;
3593 (void) key_buffer;
3594 (void) key_buffer_size;
3595 (void) alg;
3596 (void) hash;
3597 (void) hash_length;
3598 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003599 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003600
Paul Elliott068fe072023-01-16 13:59:15 +00003601 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003602#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3603 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3604 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003605}
3606
3607psa_status_t mbedtls_psa_sign_hash_complete(
3608 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3609 uint8_t *signature, size_t signature_size,
3610 size_t *signature_length)
3611{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003612#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3613 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3614 defined(MBEDTLS_ECP_RESTARTABLE)
3615
Paul Elliotta1c94092023-02-15 16:38:04 +00003616 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003617 mbedtls_mpi r;
3618 mbedtls_mpi s;
3619
Paul Elliott46845252023-02-03 14:59:11 +00003620 mbedtls_mpi_init(&r);
3621 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003622
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003623 /* Ensure max_ops is set to the current value (or default). */
3624 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3625
Paul Elliotta1c94092023-02-15 16:38:04 +00003626 if (signature_size < 2 * operation->coordinate_bytes) {
3627 status = PSA_ERROR_BUFFER_TOO_SMALL;
3628 goto exit;
3629 }
3630
Paul Elliott588f8ed2022-12-02 18:10:26 +00003631 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003632
Paul Elliott588f8ed2022-12-02 18:10:26 +00003633#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3634 status = mbedtls_to_psa_error(
3635 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003636 &r,
3637 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003638 &operation->ctx->d,
3639 operation->hash,
3640 operation->hash_length,
3641 operation->md_alg,
3642 mbedtls_psa_get_random,
3643 MBEDTLS_PSA_RANDOM_STATE,
3644 &operation->restart_ctx));
3645#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003646 status = PSA_ERROR_NOT_SUPPORTED;
3647 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003648#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3649 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003650 status = mbedtls_to_psa_error(
3651 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003652 &r,
3653 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003654 &operation->ctx->d,
3655 operation->hash,
3656 operation->hash_length,
3657 mbedtls_psa_get_random,
3658 MBEDTLS_PSA_RANDOM_STATE,
3659 mbedtls_psa_get_random,
3660 MBEDTLS_PSA_RANDOM_STATE,
3661 &operation->restart_ctx));
3662 }
3663
Paul Elliottf8e5b562023-02-19 18:43:45 +00003664 /* Hide the fact that the restart context only holds a delta of number of
3665 * ops done during the last operation, not an absolute value. */
3666 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3667
Paul Elliott724bd252023-02-08 12:35:08 +00003668 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003669 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003670 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003671 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003672 operation->coordinate_bytes)
3673 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003674
3675 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003676 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003677 }
3678
3679 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003680 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003681 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003682 operation->coordinate_bytes,
3683 operation->coordinate_bytes)
3684 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003685
3686 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003687 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003688 }
3689
Paul Elliott1bc59df2023-02-05 13:41:57 +00003690 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003691
Paul Elliott724bd252023-02-08 12:35:08 +00003692 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003693 }
Paul Elliott724bd252023-02-08 12:35:08 +00003694
3695exit:
3696
3697 mbedtls_mpi_free(&r);
3698 mbedtls_mpi_free(&s);
3699 return status;
3700
Paul Elliott588f8ed2022-12-02 18:10:26 +00003701 #else
3702
3703 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003704 (void) signature;
3705 (void) signature_size;
3706 (void) signature_length;
3707
3708 return PSA_ERROR_NOT_SUPPORTED;
3709
3710#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3711 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3712 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3713}
3714
3715psa_status_t mbedtls_psa_sign_hash_abort(
3716 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3717{
3718
3719#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3720 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3721 defined(MBEDTLS_ECP_RESTARTABLE)
3722
3723 if (operation->ctx) {
3724 mbedtls_ecdsa_free(operation->ctx);
3725 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003726 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003727 }
3728
3729 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3730
Paul Elliott93d9ca82023-02-15 18:14:21 +00003731 operation->num_ops = 0;
3732
Paul Elliott588f8ed2022-12-02 18:10:26 +00003733 return PSA_SUCCESS;
3734
3735#else
3736
3737 (void) operation;
3738
3739 return PSA_ERROR_NOT_SUPPORTED;
3740
3741#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3742 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3743 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3744}
3745
3746psa_status_t mbedtls_psa_verify_hash_start(
3747 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3748 const psa_key_attributes_t *attributes,
3749 const uint8_t *key_buffer, size_t key_buffer_size,
3750 psa_algorithm_t alg,
3751 const uint8_t *hash, size_t hash_length,
3752 const uint8_t *signature, size_t signature_length)
3753{
3754 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003755 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003756 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003757
Paul Elliott068fe072023-01-16 13:59:15 +00003758 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3759 return PSA_ERROR_NOT_SUPPORTED;
3760 }
3761
3762 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003763 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003764 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003765
3766#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003767 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3768 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003769
Paul Elliotta1c94092023-02-15 16:38:04 +00003770 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3771 mbedtls_mpi_init(&operation->r);
3772 mbedtls_mpi_init(&operation->s);
3773
Paul Elliott93d9ca82023-02-15 18:14:21 +00003774 /* Ensure num_ops is zero'ed in case of context re-use. */
3775 operation->num_ops = 0;
3776
Paul Elliott068fe072023-01-16 13:59:15 +00003777 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3778 attributes->core.bits,
3779 key_buffer,
3780 key_buffer_size,
3781 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003782
Paul Elliott068fe072023-01-16 13:59:15 +00003783 if (status != PSA_SUCCESS) {
3784 return status;
3785 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003786
Paul Elliottc569fc22023-02-10 13:02:54 +00003787 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003788
Paul Elliott1bc59df2023-02-05 13:41:57 +00003789 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003790 return PSA_ERROR_INVALID_SIGNATURE;
3791 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003792
Paul Elliott068fe072023-01-16 13:59:15 +00003793 status = mbedtls_to_psa_error(
3794 mbedtls_mpi_read_binary(&operation->r,
3795 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003796 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003797
Paul Elliott068fe072023-01-16 13:59:15 +00003798 if (status != PSA_SUCCESS) {
3799 return status;
3800 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003801
Paul Elliott068fe072023-01-16 13:59:15 +00003802 status = mbedtls_to_psa_error(
3803 mbedtls_mpi_read_binary(&operation->s,
3804 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003805 coordinate_bytes,
3806 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003807
Paul Elliott068fe072023-01-16 13:59:15 +00003808 if (status != PSA_SUCCESS) {
3809 return status;
3810 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003811
Paul Elliott2c9843f2023-02-15 17:32:42 +00003812 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003813
Paul Elliott2c9843f2023-02-15 17:32:42 +00003814 if (status != PSA_SUCCESS) {
3815 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003816 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003817
Paul Elliott0290a762023-02-09 14:30:24 +00003818 /* We only need to store the same length of hash as the private key size
3819 * here, it would be truncated by the internal implementation anyway. */
3820 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3821 coordinate_bytes);
3822
Paul Elliottba70ad42023-02-15 18:23:53 +00003823 if (required_hash_length > sizeof(operation->hash)) {
3824 /* Shouldn't happen, but better safe than sorry. */
3825 return PSA_ERROR_CORRUPTION_DETECTED;
3826 }
3827
Paul Elliott0290a762023-02-09 14:30:24 +00003828 memcpy(operation->hash, hash, required_hash_length);
3829 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003830
3831 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003832#else
Paul Elliott068fe072023-01-16 13:59:15 +00003833 (void) operation;
3834 (void) key_buffer;
3835 (void) key_buffer_size;
3836 (void) alg;
3837 (void) hash;
3838 (void) hash_length;
3839 (void) signature;
3840 (void) signature_length;
3841 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003842 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003843 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003844
Paul Elliott068fe072023-01-16 13:59:15 +00003845 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003846#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3847 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3848 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003849}
3850
3851psa_status_t mbedtls_psa_verify_hash_complete(
3852 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3853{
3854
3855#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3856 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3857 defined(MBEDTLS_ECP_RESTARTABLE)
3858
Paul Elliottf8e5b562023-02-19 18:43:45 +00003859 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3860
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003861 /* Ensure max_ops is set to the current value (or default). */
3862 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3863
Paul Elliottf8e5b562023-02-19 18:43:45 +00003864 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003865 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3866 operation->hash,
3867 operation->hash_length,
3868 &operation->ctx->Q,
3869 &operation->r,
3870 &operation->s,
3871 &operation->restart_ctx));
3872
Paul Elliottf8e5b562023-02-19 18:43:45 +00003873 /* Hide the fact that the restart context only holds a delta of number of
3874 * ops done during the last operation, not an absolute value. */
3875 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3876
3877 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003878#else
3879 (void) operation;
3880
3881 return PSA_ERROR_NOT_SUPPORTED;
3882
3883#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3884 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3885 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3886}
3887
3888psa_status_t mbedtls_psa_verify_hash_abort(
3889 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3890{
3891
3892#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3893 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3894 defined(MBEDTLS_ECP_RESTARTABLE)
3895
3896 if (operation->ctx) {
3897 mbedtls_ecdsa_free(operation->ctx);
3898 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003899 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003900 }
3901
3902 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3903
Paul Elliott93d9ca82023-02-15 18:14:21 +00003904 operation->num_ops = 0;
3905
Paul Elliott588f8ed2022-12-02 18:10:26 +00003906 mbedtls_mpi_free(&operation->r);
3907 mbedtls_mpi_free(&operation->s);
3908
3909 return PSA_SUCCESS;
3910
3911#else
3912 (void) operation;
3913
3914 return PSA_ERROR_NOT_SUPPORTED;
3915
3916#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3917 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3918 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3919}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003920
mohammad1603503973b2018-03-12 15:59:30 +02003921/****************************************************************/
3922/* Symmetric cryptography */
3923/****************************************************************/
3924
Gilles Peskine449bd832023-01-11 14:50:10 +01003925static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3926 mbedtls_svc_key_id_t key,
3927 psa_algorithm_t alg,
3928 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003929{
3930 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3931 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003932 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003933 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3934 PSA_KEY_USAGE_ENCRYPT :
3935 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003936
3937 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003938 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003939 status = PSA_ERROR_BAD_STATE;
3940 goto exit;
3941 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003942
Gilles Peskine449bd832023-01-11 14:50:10 +01003943 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003944 status = PSA_ERROR_INVALID_ARGUMENT;
3945 goto exit;
3946 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003947
Gilles Peskine449bd832023-01-11 14:50:10 +01003948 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3949 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003950 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003951 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003952
Ronald Cron49fafa92021-03-10 08:34:23 +01003953 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003954 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003955 * so we only set it (in the driver wrapper) after resources have been
3956 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003957 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003958 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003959 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003960 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003961 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003962 }
3963 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003964
Ronald Crona4af55f2020-12-14 14:36:06 +01003965 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003966 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003967 };
3968
Ronald Cronab99ac22020-10-01 16:38:28 +02003969 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003970 if (cipher_operation == MBEDTLS_ENCRYPT) {
3971 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3972 &attributes,
3973 slot->key.data,
3974 slot->key.bytes,
3975 alg);
3976 } else {
3977 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3978 &attributes,
3979 slot->key.data,
3980 slot->key.bytes,
3981 alg);
3982 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003983
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003984exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003985 if (status != PSA_SUCCESS) {
3986 psa_cipher_abort(operation);
3987 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003988
Gilles Peskine449bd832023-01-11 14:50:10 +01003989 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003990
Gilles Peskine449bd832023-01-11 14:50:10 +01003991 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003992}
3993
Gilles Peskine449bd832023-01-11 14:50:10 +01003994psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3995 mbedtls_svc_key_id_t key,
3996 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003997{
Gilles Peskine449bd832023-01-11 14:50:10 +01003998 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003999}
4000
Gilles Peskine449bd832023-01-11 14:50:10 +01004001psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4002 mbedtls_svc_key_id_t key,
4003 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004004{
Gilles Peskine449bd832023-01-11 14:50:10 +01004005 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004006}
4007
Gilles Peskine449bd832023-01-11 14:50:10 +01004008psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4009 uint8_t *iv,
4010 size_t iv_size,
4011 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004012{
Ronald Cron6d051732020-10-01 14:10:20 +02004013 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004014 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4015 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004016
Gilles Peskine449bd832023-01-11 14:50:10 +01004017 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004018 status = PSA_ERROR_BAD_STATE;
4019 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004020 }
4021
Gilles Peskine449bd832023-01-11 14:50:10 +01004022 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004023 status = PSA_ERROR_BAD_STATE;
4024 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004025 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004026
Ronald Cron2fb90522021-07-05 12:31:44 +02004027 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004028 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004029 status = PSA_ERROR_BUFFER_TOO_SMALL;
4030 goto exit;
4031 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004032
Gilles Peskine449bd832023-01-11 14:50:10 +01004033 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004034 status = PSA_ERROR_GENERIC_ERROR;
4035 goto exit;
4036 }
4037
Gilles Peskine449bd832023-01-11 14:50:10 +01004038 status = psa_generate_random(local_iv, default_iv_length);
4039 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004040 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004041 }
Ronald Cron5618a392021-03-26 09:52:26 +01004042
Gilles Peskine449bd832023-01-11 14:50:10 +01004043 status = psa_driver_wrapper_cipher_set_iv(operation,
4044 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004045
4046exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004047 if (status == PSA_SUCCESS) {
4048 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004049 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004050 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004051 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004052 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004053 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004054 }
Ronald Cron6d051732020-10-01 14:10:20 +02004055
Gilles Peskine449bd832023-01-11 14:50:10 +01004056 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004057}
4058
Gilles Peskine449bd832023-01-11 14:50:10 +01004059psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4060 const uint8_t *iv,
4061 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004062{
Ronald Cron6d051732020-10-01 14:10:20 +02004063 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004064
Gilles Peskine449bd832023-01-11 14:50:10 +01004065 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004066 status = PSA_ERROR_BAD_STATE;
4067 goto exit;
4068 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004069
Gilles Peskine449bd832023-01-11 14:50:10 +01004070 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004071 status = PSA_ERROR_BAD_STATE;
4072 goto exit;
4073 }
Ronald Crona0d68172021-03-26 10:15:08 +01004074
Gilles Peskine449bd832023-01-11 14:50:10 +01004075 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004076 status = PSA_ERROR_INVALID_ARGUMENT;
4077 goto exit;
4078 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004079
Gilles Peskine449bd832023-01-11 14:50:10 +01004080 status = psa_driver_wrapper_cipher_set_iv(operation,
4081 iv,
4082 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004083
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004084exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004086 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004087 } else {
4088 psa_cipher_abort(operation);
4089 }
4090 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004091}
4092
Gilles Peskine449bd832023-01-11 14:50:10 +01004093psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4094 const uint8_t *input,
4095 size_t input_length,
4096 uint8_t *output,
4097 size_t output_size,
4098 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004099{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004100 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004101
Gilles Peskine449bd832023-01-11 14:50:10 +01004102 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004103 status = PSA_ERROR_BAD_STATE;
4104 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004105 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004106
Gilles Peskine449bd832023-01-11 14:50:10 +01004107 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004108 status = PSA_ERROR_BAD_STATE;
4109 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004110 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004111
Gilles Peskine449bd832023-01-11 14:50:10 +01004112 status = psa_driver_wrapper_cipher_update(operation,
4113 input,
4114 input_length,
4115 output,
4116 output_size,
4117 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004118
4119exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004120 if (status != PSA_SUCCESS) {
4121 psa_cipher_abort(operation);
4122 }
Ronald Cron6d051732020-10-01 14:10:20 +02004123
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004125}
4126
Gilles Peskine449bd832023-01-11 14:50:10 +01004127psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4128 uint8_t *output,
4129 size_t output_size,
4130 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004131{
David Saadab4ecc272019-02-14 13:48:10 +02004132 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004133
Gilles Peskine449bd832023-01-11 14:50:10 +01004134 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004135 status = PSA_ERROR_BAD_STATE;
4136 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004137 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004138
Gilles Peskine449bd832023-01-11 14:50:10 +01004139 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004140 status = PSA_ERROR_BAD_STATE;
4141 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004142 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004143
Gilles Peskine449bd832023-01-11 14:50:10 +01004144 status = psa_driver_wrapper_cipher_finish(operation,
4145 output,
4146 output_size,
4147 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004148
4149exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 if (status == PSA_SUCCESS) {
4151 return psa_cipher_abort(operation);
4152 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004153 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004155
Gilles Peskine449bd832023-01-11 14:50:10 +01004156 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004157 }
mohammad1603503973b2018-03-12 15:59:30 +02004158}
4159
Gilles Peskine449bd832023-01-11 14:50:10 +01004160psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004161{
Gilles Peskine449bd832023-01-11 14:50:10 +01004162 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004163 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004164 * in use. It's ok to call abort on such an object, and there's
4165 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004166 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004167 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004168
Gilles Peskine449bd832023-01-11 14:50:10 +01004169 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004170
Ronald Cron49fafa92021-03-10 08:34:23 +01004171 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004172 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004173 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004174
Gilles Peskine449bd832023-01-11 14:50:10 +01004175 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004176}
4177
Gilles Peskine449bd832023-01-11 14:50:10 +01004178psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4179 psa_algorithm_t alg,
4180 const uint8_t *input,
4181 size_t input_length,
4182 uint8_t *output,
4183 size_t output_size,
4184 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004185{
4186 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004187 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004188 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004189 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4190 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004191
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004193 status = PSA_ERROR_INVALID_ARGUMENT;
4194 goto exit;
4195 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004196
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4198 PSA_KEY_USAGE_ENCRYPT,
4199 alg);
4200 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004201 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004203
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004204 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004205 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004206 };
4207
Gilles Peskine449bd832023-01-11 14:50:10 +01004208 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4209 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004210 status = PSA_ERROR_GENERIC_ERROR;
4211 goto exit;
4212 }
4213
Gilles Peskine449bd832023-01-11 14:50:10 +01004214 if (default_iv_length > 0) {
4215 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004216 status = PSA_ERROR_BUFFER_TOO_SMALL;
4217 goto exit;
4218 }
4219
Gilles Peskine449bd832023-01-11 14:50:10 +01004220 status = psa_generate_random(local_iv, default_iv_length);
4221 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004222 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004224 }
4225
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004226 status = psa_driver_wrapper_cipher_encrypt(
4227 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004228 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004229 mbedtls_buffer_offset(output, default_iv_length),
4230 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004231
4232exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004233 unlock_status = psa_unlock_key_slot(slot);
4234 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004235 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004236 }
Ronald Cron23919522021-07-08 19:00:07 +02004237
Gilles Peskine449bd832023-01-11 14:50:10 +01004238 if (status == PSA_SUCCESS) {
4239 if (default_iv_length > 0) {
4240 memcpy(output, local_iv, default_iv_length);
4241 }
4242 *output_length += default_iv_length;
4243 } else {
4244 *output_length = 0;
4245 }
4246
4247 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004248}
4249
Gilles Peskine449bd832023-01-11 14:50:10 +01004250psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4251 psa_algorithm_t alg,
4252 const uint8_t *input,
4253 size_t input_length,
4254 uint8_t *output,
4255 size_t output_size,
4256 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004257{
4258 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004259 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004260 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004261
Gilles Peskine449bd832023-01-11 14:50:10 +01004262 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004263 status = PSA_ERROR_INVALID_ARGUMENT;
4264 goto exit;
4265 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004266
Gilles Peskine449bd832023-01-11 14:50:10 +01004267 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4268 PSA_KEY_USAGE_DECRYPT,
4269 alg);
4270 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004271 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004272 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004273
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004274 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004275 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004276 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004277
Gilles Peskine449bd832023-01-11 14:50:10 +01004278 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4279 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004280 status = PSA_ERROR_INVALID_ARGUMENT;
4281 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004282 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004283 status = PSA_ERROR_INVALID_ARGUMENT;
4284 goto exit;
4285 }
4286
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004287 status = psa_driver_wrapper_cipher_decrypt(
4288 &attributes, slot->key.data, slot->key.bytes,
4289 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004290 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004291
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004292exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004293 unlock_status = psa_unlock_key_slot(slot);
4294 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004295 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004296 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004297
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004299 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 }
Ronald Cron23919522021-07-08 19:00:07 +02004301
Gilles Peskine449bd832023-01-11 14:50:10 +01004302 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004303}
4304
4305
mohammad16035955c982018-04-26 00:53:03 +03004306/****************************************************************/
4307/* AEAD */
4308/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004309
Paul Elliottbaff51c2021-09-28 17:44:45 +01004310/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004311static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004312{
Gilles Peskine449bd832023-01-11 14:50:10 +01004313 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004314}
4315
4316/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004317static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4318 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004319{
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004321
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004323#if defined(PSA_WANT_ALG_GCM)
4324 case PSA_ALG_GCM:
4325 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004326 * arbitrarily large nonces. Please note that we do not generally
4327 * recommend the usage of nonces of greater length than
4328 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4329 * size, which can then lead to collisions if you encrypt a very
4330 * large number of messages.*/
4331 if (nonce_length != 0) {
4332 return PSA_SUCCESS;
4333 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004334 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004335#endif /* PSA_WANT_ALG_GCM */
4336#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004337 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004338 if (nonce_length >= 7 && nonce_length <= 13) {
4339 return PSA_SUCCESS;
4340 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004341 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004342#endif /* PSA_WANT_ALG_CCM */
4343#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004344 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004345 if (nonce_length == 12) {
4346 return PSA_SUCCESS;
4347 } else if (nonce_length == 8) {
4348 return PSA_ERROR_NOT_SUPPORTED;
4349 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004350 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004351#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004352 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004353 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004354 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004355 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004356
Gilles Peskine449bd832023-01-11 14:50:10 +01004357 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004358}
4359
Gilles Peskine449bd832023-01-11 14:50:10 +01004360static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004361{
Gilles Peskine449bd832023-01-11 14:50:10 +01004362 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4363 return PSA_ERROR_INVALID_ARGUMENT;
4364 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004365
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004367}
4368
Gilles Peskine449bd832023-01-11 14:50:10 +01004369psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4370 psa_algorithm_t alg,
4371 const uint8_t *nonce,
4372 size_t nonce_length,
4373 const uint8_t *additional_data,
4374 size_t additional_data_length,
4375 const uint8_t *plaintext,
4376 size_t plaintext_length,
4377 uint8_t *ciphertext,
4378 size_t ciphertext_size,
4379 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004380{
Ronald Cron215633c2021-03-16 17:15:37 +01004381 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4382 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004383
mohammad1603f08a5502018-06-03 15:05:47 +03004384 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004385
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 status = psa_aead_check_algorithm(alg);
4387 if (status != PSA_SUCCESS) {
4388 return status;
4389 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004390
Ronald Cron9a986162021-03-26 12:40:07 +01004391 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4393 if (status != PSA_SUCCESS) {
4394 return status;
4395 }
mohammad16035955c982018-04-26 00:53:03 +03004396
Ronald Cron215633c2021-03-16 17:15:37 +01004397 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004398 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004399 };
mohammad16035955c982018-04-26 00:53:03 +03004400
Gilles Peskine449bd832023-01-11 14:50:10 +01004401 status = psa_aead_check_nonce_length(alg, nonce_length);
4402 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004403 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004404 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004405
Ronald Cronde822812021-03-17 16:08:20 +01004406 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004407 &attributes, slot->key.data, slot->key.bytes,
4408 alg,
4409 nonce, nonce_length,
4410 additional_data, additional_data_length,
4411 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004412 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004413
Gilles Peskine449bd832023-01-11 14:50:10 +01004414 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4415 memset(ciphertext, 0, ciphertext_size);
4416 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004417
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004418exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004419 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004420
Gilles Peskine449bd832023-01-11 14:50:10 +01004421 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004422}
4423
Gilles Peskine449bd832023-01-11 14:50:10 +01004424psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4425 psa_algorithm_t alg,
4426 const uint8_t *nonce,
4427 size_t nonce_length,
4428 const uint8_t *additional_data,
4429 size_t additional_data_length,
4430 const uint8_t *ciphertext,
4431 size_t ciphertext_length,
4432 uint8_t *plaintext,
4433 size_t plaintext_size,
4434 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004435{
Ronald Cron215633c2021-03-16 17:15:37 +01004436 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4437 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004438
Gilles Peskineee652a32018-06-01 19:23:52 +02004439 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004440
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 status = psa_aead_check_algorithm(alg);
4442 if (status != PSA_SUCCESS) {
4443 return status;
4444 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004445
Ronald Cron9a986162021-03-26 12:40:07 +01004446 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4448 if (status != PSA_SUCCESS) {
4449 return status;
4450 }
mohammad16035955c982018-04-26 00:53:03 +03004451
Ronald Cron215633c2021-03-16 17:15:37 +01004452 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004454 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004455
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 status = psa_aead_check_nonce_length(alg, nonce_length);
4457 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004458 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004460
Ronald Cronde822812021-03-17 16:08:20 +01004461 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004462 &attributes, slot->key.data, slot->key.bytes,
4463 alg,
4464 nonce, nonce_length,
4465 additional_data, additional_data_length,
4466 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004467 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004468
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 if (status != PSA_SUCCESS && plaintext_size != 0) {
4470 memset(plaintext, 0, plaintext_size);
4471 }
mohammad160360a64d02018-06-03 17:20:42 +03004472
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004473exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004474 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004475
Gilles Peskine449bd832023-01-11 14:50:10 +01004476 return status;
mohammad16035955c982018-04-26 00:53:03 +03004477}
4478
Gilles Peskine449bd832023-01-11 14:50:10 +01004479static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4480{
4481 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004482
Gilles Peskine449bd832023-01-11 14:50:10 +01004483 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004484#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004486 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4488 return PSA_ERROR_INVALID_ARGUMENT;
4489 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004490 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004491#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004492
Przemek Stekiel86679c72022-10-06 17:06:56 +02004493#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004495 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004496 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4497 return PSA_ERROR_INVALID_ARGUMENT;
4498 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004499 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004500#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004501
Przemek Stekiel86679c72022-10-06 17:06:56 +02004502#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004504 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 if (tag_len != 16) {
4506 return PSA_ERROR_INVALID_ARGUMENT;
4507 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004508 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004509#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004510
4511 default:
4512 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004514 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004515 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004516}
4517
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004518/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004519static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4520 int is_encrypt,
4521 mbedtls_svc_key_id_t key,
4522 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004523{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004524 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4525 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4526 psa_key_slot_t *slot = NULL;
4527 psa_key_usage_t key_usage = 0;
4528
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 status = psa_aead_check_algorithm(alg);
4530 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004531 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004533
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004535 status = PSA_ERROR_BAD_STATE;
4536 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004537 }
4538
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 if (operation->nonce_set || operation->lengths_set ||
4540 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004541 status = PSA_ERROR_BAD_STATE;
4542 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004543 }
4544
Gilles Peskine449bd832023-01-11 14:50:10 +01004545 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004546 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004548 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004550
Gilles Peskine449bd832023-01-11 14:50:10 +01004551 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4552 alg);
4553 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004554 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004556
4557 psa_key_attributes_t attributes = {
4558 .core = slot->attr
4559 };
4560
Gilles Peskine449bd832023-01-11 14:50:10 +01004561 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004562 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004564
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 if (is_encrypt) {
4566 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4567 &attributes,
4568 slot->key.data,
4569 slot->key.bytes,
4570 alg);
4571 } else {
4572 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4573 &attributes,
4574 slot->key.data,
4575 slot->key.bytes,
4576 alg);
4577 }
4578 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004579 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004580 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004581
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004583
4584exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004586
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004588 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004590 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 } else {
4592 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004593 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004594
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004596}
4597
Paul Elliott302ff6b2021-04-20 18:10:30 +01004598/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004599psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4600 mbedtls_svc_key_id_t key,
4601 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004602{
Gilles Peskine449bd832023-01-11 14:50:10 +01004603 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004604}
4605
4606/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004607psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4608 mbedtls_svc_key_id_t key,
4609 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004610{
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004612}
4613
4614/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004615psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4616 uint8_t *nonce,
4617 size_t nonce_size,
4618 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004619{
4620 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004621 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004622 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004623
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004624 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004625
Gilles Peskine449bd832023-01-11 14:50:10 +01004626 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004627 status = PSA_ERROR_BAD_STATE;
4628 goto exit;
4629 }
4630
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004632 status = PSA_ERROR_BAD_STATE;
4633 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004634 }
4635
Paul Elliotte193ea82021-10-01 13:00:16 +01004636 /* For CCM, this size may not be correct according to the PSA
4637 * specification. The PSA Crypto 1.0.1 specification states:
4638 *
4639 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4640 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4641 *
4642 * However this restriction that L has to be the smallest integer is not
4643 * applied in practice, and it is not implementable here since the
4644 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004645 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4646 operation->alg);
4647 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004648 status = PSA_ERROR_BUFFER_TOO_SMALL;
4649 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004650 }
4651
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 status = psa_generate_random(local_nonce, required_nonce_size);
4653 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004654 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004656
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004658
Paul Elliott39dc6b82021-05-11 19:16:09 +01004659exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 if (status == PSA_SUCCESS) {
4661 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004662 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004663 } else {
4664 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004665 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004666
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004668}
4669
4670/* Set the nonce for a multipart authenticated encryption or decryption
4671 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004672psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4673 const uint8_t *nonce,
4674 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004675{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004676 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4677
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004679 status = PSA_ERROR_BAD_STATE;
4680 goto exit;
4681 }
4682
Gilles Peskine449bd832023-01-11 14:50:10 +01004683 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004684 status = PSA_ERROR_BAD_STATE;
4685 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004686 }
4687
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4689 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004690 status = PSA_ERROR_INVALID_ARGUMENT;
4691 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004692 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004693
Gilles Peskine449bd832023-01-11 14:50:10 +01004694 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4695 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004696
Paul Elliott39dc6b82021-05-11 19:16:09 +01004697exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004699 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004700 } else {
4701 psa_aead_abort(operation);
4702 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004703
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004705}
4706
4707/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004708psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4709 size_t ad_length,
4710 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004711{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004712 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4713
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004715 status = PSA_ERROR_BAD_STATE;
4716 goto exit;
4717 }
4718
Gilles Peskine449bd832023-01-11 14:50:10 +01004719 if (operation->lengths_set || operation->ad_started ||
4720 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004721 status = PSA_ERROR_BAD_STATE;
4722 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004723 }
4724
Gilles Peskine449bd832023-01-11 14:50:10 +01004725 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004726#if defined(PSA_WANT_ALG_GCM)
4727 case PSA_ALG_GCM:
4728 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 * bits. Without the guard this code will generate warnings on 32bit
4730 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004731#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 if (((uint64_t) ad_length) >> 61 != 0 ||
4733 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004734 status = PSA_ERROR_INVALID_ARGUMENT;
4735 goto exit;
4736 }
Paul Elliott325d3742021-09-27 17:56:28 +01004737#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004738 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004739#endif /* PSA_WANT_ALG_GCM */
4740#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004741 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004743 status = PSA_ERROR_INVALID_ARGUMENT;
4744 goto exit;
4745 }
4746 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004747#endif /* PSA_WANT_ALG_CCM */
4748#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004749 case PSA_ALG_CHACHA20_POLY1305:
4750 /* No length restrictions for ChaChaPoly. */
4751 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004752#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004753 default:
4754 break;
4755 }
Paul Elliott325d3742021-09-27 17:56:28 +01004756
Gilles Peskine449bd832023-01-11 14:50:10 +01004757 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4758 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004759
Paul Elliott39dc6b82021-05-11 19:16:09 +01004760exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004762 operation->ad_remaining = ad_length;
4763 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004764 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 } else {
4766 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004767 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004768
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004770}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004771
4772/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004773psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4774 const uint8_t *input,
4775 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004776{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004777 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4778
Gilles Peskine449bd832023-01-11 14:50:10 +01004779 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004780 status = PSA_ERROR_BAD_STATE;
4781 goto exit;
4782 }
4783
Gilles Peskine449bd832023-01-11 14:50:10 +01004784 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004785 status = PSA_ERROR_BAD_STATE;
4786 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004787 }
4788
Gilles Peskine449bd832023-01-11 14:50:10 +01004789 if (operation->lengths_set) {
4790 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004791 status = PSA_ERROR_INVALID_ARGUMENT;
4792 goto exit;
4793 }
4794
4795 operation->ad_remaining -= input_length;
4796 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004797#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004799 status = PSA_ERROR_BAD_STATE;
4800 goto exit;
4801 }
4802#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004803
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 status = psa_driver_wrapper_aead_update_ad(operation, input,
4805 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004806
Paul Elliott39dc6b82021-05-11 19:16:09 +01004807exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004809 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004810 } else {
4811 psa_aead_abort(operation);
4812 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004813
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004815}
4816
4817/* Encrypt or decrypt a message fragment in an active multipart AEAD
4818 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004819psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4820 const uint8_t *input,
4821 size_t input_length,
4822 uint8_t *output,
4823 size_t output_size,
4824 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004825{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004826 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004827
4828 *output_length = 0;
4829
Gilles Peskine449bd832023-01-11 14:50:10 +01004830 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004831 status = PSA_ERROR_BAD_STATE;
4832 goto exit;
4833 }
4834
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004836 status = PSA_ERROR_BAD_STATE;
4837 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004838 }
4839
Gilles Peskine449bd832023-01-11 14:50:10 +01004840 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004841 /* Additional data length was supplied, but not all the additional
4842 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004843 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004844 status = PSA_ERROR_INVALID_ARGUMENT;
4845 goto exit;
4846 }
4847
4848 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004850 status = PSA_ERROR_INVALID_ARGUMENT;
4851 goto exit;
4852 }
4853
4854 operation->body_remaining -= input_length;
4855 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004856#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004857 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004858 status = PSA_ERROR_BAD_STATE;
4859 goto exit;
4860 }
4861#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004862
Gilles Peskine449bd832023-01-11 14:50:10 +01004863 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4864 output, output_size,
4865 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004866
Paul Elliott39dc6b82021-05-11 19:16:09 +01004867exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004869 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004870 } else {
4871 psa_aead_abort(operation);
4872 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004873
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004875}
4876
Gilles Peskine449bd832023-01-11 14:50:10 +01004877static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004878{
Gilles Peskine449bd832023-01-11 14:50:10 +01004879 if (operation->id == 0 || !operation->nonce_set) {
4880 return PSA_ERROR_BAD_STATE;
4881 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004882
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4884 operation->body_remaining != 0)) {
4885 return PSA_ERROR_INVALID_ARGUMENT;
4886 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004887
Gilles Peskine449bd832023-01-11 14:50:10 +01004888 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004889}
4890
Paul Elliott302ff6b2021-04-20 18:10:30 +01004891/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004892psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4893 uint8_t *ciphertext,
4894 size_t ciphertext_size,
4895 size_t *ciphertext_length,
4896 uint8_t *tag,
4897 size_t tag_size,
4898 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004899{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004900 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4901
Paul Elliott302ff6b2021-04-20 18:10:30 +01004902 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004903 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004904
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 status = psa_aead_final_checks(operation);
4906 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004907 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004909
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004911 status = PSA_ERROR_BAD_STATE;
4912 goto exit;
4913 }
4914
Gilles Peskine449bd832023-01-11 14:50:10 +01004915 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4916 ciphertext_size,
4917 ciphertext_length,
4918 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004919
Paul Elliott39dc6b82021-05-11 19:16:09 +01004920exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004921
4922
Paul Elliottf47b0952021-05-21 18:02:33 +01004923 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004924 * the zero tag size, make sure the tag is set to something implausible.
4925 * Even if the operation succeeds, make sure we clear the rest of the
4926 * buffer to prevent potential leakage of anything previously placed in
4927 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004928 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004929
Gilles Peskine449bd832023-01-11 14:50:10 +01004930 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004931
Gilles Peskine449bd832023-01-11 14:50:10 +01004932 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004933}
4934
4935/* Finish authenticating and decrypting a message in a multipart AEAD
4936 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004937psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4938 uint8_t *plaintext,
4939 size_t plaintext_size,
4940 size_t *plaintext_length,
4941 const uint8_t *tag,
4942 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004943{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004944 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4945
Paul Elliott302ff6b2021-04-20 18:10:30 +01004946 *plaintext_length = 0;
4947
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 status = psa_aead_final_checks(operation);
4949 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004950 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004951 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004952
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004954 status = PSA_ERROR_BAD_STATE;
4955 goto exit;
4956 }
4957
Gilles Peskine449bd832023-01-11 14:50:10 +01004958 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4959 plaintext_size,
4960 plaintext_length,
4961 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004962
4963exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004964 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004965
Gilles Peskine449bd832023-01-11 14:50:10 +01004966 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004967}
4968
4969/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004970psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004971{
4972 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4973
Gilles Peskine449bd832023-01-11 14:50:10 +01004974 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004975 /* The object has (apparently) been initialized but it is not (yet)
4976 * in use. It's ok to call abort on such an object, and there's
4977 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004978 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004979 }
4980
Gilles Peskine449bd832023-01-11 14:50:10 +01004981 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004982
Gilles Peskine449bd832023-01-11 14:50:10 +01004983 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004984
Gilles Peskine449bd832023-01-11 14:50:10 +01004985 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004986}
4987
Gilles Peskinee59236f2018-01-27 23:32:46 +01004988/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004989/* Generators */
4990/****************************************************************/
4991
Przemek Stekiel69c46792022-06-10 12:59:51 +02004992#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004993 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004994 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
4995 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08004996#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004997#endif /* At least one builtin KDF */
4998
Przemek Stekiel69c46792022-06-10 12:59:51 +02004999#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005000 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5001 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5002static psa_status_t psa_key_derivation_start_hmac(
5003 psa_mac_operation_t *operation,
5004 psa_algorithm_t hash_alg,
5005 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005006 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005007{
5008 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5009 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005010 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5011 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5012 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005013
Steven Cooreman72f736a2021-05-07 14:14:37 +02005014 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005015 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005016
Gilles Peskine449bd832023-01-11 14:50:10 +01005017 status = psa_driver_wrapper_mac_sign_setup(operation,
5018 &attributes,
5019 hmac_key, hmac_key_length,
5020 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005021
Gilles Peskine449bd832023-01-11 14:50:10 +01005022 psa_reset_key_attributes(&attributes);
5023 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005024}
5025#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005026
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005027#define HKDF_STATE_INIT 0 /* no input yet */
5028#define HKDF_STATE_STARTED 1 /* got salt */
5029#define HKDF_STATE_KEYED 2 /* got key */
5030#define HKDF_STATE_OUTPUT 3 /* output started */
5031
Gilles Peskinecbe66502019-05-16 16:59:18 +02005032static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005034{
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5036 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5037 } else {
5038 return operation->alg;
5039 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005040}
5041
Gilles Peskine449bd832023-01-11 14:50:10 +01005042psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005043{
5044 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5046 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005047 /* The object has (apparently) been initialized but it is not
5048 * in use. It's ok to call abort on such an object, and there's
5049 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005050 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005051#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005052 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5053 mbedtls_free(operation->ctx.hkdf.info);
5054 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5055 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005056#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005057#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5058 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005059 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5060 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5061 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5062 if (operation->ctx.tls12_prf.secret != NULL) {
5063 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5064 operation->ctx.tls12_prf.secret_length);
5065 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005066 }
5067
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 if (operation->ctx.tls12_prf.seed != NULL) {
5069 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5070 operation->ctx.tls12_prf.seed_length);
5071 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005072 }
5073
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 if (operation->ctx.tls12_prf.label != NULL) {
5075 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5076 operation->ctx.tls12_prf.label_length);
5077 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005078 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005079#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 if (operation->ctx.tls12_prf.other_secret != NULL) {
5081 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5082 operation->ctx.tls12_prf.other_secret_length);
5083 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005084 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005085#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005086 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005087
5088 /* We leave the fields Ai and output_block to be erased safely by the
5089 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005091#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5092 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005093#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005094 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5095 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5096 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5097 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005098#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005099 {
5100 status = PSA_ERROR_BAD_STATE;
5101 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005102 mbedtls_platform_zeroize(operation, sizeof(*operation));
5103 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005104}
5105
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005106psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005108{
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005110 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005112 }
5113
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005114 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005116}
5117
Gilles Peskine449bd832023-01-11 14:50:10 +01005118psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5119 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005120{
Gilles Peskine449bd832023-01-11 14:50:10 +01005121 if (operation->alg == 0) {
5122 return PSA_ERROR_BAD_STATE;
5123 }
5124 if (capacity > operation->capacity) {
5125 return PSA_ERROR_INVALID_ARGUMENT;
5126 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005127 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005128 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005129}
5130
Przemek Stekiel69c46792022-06-10 12:59:51 +02005131#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005132/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005133static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5134 psa_algorithm_t kdf_alg,
5135 uint8_t *output,
5136 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005137{
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5139 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005140 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005141 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005142#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005143 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005144#else
5145 const uint8_t last_block = 0xff;
5146#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005147
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 if (hkdf->state < HKDF_STATE_KEYED ||
5149 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005150#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005152#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 )) {
5154 return PSA_ERROR_BAD_STATE;
5155 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005156 hkdf->state = HKDF_STATE_OUTPUT;
5157
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005159 /* Copy what remains of the current block */
5160 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005162 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 }
5164 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005165 output += n;
5166 output_length -= n;
5167 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005168 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005169 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005170 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005171 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005172 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005173 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005174 * object was corrupted or if this function is called directly
5175 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005176 if (hkdf->block_number == last_block) {
5177 return PSA_ERROR_BAD_STATE;
5178 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005179
5180 /* We need a new block */
5181 ++hkdf->block_number;
5182 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005183
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5185 hash_alg,
5186 hkdf->prk,
5187 hash_length);
5188 if (status != PSA_SUCCESS) {
5189 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005190 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005191
5192 if (hkdf->block_number != 1) {
5193 status = psa_mac_update(&hkdf->hmac,
5194 hkdf->output_block,
5195 hash_length);
5196 if (status != PSA_SUCCESS) {
5197 return status;
5198 }
5199 }
5200 status = psa_mac_update(&hkdf->hmac,
5201 hkdf->info,
5202 hkdf->info_length);
5203 if (status != PSA_SUCCESS) {
5204 return status;
5205 }
5206 status = psa_mac_update(&hkdf->hmac,
5207 &hkdf->block_number, 1);
5208 if (status != PSA_SUCCESS) {
5209 return status;
5210 }
5211 status = psa_mac_sign_finish(&hkdf->hmac,
5212 hkdf->output_block,
5213 sizeof(hkdf->output_block),
5214 &hmac_output_length);
5215 if (status != PSA_SUCCESS) {
5216 return status;
5217 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005218 }
5219
Gilles Peskine449bd832023-01-11 14:50:10 +01005220 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005221}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005222#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005223
John Durkop07cc04a2020-11-16 22:08:34 -08005224#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5225 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005226static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5227 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005228 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005229{
Gilles Peskine449bd832023-01-11 14:50:10 +01005230 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5231 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005232 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5233 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005234 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005235
Janos Follath7742fee2019-06-17 12:58:10 +01005236 /* We can't be wanting more output after block 0xff, otherwise
5237 * the capacity check in psa_key_derivation_output_bytes() would have
5238 * prevented this call. It could happen only if the operation
5239 * object was corrupted or if this function is called directly
5240 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 if (tls12_prf->block_number == 0xff) {
5242 return PSA_ERROR_CORRUPTION_DETECTED;
5243 }
Janos Follath7742fee2019-06-17 12:58:10 +01005244
5245 /* We need a new block */
5246 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005247 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005248
5249 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5250 *
5251 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5252 *
5253 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5254 * HMAC_hash(secret, A(2) + seed) +
5255 * HMAC_hash(secret, A(3) + seed) + ...
5256 *
5257 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005258 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005259 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005260 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005261 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005262 * is currently extracted as `output_block` and where i is
5263 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005264 */
5265
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 status = psa_key_derivation_start_hmac(&hmac,
5267 hash_alg,
5268 tls12_prf->secret,
5269 tls12_prf->secret_length);
5270 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005271 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005272 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005273
5274 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005276 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5277 * the variable seed and in this instance means it in the context of the
5278 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 status = psa_mac_update(&hmac,
5280 tls12_prf->label,
5281 tls12_prf->label_length);
5282 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005283 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 }
5285 status = psa_mac_update(&hmac,
5286 tls12_prf->seed,
5287 tls12_prf->seed_length);
5288 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005289 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005290 }
5291 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005292 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005293 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5294 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005295 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005296 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005297 }
5298
Gilles Peskine449bd832023-01-11 14:50:10 +01005299 status = psa_mac_sign_finish(&hmac,
5300 tls12_prf->Ai, hash_length,
5301 &hmac_output_length);
5302 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005303 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 }
5305 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005306 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005307 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005308
5309 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 status = psa_key_derivation_start_hmac(&hmac,
5311 hash_alg,
5312 tls12_prf->secret,
5313 tls12_prf->secret_length);
5314 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005315 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 }
5317 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_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->label, tls12_prf->label_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->seed, tls12_prf->seed_length);
5326 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005327 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 }
5329 status = psa_mac_sign_finish(&hmac,
5330 tls12_prf->output_block, hash_length,
5331 &hmac_output_length);
5332 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005333 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005335
Janos Follath7742fee2019-06-17 12:58:10 +01005336
5337cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005338 cleanup_status = psa_mac_abort(&hmac);
5339 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005340 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005342
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005344}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005345
Janos Follath844eb0e2019-06-19 12:10:49 +01005346static psa_status_t psa_key_derivation_tls12_prf_read(
5347 psa_tls12_prf_key_derivation_t *tls12_prf,
5348 psa_algorithm_t alg,
5349 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005351{
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5353 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005354 psa_status_t status;
5355 uint8_t offset, length;
5356
Gilles Peskine449bd832023-01-11 14:50:10 +01005357 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005358 case PSA_TLS12_PRF_STATE_LABEL_SET:
5359 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5360 break;
5361 case PSA_TLS12_PRF_STATE_OUTPUT:
5362 break;
5363 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005364 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005365 }
5366
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005368 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 if (tls12_prf->left_in_block == 0) {
5370 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5371 alg);
5372 if (status != PSA_SUCCESS) {
5373 return status;
5374 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005375
5376 continue;
5377 }
5378
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005380 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005382 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005384
5385 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005387 output += length;
5388 output_length -= length;
5389 tls12_prf->left_in_block -= length;
5390 }
5391
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005393}
John Durkop07cc04a2020-11-16 22:08:34 -08005394#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5395 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005396
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005397#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5398static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5399 psa_tls12_ecjpake_to_pms_t *ecjpake,
5400 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005402{
5403 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005404 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005405
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 if (output_length != 32) {
5407 return PSA_ERROR_INVALID_ARGUMENT;
5408 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005409
Gilles Peskine449bd832023-01-11 14:50:10 +01005410 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5411 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5412 &output_size);
5413 if (status != PSA_SUCCESS) {
5414 return status;
5415 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005416
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 if (output_size != output_length) {
5418 return PSA_ERROR_GENERIC_ERROR;
5419 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005420
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005422}
5423#endif
5424
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005425psa_status_t psa_key_derivation_output_bytes(
5426 psa_key_derivation_operation_t *operation,
5427 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005428 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005429{
5430 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005432
Gilles Peskine449bd832023-01-11 14:50:10 +01005433 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005434 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005436 }
5437
Gilles Peskine449bd832023-01-11 14:50:10 +01005438 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005439 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005440 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005441 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005442 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005443 goto exit;
5444 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005446 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005447 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005448 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5449 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005450 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005451 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005453 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005454 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005455
Przemek Stekiel69c46792022-06-10 12:59:51 +02005456#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5458 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5459 output, output_length);
5460 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005461#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005462#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5463 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5465 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5466 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5467 kdf_alg, output,
5468 output_length);
5469 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005470#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5471 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005472#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005474 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5476 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005477#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5478
Gilles Peskineeab56e42018-07-12 17:12:33 +02005479 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005480 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005482 }
5483
5484exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005485 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005486 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005487 * This allows us to differentiate between exhausted operations and
5488 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5489 * operations. */
5490 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005492 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005494 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005496}
5497
David Brown7807bf72021-02-09 16:28:23 -07005498#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005499static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005500{
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 if (data_size >= 8) {
5502 mbedtls_des_key_set_parity(data);
5503 }
5504 if (data_size >= 16) {
5505 mbedtls_des_key_set_parity(data + 8);
5506 }
5507 if (data_size >= 24) {
5508 mbedtls_des_key_set_parity(data + 16);
5509 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005510}
David Brown7807bf72021-02-09 16:28:23 -07005511#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005512
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005513/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005514 * ECC keys on a Weierstrass elliptic curve require the generation
5515 * of a private key which is an integer
5516 * in the range [1, N - 1], where N is the boundary of the private key domain:
5517 * N is the prime p for Diffie-Hellman, or the order of the
5518 * curve’s base point for ECC.
5519 *
5520 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5521 * This function generates the private key using the following process:
5522 *
5523 * 1. Draw a byte string of length ceiling(m/8) bytes.
5524 * 2. If m is not a multiple of 8, set the most significant
5525 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5526 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5527 * 4. If k > N - 2, discard the result and return to step 1.
5528 * 5. Output k + 1 as the private key.
5529 *
5530 * This method allows compliance to NIST standards, specifically the methods titled
5531 * Key-Pair Generation by Testing Candidates in the following publications:
5532 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5533 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5534 * Diffie-Hellman keys.
5535 *
5536 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5537 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5538 *
5539 * Note: Function allocates memory for *data buffer, so given *data should be
5540 * always NULL.
5541 */
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005542#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5543 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005544 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5545 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5546 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005547static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005548 psa_key_slot_t *slot,
5549 size_t bits,
5550 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005551 uint8_t **data
5552 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005553{
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005554#if defined(MBEDTLS_ECP_C)
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005555 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005556 mbedtls_mpi k;
5557 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005558 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005559 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005560
Gilles Peskine449bd832023-01-11 14:50:10 +01005561 mbedtls_mpi_init(&k);
5562 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005563
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005564 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005566 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005568
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005570 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005571 goto cleanup;
5572 }
5573
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005574 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005576
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005578
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005579 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005580 /* Let m be the bit size of N. */
5581 size_t m = ecp_group.nbits;
5582
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005584
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005585 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005587
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005588 /* Note: This function is always called with *data == NULL and it
5589 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005590 *data = mbedtls_calloc(1, m_bytes);
5591 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005592 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005593 goto cleanup;
5594 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005595
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005597 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005599 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005600 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005601
5602 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005603 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005604 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005605 * (8 * ceiling(m/8) - m) bits of the first byte in
5606 * the string to zero.
5607 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005608 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005609 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005610 }
5611
5612 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 * big-endian byte string.
5614 */
5615 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005616
5617 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005618 * Result of comparison is returned. When it indicates error
5619 * then this function is called again.
5620 */
5621 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005622 }
5623
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005624 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5626 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005627cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 if (ret != 0) {
5629 status = mbedtls_to_psa_error(ret);
5630 }
5631 if (status != PSA_SUCCESS) {
5632 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005633 *data = NULL;
5634 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 mbedtls_mpi_free(&k);
5636 mbedtls_mpi_free(&diff_N_2);
5637 return status;
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005638#else /* MBEDTLS_ECP_C */
5639 (void) slot;
5640 (void) bits;
5641 (void) operation;
5642 (void) data;
5643 return PSA_ERROR_NOT_SUPPORTED;
5644#endif /* MBEDTLS_ECP_C */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005645}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005646
5647/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5648 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5649 *
5650 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5651 * draw a 32-byte string and process it as specified in
5652 * Elliptic Curves for Security [RFC7748] §5.
5653 *
5654 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5655 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5656 *
5657 * Note: Function allocates memory for *data buffer, so given *data should be
5658 * always NULL.
5659 */
5660
5661static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5662 size_t bits,
5663 psa_key_derivation_operation_t *operation,
5664 uint8_t **data
5665 )
5666{
5667 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005668 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005669
Gilles Peskine449bd832023-01-11 14:50:10 +01005670 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005671 case 255:
5672 output_length = 32;
5673 break;
5674 case 448:
5675 output_length = 56;
5676 break;
5677 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005679 break;
5680 }
5681
Gilles Peskine449bd832023-01-11 14:50:10 +01005682 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005683
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 if (*data == NULL) {
5685 return PSA_ERROR_INSUFFICIENT_MEMORY;
5686 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005687
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005689
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005691 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005693
Gilles Peskine449bd832023-01-11 14:50:10 +01005694 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005695 case 255:
5696 (*data)[0] &= 248;
5697 (*data)[31] &= 127;
5698 (*data)[31] |= 64;
5699 break;
5700 case 448:
5701 (*data)[0] &= 252;
5702 (*data)[55] |= 128;
5703 break;
5704 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005705 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005706 break;
5707 }
5708
5709 return status;
5710}
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005711#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5712 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005713 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5714 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5715 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005716
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005717static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005718 psa_key_slot_t *slot,
5719 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005721{
5722 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005723 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305724 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005725 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005726
Gilles Peskine449bd832023-01-11 14:50:10 +01005727 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5728 return PSA_ERROR_INVALID_ARGUMENT;
5729 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005730
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005731#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5732 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005733 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5734 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5735 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5737 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5738 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005739 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005740 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5741 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005742 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 }
5744 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005745 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5747 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005748 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005750 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005751 } else
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005752#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5753 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005754 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5755 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5756 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 if (key_type_is_raw_bytes(slot->attr.type)) {
5758 if (bits % 8 != 0) {
5759 return PSA_ERROR_INVALID_ARGUMENT;
5760 }
5761 data = mbedtls_calloc(1, bytes);
5762 if (data == NULL) {
5763 return PSA_ERROR_INSUFFICIENT_MEMORY;
5764 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005765
Gilles Peskine449bd832023-01-11 14:50:10 +01005766 status = psa_key_derivation_output_bytes(operation, data, bytes);
5767 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005768 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 }
David Brown12ca5032021-02-11 11:02:00 -07005770#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5772 psa_des_set_key_parity(data, bytes);
5773 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005774#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 } else {
5776 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005777 }
Ronald Crondd04d422020-11-28 15:14:42 +01005778
Ronald Cronbf33c932020-11-28 18:06:53 +01005779 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005780 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005782 };
5783
Gilles Peskine449bd832023-01-11 14:50:10 +01005784 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5785 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5786 &storage_size);
5787 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305788 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 }
Archanad8a83dc2021-06-14 10:04:16 +05305790 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 status = psa_allocate_buffer_to_slot(slot, storage_size);
5792 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305793 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005794 }
Archanad8a83dc2021-06-14 10:04:16 +05305795
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 status = psa_driver_wrapper_import_key(&attributes,
5797 data, bytes,
5798 slot->key.data,
5799 slot->key.bytes,
5800 &slot->key.bytes, &bits);
5801 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005802 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005803 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005804
5805exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005806 mbedtls_free(data);
5807 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005808}
5809
Gilles Peskine449bd832023-01-11 14:50:10 +01005810psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5811 psa_key_derivation_operation_t *operation,
5812 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005813{
5814 psa_status_t status;
5815 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005816 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005817
Ronald Cron81709fc2020-11-14 12:10:32 +01005818 *key = MBEDTLS_SVC_KEY_ID_INIT;
5819
Gilles Peskine0f84d622019-09-12 19:03:13 +02005820 /* Reject any attempt to create a zero-length key so that we don't
5821 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 if (psa_get_key_bits(attributes) == 0) {
5823 return PSA_ERROR_INVALID_ARGUMENT;
5824 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005825
Gilles Peskine449bd832023-01-11 14:50:10 +01005826 if (operation->alg == PSA_ALG_NONE) {
5827 return PSA_ERROR_BAD_STATE;
5828 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005829
Gilles Peskine449bd832023-01-11 14:50:10 +01005830 if (!operation->can_output_key) {
5831 return PSA_ERROR_NOT_PERMITTED;
5832 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005833
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5835 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005836#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005838 /* Deriving a key in a secure element is not implemented yet. */
5839 status = PSA_ERROR_NOT_SUPPORTED;
5840 }
5841#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 if (status == PSA_SUCCESS) {
5843 status = psa_generate_derived_key_internal(slot,
5844 attributes->core.bits,
5845 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005846 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005847 if (status == PSA_SUCCESS) {
5848 status = psa_finish_key_creation(slot, driver, key);
5849 }
5850 if (status != PSA_SUCCESS) {
5851 psa_fail_key_creation(slot, driver);
5852 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005853
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005855}
5856
Gilles Peskineeab56e42018-07-12 17:12:33 +02005857
5858
5859/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005860/* Key derivation */
5861/****************************************************************/
5862
Steven Cooreman932ffb72021-02-15 12:14:32 +01005863#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005864static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005865{
5866#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5868 return 1;
5869 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005870#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005871#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5873 return 1;
5874 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005875#endif
5876#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5878 return 1;
5879 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005880#endif
5881#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5883 return 1;
5884 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005885#endif
5886#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5888 return 1;
5889 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005890#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005891#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005892 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5893 return 1;
5894 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005895#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005897}
5898
Gilles Peskine449bd832023-01-11 14:50:10 +01005899static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005900{
5901 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005902 psa_status_t status = psa_hash_setup(&operation, alg);
5903 psa_hash_abort(&operation);
5904 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005905}
5906
Gilles Peskine969c5d62019-01-16 15:53:06 +01005907static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005908 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005909 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005910{
Janos Follath5fe19732019-06-20 15:09:30 +01005911 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5912 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005914
Gilles Peskine969c5d62019-01-16 15:53:06 +01005915 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 if (!is_kdf_alg_supported(kdf_alg)) {
5917 return PSA_ERROR_NOT_SUPPORTED;
5918 }
John Durkop07cc04a2020-11-16 22:08:34 -08005919
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005920 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005921 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5923 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5924 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5925 if (hash_size == 0) {
5926 return PSA_ERROR_NOT_SUPPORTED;
5927 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005928
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005929 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5930 * we might fail later, which is somewhat unfriendly and potentially
5931 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005932 psa_status_t status = psa_hash_try_support(hash_alg);
5933 if (status != PSA_SUCCESS) {
5934 return status;
5935 }
5936 } else {
5937 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005938 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005939
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5941 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
5942 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
5943 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005944 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005945#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005946 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
5948 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005949 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005950 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005951#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5952 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 operation->capacity = 255 * hash_size;
5954 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005955}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005956
Gilles Peskine449bd832023-01-11 14:50:10 +01005957static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005958{
5959#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005960 if (alg == PSA_ALG_ECDH) {
5961 return PSA_SUCCESS;
5962 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005963#endif
5964 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005965 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005966}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005967
5968static int psa_key_derivation_allows_free_form_secret_input(
5969 psa_algorithm_t kdf_alg)
5970{
5971#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
5972 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5973 return 0;
5974 }
5975#endif
5976 (void) kdf_alg;
5977 return 1;
5978}
John Durkop07cc04a2020-11-16 22:08:34 -08005979#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005980
Gilles Peskine449bd832023-01-11 14:50:10 +01005981psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
5982 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005983{
5984 psa_status_t status;
5985
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 if (operation->alg != 0) {
5987 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005988 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005989
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
5991 return PSA_ERROR_INVALID_ARGUMENT;
5992 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
5993#if defined(AT_LEAST_ONE_BUILTIN_KDF)
5994 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
5995 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
5996 status = psa_key_agreement_try_support(ka_alg);
5997 if (status != PSA_SUCCESS) {
5998 return status;
5999 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006000 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6001 return PSA_ERROR_INVALID_ARGUMENT;
6002 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6004#else
6005 return PSA_ERROR_NOT_SUPPORTED;
6006#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6007 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6008#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6009 status = psa_key_derivation_setup_kdf(operation, alg);
6010#else
6011 return PSA_ERROR_NOT_SUPPORTED;
6012#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6013 } else {
6014 return PSA_ERROR_INVALID_ARGUMENT;
6015 }
6016
6017 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006018 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 }
6020 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006021}
6022
Przemek Stekiel69c46792022-06-10 12:59:51 +02006023#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006024static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6025 psa_algorithm_t kdf_alg,
6026 psa_key_derivation_step_t step,
6027 const uint8_t *data,
6028 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006029{
Gilles Peskine449bd832023-01-11 14:50:10 +01006030 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006031 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006032 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006033 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006034#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006035 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6036 return PSA_ERROR_INVALID_ARGUMENT;
6037 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006038#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006039 if (hkdf->state != HKDF_STATE_INIT) {
6040 return PSA_ERROR_BAD_STATE;
6041 } else {
6042 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6043 hash_alg,
6044 data, data_length);
6045 if (status != PSA_SUCCESS) {
6046 return status;
6047 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006048 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006050 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006051 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006052#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006054 /* We shouldn't be in different state as HKDF_EXPAND only allows
6055 * two inputs: SECRET (this case) and INFO which does not modify
6056 * the state. It could happen only if the hkdf
6057 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 if (hkdf->state != HKDF_STATE_INIT) {
6059 return PSA_ERROR_BAD_STATE;
6060 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006061
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006062 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6064 return PSA_ERROR_INVALID_ARGUMENT;
6065 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006066
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 memcpy(hkdf->prk, data, data_length);
6068 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006069#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006070 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006071 /* HKDF: If no salt was provided, use an empty salt.
6072 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006074#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6076 return PSA_ERROR_BAD_STATE;
6077 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006078#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6080 hash_alg,
6081 NULL, 0);
6082 if (status != PSA_SUCCESS) {
6083 return status;
6084 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006085 hkdf->state = HKDF_STATE_STARTED;
6086 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006087 if (hkdf->state != HKDF_STATE_STARTED) {
6088 return PSA_ERROR_BAD_STATE;
6089 }
6090 status = psa_mac_update(&hkdf->hmac,
6091 data, data_length);
6092 if (status != PSA_SUCCESS) {
6093 return status;
6094 }
6095 status = psa_mac_sign_finish(&hkdf->hmac,
6096 hkdf->prk,
6097 sizeof(hkdf->prk),
6098 &data_length);
6099 if (status != PSA_SUCCESS) {
6100 return status;
6101 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006102 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006103
Gilles Peskine2b522db2019-04-12 15:11:49 +02006104 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006105 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006106#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006108 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006110 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006112#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006113 {
6114 /* Block 0 is empty, and the next block will be
6115 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006116 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006117 }
6118
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006120 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006121#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6123 return PSA_ERROR_INVALID_ARGUMENT;
6124 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006125#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006126#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006127 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6128 hkdf->state == HKDF_STATE_INIT) {
6129 return PSA_ERROR_BAD_STATE;
6130 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006131#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 if (hkdf->state == HKDF_STATE_OUTPUT) {
6133 return PSA_ERROR_BAD_STATE;
6134 }
6135 if (hkdf->info_set) {
6136 return PSA_ERROR_BAD_STATE;
6137 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006138 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 if (data_length != 0) {
6140 hkdf->info = mbedtls_calloc(1, data_length);
6141 if (hkdf->info == NULL) {
6142 return PSA_ERROR_INSUFFICIENT_MEMORY;
6143 }
6144 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006145 }
6146 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006148 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006150 }
6151}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006152#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006153
John Durkop07cc04a2020-11-16 22:08:34 -08006154#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6155 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006156static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6157 const uint8_t *data,
6158 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006159{
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6161 return PSA_ERROR_BAD_STATE;
6162 }
Janos Follathf08e2652019-06-13 09:05:41 +01006163
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 if (data_length != 0) {
6165 prf->seed = mbedtls_calloc(1, data_length);
6166 if (prf->seed == NULL) {
6167 return PSA_ERROR_INSUFFICIENT_MEMORY;
6168 }
Janos Follathf08e2652019-06-13 09:05:41 +01006169
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006171 prf->seed_length = data_length;
6172 }
Janos Follathf08e2652019-06-13 09:05:41 +01006173
Gilles Peskine43f958b2020-12-13 14:55:14 +01006174 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006175
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006177}
6178
Gilles Peskine449bd832023-01-11 14:50:10 +01006179static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6180 const uint8_t *data,
6181 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006182{
Gilles Peskine449bd832023-01-11 14:50:10 +01006183 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6184 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6185 return PSA_ERROR_BAD_STATE;
6186 }
Janos Follath81550542019-06-13 14:26:34 +01006187
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 if (data_length != 0) {
6189 prf->secret = mbedtls_calloc(1, data_length);
6190 if (prf->secret == NULL) {
6191 return PSA_ERROR_INSUFFICIENT_MEMORY;
6192 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006193
Gilles Peskine449bd832023-01-11 14:50:10 +01006194 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006195 prf->secret_length = data_length;
6196 }
Janos Follath81550542019-06-13 14:26:34 +01006197
Gilles Peskine43f958b2020-12-13 14:55:14 +01006198 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006199
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006201}
6202
Gilles Peskine449bd832023-01-11 14:50:10 +01006203static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6204 const uint8_t *data,
6205 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006206{
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6208 return PSA_ERROR_BAD_STATE;
6209 }
Janos Follath63028dd2019-06-13 09:15:47 +01006210
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 if (data_length != 0) {
6212 prf->label = mbedtls_calloc(1, data_length);
6213 if (prf->label == NULL) {
6214 return PSA_ERROR_INSUFFICIENT_MEMORY;
6215 }
Janos Follath63028dd2019-06-13 09:15:47 +01006216
Gilles Peskine449bd832023-01-11 14:50:10 +01006217 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006218 prf->label_length = data_length;
6219 }
Janos Follath63028dd2019-06-13 09:15:47 +01006220
Gilles Peskine43f958b2020-12-13 14:55:14 +01006221 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006222
Gilles Peskine449bd832023-01-11 14:50:10 +01006223 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006224}
6225
Gilles Peskine449bd832023-01-11 14:50:10 +01006226static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6227 psa_key_derivation_step_t step,
6228 const uint8_t *data,
6229 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006230{
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006232 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006233 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006234 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006236 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006238 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006240 }
6241}
John Durkop07cc04a2020-11-16 22:08:34 -08006242#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6243 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6244
6245#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6246static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6247 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006248 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006250{
6251 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006252 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6253 4 + data_length + prf->other_secret_length :
6254 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6257 return PSA_ERROR_INVALID_ARGUMENT;
6258 }
John Durkop07cc04a2020-11-16 22:08:34 -08006259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 uint8_t *pms = mbedtls_calloc(1, pms_len);
6261 if (pms == NULL) {
6262 return PSA_ERROR_INSUFFICIENT_MEMORY;
6263 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006264 uint8_t *cur = pms;
6265
6266 /* pure-PSK:
6267 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006268 *
6269 * The premaster secret is formed as follows: if the PSK is N octets
6270 * long, concatenate a uint16 with the value N, N zero octets, a second
6271 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006272 *
6273 * mixed-PSK:
6274 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6275 * follows: concatenate a uint16 with the length of the other secret,
6276 * the other secret itself, uint16 with the length of PSK, and the
6277 * PSK itself.
6278 * For details please check:
6279 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6280 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6281 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006282 */
6283
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6285 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6286 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6287 if (prf->other_secret_length != 0) {
6288 memcpy(cur, prf->other_secret, prf->other_secret_length);
6289 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006290 cur += prf->other_secret_length;
6291 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 } else {
6293 *cur++ = MBEDTLS_BYTE_1(data_length);
6294 *cur++ = MBEDTLS_BYTE_0(data_length);
6295 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006296 cur += data_length;
6297 }
6298
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 *cur++ = MBEDTLS_BYTE_1(data_length);
6300 *cur++ = MBEDTLS_BYTE_0(data_length);
6301 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006302 cur += data_length;
6303
Gilles Peskine449bd832023-01-11 14:50:10 +01006304 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006305
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 mbedtls_platform_zeroize(pms, pms_len);
6307 mbedtls_free(pms);
6308 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006309}
Janos Follath6660f0e2019-06-17 08:44:03 +01006310
Przemek Stekiele47201b2022-04-19 13:53:28 +02006311static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6312 psa_tls12_prf_key_derivation_t *prf,
6313 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006315{
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6317 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006318 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006319
6320 if (data_length != 0) {
6321 prf->other_secret = mbedtls_calloc(1, data_length);
6322 if (prf->other_secret == NULL) {
6323 return PSA_ERROR_INSUFFICIENT_MEMORY;
6324 }
6325
6326 memcpy(prf->other_secret, data, data_length);
6327 prf->other_secret_length = data_length;
6328 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006329 prf->other_secret_length = 0;
6330 }
6331
6332 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6333
Gilles Peskine449bd832023-01-11 14:50:10 +01006334 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006335}
6336
Janos Follath6660f0e2019-06-17 08:44:03 +01006337static psa_status_t psa_tls12_prf_psk_to_ms_input(
6338 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006339 psa_key_derivation_step_t step,
6340 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006342{
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006344 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 return psa_tls12_prf_psk_to_ms_set_key(prf,
6346 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006347 break;
6348 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6350 data,
6351 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006352 break;
6353 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006355 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006356
Przemek Stekiele47201b2022-04-19 13:53:28 +02006357 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006358}
John Durkop07cc04a2020-11-16 22:08:34 -08006359#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006360
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006361#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6362static psa_status_t psa_tls12_ecjpake_to_pms_input(
6363 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006364 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006365 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006366 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006367{
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6369 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6370 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006371 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006372
6373 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006374 if (data[0] != 0x04) {
6375 return PSA_ERROR_INVALID_ARGUMENT;
6376 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006377
6378 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006380
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006382}
6383#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006384/** Check whether the given key type is acceptable for the given
6385 * input step of a key derivation.
6386 *
6387 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6388 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6389 * Both secret and non-secret inputs can alternatively have the type
6390 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6391 * that the input was passed as a buffer rather than via a key object.
6392 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006393static int psa_key_derivation_check_input_type(
6394 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006396{
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006398 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 if (key_type == PSA_KEY_TYPE_DERIVE) {
6400 return PSA_SUCCESS;
6401 }
6402 if (key_type == PSA_KEY_TYPE_NONE) {
6403 return PSA_SUCCESS;
6404 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006405 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006406 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 if (key_type == PSA_KEY_TYPE_DERIVE) {
6408 return PSA_SUCCESS;
6409 }
6410 if (key_type == PSA_KEY_TYPE_NONE) {
6411 return PSA_SUCCESS;
6412 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006413 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006414 case PSA_KEY_DERIVATION_INPUT_LABEL:
6415 case PSA_KEY_DERIVATION_INPUT_SALT:
6416 case PSA_KEY_DERIVATION_INPUT_INFO:
6417 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6419 return PSA_SUCCESS;
6420 }
6421 if (key_type == PSA_KEY_TYPE_NONE) {
6422 return PSA_SUCCESS;
6423 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006424 break;
6425 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006427}
6428
Janos Follathb80a94e2019-06-12 15:54:46 +01006429static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006430 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006431 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006432 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006433 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006435{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006436 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006438
Gilles Peskine449bd832023-01-11 14:50:10 +01006439 status = psa_key_derivation_check_input_type(step, key_type);
6440 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006441 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006442 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006443
Przemek Stekiel69c46792022-06-10 12:59:51 +02006444#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006445 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6446 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6447 step, data, data_length);
6448 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006449#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006450#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6452 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6453 step, data, data_length);
6454 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006455#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6456#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6458 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6459 step, data, data_length);
6460 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006461#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006462#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006464 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6466 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006467#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006468 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006469 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006470 (void) data;
6471 (void) data_length;
6472 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006474 }
6475
Gilles Peskine224b0d62019-09-23 18:13:17 +02006476exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 if (status != PSA_SUCCESS) {
6478 psa_key_derivation_abort(operation);
6479 }
6480 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006481}
6482
Janos Follath51f4a0f2019-06-14 11:35:55 +01006483psa_status_t psa_key_derivation_input_bytes(
6484 psa_key_derivation_operation_t *operation,
6485 psa_key_derivation_step_t step,
6486 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006487 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006488{
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 return psa_key_derivation_input_internal(operation, step,
6490 PSA_KEY_TYPE_NONE,
6491 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006492}
6493
Janos Follath51f4a0f2019-06-14 11:35:55 +01006494psa_status_t psa_key_derivation_input_key(
6495 psa_key_derivation_operation_t *operation,
6496 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006498{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006499 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006500 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006501 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006502
Ronald Cron5c522922020-11-14 16:35:34 +01006503 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6505 if (status != PSA_SUCCESS) {
6506 psa_key_derivation_abort(operation);
6507 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006508 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006509
6510 /* Passing a key object as a SECRET input unlocks the permission
6511 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006512 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006513 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006514 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006515
Gilles Peskine449bd832023-01-11 14:50:10 +01006516 status = psa_key_derivation_input_internal(operation,
6517 step, slot->attr.type,
6518 slot->key.data,
6519 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006520
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006522
Gilles Peskine449bd832023-01-11 14:50:10 +01006523 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006524}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006525
6526
6527
6528/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006529/* Key agreement */
6530/****************************************************************/
6531
Gilles Peskine449bd832023-01-11 14:50:10 +01006532psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6533 const uint8_t *key_buffer,
6534 size_t key_buffer_size,
6535 psa_algorithm_t alg,
6536 const uint8_t *peer_key,
6537 size_t peer_key_length,
6538 uint8_t *shared_secret,
6539 size_t shared_secret_size,
6540 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006541{
Gilles Peskine449bd832023-01-11 14:50:10 +01006542 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006543#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006544 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006545 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6546 key_buffer_size, alg,
6547 peer_key, peer_key_length,
6548 shared_secret,
6549 shared_secret_size,
6550 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006551#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006552 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006553 (void) attributes;
6554 (void) key_buffer;
6555 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006556 (void) peer_key;
6557 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006558 (void) shared_secret;
6559 (void) shared_secret_size;
6560 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006561 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006562 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006563}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006564
Aditya Deshpande17845b82022-10-13 17:21:01 +01006565/** Internal function for raw key agreement
6566 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006567 * to the driver's implementation if a driver is present.
6568 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006569 * (psa_key_agreement_raw_builtin).
6570 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006571static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6572 psa_key_slot_t *private_key,
6573 const uint8_t *peer_key,
6574 size_t peer_key_length,
6575 uint8_t *shared_secret,
6576 size_t shared_secret_size,
6577 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006578{
Gilles Peskine449bd832023-01-11 14:50:10 +01006579 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6580 return PSA_ERROR_NOT_SUPPORTED;
6581 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006582
6583 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006584 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006585 };
6586
Gilles Peskine449bd832023-01-11 14:50:10 +01006587 return psa_driver_wrapper_key_agreement(&attributes,
6588 private_key->key.data,
6589 private_key->key.bytes, alg,
6590 peer_key, peer_key_length,
6591 shared_secret,
6592 shared_secret_size,
6593 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006594}
6595
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006596/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006597 * to potentially free embedded data structures and wipe confidential data.
6598 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006599static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6600 psa_key_derivation_step_t step,
6601 psa_key_slot_t *private_key,
6602 const uint8_t *peer_key,
6603 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006604{
6605 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006606 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006607 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006608 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006609
6610 /* Step 1: run the secret agreement algorithm to generate the shared
6611 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006612 status = psa_key_agreement_raw_internal(ka_alg,
6613 private_key,
6614 peer_key, peer_key_length,
6615 shared_secret,
6616 sizeof(shared_secret),
6617 &shared_secret_length);
6618 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006619 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006620 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006621
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006622 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006623 * the shared secret. A shared secret is permitted wherever a key
6624 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006625 status = psa_key_derivation_input_internal(operation, step,
6626 PSA_KEY_TYPE_DERIVE,
6627 shared_secret,
6628 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006629exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006630 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6631 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006632}
6633
Gilles Peskine449bd832023-01-11 14:50:10 +01006634psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6635 psa_key_derivation_step_t step,
6636 mbedtls_svc_key_id_t private_key,
6637 const uint8_t *peer_key,
6638 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006639{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006640 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006641 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006642 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006643
Gilles Peskine449bd832023-01-11 14:50:10 +01006644 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6645 return PSA_ERROR_INVALID_ARGUMENT;
6646 }
Ronald Cron5c522922020-11-14 16:35:34 +01006647 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006648 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6649 if (status != PSA_SUCCESS) {
6650 return status;
6651 }
6652 status = psa_key_agreement_internal(operation, step,
6653 slot,
6654 peer_key, peer_key_length);
6655 if (status != PSA_SUCCESS) {
6656 psa_key_derivation_abort(operation);
6657 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006658 /* If a private key has been added as SECRET, we allow the derived
6659 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006660 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006661 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006662 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006663 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006664
Gilles Peskine449bd832023-01-11 14:50:10 +01006665 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006666
Gilles Peskine449bd832023-01-11 14:50:10 +01006667 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006668}
6669
Gilles Peskine449bd832023-01-11 14:50:10 +01006670psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6671 mbedtls_svc_key_id_t private_key,
6672 const uint8_t *peer_key,
6673 size_t peer_key_length,
6674 uint8_t *output,
6675 size_t output_size,
6676 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006677{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006678 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006679 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006680 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006681
Gilles Peskine449bd832023-01-11 14:50:10 +01006682 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006683 status = PSA_ERROR_INVALID_ARGUMENT;
6684 goto exit;
6685 }
Ronald Cron5c522922020-11-14 16:35:34 +01006686 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006687 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6688 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006689 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006690 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006691
Gilles Peskine3e561302022-04-14 00:17:15 +02006692 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6693 * for the output size. The PSA specification only guarantees that this
6694 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6695 * but it might be nice to allow smaller buffers if the output fits.
6696 * At the time of writing this comment, with only ECDH implemented,
6697 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6698 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6699 * be exact for it as well. */
6700 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006701 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6702 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006703 status = PSA_ERROR_BUFFER_TOO_SMALL;
6704 goto exit;
6705 }
6706
Gilles Peskine449bd832023-01-11 14:50:10 +01006707 status = psa_key_agreement_raw_internal(alg, slot,
6708 peer_key, peer_key_length,
6709 output, output_size,
6710 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006711
6712exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006713 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006714 /* If an error happens and is not handled properly, the output
6715 * may be used as a key to protect sensitive data. Arrange for such
6716 * a key to be random, which is likely to result in decryption or
6717 * verification errors. This is better than filling the buffer with
6718 * some constant data such as zeros, which would result in the data
6719 * being protected with a reproducible, easily knowable key.
6720 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006721 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006722 *output_length = output_size;
6723 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006724
Gilles Peskine449bd832023-01-11 14:50:10 +01006725 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006726
Gilles Peskine449bd832023-01-11 14:50:10 +01006727 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006728}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006729
6730
Gilles Peskine30524eb2020-11-13 17:02:26 +01006731
Gilles Peskine86a440b2018-11-12 18:39:40 +01006732/****************************************************************/
6733/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006734/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006735
Gilles Peskine30524eb2020-11-13 17:02:26 +01006736/** Initialize the PSA random generator.
6737 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006738static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006739{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006740#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006741 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006742#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6743
Gilles Peskine30524eb2020-11-13 17:02:26 +01006744 /* Set default configuration if
6745 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006746 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006747 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006748 }
6749 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006750 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006751 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006752
Gilles Peskine449bd832023-01-11 14:50:10 +01006753 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006754#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6755 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6756 /* The PSA entropy injection feature depends on using NV seed as an entropy
6757 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006758 mbedtls_entropy_add_source(&rng->entropy,
6759 mbedtls_nv_seed_poll, NULL,
6760 MBEDTLS_ENTROPY_BLOCK_SIZE,
6761 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006762#endif
6763
Gilles Peskine449bd832023-01-11 14:50:10 +01006764 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006765#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006766}
6767
6768/** Deinitialize the PSA random generator.
6769 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006770static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006771{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006772#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006773 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006774#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01006775 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
6776 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006777#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006778}
6779
6780/** Seed the PSA random generator.
6781 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006782static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006783{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006784#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6785 /* Do nothing: the external RNG seeds itself. */
6786 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006787 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006788#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006789 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01006790 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
6791 drbg_seed, sizeof(drbg_seed) - 1);
6792 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006793#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006794}
6795
Gilles Peskine449bd832023-01-11 14:50:10 +01006796psa_status_t psa_generate_random(uint8_t *output,
6797 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006798{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006799 GUARD_MODULE_INITIALIZED;
6800
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006801#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6802
6803 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006804 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6805 output, output_size,
6806 &output_length);
6807 if (status != PSA_SUCCESS) {
6808 return status;
6809 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006810 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006811 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006812 if (output_length != output_size) {
6813 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6814 }
6815 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006816
6817#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6818
Gilles Peskine449bd832023-01-11 14:50:10 +01006819 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006820 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006821 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6822 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6823 output_size);
6824 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6825 output, request_size);
6826 if (ret != 0) {
6827 return mbedtls_to_psa_error(ret);
6828 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006829 output_size -= request_size;
6830 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006831 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006832 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006833#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006834}
6835
Gilles Peskine8814fc42020-12-14 15:33:44 +01006836/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006837 *
6838 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6839 * `psa_generate_random(...)`. The state parameter is ignored since the
6840 * PSA API doesn't support passing an explicit state.
6841 *
6842 * In the non-external case, psa_generate_random() calls an
6843 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6844 * and semantics as mbedtls_psa_get_random(). As an optimization,
6845 * instead of doing this back-and-forth between the PSA API and the
6846 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6847 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006848 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006849#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6850int mbedtls_psa_get_random(void *p_rng,
6851 unsigned char *output,
6852 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01006853{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006854 /* This function takes a pointer to the RNG state because that's what
6855 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6856 * its own state internally and doesn't let the caller access that state.
6857 * So we just ignore the state parameter, and in practice we'll pass
6858 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006859 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006860 psa_status_t status = psa_generate_random(output, output_size);
6861 if (status == PSA_SUCCESS) {
6862 return 0;
6863 } else {
6864 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
6865 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01006866}
6867#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6868
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006869#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006870#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006871
Gilles Peskine449bd832023-01-11 14:50:10 +01006872psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
6873 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006874{
Gilles Peskine449bd832023-01-11 14:50:10 +01006875 if (global_data.initialized) {
6876 return PSA_ERROR_NOT_PERMITTED;
6877 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006878
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
6880 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
6881 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
6882 return PSA_ERROR_INVALID_ARGUMENT;
6883 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006884
Gilles Peskine449bd832023-01-11 14:50:10 +01006885 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006886}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006887#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006888
Ronald Cron3772afe2021-02-08 16:10:05 +01006889/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006890 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006891 * \param type The key type
6892 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006893 *
6894 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006895 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006896 * \retval #PSA_ERROR_INVALID_ARGUMENT
6897 * The size in bits of the key is not valid.
6898 * \retval #PSA_ERROR_NOT_SUPPORTED
6899 * The type and/or the size in bits of the key or the combination of
6900 * the two is not supported.
6901 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006902static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006903 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006904{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006905 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006906
Gilles Peskine449bd832023-01-11 14:50:10 +01006907 if (key_type_is_raw_bytes(type)) {
6908 status = psa_validate_unstructured_key_bit_size(type, bits);
6909 if (status != PSA_SUCCESS) {
6910 return status;
6911 }
6912 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006913#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006914 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6915 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
6916 return PSA_ERROR_NOT_SUPPORTED;
6917 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006918
Ronald Cron01b2aba2020-10-05 09:42:02 +02006919 /* Accept only byte-aligned keys, for the same reasons as
6920 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006921 if (bits % 8 != 0) {
6922 return PSA_ERROR_NOT_SUPPORTED;
6923 }
6924 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006925#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006926
Ronald Cron5c4d3862020-12-07 11:07:24 +01006927#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006928 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01006929 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006930 return PSA_SUCCESS;
6931 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006932#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006933 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006934 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006935 }
6936
Gilles Peskine449bd832023-01-11 14:50:10 +01006937 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006938}
6939
Ronald Cron55ed0592020-10-05 10:30:40 +02006940psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006941 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01006942 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006943{
6944 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006945 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006946
Gilles Peskine449bd832023-01-11 14:50:10 +01006947 if ((attributes->domain_parameters == NULL) &&
6948 (attributes->domain_parameters_size != 0)) {
6949 return PSA_ERROR_INVALID_ARGUMENT;
6950 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02006951
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 if (key_type_is_raw_bytes(type)) {
6953 status = psa_generate_random(key_buffer, key_buffer_size);
6954 if (status != PSA_SUCCESS) {
6955 return status;
6956 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006957
David Brown12ca5032021-02-11 11:02:00 -07006958#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006959 if (type == PSA_KEY_TYPE_DES) {
6960 psa_des_set_key_parity(key_buffer, key_buffer_size);
6961 }
David Brown8107e312021-02-12 08:08:46 -07006962#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01006963 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01006964
Jaeden Amero424fa932021-05-14 08:34:32 +01006965#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
6966 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01006967 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
6968 return mbedtls_psa_rsa_generate_key(attributes,
6969 key_buffer,
6970 key_buffer_size,
6971 key_buffer_length);
6972 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01006973#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
6974 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006975
John Durkop9814fa22020-11-04 12:28:15 -08006976#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006977 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6978 return mbedtls_psa_ecp_generate_key(attributes,
6979 key_buffer,
6980 key_buffer_size,
6981 key_buffer_length);
6982 } else
John Durkop9814fa22020-11-04 12:28:15 -08006983#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006984 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006985 (void) key_buffer_length;
6986 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02006987 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006988
Gilles Peskine449bd832023-01-11 14:50:10 +01006989 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006990}
Darryl Green0c6575a2018-11-07 16:05:30 +00006991
Gilles Peskine449bd832023-01-11 14:50:10 +01006992psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
6993 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006994{
6995 psa_status_t status;
6996 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006997 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006998 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006999
Ronald Cron81709fc2020-11-14 12:10:32 +01007000 *key = MBEDTLS_SVC_KEY_ID_INIT;
7001
Gilles Peskine0f84d622019-09-12 19:03:13 +02007002 /* Reject any attempt to create a zero-length key so that we don't
7003 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007004 if (psa_get_key_bits(attributes) == 0) {
7005 return PSA_ERROR_INVALID_ARGUMENT;
7006 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007007
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007008 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007009 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7010 return PSA_ERROR_INVALID_ARGUMENT;
7011 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007012
Gilles Peskine449bd832023-01-11 14:50:10 +01007013 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7014 &slot, &driver);
7015 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007016 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007017 }
Gilles Peskine11792082019-08-06 18:36:36 +02007018
Ronald Cron977c2472020-10-13 08:32:21 +02007019 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307020 * storage ( thus not in the case of generating a key in a secure element
7021 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7022 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007023 if (slot->key.data == NULL) {
7024 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7025 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007026 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007027 attributes->core.type, attributes->core.bits);
7028 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007029 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007030 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007031
7032 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007033 attributes->core.type,
7034 attributes->core.bits);
7035 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007036 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007037 attributes, &key_buffer_size);
7038 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007039 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007040 }
Ronald Cron977c2472020-10-13 08:32:21 +02007041 }
Ronald Cron977c2472020-10-13 08:32:21 +02007042
Gilles Peskine449bd832023-01-11 14:50:10 +01007043 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7044 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007045 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007046 }
Ronald Cron977c2472020-10-13 08:32:21 +02007047 }
7048
Gilles Peskine449bd832023-01-11 14:50:10 +01007049 status = psa_driver_wrapper_generate_key(attributes,
7050 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007051
Gilles Peskine449bd832023-01-11 14:50:10 +01007052 if (status != PSA_SUCCESS) {
7053 psa_remove_key_data_from_memory(slot);
7054 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007055
Gilles Peskine11792082019-08-06 18:36:36 +02007056exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007057 if (status == PSA_SUCCESS) {
7058 status = psa_finish_key_creation(slot, driver, key);
7059 }
7060 if (status != PSA_SUCCESS) {
7061 psa_fail_key_creation(slot, driver);
7062 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007063
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007065}
7066
Gilles Peskinee59236f2018-01-27 23:32:46 +01007067/****************************************************************/
7068/* Module setup */
7069/****************************************************************/
7070
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007071#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007072psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007073 void (* entropy_init)(mbedtls_entropy_context *ctx),
7074 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007075{
Gilles Peskine449bd832023-01-11 14:50:10 +01007076 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7077 return PSA_ERROR_BAD_STATE;
7078 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007079 global_data.rng.entropy_init = entropy_init;
7080 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007081 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007082}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007083#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007084
Gilles Peskine449bd832023-01-11 14:50:10 +01007085void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007086{
Gilles Peskine449bd832023-01-11 14:50:10 +01007087 psa_wipe_all_key_slots();
7088 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7089 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007090 }
7091 /* Wipe all remaining data, including configuration.
7092 * In particular, this sets all state indicator to the value
7093 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007094 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007095
7096 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007097 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007098}
7099
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007100#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7101/** Recover a transaction that was interrupted by a power failure.
7102 *
7103 * This function is called during initialization, before psa_crypto_init()
7104 * returns. If this function returns a failure status, the initialization
7105 * fails.
7106 */
7107static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007108 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007109{
Gilles Peskine449bd832023-01-11 14:50:10 +01007110 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007111 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7112 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007113 /* TODO - fall through to the failure case until this
7114 * is implemented.
7115 * https://github.com/ARMmbed/mbed-crypto/issues/218
7116 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007117 default:
7118 /* We found an unsupported transaction in the storage.
7119 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007120 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007121 }
7122}
7123#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7124
Gilles Peskine449bd832023-01-11 14:50:10 +01007125psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007126{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007127 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007128
Gilles Peskinec6b69072018-11-20 21:42:52 +01007129 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007130 if (global_data.initialized != 0) {
7131 return PSA_SUCCESS;
7132 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007133
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007134 /* Init drivers */
7135 status = psa_driver_wrapper_init();
7136 if (status != PSA_SUCCESS) {
7137 goto exit;
7138 }
7139 global_data.drivers_initialized = 1;
7140
Gilles Peskine30524eb2020-11-13 17:02:26 +01007141 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007142 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007143 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007144 status = mbedtls_psa_random_seed(&global_data.rng);
7145 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007146 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007147 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007148 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007149
Gilles Peskine449bd832023-01-11 14:50:10 +01007150 status = psa_initialize_key_slots();
7151 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007152 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007153 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007154
Gilles Peskine4b734222019-07-24 15:56:31 +02007155#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007156 status = psa_crypto_load_transaction();
7157 if (status == PSA_SUCCESS) {
7158 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7159 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007160 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007161 }
7162 status = psa_crypto_stop_transaction();
7163 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007164 /* There's no transaction to complete. It's all good. */
7165 status = PSA_SUCCESS;
7166 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007167#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007168
Gilles Peskinec6b69072018-11-20 21:42:52 +01007169 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007170 global_data.initialized = 1;
7171
7172exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007173 if (status != PSA_SUCCESS) {
7174 mbedtls_psa_crypto_free();
7175 }
7176 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007177}
7178
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007179psa_status_t psa_crypto_driver_pake_get_password_len(
7180 const psa_crypto_driver_pake_inputs_t *inputs,
7181 size_t *password_len)
7182{
7183 if (inputs->password_len == 0) {
7184 return PSA_ERROR_BAD_STATE;
7185 }
7186
7187 *password_len = inputs->password_len;
7188
7189 return PSA_SUCCESS;
7190}
7191
7192psa_status_t psa_crypto_driver_pake_get_password(
7193 const psa_crypto_driver_pake_inputs_t *inputs,
7194 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7195{
7196 if (inputs->password_len == 0) {
7197 return PSA_ERROR_BAD_STATE;
7198 }
7199
7200 if (buffer_size < inputs->password_len) {
7201 return PSA_ERROR_BUFFER_TOO_SMALL;
7202 }
7203
7204 memcpy(buffer, inputs->password, inputs->password_len);
7205 *buffer_length = inputs->password_len;
7206
7207 return PSA_SUCCESS;
7208}
7209
7210psa_status_t psa_crypto_driver_pake_get_role(
7211 const psa_crypto_driver_pake_inputs_t *inputs,
7212 psa_pake_role_t *role)
7213{
7214 if (inputs->role == PSA_PAKE_ROLE_NONE) {
7215 return PSA_ERROR_BAD_STATE;
7216 }
7217
7218 *role = inputs->role;
7219
7220 return PSA_SUCCESS;
7221}
7222
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007223psa_status_t psa_crypto_driver_pake_get_user_len(
7224 const psa_crypto_driver_pake_inputs_t *inputs,
7225 size_t *user_len)
7226{
7227 if (inputs->user_len == 0) {
7228 return PSA_ERROR_BAD_STATE;
7229 }
7230
7231 *user_len = inputs->user_len;
7232
7233 return PSA_SUCCESS;
7234}
7235
7236psa_status_t psa_crypto_driver_pake_get_user(
7237 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007238 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007239{
7240 if (inputs->user_len == 0) {
7241 return PSA_ERROR_BAD_STATE;
7242 }
7243
Przemek Stekielc0e62502023-03-14 11:49:36 +01007244 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007245 return PSA_ERROR_BUFFER_TOO_SMALL;
7246 }
7247
Przemek Stekielc0e62502023-03-14 11:49:36 +01007248 memcpy(user_id, inputs->user, inputs->user_len);
7249 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007250
7251 return PSA_SUCCESS;
7252}
7253
7254psa_status_t psa_crypto_driver_pake_get_peer_len(
7255 const psa_crypto_driver_pake_inputs_t *inputs,
7256 size_t *peer_len)
7257{
7258 if (inputs->peer_len == 0) {
7259 return PSA_ERROR_BAD_STATE;
7260 }
7261
7262 *peer_len = inputs->peer_len;
7263
7264 return PSA_SUCCESS;
7265}
7266
7267psa_status_t psa_crypto_driver_pake_get_peer(
7268 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007269 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007270{
7271 if (inputs->peer_len == 0) {
7272 return PSA_ERROR_BAD_STATE;
7273 }
7274
Przemek Stekielc0e62502023-03-14 11:49:36 +01007275 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007276 return PSA_ERROR_BUFFER_TOO_SMALL;
7277 }
7278
Przemek Stekielc0e62502023-03-14 11:49:36 +01007279 memcpy(peer_id, inputs->peer, inputs->peer_len);
7280 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007281
7282 return PSA_SUCCESS;
7283}
7284
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007285psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7286 const psa_crypto_driver_pake_inputs_t *inputs,
7287 psa_pake_cipher_suite_t *cipher_suite)
7288{
7289 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7290 return PSA_ERROR_BAD_STATE;
7291 }
7292
7293 *cipher_suite = inputs->cipher_suite;
7294
7295 return PSA_SUCCESS;
7296}
7297
Neil Armstronga7d08c32022-06-01 18:21:20 +02007298psa_status_t psa_pake_setup(
7299 psa_pake_operation_t *operation,
7300 const psa_pake_cipher_suite_t *cipher_suite)
7301{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007302 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007303
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007304 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007305 status = PSA_ERROR_BAD_STATE;
7306 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007307 }
7308
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007309 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007310 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007311 status = PSA_ERROR_INVALID_ARGUMENT;
7312 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007313 }
7314
Przemek Stekiel51eac532022-12-07 11:04:51 +01007315 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7316
Przemek Stekiele12ed362022-12-21 12:54:46 +01007317 operation->alg = cipher_suite->algorithm;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007318 operation->data.inputs.cipher_suite = *cipher_suite;
7319
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007320#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007321 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007322 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007323 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007324
Przemek Stekiele12ed362022-12-21 12:54:46 +01007325 computation_stage->state = PSA_PAKE_STATE_SETUP;
7326 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7327 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7328 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007329 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007330#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007331 {
7332 status = PSA_ERROR_NOT_SUPPORTED;
7333 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007334 }
7335
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007336 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7337
Przemek Stekiel51eac532022-12-07 11:04:51 +01007338 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007339exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007340 psa_pake_abort(operation);
7341 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007342}
7343
7344psa_status_t psa_pake_set_password_key(
7345 psa_pake_operation_t *operation,
7346 mbedtls_svc_key_id_t password)
7347{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007348 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007349 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7350 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007351
Przemek Stekiel51eac532022-12-07 11:04:51 +01007352 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007353 status = PSA_ERROR_BAD_STATE;
7354 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007355 }
7356
Przemek Stekiel6c764412022-11-22 14:05:12 +01007357 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7358 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007359 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007360 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007361 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007362 }
7363
Przemek Stekiel6c764412022-11-22 14:05:12 +01007364 psa_key_attributes_t attributes = {
7365 .core = slot->attr
7366 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007367
Przemek Stekiel51eac532022-12-07 11:04:51 +01007368 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007369
Przemek Stekiel51eac532022-12-07 11:04:51 +01007370 if (type != PSA_KEY_TYPE_PASSWORD &&
7371 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7372 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007373 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007374 }
7375
Przemek Stekiel51eac532022-12-07 11:04:51 +01007376 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7377 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007378 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007379 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007380 }
7381
7382 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7383 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007384 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007385exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007386 if (status != PSA_SUCCESS) {
7387 psa_pake_abort(operation);
7388 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007389 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007390 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007391}
7392
7393psa_status_t psa_pake_set_user(
7394 psa_pake_operation_t *operation,
7395 const uint8_t *user_id,
7396 size_t user_id_len)
7397{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007398 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007399
7400 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007401 status = PSA_ERROR_BAD_STATE;
7402 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007403 }
7404
Przemek Stekiel51eac532022-12-07 11:04:51 +01007405 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007406 status = PSA_ERROR_INVALID_ARGUMENT;
7407 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007408 }
7409
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007410 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007411 status = PSA_ERROR_BAD_STATE;
7412 goto exit;
7413 }
7414
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007415 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007416 if ((user_id_len != sizeof(jpake_server_id) ||
7417 memcmp(user_id, jpake_server_id, user_id_len) != 0) &&
7418 (user_id_len != sizeof(jpake_client_id) ||
7419 memcmp(user_id, jpake_client_id, user_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007420 status = PSA_ERROR_NOT_SUPPORTED;
7421 goto exit;
7422 }
7423
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007424 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7425 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007426 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7427 goto exit;
7428 }
7429
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007430 memcpy(operation->data.inputs.user, user_id, user_id_len);
7431 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007432
7433 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007434exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007435 psa_pake_abort(operation);
7436 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007437}
7438
7439psa_status_t psa_pake_set_peer(
7440 psa_pake_operation_t *operation,
7441 const uint8_t *peer_id,
7442 size_t peer_id_len)
7443{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007444 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007445
7446 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007447 status = PSA_ERROR_BAD_STATE;
7448 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007449 }
7450
Przemek Stekiel51eac532022-12-07 11:04:51 +01007451 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007452 status = PSA_ERROR_INVALID_ARGUMENT;
7453 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007454 }
7455
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007456 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007457 status = PSA_ERROR_BAD_STATE;
7458 goto exit;
7459 }
7460
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007461 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007462 if ((peer_id_len != sizeof(jpake_server_id) ||
7463 memcmp(peer_id, jpake_server_id, peer_id_len) != 0) &&
7464 (peer_id_len != sizeof(jpake_client_id) ||
7465 memcmp(peer_id, jpake_client_id, peer_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007466 status = PSA_ERROR_NOT_SUPPORTED;
7467 goto exit;
7468 }
7469
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007470 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7471 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007472 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7473 goto exit;
7474 }
7475
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007476 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7477 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007478
7479 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007480exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007481 psa_pake_abort(operation);
7482 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007483}
7484
7485psa_status_t psa_pake_set_role(
7486 psa_pake_operation_t *operation,
7487 psa_pake_role_t role)
7488{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007489 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007490
Przemek Stekiel51eac532022-12-07 11:04:51 +01007491 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007492 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007493 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007494 }
7495
Przemek Stekiel09104b82023-03-06 14:11:51 +01007496 switch (operation->alg) {
7497#if defined(PSA_WANT_ALG_JPAKE)
7498 case PSA_ALG_JPAKE:
7499 if (role == PSA_PAKE_ROLE_NONE) {
7500 return PSA_SUCCESS;
7501 }
7502 status = PSA_ERROR_INVALID_ARGUMENT;
7503 break;
7504#endif
7505 default:
7506 (void) role;
7507 status = PSA_ERROR_NOT_SUPPORTED;
7508 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007509 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007510exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007511 psa_pake_abort(operation);
7512 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007513}
7514
Przemek Stekielb09c4872023-01-17 12:05:38 +01007515/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007516#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007517static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007518 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007519{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007520 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007521 case PSA_PAKE_OUTPUT_X1_X2:
7522 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007523 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007524 case PSA_PAKE_X1_STEP_KEY_SHARE:
7525 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007526 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7527 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007528 case PSA_PAKE_X1_STEP_ZK_PROOF:
7529 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007530 case PSA_PAKE_X2_STEP_KEY_SHARE:
7531 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007532 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7533 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007534 case PSA_PAKE_X2_STEP_ZK_PROOF:
7535 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007536 default:
7537 return PSA_JPAKE_STEP_INVALID;
7538 }
7539 break;
7540 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007541 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007542 case PSA_PAKE_X1_STEP_KEY_SHARE:
7543 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007544 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7545 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007546 case PSA_PAKE_X1_STEP_ZK_PROOF:
7547 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007548 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007549 return PSA_JPAKE_STEP_INVALID;
7550 }
7551 break;
7552 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007553 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007554 case PSA_PAKE_X1_STEP_KEY_SHARE:
7555 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007556 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7557 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007558 case PSA_PAKE_X1_STEP_ZK_PROOF:
7559 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007560 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007561 return PSA_JPAKE_STEP_INVALID;
7562 }
7563 break;
7564 default:
7565 return PSA_JPAKE_STEP_INVALID;
7566 }
7567 return PSA_JPAKE_STEP_INVALID;
7568}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007569#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007570
Przemek Stekiel2797d372022-12-22 11:19:22 +01007571static psa_status_t psa_pake_complete_inputs(
7572 psa_pake_operation_t *operation)
7573{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007574 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007575 /* Create copy of the inputs on stack as inputs share memory
7576 with the driver context which will be setup by the driver. */
7577 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007578
Przemek Stekielfde11282023-03-13 16:06:09 +01007579 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007580 return PSA_ERROR_BAD_STATE;
7581 }
7582
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007583 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007584 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7585 return PSA_ERROR_BAD_STATE;
7586 }
7587 if (memcmp(inputs.user, jpake_client_id, inputs.user_len) == 0 &&
7588 memcmp(inputs.peer, jpake_server_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007589 inputs.role = PSA_PAKE_ROLE_CLIENT;
7590 } else
Przemek Stekielfde11282023-03-13 16:06:09 +01007591 if (memcmp(inputs.user, jpake_server_id, inputs.user_len) == 0 &&
7592 memcmp(inputs.peer, jpake_client_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007593 inputs.role = PSA_PAKE_ROLE_SERVER;
7594 }
7595
7596 if (inputs.role != PSA_PAKE_ROLE_CLIENT &&
7597 inputs.role != PSA_PAKE_ROLE_SERVER) {
7598 return PSA_ERROR_NOT_SUPPORTED;
7599 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007600 }
7601
Przemek Stekiel18620a32023-01-17 16:34:52 +01007602 /* Clear driver context */
7603 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7604
7605 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007606
7607 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007608 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7609 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007610
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007611 /* User and peer are translated to role. */
7612 mbedtls_free(inputs.user);
7613 mbedtls_free(inputs.peer);
7614
Przemek Stekiel2797d372022-12-22 11:19:22 +01007615 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007616#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007617 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007618 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007619 psa_jpake_computation_stage_t *computation_stage =
7620 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007621 computation_stage->state = PSA_PAKE_STATE_READY;
7622 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7623 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7624 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007625 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007626#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007627 {
7628 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007629 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007630 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007631 return status;
7632}
7633
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007634#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007635static psa_status_t psa_jpake_output_prologue(
7636 psa_pake_operation_t *operation,
7637 psa_pake_step_t step)
7638{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007639 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7640 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7641 step != PSA_PAKE_STEP_ZK_PROOF) {
7642 return PSA_ERROR_INVALID_ARGUMENT;
7643 }
7644
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007645 psa_jpake_computation_stage_t *computation_stage =
7646 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007647
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007648 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7649 return PSA_ERROR_BAD_STATE;
7650 }
7651
7652 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7653 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
7654 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
7655 return PSA_ERROR_BAD_STATE;
7656 }
7657
7658 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7659 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007660 return PSA_ERROR_BAD_STATE;
7661 }
7662
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007663 switch (computation_stage->output_step) {
7664 case PSA_PAKE_STEP_X1_X2:
7665 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007666 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007667 case PSA_PAKE_STEP_X2S:
7668 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007669 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007670 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007671 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007672 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007673
7674 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7675 }
7676
7677 /* Check if step matches current sequence */
7678 switch (computation_stage->sequence) {
7679 case PSA_PAKE_X1_STEP_KEY_SHARE:
7680 case PSA_PAKE_X2_STEP_KEY_SHARE:
7681 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7682 return PSA_ERROR_BAD_STATE;
7683 }
7684 break;
7685
7686 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7687 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7688 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7689 return PSA_ERROR_BAD_STATE;
7690 }
7691 break;
7692
7693 case PSA_PAKE_X1_STEP_ZK_PROOF:
7694 case PSA_PAKE_X2_STEP_ZK_PROOF:
7695 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7696 return PSA_ERROR_BAD_STATE;
7697 }
7698 break;
7699
7700 default:
7701 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007702 }
7703
7704 return PSA_SUCCESS;
7705}
7706
7707static psa_status_t psa_jpake_output_epilogue(
7708 psa_pake_operation_t *operation)
7709{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007710 psa_jpake_computation_stage_t *computation_stage =
7711 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007712
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007713 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007714 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007715 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007716 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007717 computation_stage->state = PSA_PAKE_STATE_READY;
7718 computation_stage->output_step++;
7719 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7720 } else {
7721 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007722 }
7723
7724 return PSA_SUCCESS;
7725}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007726#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007727
Neil Armstronga7d08c32022-06-01 18:21:20 +02007728psa_status_t psa_pake_output(
7729 psa_pake_operation_t *operation,
7730 psa_pake_step_t step,
7731 uint8_t *output,
7732 size_t output_size,
7733 size_t *output_length)
7734{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007735 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007736 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007737 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007738
7739 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007740 status = psa_pake_complete_inputs(operation);
7741 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007742 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007743 }
7744 }
7745
7746 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007747 status = PSA_ERROR_BAD_STATE;
7748 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007749 }
7750
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007751 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007752 status = PSA_ERROR_INVALID_ARGUMENT;
7753 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007754 }
7755
Przemek Stekiele12ed362022-12-21 12:54:46 +01007756 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007757#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007758 case PSA_ALG_JPAKE:
7759 status = psa_jpake_output_prologue(operation, step);
7760 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007761 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007762 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007763 driver_step = convert_jpake_computation_stage_to_driver_step(
7764 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007765 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007766#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007767 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007768 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007769 status = PSA_ERROR_NOT_SUPPORTED;
7770 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007771 }
7772
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007773 status = psa_driver_wrapper_pake_output(operation, driver_step,
7774 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007775
7776 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007777 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007778 }
7779
7780 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007781#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007782 case PSA_ALG_JPAKE:
7783 status = psa_jpake_output_epilogue(operation);
7784 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007785 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007786 }
7787 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007788#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007789 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007790 status = PSA_ERROR_NOT_SUPPORTED;
7791 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007792 }
7793
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007794 return PSA_SUCCESS;
7795exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007796 psa_pake_abort(operation);
7797 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007798}
7799
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007800#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007801static psa_status_t psa_jpake_input_prologue(
7802 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007803 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007804{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007805 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7806 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7807 step != PSA_PAKE_STEP_ZK_PROOF) {
7808 return PSA_ERROR_INVALID_ARGUMENT;
7809 }
7810
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007811 psa_jpake_computation_stage_t *computation_stage =
7812 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007813
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007814 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7815 return PSA_ERROR_BAD_STATE;
7816 }
7817
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007818 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7819 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
7820 computation_stage->state != PSA_PAKE_INPUT_X4S) {
7821 return PSA_ERROR_BAD_STATE;
7822 }
7823
7824 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7825 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007826 return PSA_ERROR_BAD_STATE;
7827 }
7828
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007829 switch (computation_stage->input_step) {
7830 case PSA_PAKE_STEP_X1_X2:
7831 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007832 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007833 case PSA_PAKE_STEP_X2S:
7834 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007835 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007836 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007837 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007838 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007839
7840 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7841 }
7842
7843 /* Check if step matches current sequence */
7844 switch (computation_stage->sequence) {
7845 case PSA_PAKE_X1_STEP_KEY_SHARE:
7846 case PSA_PAKE_X2_STEP_KEY_SHARE:
7847 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7848 return PSA_ERROR_BAD_STATE;
7849 }
7850 break;
7851
7852 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7853 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7854 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7855 return PSA_ERROR_BAD_STATE;
7856 }
7857 break;
7858
7859 case PSA_PAKE_X1_STEP_ZK_PROOF:
7860 case PSA_PAKE_X2_STEP_ZK_PROOF:
7861 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7862 return PSA_ERROR_BAD_STATE;
7863 }
7864 break;
7865
7866 default:
7867 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007868 }
7869
7870 return PSA_SUCCESS;
7871}
7872
Przemek Stekiele12ed362022-12-21 12:54:46 +01007873static psa_status_t psa_jpake_input_epilogue(
7874 psa_pake_operation_t *operation)
7875{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007876 psa_jpake_computation_stage_t *computation_stage =
7877 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007878
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007879 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007880 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007881 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007882 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007883 computation_stage->state = PSA_PAKE_STATE_READY;
7884 computation_stage->input_step++;
7885 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7886 } else {
7887 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007888 }
7889
7890 return PSA_SUCCESS;
7891}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007892#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007893
Neil Armstronga7d08c32022-06-01 18:21:20 +02007894psa_status_t psa_pake_input(
7895 psa_pake_operation_t *operation,
7896 psa_pake_step_t step,
7897 const uint8_t *input,
7898 size_t input_length)
7899{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007900 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007901 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007902
7903 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007904 status = psa_pake_complete_inputs(operation);
7905 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007906 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007907 }
7908 }
7909
7910 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007911 status = PSA_ERROR_BAD_STATE;
7912 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007913 }
7914
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007915 if (input_length == 0 || input_length > PSA_PAKE_INPUT_MAX_SIZE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007916 status = PSA_ERROR_INVALID_ARGUMENT;
7917 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007918 }
7919
Przemek Stekiele12ed362022-12-21 12:54:46 +01007920 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007921#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007922 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007923 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007924 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007925 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007926 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007927 driver_step = convert_jpake_computation_stage_to_driver_step(
7928 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007929 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007930#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007931 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007932 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007933 status = PSA_ERROR_NOT_SUPPORTED;
7934 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007935 }
7936
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007937 status = psa_driver_wrapper_pake_input(operation, driver_step,
7938 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007939
7940 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007941 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007942 }
7943
7944 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007945#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007946 case PSA_ALG_JPAKE:
7947 status = psa_jpake_input_epilogue(operation);
7948 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007949 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007950 }
7951 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007952#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007953 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007954 status = PSA_ERROR_NOT_SUPPORTED;
7955 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007956 }
7957
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007958 return PSA_SUCCESS;
7959exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007960 psa_pake_abort(operation);
7961 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007962}
7963
7964psa_status_t psa_pake_get_implicit_key(
7965 psa_pake_operation_t *operation,
7966 psa_key_derivation_operation_t *output)
7967{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007968 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007969 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007970 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01007971 size_t shared_key_len = 0;
7972
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007973 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007974 status = PSA_ERROR_BAD_STATE;
7975 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007976 }
7977
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007978#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007979 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007980 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01007981 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007982 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
7983 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007984 status = PSA_ERROR_BAD_STATE;
7985 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007986 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01007987 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007988#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007989 {
7990 status = PSA_ERROR_NOT_SUPPORTED;
7991 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007992 }
7993
Przemek Stekiel0c781802022-11-29 14:53:13 +01007994 status = psa_driver_wrapper_pake_get_implicit_key(operation,
7995 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01007996 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01007997 &shared_key_len);
7998
7999 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008000 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008001 }
8002
8003 status = psa_key_derivation_input_bytes(output,
8004 PSA_KEY_DERIVATION_INPUT_SECRET,
8005 shared_key,
8006 shared_key_len);
8007
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008008 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008009exit:
8010 abort_status = psa_pake_abort(operation);
8011 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008012}
8013
8014psa_status_t psa_pake_abort(
8015 psa_pake_operation_t *operation)
8016{
Przemek Stekield69dca92023-01-31 19:59:20 +01008017 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008018
Przemek Stekield69dca92023-01-31 19:59:20 +01008019 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008020 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008021 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008022
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008023 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8024 if (operation->data.inputs.password != NULL) {
8025 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008026 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008027 mbedtls_free(operation->data.inputs.password);
8028 }
8029 if (operation->data.inputs.user != NULL) {
8030 mbedtls_free(operation->data.inputs.user);
8031 }
8032 if (operation->data.inputs.peer != NULL) {
8033 mbedtls_free(operation->data.inputs.peer);
8034 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008035 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008036 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008037
Przemek Stekield69dca92023-01-31 19:59:20 +01008038 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008039}
Neil Armstronga7d08c32022-06-01 18:21:20 +02008040
Gilles Peskinee59236f2018-01-27 23:32:46 +01008041#endif /* MBEDTLS_PSA_CRYPTO_C */