blob: 42ebfcf5ca05e7f56f1cfa8c8b33e1086e055743 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Ronald Cronafbc7ed2023-01-24 16:02:04 +010022#include "psa_crypto_core_common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010023
24#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030025
John Durkop07cc04a2020-11-16 22:08:34 -080026#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
27#include "check_crypto_config.h"
28#endif
29
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010031#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010032
Ronald Crond6d28882020-12-14 14:56:02 +010033#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010034#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010035#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020036#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010037#include "psa_crypto_ecp.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010038#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010039#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010040#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010041#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020042#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020043#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020044#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010045#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010046/* Include internal declarations that are useful for implementing persistently
47 * stored keys. */
48#include "psa_crypto_storage.h"
49
Gilles Peskineb2b64d32020-12-14 16:43:58 +010050#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010051
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010052#include <stdlib.h>
53#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010054#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010055
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010056#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020057#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000058#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020059#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020061#include "mbedtls/chacha20.h"
62#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/cipher.h"
64#include "mbedtls/ccm.h"
65#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020067#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010068#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010069#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/error.h"
71#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010072#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000074#include "md_wrap.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000076#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000078#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010080#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/sha1.h"
82#include "mbedtls/sha256.h"
83#include "mbedtls/sha512.h"
Paul Elliott588f8ed2022-12-02 18:10:26 +000084#include "hash_info.h"
Przemek Stekiel472b3f32022-12-01 14:38:49 +010085#include "mbedtls/dhm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086
Gilles Peskine449bd832023-01-11 14:50:10 +010087#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020088
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020089#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
90 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
91 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020092#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020093#endif
94
Przemek Stekielfde11282023-03-13 16:06:09 +010095/* The only two JPAKE user/peer identifiers supported for the time being. */
96static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
97static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
Przemek Stekiel18cd6c92023-03-06 15:40:35 +010098
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010099/****************************************************************/
100/* Global data, support functions and library management */
101/****************************************************************/
102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200104{
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200106}
107
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100108/* Values for psa_global_data_t::rng_state */
109#define RNG_NOT_INITIALIZED 0
110#define RNG_INITIALIZED 1
111#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100114 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100115 unsigned rng_state : 2;
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100116 unsigned drivers_initialized : 1;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100117 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118} psa_global_data_t;
119
120static psa_global_data_t global_data;
121
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100122#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
123mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
124 &global_data.rng.drbg;
125#endif
126
itayzafrir0adf0fc2018-09-06 16:24:41 +0300127#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 if (global_data.initialized == 0) \
129 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300130
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100131int psa_can_do_hash(psa_algorithm_t hash_alg)
132{
133 (void) hash_alg;
134 return global_data.drivers_initialized;
135}
136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100138{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100139 /* Mbed TLS error codes can combine a high-level error code and a
140 * low-level error code. The low-level error usually reflects the
141 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 int low_level_ret = -(-ret & 0x007f);
143 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100144 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100146
147 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
148 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200150 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
151 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
152 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
153 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
154 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200156 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200158 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200160
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000162 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000163#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100164 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100166
167 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100169 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100171
Gilles Peskine26869f22019-05-06 15:25:00 +0200172 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200174
175 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200177 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200179
Gilles Peskinea5905292018-02-07 20:59:33 +0100180 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100182 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100184 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100186 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100188 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100190 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100192 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
196 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100197 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
198 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100199 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100201 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
202 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100204 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100206#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100207
208 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100210
Gilles Peskinee59236f2018-01-27 23:32:46 +0100211 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
212 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
213 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100215
216 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200218 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100220 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100222
Gilles Peskine82e57d12020-11-13 21:31:17 +0100223#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100225 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
226 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100227 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100229 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
230 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100232 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100234#endif
235
Gilles Peskinea5905292018-02-07 20:59:33 +0100236 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100238 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100240 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100242 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100244
Gilles Peskinef76aa772018-10-29 19:24:33 +0100245 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100247 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100249 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100251 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100253 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100255 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100257 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100259 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100261
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100262 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100264 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
265 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100267 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100269 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
270 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100272 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100274 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
275 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100277 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100279 case MBEDTLS_ERR_PK_INVALID_ALG:
280 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
281 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100283 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200285 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100287
Gilles Peskineff2d2002019-05-06 15:26:23 +0200288 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200290 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200292
Gilles Peskinea5905292018-02-07 20:59:33 +0100293 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100295 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100297 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100299 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100301 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
302 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100308 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200310
itayzafrir5c753392018-05-08 11:18:38 +0300311 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300312 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300314 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300316 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300318 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
319 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300321 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100323 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100325
Paul Elliott588f8ed2022-12-02 18:10:26 +0000326 case MBEDTLS_ERR_ECP_IN_PROGRESS:
327 return PSA_OPERATION_INCOMPLETE;
328
Janos Follatha13b9052019-11-22 12:48:59 +0000329 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300331
Gilles Peskinee59236f2018-01-27 23:32:46 +0100332 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100334 }
335}
336
Paul Elliottdc42ca82023-02-24 18:11:59 +0000337/**
338 * \brief For output buffers which contain "tags"
339 * (outputs that may be checked for validity like
340 * hashes, MACs and signatures), fill the unused
341 * part of the output buffer (the whole buffer on
342 * error, the trailing part on success) with
343 * something that isn't a valid tag (barring an
344 * attack on the tag and deliberately-crafted
345 * input), in case the caller doesn't check the
346 * return status properly.
347 *
348 * \param output_buffer Pointer to buffer to wipe. May not be NULL
349 * unless \p output_buffer_size is zero.
350 * \param status Status of function called to generate
351 * output_buffer originally
352 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
353 * could be NULL.
354 * \param output_buffer_length Length of data written to output_buffer, must be
355 * less than \p output_buffer_size
356 */
357static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000358 size_t output_buffer_size, size_t output_buffer_length)
359{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000360 size_t offset = 0;
361
362 if (output_buffer_size == 0) {
363 /* If output_buffer_size is 0 then we have nothing to do. We must not
364 call memset because output_buffer may be NULL in this case */
365 return;
366 }
367
368 if (status == PSA_SUCCESS) {
369 offset = output_buffer_length;
370 }
371
372 memset(output_buffer + offset, '!', output_buffer_size - offset);
373}
374
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200375
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200376
377
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100378/****************************************************************/
379/* Key management */
380/****************************************************************/
381
Valerio Settid4a5d462023-04-05 18:19:01 +0200382#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100383mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
384 size_t bits,
385 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200386{
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100388 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100390#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100391 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100393#endif
394#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100395 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100397#endif
398#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100399 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100401#endif
402#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100403 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100405#endif
406#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100407 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100409 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 if (bits_is_sloppy) {
411 return MBEDTLS_ECP_DP_SECP521R1;
412 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100413 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100414#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100415 }
416 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100417
Paul Elliott8ff510a2020-06-02 17:19:28 +0100418 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100420#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100421 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100423#endif
424#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100425 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100427#endif
428#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100429 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100431#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100432 }
433 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100434
Paul Elliott8ff510a2020-06-02 17:19:28 +0100435 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100437#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100438 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100440 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 if (bits_is_sloppy) {
442 return MBEDTLS_ECP_DP_CURVE25519;
443 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100444 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100445#endif
446#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100447 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100449#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100450 }
451 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100452
Paul Elliott8ff510a2020-06-02 17:19:28 +0100453 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100455#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100456 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100458#endif
459#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100460 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100462#endif
463#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100464 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100466#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100467 }
468 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200469 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100470
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100471 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200473}
Valerio Settid4a5d462023-04-05 18:19:01 +0200474#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
477 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200478{
479 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200481 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200482 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200483 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200484 case PSA_KEY_TYPE_PASSWORD:
485 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200486 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100487#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200488 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (bits != 128 && bits != 192 && bits != 256) {
490 return PSA_ERROR_INVALID_ARGUMENT;
491 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200492 break;
493#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200494#if defined(PSA_WANT_KEY_TYPE_ARIA)
495 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 if (bits != 128 && bits != 192 && bits != 256) {
497 return PSA_ERROR_INVALID_ARGUMENT;
498 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200499 break;
500#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100501#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200502 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if (bits != 128 && bits != 192 && bits != 256) {
504 return PSA_ERROR_INVALID_ARGUMENT;
505 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200506 break;
507#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100508#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200509 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if (bits != 64 && bits != 128 && bits != 192) {
511 return PSA_ERROR_INVALID_ARGUMENT;
512 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200513 break;
514#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100515#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200516 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if (bits != 256) {
518 return PSA_ERROR_INVALID_ARGUMENT;
519 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200520 break;
521#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200522 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200524 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 if (bits % 8 != 0) {
526 return PSA_ERROR_INVALID_ARGUMENT;
527 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200530}
531
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100532/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100533 *
Steven Cooremancd640932021-03-08 12:00:27 +0100534 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
535 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100536 *
537 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100538 * \param[in] key_type The key type of the key to be used with the
539 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100540 *
541 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100542 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100543 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100544 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100545 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100546MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100547 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100549{
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 if (PSA_ALG_IS_HMAC(algorithm)) {
551 if (key_type == PSA_KEY_TYPE_HMAC) {
552 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100553 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100554 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
557 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
558 * key. */
559 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
560 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
561 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
562 * the block length (larger than 1) for block ciphers. */
563 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
564 return PSA_SUCCESS;
565 }
566 }
567 }
568
569 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100570}
571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
573 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200574{
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 if (slot->key.data != NULL) {
576 return PSA_ERROR_ALREADY_EXISTS;
577 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 slot->key.data = mbedtls_calloc(1, buffer_length);
580 if (slot->key.data == NULL) {
581 return PSA_ERROR_INSUFFICIENT_MEMORY;
582 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200583
Ronald Cronea0f8a62020-11-25 17:52:23 +0100584 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200586}
587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
589 const uint8_t *data,
590 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200591{
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 psa_status_t status = psa_allocate_buffer_to_slot(slot,
593 data_length);
594 if (status != PSA_SUCCESS) {
595 return status;
596 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 memcpy(slot->key.data, data, data_length);
599 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200600}
601
Ronald Crona0fe59f2020-11-29 11:14:53 +0100602psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100603 const psa_key_attributes_t *attributes,
604 const uint8_t *data, size_t data_length,
605 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100607{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100608 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
609 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100610
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200611 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 if (data_length == 0) {
613 return PSA_ERROR_NOT_SUPPORTED;
614 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 if (key_type_is_raw_bytes(type)) {
617 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
620 *bits);
621 if (status != PSA_SUCCESS) {
622 return status;
623 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200624
Ronald Crondd04d422020-11-28 15:14:42 +0100625 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100627 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 return PSA_SUCCESS;
631 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100632#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) || \
633 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY)
634 if (PSA_KEY_TYPE_IS_DH(type)) {
635 if (data_length != 256 && data_length != 384 &&
636 data_length != 512 && data_length != 768 &&
637 data_length != 1024) {
638 return PSA_ERROR_INVALID_ARGUMENT;
639 }
640
641 /* Copy the key material. */
642 memcpy(key_buffer, data, data_length);
643 *key_buffer_length = data_length;
644 *bits = PSA_BYTES_TO_BITS(data_length);
645 (void) key_buffer_size;
646
647 return PSA_SUCCESS;
648 }
649#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) ||
650 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY) */
John Durkop9814fa22020-11-04 12:28:15 -0800651#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
653 if (PSA_KEY_TYPE_IS_ECC(type)) {
654 return mbedtls_psa_ecp_import_key(attributes,
655 data, data_length,
656 key_buffer, key_buffer_size,
657 key_buffer_length,
658 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100659 }
John Durkop9814fa22020-11-04 12:28:15 -0800660#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
661 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700662#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
664 if (PSA_KEY_TYPE_IS_RSA(type)) {
665 return mbedtls_psa_rsa_import_key(attributes,
666 data, data_length,
667 key_buffer, key_buffer_size,
668 key_buffer_length,
669 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200670 }
John Durkop9814fa22020-11-04 12:28:15 -0800671#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
672 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100673 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100676}
677
Gilles Peskinef603c712019-01-19 13:40:11 +0100678/** Calculate the intersection of two algorithm usage policies.
679 *
680 * Return 0 (which allows no operation) on incompatibility.
681 */
682static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100683 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100684 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100686{
Gilles Peskine549ea862019-05-22 11:45:59 +0200687 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 if (alg1 == alg2) {
689 return alg1;
690 }
Steven Cooreman03488022021-02-18 12:07:20 +0100691 /* If the policies are from the same hash-and-sign family, check
692 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
694 PSA_ALG_IS_SIGN_HASH(alg2) &&
695 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
696 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
697 return alg2;
698 }
699 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
700 return alg1;
701 }
Steven Cooreman03488022021-02-18 12:07:20 +0100702 }
703 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100704 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100705 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
707 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
708 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
709 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
710 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100711 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100712
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100713 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
715 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
716 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
717 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100718 }
719 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
721 (alg1_len <= alg2_len)) {
722 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100723 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
725 (alg2_len <= alg1_len)) {
726 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100727 }
728 }
729 /* If the policies are from the same MAC family, check whether one
730 * of them is a minimum-MAC-length policy. Calculate the most
731 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
733 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
734 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100735 /* Validate the combination of key type and algorithm. Since the base
736 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
738 return 0;
739 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100740
Steven Cooreman31a876d2021-03-03 20:47:40 +0100741 /* Get the (exact or at-least) output lengths for both sides of the
742 * requested intersection. None of the currently supported algorithms
743 * have an output length dependent on the actual key size, so setting it
744 * to a bogus value of 0 is currently OK.
745 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100746 * Note that for at-least-this-length wildcard algorithms, the output
747 * length is set to the shortest allowed length, which allows us to
748 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
750 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100751 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100752
Steven Cooremana1d83222021-02-25 10:20:29 +0100753 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
755 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
756 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100757 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100758
759 /* If only one is an at-least-this-length policy, the intersection would
760 * be the other (fixed-length) policy as long as said fixed length is
761 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
763 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100764 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
766 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100767 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100768
Steven Cooremancd640932021-03-08 12:00:27 +0100769 /* If none of them are wildcards, check whether they define the same tag
770 * length. This is still possible here when one is default-length and
771 * the other specific-length. Ensure to always return the
772 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (alg1_len == alg2_len) {
774 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
775 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100776 }
777 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100779}
780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781static int psa_key_algorithm_permits(psa_key_type_t key_type,
782 psa_algorithm_t policy_alg,
783 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200784{
Gilles Peskine549ea862019-05-22 11:45:59 +0200785 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if (requested_alg == policy_alg) {
787 return 1;
788 }
Steven Cooreman03488022021-02-18 12:07:20 +0100789 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
790 * and requested_alg is the same hash-and-sign family with any hash,
791 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
793 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
794 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
795 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100796 }
797 /* If policy_alg is a wildcard AEAD algorithm of the same base as
798 * the requested algorithm, check the requested tag length to be
799 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if (PSA_ALG_IS_AEAD(policy_alg) &&
801 PSA_ALG_IS_AEAD(requested_alg) &&
802 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
803 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
804 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
805 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
806 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100807 }
Steven Cooremancd640932021-03-08 12:00:27 +0100808 /* If policy_alg is a MAC algorithm of the same base as the requested
809 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 if (PSA_ALG_IS_MAC(policy_alg) &&
811 PSA_ALG_IS_MAC(requested_alg) &&
812 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
813 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100814 /* Validate the combination of key type and algorithm. Since the policy
815 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
817 return 0;
818 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100819
Steven Cooreman31a876d2021-03-03 20:47:40 +0100820 /* Get both the requested output length for the algorithm which is to be
821 * verified, and the default output length for the base algorithm.
822 * Note that none of the currently supported algorithms have an output
823 * length dependent on actual key size, so setting it to a bogus value
824 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100825 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100827 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 key_type, 0,
829 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100830
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100831 /* If the policy is default-length, only allow an algorithm with
832 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
834 return requested_output_length == default_output_length;
835 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100836
837 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100838 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
840 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
841 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100842 }
843
Steven Cooremancd640932021-03-08 12:00:27 +0100844 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
845 * check for the requested MAC length to be equal to or longer than the
846 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
848 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
849 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100850 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200851 }
Steven Cooremance48e852020-10-05 16:02:45 +0200852 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200853 * a key derivation with that key agreement should also be allowed. This
854 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
856 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
857 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
858 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200859 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100860 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200862}
863
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100864/** Test whether a policy permits an algorithm.
865 *
866 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100867 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100868 * \note This function requires providing the key type for which the policy is
869 * being validated, since some algorithm policy definitions (e.g. MAC)
870 * have different properties depending on what kind of cipher it is
871 * combined with.
872 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100873 * \retval PSA_SUCCESS When \p alg is a specific algorithm
874 * allowed by the \p policy.
875 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
876 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
877 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100878 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100879static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
880 psa_key_type_t key_type,
881 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100882{
Steven Cooremand788fab2021-02-25 11:29:17 +0100883 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if (alg == 0) {
885 return PSA_ERROR_INVALID_ARGUMENT;
886 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100887
Steven Cooreman7e39f052021-02-23 14:18:32 +0100888 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if (PSA_ALG_IS_WILDCARD(alg)) {
890 return PSA_ERROR_INVALID_ARGUMENT;
891 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
894 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
895 return PSA_SUCCESS;
896 } else {
897 return PSA_ERROR_NOT_PERMITTED;
898 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100899}
900
Gilles Peskinef603c712019-01-19 13:40:11 +0100901/** Restrict a key policy based on a constraint.
902 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100903 * \note This function requires providing the key type for which the policy is
904 * being restricted, since some algorithm policy definitions (e.g. MAC)
905 * have different properties depending on what kind of cipher it is
906 * combined with.
907 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100908 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100909 * \param[in,out] policy The policy to restrict.
910 * \param[in] constraint The policy constraint to apply.
911 *
912 * \retval #PSA_SUCCESS
913 * \c *policy contains the intersection of the original value of
914 * \c *policy and \c *constraint.
915 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100916 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100917 * \c *policy is unchanged.
918 */
919static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100920 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100921 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100923{
924 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 psa_key_policy_algorithm_intersection(key_type, policy->alg,
926 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200927 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
929 constraint->alg2);
930 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
931 return PSA_ERROR_INVALID_ARGUMENT;
932 }
933 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
934 return PSA_ERROR_INVALID_ARGUMENT;
935 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100936 policy->usage &= constraint->usage;
937 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200938 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100940}
941
Przemek Stekiel061f6942022-11-30 10:51:35 +0100942/** Get the description of a key given its identifier and policy constraints
943 * and lock it.
944 *
945 * The key must have allow all the usage flags set in \p usage. If \p alg is
946 * nonzero, the key must allow operations with this algorithm. If \p alg is
947 * zero, the algorithm is not checked.
948 *
949 * In case of a persistent key, the function loads the description of the key
950 * into a key slot if not already done.
951 *
952 * On success, the returned key slot is locked. It is the responsibility of
953 * the caller to unlock the key slot when it does not access it anymore.
954 */
955static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100956 mbedtls_svc_key_id_t key,
957 psa_key_slot_t **p_slot,
958 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100960{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200961 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100962 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 status = psa_get_and_lock_key_slot(key, p_slot);
965 if (status != PSA_SUCCESS) {
966 return status;
967 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200968 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100969
970 /* Enforce that usage policy for the key slot contains all the flags
971 * required by the usage parameter. There is one exception: public
972 * keys can always be exported, so we treat public key objects as
973 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100975 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100979 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200980 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100981 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100982
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800983 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 if (alg != 0) {
985 status = psa_key_policy_permits(&slot->attr.policy,
986 slot->attr.type,
987 alg);
988 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100989 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100991 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100992
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200994
995error:
996 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001000}
Darryl Green940d72c2018-07-13 13:18:51 +01001001
Ronald Cron5c522922020-11-14 16:35:34 +01001002/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001003 *
1004 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001005 * available, as opposed to a key in a secure element and/or to be used
1006 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001007 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001008 * This is a temporary function that may be used instead of
1009 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1010 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001011 *
Ronald Cron5c522922020-11-14 16:35:34 +01001012 * On success, the returned key slot is locked. It is the responsibility of the
1013 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001014 */
Ronald Cron5c522922020-11-14 16:35:34 +01001015static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1016 mbedtls_svc_key_id_t key,
1017 psa_key_slot_t **p_slot,
1018 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001020{
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1022 usage, alg);
1023 if (status != PSA_SUCCESS) {
1024 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001025 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1028 psa_unlock_key_slot(*p_slot);
1029 *p_slot = NULL;
1030 return PSA_ERROR_NOT_SUPPORTED;
1031 }
1032
1033 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001034}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001035
Gilles Peskine449bd832023-01-11 14:50:10 +01001036psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001037{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001038 /* Data pointer will always be either a valid pointer or NULL in an
1039 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 if (slot->key.data != NULL) {
1041 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1042 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001043
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001045 slot->key.data = NULL;
1046 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001049}
1050
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001051/** Completely wipe a slot in memory, including its policy.
1052 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001053psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001054{
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 /*
1058 * As the return error code may not be handled in case of multiple errors,
1059 * do our best to report an unexpected lock counter. Assert with
1060 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1061 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1062 * function is called as part of the execution of a test suite, the
1063 * execution of the test suite is stopped in error if the assertion fails.
1064 */
1065 if (slot->lock_count != 1) {
1066 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001067 status = PSA_ERROR_CORRUPTION_DETECTED;
1068 }
1069
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001070 /* Multipart operations may still be using the key. This is safe
1071 * because all multipart operation objects are independent from
1072 * the key slot: if they need to access the key after the setup
1073 * phase, they have a copy of the key. Note that this means that
1074 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001075 /* At this point, key material and other type-specific content has
1076 * been wiped. Clear remaining metadata. We can call memset and not
1077 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 memset(slot, 0, sizeof(*slot));
1079 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001080}
1081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001083{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001084 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001085 psa_status_t status; /* status of the last operation */
1086 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001087#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1088 psa_se_drv_table_entry_t *driver;
1089#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (mbedtls_svc_key_id_is_null(key)) {
1092 return PSA_SUCCESS;
1093 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001094
Ronald Cronf2911112020-10-29 17:51:10 +01001095 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001096 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001097 * key, this will load the key description from persistent memory if not
1098 * done yet. We cannot avoid this loading as without it we don't know if
1099 * the key is operated by an SE or not and this information is needed by
1100 * the current implementation.
1101 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 status = psa_get_and_lock_key_slot(key, &slot);
1103 if (status != PSA_SUCCESS) {
1104 return status;
1105 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001106
Ronald Cronf2911112020-10-29 17:51:10 +01001107 /*
1108 * If the key slot containing the key description is under access by the
1109 * library (apart from the present access), the key cannot be destroyed
1110 * yet. For the time being, just return in error. Eventually (to be
1111 * implemented), the key should be destroyed when all accesses have
1112 * stopped.
1113 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 if (slot->lock_count > 1) {
1115 psa_unlock_key_slot(slot);
1116 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001117 }
1118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001120 /* Refuse the destruction of a read-only key (which may or may not work
1121 * if we attempt it, depending on whether the key is merely read-only
1122 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001123 * Just do the best we can, which is to wipe the copy in memory
1124 * (done in this function's cleanup code). */
1125 overall_status = PSA_ERROR_NOT_PERMITTED;
1126 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001127 }
1128
Gilles Peskine354f7672019-07-12 23:46:38 +02001129#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1131 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001132 /* For a key in a secure element, we need to do three things:
1133 * remove the key file in internal storage, destroy the
1134 * key inside the secure element, and update the driver's
1135 * persistent data. Start a transaction that will encompass these
1136 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001138 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001140 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 status = psa_crypto_save_transaction();
1142 if (status != PSA_SUCCESS) {
1143 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001144 /* We should still try to destroy the key in the secure
1145 * element and the key metadata in storage. This is especially
1146 * important if the error is that the storage is full.
1147 * But how to do it exactly without risking an inconsistent
1148 * state after a reset?
1149 * https://github.com/ARMmbed/mbed-crypto/issues/215
1150 */
1151 overall_status = status;
1152 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001153 }
1154
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 status = psa_destroy_se_key(driver,
1156 psa_key_slot_get_slot_number(slot));
1157 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001158 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001160 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001161#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1162
Darryl Greend49a4992018-06-18 17:27:26 +01001163#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1165 status = psa_destroy_persistent_key(slot->attr.id);
1166 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001167 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001169
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001170 /* TODO: other slots may have a copy of the same key. We should
1171 * invalidate them.
1172 * https://github.com/ARMmbed/mbed-crypto/issues/214
1173 */
Darryl Greend49a4992018-06-18 17:27:26 +01001174 }
1175#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001176
Gilles Peskinefc762652019-07-22 19:30:34 +02001177#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 if (driver != NULL) {
1179 status = psa_save_se_persistent_data(driver);
1180 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001181 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 }
1183 status = psa_crypto_stop_transaction();
1184 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001185 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001187 }
1188#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1189
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001190exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001192 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001194 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 }
1196 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001197}
1198
John Durkop07cc04a2020-11-16 22:08:34 -08001199#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001200 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001201static psa_status_t psa_get_rsa_public_exponent(
1202 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001204{
1205 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001207 uint8_t *buffer = NULL;
1208 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001210
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1212 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001213 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 }
1215 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001216 /* It's the default value, which is reported as an empty string,
1217 * so there's nothing to do. */
1218 goto exit;
1219 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 buflen = mbedtls_mpi_size(&mpi);
1222 buffer = mbedtls_calloc(1, buflen);
1223 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001224 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1225 goto exit;
1226 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1228 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001229 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001231 attributes->domain_parameters = buffer;
1232 attributes->domain_parameters_size = buflen;
1233
1234exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 mbedtls_mpi_free(&mpi);
1236 if (ret != 0) {
1237 mbedtls_free(buffer);
1238 }
1239 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001240}
John Durkop07cc04a2020-11-16 22:08:34 -08001241#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001242 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001243
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001244/** Retrieve all the publicly-accessible attributes of a key.
1245 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001246psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1247 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001248{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001249 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001250 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001251 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1256 if (status != PSA_SUCCESS) {
1257 return status;
1258 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001259
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001260 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1262 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001263
1264#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1266 psa_set_key_slot_number(attributes,
1267 psa_key_slot_get_slot_number(slot));
1268 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001269#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001272#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001273 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001274 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001275 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001276 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001277 * is not yet implemented.
1278 * https://github.com/ARMmbed/mbed-crypto/issues/216
1279 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001281 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001282
Ronald Cron90857082020-11-25 14:58:33 +01001283 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 slot->attr.type,
1285 slot->key.data,
1286 slot->key.bytes,
1287 &rsa);
1288 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001289 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 status = psa_get_rsa_public_exponent(rsa,
1293 attributes);
1294 mbedtls_rsa_free(rsa);
1295 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001296 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001297 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001298#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001299 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001300 default:
1301 /* Nothing else to do. */
1302 break;
1303 }
1304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 if (status != PSA_SUCCESS) {
1306 psa_reset_key_attributes(attributes);
1307 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001312}
1313
Gilles Peskinec8000c02019-08-02 20:15:51 +02001314#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1315psa_status_t psa_get_key_slot_number(
1316 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001318{
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001320 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 return PSA_SUCCESS;
1322 } else {
1323 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001324 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001325}
1326#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1329 size_t key_buffer_size,
1330 uint8_t *data,
1331 size_t data_size,
1332 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001333{
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 if (key_buffer_size > data_size) {
1335 return PSA_ERROR_BUFFER_TOO_SMALL;
1336 }
1337 memcpy(data, key_buffer, key_buffer_size);
1338 memset(data + key_buffer_size, 0,
1339 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001340 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001342}
1343
Ronald Cron7285cda2020-11-26 14:46:37 +01001344psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001345 const psa_key_attributes_t *attributes,
1346 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001348{
Ronald Crond18b5f82020-11-25 16:46:05 +01001349 psa_key_type_t type = attributes->core.type;
1350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 if (key_type_is_raw_bytes(type) ||
1352 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001353 PSA_KEY_TYPE_IS_ECC(type) ||
1354 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 return psa_export_key_buffer_internal(
1356 key_buffer, key_buffer_size,
1357 data, data_size, data_length);
1358 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001359 /* This shouldn't happen in the reference implementation, but
1360 it is valid for a special-purpose implementation to omit
1361 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001363 }
1364}
1365
Gilles Peskine449bd832023-01-11 14:50:10 +01001366psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1367 uint8_t *data,
1368 size_t data_size,
1369 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001370{
1371 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1372 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1373 psa_key_slot_t *slot;
1374
Ronald Crone907e552021-01-18 13:22:38 +01001375 /* Reject a zero-length output buffer now, since this can never be a
1376 * valid key representation. This way we know that data must be a valid
1377 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 if (data_size == 0) {
1379 return PSA_ERROR_BUFFER_TOO_SMALL;
1380 }
Ronald Crone907e552021-01-18 13:22:38 +01001381
Ronald Cron9486f9d2020-11-26 13:36:23 +01001382 /* Set the key to empty now, so that even when there are errors, we always
1383 * set data_length to a value between 0 and data_size. On error, setting
1384 * the key to empty is a good choice because an empty key representation is
1385 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001386 *data_length = 0;
1387
Ronald Cron9486f9d2020-11-26 13:36:23 +01001388 /* Export requires the EXPORT flag. There is an exception for public keys,
1389 * which don't require any flag, but
1390 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1391 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1393 PSA_KEY_USAGE_EXPORT, 0);
1394 if (status != PSA_SUCCESS) {
1395 return status;
1396 }
mohammad160306e79202018-03-28 13:17:44 +03001397
Ronald Crond18b5f82020-11-25 16:46:05 +01001398 psa_key_attributes_t attributes = {
1399 .core = slot->attr
1400 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 status = psa_driver_wrapper_export_key(&attributes,
1402 slot->key.data, slot->key.bytes,
1403 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001408}
1409
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001410#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) || \
1411 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY)
1412static psa_status_t psa_ffdh_set_prime_generator(size_t key_size,
1413 mbedtls_mpi *P,
1414 mbedtls_mpi *G)
1415{
1416 const unsigned char *dhm_P = NULL;
1417 const unsigned char *dhm_G = NULL;
1418 size_t dhm_size_P = 0;
1419 size_t dhm_size_G = 0;
1420 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1421
1422 if (P == NULL && G == NULL) {
1423 return PSA_ERROR_INVALID_ARGUMENT;
1424 }
1425
1426 static const unsigned char dhm_P_2048[] =
1427 MBEDTLS_DHM_RFC7919_FFDHE2048_P_BIN;
1428 static const unsigned char dhm_P_3072[] =
1429 MBEDTLS_DHM_RFC7919_FFDHE3072_P_BIN;
1430 static const unsigned char dhm_P_4096[] =
1431 MBEDTLS_DHM_RFC7919_FFDHE4096_P_BIN;
1432 static const unsigned char dhm_P_6144[] =
1433 MBEDTLS_DHM_RFC7919_FFDHE6144_P_BIN;
1434 static const unsigned char dhm_P_8192[] =
1435 MBEDTLS_DHM_RFC7919_FFDHE8192_P_BIN;
1436 static const unsigned char dhm_G_2048[] =
1437 MBEDTLS_DHM_RFC7919_FFDHE2048_G_BIN;
1438 static const unsigned char dhm_G_3072[] =
1439 MBEDTLS_DHM_RFC7919_FFDHE3072_G_BIN;
1440 static const unsigned char dhm_G_4096[] =
1441 MBEDTLS_DHM_RFC7919_FFDHE4096_G_BIN;
1442 static const unsigned char dhm_G_6144[] =
1443 MBEDTLS_DHM_RFC7919_FFDHE6144_G_BIN;
1444 static const unsigned char dhm_G_8192[] =
1445 MBEDTLS_DHM_RFC7919_FFDHE8192_G_BIN;
1446
1447 if (key_size <= 256) {
1448 dhm_P = dhm_P_2048;
1449 dhm_G = dhm_G_2048;
1450 dhm_size_P = sizeof(dhm_P_2048);
1451 dhm_size_G = sizeof(dhm_G_2048);
1452 } else if (key_size <= 384) {
1453 dhm_P = dhm_P_3072;
1454 dhm_G = dhm_G_3072;
1455 dhm_size_P = sizeof(dhm_P_3072);
1456 dhm_size_G = sizeof(dhm_G_3072);
1457 } else if (key_size <= 512) {
1458 dhm_P = dhm_P_4096;
1459 dhm_G = dhm_G_4096;
1460 dhm_size_P = sizeof(dhm_P_4096);
1461 dhm_size_G = sizeof(dhm_G_4096);
1462 } else if (key_size <= 768) {
1463 dhm_P = dhm_P_6144;
1464 dhm_G = dhm_G_6144;
1465 dhm_size_P = sizeof(dhm_P_6144);
1466 dhm_size_G = sizeof(dhm_G_6144);
1467 } else if (key_size <= 1024) {
1468 dhm_P = dhm_P_8192;
1469 dhm_G = dhm_G_8192;
1470 dhm_size_P = sizeof(dhm_P_8192);
1471 dhm_size_G = sizeof(dhm_G_8192);
1472 } else {
1473 return PSA_ERROR_INVALID_ARGUMENT;
1474 }
1475
1476 if (P != NULL) {
1477 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(P, dhm_P,
1478 dhm_size_P));
1479 }
1480 if (G != NULL) {
1481 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(G, dhm_G,
1482 dhm_size_G));
1483 }
1484
1485cleanup:
1486 if (ret != 0) {
1487 return mbedtls_to_psa_error(ret);
1488 }
1489
1490 return PSA_SUCCESS;
1491}
1492
1493static psa_status_t psa_export_ffdh_public_key_internal(
1494 const uint8_t *key_buffer,
1495 size_t key_buffer_size,
1496 uint8_t *data,
1497 size_t data_size,
1498 size_t *data_length)
1499{
1500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1501 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1502 mbedtls_mpi GX, G, X, P;
1503
1504 mbedtls_mpi_init(&GX); mbedtls_mpi_init(&G);
1505 mbedtls_mpi_init(&X); mbedtls_mpi_init(&P);
1506
1507 status = psa_ffdh_set_prime_generator(key_buffer_size, &P, &G);
1508
1509 if (status == PSA_SUCCESS) {
1510 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&X, key_buffer,
1511 key_buffer_size));
1512
1513 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&GX, &G, &X, &P, NULL));
1514 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&GX, data, data_size));
1515
1516 *data_length = mbedtls_mpi_size(&GX);
1517 }
1518cleanup:
1519 mbedtls_mpi_free(&P); mbedtls_mpi_free(&G);
1520 mbedtls_mpi_free(&X); mbedtls_mpi_free(&GX);
1521
1522 if (status == PSA_SUCCESS && ret != 0) {
1523 return mbedtls_to_psa_error(ret);
1524 }
1525
1526 return status;
1527}
1528
1529#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR ||
1530 MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY */
1531
Ronald Cron7285cda2020-11-26 14:46:37 +01001532psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001533 const psa_key_attributes_t *attributes,
1534 const uint8_t *key_buffer,
1535 size_t key_buffer_size,
1536 uint8_t *data,
1537 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001539{
Ronald Crond18b5f82020-11-25 16:46:05 +01001540 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001541
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001542 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1543 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001545 /* Exporting public -> public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 return psa_export_key_buffer_internal(
1547 key_buffer, key_buffer_size,
1548 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001549 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001550
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001552#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1554 return mbedtls_psa_rsa_export_public_key(attributes,
1555 key_buffer,
1556 key_buffer_size,
1557 data,
1558 data_size,
1559 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001560#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001561 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001563#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1564 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001565 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
John Durkop6ba40d12020-11-10 08:50:04 -08001566#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1568 return mbedtls_psa_ecp_export_public_key(attributes,
1569 key_buffer,
1570 key_buffer_size,
1571 data,
1572 data_size,
1573 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001574#else
1575 /* We don't know how to convert a private ECC key to public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001577#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1578 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001579 }
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001580#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) || \
1581 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY)
1582 else {
1583 return psa_export_ffdh_public_key_internal(key_buffer,
1584 key_buffer_size,
1585 data, data_size,
1586 data_length);
1587 }
1588#else
1589 /* We don't know how to convert a private FFDH key to public */
1590 return PSA_ERROR_NOT_SUPPORTED;
1591#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) ||
1592 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001594 /* This shouldn't happen in the reference implementation, but
1595 it is valid for a special-purpose implementation to omit
1596 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001598 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001599}
Ronald Crond18b5f82020-11-25 16:46:05 +01001600
Gilles Peskine449bd832023-01-11 14:50:10 +01001601psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1602 uint8_t *data,
1603 size_t data_size,
1604 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001605{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001606 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001607 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001608 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001609
Ronald Crone907e552021-01-18 13:22:38 +01001610 /* Reject a zero-length output buffer now, since this can never be a
1611 * valid key representation. This way we know that data must be a valid
1612 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 if (data_size == 0) {
1614 return PSA_ERROR_BUFFER_TOO_SMALL;
1615 }
Ronald Crone907e552021-01-18 13:22:38 +01001616
Darryl Greendd8fb772018-11-07 16:00:44 +00001617 /* Set the key to empty now, so that even when there are errors, we always
1618 * set data_length to a value between 0 and data_size. On error, setting
1619 * the key to empty is a good choice because an empty key representation is
1620 * unlikely to be accepted anywhere. */
1621 *data_length = 0;
1622
1623 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1625 if (status != PSA_SUCCESS) {
1626 return status;
1627 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1630 status = PSA_ERROR_INVALID_ARGUMENT;
1631 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001632 }
1633
Ronald Crond18b5f82020-11-25 16:46:05 +01001634 psa_key_attributes_t attributes = {
1635 .core = slot->attr
1636 };
Ronald Cron67227982020-11-26 15:16:05 +01001637 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001638 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001640
1641exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001643
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001645}
1646
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001647MBEDTLS_STATIC_ASSERT(
1648 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1649 "One or more key attribute flag is listed as both external-only and dual-use")
1650MBEDTLS_STATIC_ASSERT(
1651 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1652 "One or more key attribute flag is listed as both internal-only and dual-use")
1653MBEDTLS_STATIC_ASSERT(
1654 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1655 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001656
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001657/** Validate that a key policy is internally well-formed.
1658 *
1659 * This function only rejects invalid policies. It does not validate the
1660 * consistency of the policy with respect to other attributes of the key
1661 * such as the key type.
1662 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001663static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001664{
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1666 PSA_KEY_USAGE_COPY |
1667 PSA_KEY_USAGE_ENCRYPT |
1668 PSA_KEY_USAGE_DECRYPT |
1669 PSA_KEY_USAGE_SIGN_MESSAGE |
1670 PSA_KEY_USAGE_VERIFY_MESSAGE |
1671 PSA_KEY_USAGE_SIGN_HASH |
1672 PSA_KEY_USAGE_VERIFY_HASH |
1673 PSA_KEY_USAGE_VERIFY_DERIVATION |
1674 PSA_KEY_USAGE_DERIVE)) != 0) {
1675 return PSA_ERROR_INVALID_ARGUMENT;
1676 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001677
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001679}
1680
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001681/** Validate the internal consistency of key attributes.
1682 *
1683 * This function only rejects invalid attribute values. If does not
1684 * validate the consistency of the attributes with any key data that may
1685 * be involved in the creation of the key.
1686 *
1687 * Call this function early in the key creation process.
1688 *
1689 * \param[in] attributes Key attributes for the new key.
1690 * \param[out] p_drv On any return, the driver for the key, if any.
1691 * NULL for a transparent key.
1692 *
1693 */
1694static psa_status_t psa_validate_key_attributes(
1695 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001697{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001698 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1700 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001701
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 status = psa_validate_key_location(lifetime, p_drv);
1703 if (status != PSA_SUCCESS) {
1704 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001705 }
1706
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 status = psa_validate_key_persistence(lifetime);
1708 if (status != PSA_SUCCESS) {
1709 return status;
1710 }
1711
1712 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1713 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1714 return PSA_ERROR_INVALID_ARGUMENT;
1715 }
1716 } else {
1717 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1718 return PSA_ERROR_INVALID_ARGUMENT;
1719 }
1720 }
1721
1722 status = psa_validate_key_policy(&attributes->core.policy);
1723 if (status != PSA_SUCCESS) {
1724 return status;
1725 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001726
1727 /* Refuse to create overly large keys.
1728 * Note that this doesn't trigger on import if the attributes don't
1729 * explicitly specify a size (so psa_get_key_bits returns 0), so
1730 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1732 return PSA_ERROR_NOT_SUPPORTED;
1733 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001734
Gilles Peskine91e8c332019-08-02 19:19:39 +02001735 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1737 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1738 return PSA_ERROR_INVALID_ARGUMENT;
1739 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001740
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001742}
1743
Gilles Peskine4747d192019-04-17 15:05:45 +02001744/** Prepare a key slot to receive key material.
1745 *
1746 * This function allocates a key slot and sets its metadata.
1747 *
1748 * If this function fails, call psa_fail_key_creation().
1749 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001750 * This function is intended to be used as follows:
1751 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001752 * it with the specified attributes, and in case of a volatile key assign it
1753 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001754 * -# Populate the slot with the key material.
1755 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1756 * In case of failure at any step, stop the sequence and call
1757 * psa_fail_key_creation().
1758 *
Ronald Cron5c522922020-11-14 16:35:34 +01001759 * On success, the key slot is locked. It is the responsibility of the caller
1760 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001761 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001762 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001763 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001764 * \param[out] p_slot On success, a pointer to the prepared slot.
1765 * \param[out] p_drv On any return, the driver for the key, if any.
1766 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001767 *
1768 * \retval #PSA_SUCCESS
1769 * The key slot is ready to receive key material.
1770 * \return If this function fails, the key slot is an invalid state.
1771 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001772 */
1773static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001774 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001775 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001776 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001778{
1779 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001780 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001781 psa_key_slot_t *slot;
1782
Gilles Peskinedf179142019-07-15 22:02:14 +02001783 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001784 *p_drv = NULL;
1785
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 status = psa_validate_key_attributes(attributes, p_drv);
1787 if (status != PSA_SUCCESS) {
1788 return status;
1789 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001790
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1792 if (status != PSA_SUCCESS) {
1793 return status;
1794 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001795 slot = *p_slot;
1796
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001797 /* We're storing the declared bit-size of the key. It's up to each
1798 * creation mechanism to verify that this information is correct.
1799 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001800 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001801 * is optional (import, copy). In case of a volatile key, assign it the
1802 * volatile key identifier associated to the slot returned to contain its
1803 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001804
1805 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001807#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1808 slot->attr.id = volatile_key_id;
1809#else
1810 slot->attr.id.key_id = volatile_key_id;
1811#endif
1812 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001813
Gilles Peskine91e8c332019-08-02 19:19:39 +02001814 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001815 * external-only flags, query `attributes`. Thanks to the check
1816 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001817 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001818 * may have set. */
1819 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001820
Gilles Peskinecbaff462019-07-12 23:46:04 +02001821#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001822 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001823 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001824 * create the key file in internal storage, create the
1825 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001826 * persistent data. This is done by starting a transaction that will
1827 * encompass these three actions.
1828 * For registering a volatile key, we just need to find an appropriate
1829 * slot number inside the SE. Since the key is designated volatile, creating
1830 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001831 /* The first thing to do is to find a slot number for the new key.
1832 * We save the slot number in persistent storage as part of the
1833 * transaction data. It will be needed to recover if the power
1834 * fails during the key creation process, to clean up on the secure
1835 * element side after restarting. Obtaining a slot number from the
1836 * secure element driver updates its persistent state, but we do not yet
1837 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001838 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001840 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1842 &slot_number);
1843 if (status != PSA_SUCCESS) {
1844 return status;
1845 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1848 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001849 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001850 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001851 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 status = psa_crypto_save_transaction();
1853 if (status != PSA_SUCCESS) {
1854 (void) psa_crypto_stop_transaction();
1855 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001856 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001857 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001858
1859 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001861 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001862
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001864 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001866 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001867#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1868
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001870}
Gilles Peskine4747d192019-04-17 15:05:45 +02001871
1872/** Finalize the creation of a key once its key material has been set.
1873 *
1874 * This entails writing the key to persistent storage.
1875 *
1876 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001877 * See the documentation of psa_start_key_creation() for the intended use
1878 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001879 *
Ronald Cron5c522922020-11-14 16:35:34 +01001880 * If the finalization succeeds, the function unlocks the key slot (it was
1881 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1882 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001883 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001884 * \param[in,out] slot Pointer to the slot with key material.
1885 * \param[in] driver The secure element driver for the key,
1886 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001887 * \param[out] key On success, identifier of the key. Note that the
1888 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001889 *
1890 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001891 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001892 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1893 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1894 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1895 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1896 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1897 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001898 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001899 * \return If this function fails, the key slot is an invalid state.
1900 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001901 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001902static psa_status_t psa_finish_key_creation(
1903 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001904 psa_se_drv_table_entry_t *driver,
1905 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001906{
1907 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001908 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001909 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001910
1911#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001913#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001915 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001916 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001918
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001919 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1920 sizeof(data.slot_number),
1921 "Slot number size does not match psa_se_key_data_storage_t");
1922
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1924 status = psa_save_persistent_key(&slot->attr,
1925 (uint8_t *) &data,
1926 sizeof(data));
1927 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001928#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1929 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001930 /* Key material is saved in export representation in the slot, so
1931 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 status = psa_save_persistent_key(&slot->attr,
1933 slot->key.data,
1934 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001935 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001936 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001937#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1938
Gilles Peskinecbaff462019-07-12 23:46:04 +02001939#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001940 /* Finish the transaction for a key creation. This does not
1941 * happen when registering an existing key. Detect this case
1942 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001943 * creation of a persistent key in a secure element requires a transaction,
1944 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 if (driver != NULL &&
1946 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1947 status = psa_save_se_persistent_data(driver);
1948 if (status != PSA_SUCCESS) {
1949 psa_destroy_persistent_key(slot->attr.id);
1950 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001951 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001953 }
1954#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001957 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 status = psa_unlock_key_slot(slot);
1959 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001960 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001962 }
Ronald Cron50972942020-11-14 11:28:25 +01001963
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001965}
1966
1967/** Abort the creation of a key.
1968 *
1969 * You may call this function after calling psa_start_key_creation(),
1970 * or after psa_finish_key_creation() fails. In other circumstances, this
1971 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001972 * See the documentation of psa_start_key_creation() for the intended use
1973 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001974 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001975 * \param[in,out] slot Pointer to the slot with key material.
1976 * \param[in] driver The secure element driver for the key,
1977 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001978 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001979static void psa_fail_key_creation(psa_key_slot_t *slot,
1980 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001981{
Gilles Peskine011e4282019-06-26 18:34:38 +02001982 (void) driver;
1983
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001985 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001987
1988#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001989 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001990 * element, and the failure happened later (when saving metadata
1991 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001992 * element.
1993 * https://github.com/ARMmbed/mbed-crypto/issues/217
1994 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001995
Gilles Peskined7729582019-08-05 15:55:54 +02001996 /* Abort the ongoing transaction if any (there may not be one if
1997 * the creation process failed before starting one, or if the
1998 * key creation is a registration of a key in a secure element).
1999 * Earlier functions must already have done what it takes to undo any
2000 * partial creation. All that's left is to update the transaction data
2001 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02002003#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2004
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02002006}
2007
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002008/** Validate optional attributes during key creation.
2009 *
2010 * Some key attributes are optional during key creation. If they are
2011 * specified in the attributes structure, check that they are consistent
2012 * with the data in the slot.
2013 *
2014 * This function should be called near the end of key creation, after
2015 * the slot in memory is fully populated but before saving persistent data.
2016 */
2017static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002018 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002020{
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 if (attributes->core.type != 0) {
2022 if (attributes->core.type != slot->attr.type) {
2023 return PSA_ERROR_INVALID_ARGUMENT;
2024 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002025 }
2026
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07002028#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2030 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002031 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002032 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002033 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002034
Ronald Cron90857082020-11-25 14:58:33 +01002035 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 slot->attr.type,
2037 slot->key.data,
2038 slot->key.bytes,
2039 &rsa);
2040 if (status != PSA_SUCCESS) {
2041 return status;
2042 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 mbedtls_mpi_init(&actual);
2045 mbedtls_mpi_init(&required);
2046 ret = mbedtls_rsa_export(rsa,
2047 NULL, NULL, NULL, NULL, &actual);
2048 mbedtls_rsa_free(rsa);
2049 mbedtls_free(rsa);
2050 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002051 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 }
2053 ret = mbedtls_mpi_read_binary(&required,
2054 attributes->domain_parameters,
2055 attributes->domain_parameters_size);
2056 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002057 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 }
2059 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002060 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 }
2062rsa_exit:
2063 mbedtls_mpi_free(&actual);
2064 mbedtls_mpi_free(&required);
2065 if (ret != 0) {
2066 return mbedtls_to_psa_error(ret);
2067 }
2068 } else
John Durkop9814fa22020-11-04 12:28:15 -08002069#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2070 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002071 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002073 }
2074 }
2075
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 if (attributes->core.bits != 0) {
2077 if (attributes->core.bits != slot->attr.bits) {
2078 return PSA_ERROR_INVALID_ARGUMENT;
2079 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002080 }
2081
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002083}
2084
Gilles Peskine449bd832023-01-11 14:50:10 +01002085psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2086 const uint8_t *data,
2087 size_t data_length,
2088 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002089{
2090 psa_status_t status;
2091 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002092 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002093 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302094 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002095
Ronald Cron81709fc2020-11-14 12:10:32 +01002096 *key = MBEDTLS_SVC_KEY_ID_INIT;
2097
Gilles Peskine0f84d622019-09-12 19:03:13 +02002098 /* Reject zero-length symmetric keys (including raw data key objects).
2099 * This also rejects any key which might be encoded as an empty string,
2100 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 if (data_length == 0) {
2102 return PSA_ERROR_INVALID_ARGUMENT;
2103 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002104
Archana449608b2021-09-08 15:36:05 +05302105 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 if (data_length > SIZE_MAX / 8) {
2107 return PSA_ERROR_NOT_SUPPORTED;
2108 }
Archana6ed4bda2021-08-04 10:47:15 +05302109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2111 &slot, &driver);
2112 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002113 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002115
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002116 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302117 * storage ( thus not in the case of importing a key in a secure element
2118 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2119 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 if (slot->key.data == NULL) {
2121 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302122 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 attributes, data, data_length, &storage_size);
2124 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302125 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 }
Archanad8a83dc2021-06-14 10:04:16 +05302127 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 status = psa_allocate_buffer_to_slot(slot, storage_size);
2129 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002130 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002132 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002133
2134 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 status = psa_driver_wrapper_import_key(attributes,
2136 data, data_length,
2137 slot->key.data,
2138 slot->key.bytes,
2139 &slot->key.bytes, &bits);
2140 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002141 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002143
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002145 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002147 status = PSA_ERROR_INVALID_ARGUMENT;
2148 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002149 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002150
Archana6ed4bda2021-08-04 10:47:15 +05302151 /* Enforce a size limit, and in particular ensure that the bit
2152 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302154 status = PSA_ERROR_NOT_SUPPORTED;
2155 goto exit;
2156 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 status = psa_validate_optional_attributes(slot, attributes);
2158 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002159 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002161
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002163exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 if (status != PSA_SUCCESS) {
2165 psa_fail_key_creation(slot, driver);
2166 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002167
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002169}
2170
Gilles Peskined7729582019-08-05 15:55:54 +02002171#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2172psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002174{
2175 psa_status_t status;
2176 psa_key_slot_t *slot = NULL;
2177 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002178 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002179
2180 /* Leaving attributes unspecified is not currently supported.
2181 * It could make sense to query the key type and size from the
2182 * secure element, but not all secure elements support this
2183 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2185 return PSA_ERROR_NOT_SUPPORTED;
2186 }
2187 if (psa_get_key_bits(attributes) == 0) {
2188 return PSA_ERROR_NOT_SUPPORTED;
2189 }
Gilles Peskined7729582019-08-05 15:55:54 +02002190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2192 &slot, &driver);
2193 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002194 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 }
Gilles Peskined7729582019-08-05 15:55:54 +02002196
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002198
2199exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 if (status != PSA_SUCCESS) {
2201 psa_fail_key_creation(slot, driver);
2202 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002203
Gilles Peskined7729582019-08-05 15:55:54 +02002204 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 psa_close_key(key);
2206 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002207}
2208#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2211 const psa_key_attributes_t *specified_attributes,
2212 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002213{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002214 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002215 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002216 psa_key_slot_t *source_slot = NULL;
2217 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002218 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002219 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302220 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002221
Ronald Cron81709fc2020-11-14 12:10:32 +01002222 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2223
Archana8a180362021-07-05 02:18:48 +05302224 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2226 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002227 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 status = psa_validate_optional_attributes(source_slot,
2231 specified_attributes);
2232 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002233 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002235
Archana9d17bf42021-09-10 06:22:44 +05302236 /* The target key type and number of bits have been validated by
2237 * psa_validate_optional_attributes() to be either equal to zero or
2238 * equal to the ones of the source key. So it is safe to inherit
2239 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302240 * */
Archana9d17bf42021-09-10 06:22:44 +05302241 actual_attributes.core.bits = source_slot->attr.bits;
2242 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302243
2244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 status = psa_restrict_key_policy(source_slot->attr.type,
2246 &actual_attributes.core.policy,
2247 &source_slot->attr.policy);
2248 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002249 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002251
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2253 &target_slot, &driver);
2254 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002255 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 }
2257 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2258 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302259 /*
Archana9d17bf42021-09-10 06:22:44 +05302260 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302261 * the source key would need to be exported as plaintext and re-imported
2262 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302263 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302264 * appropriate API invocations from the application, if needed.
2265 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002266 status = PSA_ERROR_NOT_SUPPORTED;
2267 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002268 }
Archana8a180362021-07-05 02:18:48 +05302269 /*
2270 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302271 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302272 * - For opaque keys this translates to an invocation of the drivers'
2273 * copy_key entry point through the dispatch layer.
2274 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2276 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2277 &storage_size);
2278 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302279 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 }
Archana374fe5b2021-09-08 15:50:28 +05302281
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2283 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302284 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 }
Archana374fe5b2021-09-08 15:50:28 +05302286
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 status = psa_driver_wrapper_copy_key(&actual_attributes,
2288 source_slot->key.data,
2289 source_slot->key.bytes,
2290 target_slot->key.data,
2291 target_slot->key.bytes,
2292 &target_slot->key.bytes);
2293 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302294 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 }
2296 } else {
2297 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302298 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 source_slot->key.bytes);
2300 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302301 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 }
Archana8a180362021-07-05 02:18:48 +05302303 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002305exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 if (status != PSA_SUCCESS) {
2307 psa_fail_key_creation(target_slot, driver);
2308 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002309
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002313}
2314
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002315
2316
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002317/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002318/* Message digests */
2319/****************************************************************/
2320
Gilles Peskine449bd832023-01-11 14:50:10 +01002321psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002322{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002323 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 if (operation->id == 0) {
2325 return PSA_SUCCESS;
2326 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002327
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002329 operation->id = 0;
2330
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002332}
2333
Gilles Peskine449bd832023-01-11 14:50:10 +01002334psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2335 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002336{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002337 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002338
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002339 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002341 status = PSA_ERROR_BAD_STATE;
2342 goto exit;
2343 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002344
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002346 status = PSA_ERROR_INVALID_ARGUMENT;
2347 goto exit;
2348 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002349
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002350 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2351 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002353
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002355
2356exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 if (status != PSA_SUCCESS) {
2358 psa_hash_abort(operation);
2359 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002360
2361 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002362}
2363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2365 const uint8_t *input,
2366 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002367{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002368 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2369
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002371 status = PSA_ERROR_BAD_STATE;
2372 goto exit;
2373 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002374
Steven Cooremanc8288352021-03-04 14:02:19 +01002375 /* Don't require hash implementations to behave correctly on a
2376 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 if (input_length == 0) {
2378 return PSA_SUCCESS;
2379 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002382
2383exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 if (status != PSA_SUCCESS) {
2385 psa_hash_abort(operation);
2386 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002387
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002389}
2390
Gilles Peskine449bd832023-01-11 14:50:10 +01002391psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2392 uint8_t *hash,
2393 size_t hash_size,
2394 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002395{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002396 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 if (operation->id == 0) {
2398 return PSA_ERROR_BAD_STATE;
2399 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002400
Steven Cooreman1e582352021-02-18 17:24:37 +01002401 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 operation, hash, hash_size, hash_length);
2403 psa_hash_abort(operation);
2404 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002405}
2406
Gilles Peskine449bd832023-01-11 14:50:10 +01002407psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2408 const uint8_t *hash,
2409 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002410{
Ronald Cron69a63422021-10-18 09:47:58 +02002411 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002412 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002413 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 operation,
2415 actual_hash, sizeof(actual_hash),
2416 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002419 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002421
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002423 status = PSA_ERROR_INVALID_SIGNATURE;
2424 goto exit;
2425 }
2426
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002428 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002430
2431exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2433 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002434 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002438}
2439
Gilles Peskine449bd832023-01-11 14:50:10 +01002440psa_status_t psa_hash_compute(psa_algorithm_t alg,
2441 const uint8_t *input, size_t input_length,
2442 uint8_t *hash, size_t hash_size,
2443 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002444{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002445 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 if (!PSA_ALG_IS_HASH(alg)) {
2447 return PSA_ERROR_INVALID_ARGUMENT;
2448 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002449
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2451 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002452}
2453
Gilles Peskine449bd832023-01-11 14:50:10 +01002454psa_status_t psa_hash_compare(psa_algorithm_t alg,
2455 const uint8_t *input, size_t input_length,
2456 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002457{
Ronald Cron69a63422021-10-18 09:47:58 +02002458 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002459 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002460
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 if (!PSA_ALG_IS_HASH(alg)) {
2462 return PSA_ERROR_INVALID_ARGUMENT;
2463 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002464
Steven Cooreman1e582352021-02-18 17:24:37 +01002465 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 alg, input, input_length,
2467 actual_hash, sizeof(actual_hash),
2468 &actual_hash_length);
2469 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002470 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 }
2472 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002473 status = PSA_ERROR_INVALID_SIGNATURE;
2474 goto exit;
2475 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002477 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002479
2480exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2482 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002483}
2484
Gilles Peskine449bd832023-01-11 14:50:10 +01002485psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2486 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002487{
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 if (source_operation->id == 0 ||
2489 target_operation->id != 0) {
2490 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002491 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002492
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2494 target_operation);
2495 if (status != PSA_SUCCESS) {
2496 psa_hash_abort(target_operation);
2497 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002498
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002500}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002501
2502
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002503/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002504/* MAC */
2505/****************************************************************/
2506
Gilles Peskine449bd832023-01-11 14:50:10 +01002507psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002508{
Steven Cooremane6804192021-03-19 18:28:56 +01002509 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 if (operation->id == 0) {
2511 return PSA_SUCCESS;
2512 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002513
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002515 operation->mac_size = 0;
2516 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002517 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002518
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002520}
2521
Ronald Cron2dff3b22021-06-17 16:33:22 +02002522static psa_status_t psa_mac_finalize_alg_and_key_validation(
2523 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002524 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002526{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002527 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 psa_key_type_t key_type = psa_get_key_type(attributes);
2529 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002530
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 if (!PSA_ALG_IS_MAC(alg)) {
2532 return PSA_ERROR_INVALID_ARGUMENT;
2533 }
Steven Cooremane6804192021-03-19 18:28:56 +01002534
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002535 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 status = psa_mac_key_can_do(alg, key_type);
2537 if (status != PSA_SUCCESS) {
2538 return status;
2539 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002540
Steven Cooremand1a68f12021-05-10 11:13:41 +02002541 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002543
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002545 /* A very short MAC is too short for security since it can be
2546 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2547 * so we make this our minimum, even though 32 bits is still
2548 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002550 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002551
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2553 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002554 /* It's impossible to "truncate" to a larger length than the full length
2555 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002557 }
2558
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002560 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2561 * that is disabled in the compile-time configuration. The result can
2562 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2563 * configuration into account. In this case, force a return of
2564 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2565 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2566 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2567 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2568 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002570 }
2571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002573}
2574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2576 mbedtls_svc_key_id_t key,
2577 psa_algorithm_t alg,
2578 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002579{
2580 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2581 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002582 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002583
2584 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002586 status = PSA_ERROR_BAD_STATE;
2587 goto exit;
2588 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002589
2590 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 key,
2592 &slot,
2593 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2594 alg);
2595 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002596 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002598
2599 psa_key_attributes_t attributes = {
2600 .core = slot->attr
2601 };
2602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2604 &operation->mac_size);
2605 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002606 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002608
2609 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002610 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 if (is_sign) {
2612 status = psa_driver_wrapper_mac_sign_setup(operation,
2613 &attributes,
2614 slot->key.data,
2615 slot->key.bytes,
2616 alg);
2617 } else {
2618 status = psa_driver_wrapper_mac_verify_setup(operation,
2619 &attributes,
2620 slot->key.data,
2621 slot->key.bytes,
2622 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002623 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002624
Gilles Peskinefbfac682018-07-08 20:51:54 +02002625exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 if (status != PSA_SUCCESS) {
2627 psa_mac_abort(operation);
2628 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002629
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002633}
2634
Gilles Peskine449bd832023-01-11 14:50:10 +01002635psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2636 mbedtls_svc_key_id_t key,
2637 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002638{
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002640}
2641
Gilles Peskine449bd832023-01-11 14:50:10 +01002642psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2643 mbedtls_svc_key_id_t key,
2644 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002645{
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002647}
2648
Gilles Peskine449bd832023-01-11 14:50:10 +01002649psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2650 const uint8_t *input,
2651 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002652{
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 if (operation->id == 0) {
2654 return PSA_ERROR_BAD_STATE;
2655 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002656
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002657 /* Don't require hash implementations to behave correctly on a
2658 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 if (input_length == 0) {
2660 return PSA_SUCCESS;
2661 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002662
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2664 input, input_length);
2665 if (status != PSA_SUCCESS) {
2666 psa_mac_abort(operation);
2667 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002670}
2671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2673 uint8_t *mac,
2674 size_t mac_size,
2675 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002676{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002677 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2678 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002679
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002681 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002682 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002683 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002684
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002686 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002687 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002688 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002689
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002690 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2691 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002693 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002694 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002695 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002696
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002698 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002699 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002700 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002701
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 status = psa_driver_wrapper_mac_sign_finish(operation,
2703 mac, operation->mac_size,
2704 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002705
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002706exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002707 /* In case of success, set the potential excess room in the output buffer
2708 * to an invalid value, to avoid potentially leaking a longer MAC.
2709 * In case of error, set the output length and content to a safe default,
2710 * such that in case the caller misses an error check, the output would be
2711 * an unachievable MAC.
2712 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002714 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002715 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002716 }
mohammad16036df908f2018-04-02 08:34:15 -07002717
Paul Elliottdc42ca82023-02-24 18:11:59 +00002718 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002723}
2724
Gilles Peskine449bd832023-01-11 14:50:10 +01002725psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2726 const uint8_t *mac,
2727 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002728{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002729 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2730 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002731
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002733 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002734 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002735 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002736
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002738 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002739 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002740 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002741
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002743 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002744 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002745 }
2746
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 status = psa_driver_wrapper_mac_verify_finish(operation,
2748 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002749
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002750exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002752
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002754}
2755
Gilles Peskine449bd832023-01-11 14:50:10 +01002756static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2757 psa_algorithm_t alg,
2758 const uint8_t *input,
2759 size_t input_length,
2760 uint8_t *mac,
2761 size_t mac_size,
2762 size_t *mac_length,
2763 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002764{
2765 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002766 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2767 psa_key_slot_t *slot;
2768 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002769
Ronald Cron51131b52021-06-17 17:17:20 +02002770 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 key,
2772 &slot,
2773 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2774 alg);
2775 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002776 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002778
Ronald Cron51131b52021-06-17 17:17:20 +02002779 psa_key_attributes_t attributes = {
2780 .core = slot->attr
2781 };
2782
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2784 &operation_mac_size);
2785 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002786 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002788
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002790 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002791 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002792 }
2793
2794 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 &attributes,
2796 slot->key.data, slot->key.bytes,
2797 alg,
2798 input, input_length,
2799 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002800
2801exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002802 /* In case of success, set the potential excess room in the output buffer
2803 * to an invalid value, to avoid potentially leaking a longer MAC.
2804 * In case of error, set the output length and content to a safe default,
2805 * such that in case the caller misses an error check, the output would be
2806 * an unachievable MAC.
2807 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002809 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002810 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002811 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002812
2813 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002814
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002816
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002818}
2819
Gilles Peskine449bd832023-01-11 14:50:10 +01002820psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002821 psa_algorithm_t alg,
2822 const uint8_t *input,
2823 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 uint8_t *mac,
2825 size_t mac_size,
2826 size_t *mac_length)
2827{
2828 return psa_mac_compute_internal(key, alg,
2829 input, input_length,
2830 mac, mac_size, mac_length, 1);
2831}
2832
2833psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2834 psa_algorithm_t alg,
2835 const uint8_t *input,
2836 size_t input_length,
2837 const uint8_t *mac,
2838 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002839{
2840 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002841 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2842 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002843
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 status = psa_mac_compute_internal(key, alg,
2845 input, input_length,
2846 actual_mac, sizeof(actual_mac),
2847 &actual_mac_length, 0);
2848 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002849 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002851
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002853 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002854 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002855 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002857 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002858 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002859 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002860
2861exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002863
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002865}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002866
Gilles Peskinee59236f2018-01-27 23:32:46 +01002867/****************************************************************/
2868/* Asymmetric cryptography */
2869/****************************************************************/
2870
Gilles Peskine449bd832023-01-11 14:50:10 +01002871static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2872 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002873{
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 if (input_is_message) {
2875 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2876 return PSA_ERROR_INVALID_ARGUMENT;
2877 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002878
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2880 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2881 return PSA_ERROR_INVALID_ARGUMENT;
2882 }
2883 }
2884 } else {
2885 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2886 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002887 }
2888 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002889
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002891}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002892
Gilles Peskine449bd832023-01-11 14:50:10 +01002893static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2894 int input_is_message,
2895 psa_algorithm_t alg,
2896 const uint8_t *input,
2897 size_t input_length,
2898 uint8_t *signature,
2899 size_t signature_size,
2900 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002901{
2902 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2903 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2904 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002905
2906 *signature_length = 0;
2907
Gilles Peskine449bd832023-01-11 14:50:10 +01002908 status = psa_sign_verify_check_alg(input_is_message, alg);
2909 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002910 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002912
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002913 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002914 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002915 * buffer can in principle be empty since it doesn't actually have
2916 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 if (signature_size == 0) {
2918 return PSA_ERROR_BUFFER_TOO_SMALL;
2919 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002920
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002921 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 key, &slot,
2923 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2924 PSA_KEY_USAGE_SIGN_HASH,
2925 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002926
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002928 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002930
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002932 status = PSA_ERROR_INVALID_ARGUMENT;
2933 goto exit;
2934 }
2935
2936 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002938 };
2939
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002941 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002942 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002943 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 signature, signature_size, signature_length);
2945 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002946
2947 status = psa_driver_wrapper_sign_hash(
2948 &attributes, slot->key.data, slot->key.bytes,
2949 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002951 }
2952
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002953
2954exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002955 psa_wipe_tag_output_buffer(signature, status, signature_size,
2956 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002957
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002959
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002961}
2962
Gilles Peskine449bd832023-01-11 14:50:10 +01002963static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2964 int input_is_message,
2965 psa_algorithm_t alg,
2966 const uint8_t *input,
2967 size_t input_length,
2968 const uint8_t *signature,
2969 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002970{
2971 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2972 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2973 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002974
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 status = psa_sign_verify_check_alg(input_is_message, alg);
2976 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002977 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002979
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002980 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 key, &slot,
2982 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2983 PSA_KEY_USAGE_VERIFY_HASH,
2984 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002985
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 if (status != PSA_SUCCESS) {
2987 return status;
2988 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002989
2990 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002992 };
2993
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002995 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002996 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002997 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 signature, signature_length);
2999 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003000 status = psa_driver_wrapper_verify_hash(
3001 &attributes, slot->key.data, slot->key.bytes,
3002 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003004 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003005
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003007
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003009
3010}
3011
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003012psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003013 const psa_key_attributes_t *attributes,
3014 const uint8_t *key_buffer,
3015 size_t key_buffer_size,
3016 psa_algorithm_t alg,
3017 const uint8_t *input,
3018 size_t input_length,
3019 uint8_t *signature,
3020 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003022{
3023 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003026 size_t hash_length;
3027 uint8_t hash[PSA_HASH_MAX_SIZE];
3028
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003029 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 PSA_ALG_SIGN_GET_HASH(alg),
3031 input, input_length,
3032 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003033
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003035 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003037
3038 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 attributes, key_buffer, key_buffer_size,
3040 alg, hash, hash_length,
3041 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003042 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003043
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003045}
3046
Gilles Peskine449bd832023-01-11 14:50:10 +01003047psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3048 psa_algorithm_t alg,
3049 const uint8_t *input,
3050 size_t input_length,
3051 uint8_t *signature,
3052 size_t signature_size,
3053 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003054{
3055 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003056 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003058}
3059
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003060psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003061 const psa_key_attributes_t *attributes,
3062 const uint8_t *key_buffer,
3063 size_t key_buffer_size,
3064 psa_algorithm_t alg,
3065 const uint8_t *input,
3066 size_t input_length,
3067 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003069{
3070 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003071
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003073 size_t hash_length;
3074 uint8_t hash[PSA_HASH_MAX_SIZE];
3075
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003076 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 PSA_ALG_SIGN_GET_HASH(alg),
3078 input, input_length,
3079 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003080
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003082 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003084
3085 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 attributes, key_buffer, key_buffer_size,
3087 alg, hash, hash_length,
3088 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003089 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003090
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003092}
3093
Gilles Peskine449bd832023-01-11 14:50:10 +01003094psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3095 psa_algorithm_t alg,
3096 const uint8_t *input,
3097 size_t input_length,
3098 const uint8_t *signature,
3099 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003100{
3101 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003102 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003104}
3105
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003106psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003107 const psa_key_attributes_t *attributes,
3108 const uint8_t *key_buffer, size_t key_buffer_size,
3109 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003111{
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3113 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3114 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003115#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3117 return mbedtls_psa_rsa_sign_hash(
3118 attributes,
3119 key_buffer, key_buffer_size,
3120 alg, hash, hash_length,
3121 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003122#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3123 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 } else {
3125 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003126 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3128 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003129#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3131 return mbedtls_psa_ecdsa_sign_hash(
3132 attributes,
3133 key_buffer, key_buffer_size,
3134 alg, hash, hash_length,
3135 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003136#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3137 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 } else {
3139 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003140 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003141 }
Ronald Cron4501c982021-03-19 20:09:32 +01003142
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 (void) key_buffer;
3144 (void) key_buffer_size;
3145 (void) hash;
3146 (void) hash_length;
3147 (void) signature;
3148 (void) signature_size;
3149 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003150
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003152}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003153
Gilles Peskine449bd832023-01-11 14:50:10 +01003154psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3155 psa_algorithm_t alg,
3156 const uint8_t *hash,
3157 size_t hash_length,
3158 uint8_t *signature,
3159 size_t signature_size,
3160 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003161{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003162 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003163 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003165}
3166
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003167psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003168 const psa_key_attributes_t *attributes,
3169 const uint8_t *key_buffer, size_t key_buffer_size,
3170 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003171 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003172{
Gilles Peskine449bd832023-01-11 14:50:10 +01003173 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3174 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3175 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003176#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003177 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3178 return mbedtls_psa_rsa_verify_hash(
3179 attributes,
3180 key_buffer, key_buffer_size,
3181 alg, hash, hash_length,
3182 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003183#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3184 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003185 } else {
3186 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003187 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003188 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3189 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003190#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3192 return mbedtls_psa_ecdsa_verify_hash(
3193 attributes,
3194 key_buffer, key_buffer_size,
3195 alg, hash, hash_length,
3196 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003197#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3198 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 } else {
3200 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003201 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003202 }
Ronald Cron4501c982021-03-19 20:09:32 +01003203
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 (void) key_buffer;
3205 (void) key_buffer_size;
3206 (void) hash;
3207 (void) hash_length;
3208 (void) signature;
3209 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003210
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003212}
3213
Gilles Peskine449bd832023-01-11 14:50:10 +01003214psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3215 psa_algorithm_t alg,
3216 const uint8_t *hash,
3217 size_t hash_length,
3218 const uint8_t *signature,
3219 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003220{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003221 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003222 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003223 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003224}
3225
Gilles Peskine449bd832023-01-11 14:50:10 +01003226psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3227 psa_algorithm_t alg,
3228 const uint8_t *input,
3229 size_t input_length,
3230 const uint8_t *salt,
3231 size_t salt_length,
3232 uint8_t *output,
3233 size_t output_size,
3234 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003235{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003236 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003237 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003238 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003239
Darryl Green5cc689a2018-07-24 15:34:10 +01003240 (void) input;
3241 (void) input_length;
3242 (void) salt;
3243 (void) output;
3244 (void) output_size;
3245
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003246 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003247
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3249 return PSA_ERROR_INVALID_ARGUMENT;
3250 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003251
Ronald Cron5c522922020-11-14 16:35:34 +01003252 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3254 if (status != PSA_SUCCESS) {
3255 return status;
3256 }
3257 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3258 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003259 status = PSA_ERROR_INVALID_ARGUMENT;
3260 goto exit;
3261 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003262
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003263 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003265 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003266
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003267 status = psa_driver_wrapper_asymmetric_encrypt(
3268 &attributes, slot->key.data, slot->key.bytes,
3269 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003271exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003272 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003273
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003275}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003276
Gilles Peskine449bd832023-01-11 14:50:10 +01003277psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3278 psa_algorithm_t alg,
3279 const uint8_t *input,
3280 size_t input_length,
3281 const uint8_t *salt,
3282 size_t salt_length,
3283 uint8_t *output,
3284 size_t output_size,
3285 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003286{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003287 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003288 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003289 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003290
Darryl Green5cc689a2018-07-24 15:34:10 +01003291 (void) input;
3292 (void) input_length;
3293 (void) salt;
3294 (void) output;
3295 (void) output_size;
3296
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003297 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003298
Gilles Peskine449bd832023-01-11 14:50:10 +01003299 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3300 return PSA_ERROR_INVALID_ARGUMENT;
3301 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003302
Ronald Cron5c522922020-11-14 16:35:34 +01003303 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003304 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3305 if (status != PSA_SUCCESS) {
3306 return status;
3307 }
3308 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003309 status = PSA_ERROR_INVALID_ARGUMENT;
3310 goto exit;
3311 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003312
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003313 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003314 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003315 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003316
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003317 status = psa_driver_wrapper_asymmetric_decrypt(
3318 &attributes, slot->key.data, slot->key.bytes,
3319 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003321
3322exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003324
Gilles Peskine449bd832023-01-11 14:50:10 +01003325 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003326}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003327
Paul Elliott9fe12f62022-11-30 19:16:02 +00003328/****************************************************************/
3329/* Asymmetric interruptible cryptography */
3330/****************************************************************/
3331
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003332static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3333
Paul Elliott9fe12f62022-11-30 19:16:02 +00003334void psa_interruptible_set_max_ops(uint32_t max_ops)
3335{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003336 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003337}
3338
3339uint32_t psa_interruptible_get_max_ops(void)
3340{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003341 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003342}
3343
Paul Elliott9fe12f62022-11-30 19:16:02 +00003344uint32_t psa_sign_hash_get_num_ops(
3345 const psa_sign_hash_interruptible_operation_t *operation)
3346{
Paul Elliott296ede92022-12-15 17:00:30 +00003347 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003348}
3349
3350uint32_t psa_verify_hash_get_num_ops(
3351 const psa_verify_hash_interruptible_operation_t *operation)
3352{
Paul Elliott296ede92022-12-15 17:00:30 +00003353 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003354}
3355
Paul Elliott59ad9452022-12-18 15:09:02 +00003356static psa_status_t psa_sign_hash_abort_internal(
3357 psa_sign_hash_interruptible_operation_t *operation)
3358{
3359 if (operation->id == 0) {
3360 /* The object has (apparently) been initialized but it is not (yet)
3361 * in use. It's ok to call abort on such an object, and there's
3362 * nothing to do. */
3363 return PSA_SUCCESS;
3364 }
3365
3366 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3367
3368 status = psa_driver_wrapper_sign_hash_abort(operation);
3369
3370 operation->id = 0;
3371
Paul Elliotte6145dc2023-02-07 12:51:21 +00003372 /* Do not clear either the error_occurred or num_ops elements here as they
3373 * only want to be cleared by the application calling abort, not by abort
3374 * being called at completion of an operation. */
3375
Paul Elliott59ad9452022-12-18 15:09:02 +00003376 return status;
3377}
3378
Paul Elliott9fe12f62022-11-30 19:16:02 +00003379psa_status_t psa_sign_hash_start(
3380 psa_sign_hash_interruptible_operation_t *operation,
3381 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3382 const uint8_t *hash, size_t hash_length)
3383{
3384 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3385 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3386 psa_key_slot_t *slot;
3387
Paul Elliottc9774412023-02-06 15:14:07 +00003388 /* Check that start has not been previously called, or operation has not
3389 * previously errored. */
3390 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003391 return PSA_ERROR_BAD_STATE;
3392 }
3393
Paul Elliott9fe12f62022-11-30 19:16:02 +00003394 status = psa_sign_verify_check_alg(0, alg);
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 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3401 PSA_KEY_USAGE_SIGN_HASH,
3402 alg);
3403
3404 if (status != PSA_SUCCESS) {
3405 goto exit;
3406 }
3407
3408 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3409 status = PSA_ERROR_INVALID_ARGUMENT;
3410 goto exit;
3411 }
3412
3413 psa_key_attributes_t attributes = {
3414 .core = slot->attr
3415 };
3416
Paul Elliott296ede92022-12-15 17:00:30 +00003417 /* Ensure ops count gets reset, in case of operation re-use. */
3418 operation->num_ops = 0;
3419
Paul Elliott9fe12f62022-11-30 19:16:02 +00003420 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3421 slot->key.data,
3422 slot->key.bytes, alg,
3423 hash, hash_length);
3424exit:
3425
3426 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003427 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003428 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003429 }
3430
3431 unlock_status = psa_unlock_key_slot(slot);
3432
Paul Elliottc9774412023-02-06 15:14:07 +00003433 if (unlock_status != PSA_SUCCESS) {
3434 operation->error_occurred = 1;
3435 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003436
Paul Elliottc9774412023-02-06 15:14:07 +00003437 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003438}
3439
3440
3441psa_status_t psa_sign_hash_complete(
3442 psa_sign_hash_interruptible_operation_t *operation,
3443 uint8_t *signature, size_t signature_size,
3444 size_t *signature_length)
3445{
3446 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3447
3448 *signature_length = 0;
3449
Paul Elliottc9774412023-02-06 15:14:07 +00003450 /* Check that start has been called first, and that operation has not
3451 * previously errored. */
3452 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003453 status = PSA_ERROR_BAD_STATE;
3454 goto exit;
3455 }
3456
Paul Elliott096abc42023-02-03 18:33:23 +00003457 /* Immediately reject a zero-length signature buffer. This guarantees that
3458 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003459 if (signature_size == 0) {
3460 status = PSA_ERROR_BUFFER_TOO_SMALL;
3461 goto exit;
3462 }
3463
3464 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3465 signature_size,
3466 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003467
Paul Elliott296ede92022-12-15 17:00:30 +00003468 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003469 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003470
Paul Elliottebe225c2023-02-10 14:32:53 +00003471exit:
3472
Paul Elliott59200a22023-02-21 15:07:40 +00003473 psa_wipe_tag_output_buffer(signature, status, signature_size,
3474 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003475
Paul Elliott53bb3122023-02-10 14:22:22 +00003476 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003477 if (status != PSA_SUCCESS) {
3478 operation->error_occurred = 1;
3479 }
3480
Paul Elliott59ad9452022-12-18 15:09:02 +00003481 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003482 }
3483
3484 return status;
3485}
3486
3487psa_status_t psa_sign_hash_abort(
3488 psa_sign_hash_interruptible_operation_t *operation)
3489{
Paul Elliott59ad9452022-12-18 15:09:02 +00003490 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3491
3492 status = psa_sign_hash_abort_internal(operation);
3493
3494 /* We clear the number of ops done here, so that it is not cleared when
3495 * the operation fails or succeeds, only on manual abort. */
3496 operation->num_ops = 0;
3497
Paul Elliottc9774412023-02-06 15:14:07 +00003498 /* Likewise, failure state. */
3499 operation->error_occurred = 0;
3500
Paul Elliott59ad9452022-12-18 15:09:02 +00003501 return status;
3502}
3503
3504static psa_status_t psa_verify_hash_abort_internal(
3505 psa_verify_hash_interruptible_operation_t *operation)
3506{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003507 if (operation->id == 0) {
3508 /* The object has (apparently) been initialized but it is not (yet)
3509 * in use. It's ok to call abort on such an object, and there's
3510 * nothing to do. */
3511 return PSA_SUCCESS;
3512 }
3513
Paul Elliott59ad9452022-12-18 15:09:02 +00003514 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3515
3516 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003517
3518 operation->id = 0;
3519
Paul Elliotte6145dc2023-02-07 12:51:21 +00003520 /* Do not clear either the error_occurred or num_ops elements here as they
3521 * only want to be cleared by the application calling abort, not by abort
3522 * being called at completion of an operation. */
3523
Paul Elliott59ad9452022-12-18 15:09:02 +00003524 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003525}
3526
3527psa_status_t psa_verify_hash_start(
3528 psa_verify_hash_interruptible_operation_t *operation,
3529 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3530 const uint8_t *hash, size_t hash_length,
3531 const uint8_t *signature, size_t signature_length)
3532{
3533 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3534 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3535 psa_key_slot_t *slot;
3536
Paul Elliottc9774412023-02-06 15:14:07 +00003537 /* Check that start has not been previously called, or operation has not
3538 * previously errored. */
3539 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003540 return PSA_ERROR_BAD_STATE;
3541 }
3542
3543 status = psa_sign_verify_check_alg(0, alg);
3544 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003545 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003546 return status;
3547 }
3548
3549 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3550 PSA_KEY_USAGE_VERIFY_HASH,
3551 alg);
3552
3553 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003554 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003555 return status;
3556 }
3557
3558 psa_key_attributes_t attributes = {
3559 .core = slot->attr
3560 };
3561
Paul Elliott296ede92022-12-15 17:00:30 +00003562 /* Ensure ops count gets reset, in case of operation re-use. */
3563 operation->num_ops = 0;
3564
Paul Elliott9fe12f62022-11-30 19:16:02 +00003565 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3566 slot->key.data,
3567 slot->key.bytes,
3568 alg, hash, hash_length,
3569 signature, signature_length);
3570
3571 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003572 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003573 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003574 }
3575
3576 unlock_status = psa_unlock_key_slot(slot);
3577
Paul Elliottc9774412023-02-06 15:14:07 +00003578 if (unlock_status != PSA_SUCCESS) {
3579 operation->error_occurred = 1;
3580 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003581
Paul Elliottc9774412023-02-06 15:14:07 +00003582 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003583}
3584
3585psa_status_t psa_verify_hash_complete(
3586 psa_verify_hash_interruptible_operation_t *operation)
3587{
3588 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3589
Paul Elliottc9774412023-02-06 15:14:07 +00003590 /* Check that start has been called first, and that operation has not
3591 * previously errored. */
3592 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003593 status = PSA_ERROR_BAD_STATE;
3594 goto exit;
3595 }
3596
3597 status = psa_driver_wrapper_verify_hash_complete(operation);
3598
Paul Elliott296ede92022-12-15 17:00:30 +00003599 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003600 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003601 operation);
3602
Paul Elliottebe225c2023-02-10 14:32:53 +00003603exit:
3604
Paul Elliott9fe12f62022-11-30 19:16:02 +00003605 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003606 if (status != PSA_SUCCESS) {
3607 operation->error_occurred = 1;
3608 }
3609
Paul Elliott59ad9452022-12-18 15:09:02 +00003610 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003611 }
3612
3613 return status;
3614}
3615
3616psa_status_t psa_verify_hash_abort(
3617 psa_verify_hash_interruptible_operation_t *operation)
3618{
Paul Elliott59ad9452022-12-18 15:09:02 +00003619 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003620
Paul Elliott59ad9452022-12-18 15:09:02 +00003621 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003622
Paul Elliott59ad9452022-12-18 15:09:02 +00003623 /* We clear the number of ops done here, so that it is not cleared when
3624 * the operation fails or succeeds, only on manual abort. */
3625 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003626
Paul Elliottc9774412023-02-06 15:14:07 +00003627 /* Likewise, failure state. */
3628 operation->error_occurred = 0;
3629
Paul Elliott59ad9452022-12-18 15:09:02 +00003630 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003631}
3632
Paul Elliott588f8ed2022-12-02 18:10:26 +00003633/****************************************************************/
3634/* Asymmetric interruptible cryptography internal */
3635/* implementations */
3636/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003637
Paul Elliott588f8ed2022-12-02 18:10:26 +00003638void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3639{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003640
Paul Elliott588f8ed2022-12-02 18:10:26 +00003641#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3642 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3643 defined(MBEDTLS_ECP_RESTARTABLE)
3644
3645 /* Internal implementation uses zero to indicate infinite number max ops,
3646 * therefore avoid this value, and set to minimum possible. */
3647 if (max_ops == 0) {
3648 max_ops = 1;
3649 }
3650
Paul Elliott588f8ed2022-12-02 18:10:26 +00003651 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003652#else
3653 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003654#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3655 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3656 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3657}
3658
Paul Elliott588f8ed2022-12-02 18:10:26 +00003659uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003660 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003661{
3662#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3663 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3664 defined(MBEDTLS_ECP_RESTARTABLE)
3665
Paul Elliott93d9ca82023-02-15 18:14:21 +00003666 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003667#else
3668 (void) operation;
3669 return 0;
3670#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3671 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3672 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3673}
3674
3675uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003676 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003677{
3678 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3679 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3680 defined(MBEDTLS_ECP_RESTARTABLE)
3681
Paul Elliott93d9ca82023-02-15 18:14:21 +00003682 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003683#else
3684 (void) operation;
3685 return 0;
3686#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3687 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3688 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3689}
3690
3691psa_status_t mbedtls_psa_sign_hash_start(
3692 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3693 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3694 size_t key_buffer_size, psa_algorithm_t alg,
3695 const uint8_t *hash, size_t hash_length)
3696{
3697 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003698 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003699
Paul Elliott068fe072023-01-16 13:59:15 +00003700 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3701 return PSA_ERROR_NOT_SUPPORTED;
3702 }
3703
3704 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003705 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003706 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003707
3708#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003709 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3710 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003711
Paul Elliotta1c94092023-02-15 16:38:04 +00003712 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3713
Paul Elliott93d9ca82023-02-15 18:14:21 +00003714 /* Ensure num_ops is zero'ed in case of context re-use. */
3715 operation->num_ops = 0;
3716
Paul Elliott068fe072023-01-16 13:59:15 +00003717 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3718 attributes->core.bits,
3719 key_buffer,
3720 key_buffer_size,
3721 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003722
Paul Elliott068fe072023-01-16 13:59:15 +00003723 if (status != PSA_SUCCESS) {
3724 return status;
3725 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003726
Paul Elliott1bc59df2023-02-05 13:41:57 +00003727 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003728 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003729
Paul Elliott068fe072023-01-16 13:59:15 +00003730 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3731 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3732 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003733
Paul Elliott0290a762023-02-09 14:30:24 +00003734 /* We only need to store the same length of hash as the private key size
3735 * here, it would be truncated by the internal implementation anyway. */
3736 required_hash_length = (hash_length < operation->coordinate_bytes ?
3737 hash_length : operation->coordinate_bytes);
3738
Paul Elliottba70ad42023-02-15 18:23:53 +00003739 if (required_hash_length > sizeof(operation->hash)) {
3740 /* Shouldn't happen, but better safe than sorry. */
3741 return PSA_ERROR_CORRUPTION_DETECTED;
3742 }
3743
Paul Elliott0290a762023-02-09 14:30:24 +00003744 memcpy(operation->hash, hash, required_hash_length);
3745 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003746
3747 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003748
3749#else
Paul Elliott068fe072023-01-16 13:59:15 +00003750 (void) operation;
3751 (void) key_buffer;
3752 (void) key_buffer_size;
3753 (void) alg;
3754 (void) hash;
3755 (void) hash_length;
3756 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003757 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003758
Paul Elliott068fe072023-01-16 13:59:15 +00003759 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003760#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3761 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3762 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003763}
3764
3765psa_status_t mbedtls_psa_sign_hash_complete(
3766 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3767 uint8_t *signature, size_t signature_size,
3768 size_t *signature_length)
3769{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003770#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3771 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3772 defined(MBEDTLS_ECP_RESTARTABLE)
3773
Paul Elliotta1c94092023-02-15 16:38:04 +00003774 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003775 mbedtls_mpi r;
3776 mbedtls_mpi s;
3777
Paul Elliott46845252023-02-03 14:59:11 +00003778 mbedtls_mpi_init(&r);
3779 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003780
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003781 /* Ensure max_ops is set to the current value (or default). */
3782 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3783
Paul Elliotta1c94092023-02-15 16:38:04 +00003784 if (signature_size < 2 * operation->coordinate_bytes) {
3785 status = PSA_ERROR_BUFFER_TOO_SMALL;
3786 goto exit;
3787 }
3788
Paul Elliott588f8ed2022-12-02 18:10:26 +00003789 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003790
Paul Elliott588f8ed2022-12-02 18:10:26 +00003791#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3792 status = mbedtls_to_psa_error(
3793 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003794 &r,
3795 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003796 &operation->ctx->d,
3797 operation->hash,
3798 operation->hash_length,
3799 operation->md_alg,
3800 mbedtls_psa_get_random,
3801 MBEDTLS_PSA_RANDOM_STATE,
3802 &operation->restart_ctx));
3803#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003804 status = PSA_ERROR_NOT_SUPPORTED;
3805 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003806#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3807 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003808 status = mbedtls_to_psa_error(
3809 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003810 &r,
3811 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003812 &operation->ctx->d,
3813 operation->hash,
3814 operation->hash_length,
3815 mbedtls_psa_get_random,
3816 MBEDTLS_PSA_RANDOM_STATE,
3817 mbedtls_psa_get_random,
3818 MBEDTLS_PSA_RANDOM_STATE,
3819 &operation->restart_ctx));
3820 }
3821
Paul Elliottf8e5b562023-02-19 18:43:45 +00003822 /* Hide the fact that the restart context only holds a delta of number of
3823 * ops done during the last operation, not an absolute value. */
3824 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3825
Paul Elliott724bd252023-02-08 12:35:08 +00003826 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003827 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003828 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003829 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003830 operation->coordinate_bytes)
3831 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003832
3833 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003834 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003835 }
3836
3837 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003838 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003839 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003840 operation->coordinate_bytes,
3841 operation->coordinate_bytes)
3842 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003843
3844 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003845 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003846 }
3847
Paul Elliott1bc59df2023-02-05 13:41:57 +00003848 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003849
Paul Elliott724bd252023-02-08 12:35:08 +00003850 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003851 }
Paul Elliott724bd252023-02-08 12:35:08 +00003852
3853exit:
3854
3855 mbedtls_mpi_free(&r);
3856 mbedtls_mpi_free(&s);
3857 return status;
3858
Paul Elliott588f8ed2022-12-02 18:10:26 +00003859 #else
3860
3861 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003862 (void) signature;
3863 (void) signature_size;
3864 (void) signature_length;
3865
3866 return PSA_ERROR_NOT_SUPPORTED;
3867
3868#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3869 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3870 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3871}
3872
3873psa_status_t mbedtls_psa_sign_hash_abort(
3874 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3875{
3876
3877#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3878 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3879 defined(MBEDTLS_ECP_RESTARTABLE)
3880
3881 if (operation->ctx) {
3882 mbedtls_ecdsa_free(operation->ctx);
3883 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003884 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003885 }
3886
3887 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3888
Paul Elliott93d9ca82023-02-15 18:14:21 +00003889 operation->num_ops = 0;
3890
Paul Elliott588f8ed2022-12-02 18:10:26 +00003891 return PSA_SUCCESS;
3892
3893#else
3894
3895 (void) operation;
3896
3897 return PSA_ERROR_NOT_SUPPORTED;
3898
3899#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3900 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3901 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3902}
3903
3904psa_status_t mbedtls_psa_verify_hash_start(
3905 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3906 const psa_key_attributes_t *attributes,
3907 const uint8_t *key_buffer, size_t key_buffer_size,
3908 psa_algorithm_t alg,
3909 const uint8_t *hash, size_t hash_length,
3910 const uint8_t *signature, size_t signature_length)
3911{
3912 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003913 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003914 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003915
Paul Elliott068fe072023-01-16 13:59:15 +00003916 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3917 return PSA_ERROR_NOT_SUPPORTED;
3918 }
3919
3920 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003921 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003922 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003923
3924#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003925 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3926 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003927
Paul Elliotta1c94092023-02-15 16:38:04 +00003928 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3929 mbedtls_mpi_init(&operation->r);
3930 mbedtls_mpi_init(&operation->s);
3931
Paul Elliott93d9ca82023-02-15 18:14:21 +00003932 /* Ensure num_ops is zero'ed in case of context re-use. */
3933 operation->num_ops = 0;
3934
Paul Elliott068fe072023-01-16 13:59:15 +00003935 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3936 attributes->core.bits,
3937 key_buffer,
3938 key_buffer_size,
3939 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003940
Paul Elliott068fe072023-01-16 13:59:15 +00003941 if (status != PSA_SUCCESS) {
3942 return status;
3943 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003944
Paul Elliottc569fc22023-02-10 13:02:54 +00003945 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003946
Paul Elliott1bc59df2023-02-05 13:41:57 +00003947 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003948 return PSA_ERROR_INVALID_SIGNATURE;
3949 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003950
Paul Elliott068fe072023-01-16 13:59:15 +00003951 status = mbedtls_to_psa_error(
3952 mbedtls_mpi_read_binary(&operation->r,
3953 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003954 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003955
Paul Elliott068fe072023-01-16 13:59:15 +00003956 if (status != PSA_SUCCESS) {
3957 return status;
3958 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003959
Paul Elliott068fe072023-01-16 13:59:15 +00003960 status = mbedtls_to_psa_error(
3961 mbedtls_mpi_read_binary(&operation->s,
3962 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003963 coordinate_bytes,
3964 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003965
Paul Elliott068fe072023-01-16 13:59:15 +00003966 if (status != PSA_SUCCESS) {
3967 return status;
3968 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003969
Paul Elliott2c9843f2023-02-15 17:32:42 +00003970 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003971
Paul Elliott2c9843f2023-02-15 17:32:42 +00003972 if (status != PSA_SUCCESS) {
3973 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003974 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003975
Paul Elliott0290a762023-02-09 14:30:24 +00003976 /* We only need to store the same length of hash as the private key size
3977 * here, it would be truncated by the internal implementation anyway. */
3978 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3979 coordinate_bytes);
3980
Paul Elliottba70ad42023-02-15 18:23:53 +00003981 if (required_hash_length > sizeof(operation->hash)) {
3982 /* Shouldn't happen, but better safe than sorry. */
3983 return PSA_ERROR_CORRUPTION_DETECTED;
3984 }
3985
Paul Elliott0290a762023-02-09 14:30:24 +00003986 memcpy(operation->hash, hash, required_hash_length);
3987 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003988
3989 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003990#else
Paul Elliott068fe072023-01-16 13:59:15 +00003991 (void) operation;
3992 (void) key_buffer;
3993 (void) key_buffer_size;
3994 (void) alg;
3995 (void) hash;
3996 (void) hash_length;
3997 (void) signature;
3998 (void) signature_length;
3999 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00004000 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00004001 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004002
Paul Elliott068fe072023-01-16 13:59:15 +00004003 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004004#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4005 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4006 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004007}
4008
4009psa_status_t mbedtls_psa_verify_hash_complete(
4010 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4011{
4012
4013#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4014 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4015 defined(MBEDTLS_ECP_RESTARTABLE)
4016
Paul Elliottf8e5b562023-02-19 18:43:45 +00004017 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4018
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004019 /* Ensure max_ops is set to the current value (or default). */
4020 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4021
Paul Elliottf8e5b562023-02-19 18:43:45 +00004022 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004023 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4024 operation->hash,
4025 operation->hash_length,
4026 &operation->ctx->Q,
4027 &operation->r,
4028 &operation->s,
4029 &operation->restart_ctx));
4030
Paul Elliottf8e5b562023-02-19 18:43:45 +00004031 /* Hide the fact that the restart context only holds a delta of number of
4032 * ops done during the last operation, not an absolute value. */
4033 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4034
4035 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004036#else
4037 (void) operation;
4038
4039 return PSA_ERROR_NOT_SUPPORTED;
4040
4041#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4042 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4043 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4044}
4045
4046psa_status_t mbedtls_psa_verify_hash_abort(
4047 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4048{
4049
4050#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4051 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4052 defined(MBEDTLS_ECP_RESTARTABLE)
4053
4054 if (operation->ctx) {
4055 mbedtls_ecdsa_free(operation->ctx);
4056 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004057 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004058 }
4059
4060 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4061
Paul Elliott93d9ca82023-02-15 18:14:21 +00004062 operation->num_ops = 0;
4063
Paul Elliott588f8ed2022-12-02 18:10:26 +00004064 mbedtls_mpi_free(&operation->r);
4065 mbedtls_mpi_free(&operation->s);
4066
4067 return PSA_SUCCESS;
4068
4069#else
4070 (void) operation;
4071
4072 return PSA_ERROR_NOT_SUPPORTED;
4073
4074#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4075 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4076 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4077}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004078
mohammad1603503973b2018-03-12 15:59:30 +02004079/****************************************************************/
4080/* Symmetric cryptography */
4081/****************************************************************/
4082
Gilles Peskine449bd832023-01-11 14:50:10 +01004083static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4084 mbedtls_svc_key_id_t key,
4085 psa_algorithm_t alg,
4086 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004087{
4088 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4089 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004090 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4092 PSA_KEY_USAGE_ENCRYPT :
4093 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02004094
4095 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004096 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004097 status = PSA_ERROR_BAD_STATE;
4098 goto exit;
4099 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004100
Gilles Peskine449bd832023-01-11 14:50:10 +01004101 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004102 status = PSA_ERROR_INVALID_ARGUMENT;
4103 goto exit;
4104 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004105
Gilles Peskine449bd832023-01-11 14:50:10 +01004106 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4107 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004108 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004109 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004110
Ronald Cron49fafa92021-03-10 08:34:23 +01004111 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004112 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004113 * so we only set it (in the driver wrapper) after resources have been
4114 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004115 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004116 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004117 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004119 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004120 }
4121 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004122
Ronald Crona4af55f2020-12-14 14:36:06 +01004123 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004125 };
4126
Ronald Cronab99ac22020-10-01 16:38:28 +02004127 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004128 if (cipher_operation == MBEDTLS_ENCRYPT) {
4129 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4130 &attributes,
4131 slot->key.data,
4132 slot->key.bytes,
4133 alg);
4134 } else {
4135 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4136 &attributes,
4137 slot->key.data,
4138 slot->key.bytes,
4139 alg);
4140 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004141
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004142exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004143 if (status != PSA_SUCCESS) {
4144 psa_cipher_abort(operation);
4145 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004146
Gilles Peskine449bd832023-01-11 14:50:10 +01004147 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004148
Gilles Peskine449bd832023-01-11 14:50:10 +01004149 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004150}
4151
Gilles Peskine449bd832023-01-11 14:50:10 +01004152psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4153 mbedtls_svc_key_id_t key,
4154 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004155{
Gilles Peskine449bd832023-01-11 14:50:10 +01004156 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004157}
4158
Gilles Peskine449bd832023-01-11 14:50:10 +01004159psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4160 mbedtls_svc_key_id_t key,
4161 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004162{
Gilles Peskine449bd832023-01-11 14:50:10 +01004163 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004164}
4165
Gilles Peskine449bd832023-01-11 14:50:10 +01004166psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4167 uint8_t *iv,
4168 size_t iv_size,
4169 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004170{
Ronald Cron6d051732020-10-01 14:10:20 +02004171 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004172 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4173 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004174
Gilles Peskine449bd832023-01-11 14:50:10 +01004175 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004176 status = PSA_ERROR_BAD_STATE;
4177 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004178 }
4179
Gilles Peskine449bd832023-01-11 14:50:10 +01004180 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004181 status = PSA_ERROR_BAD_STATE;
4182 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004183 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004184
Ronald Cron2fb90522021-07-05 12:31:44 +02004185 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004186 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004187 status = PSA_ERROR_BUFFER_TOO_SMALL;
4188 goto exit;
4189 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004190
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004192 status = PSA_ERROR_GENERIC_ERROR;
4193 goto exit;
4194 }
4195
Gilles Peskine449bd832023-01-11 14:50:10 +01004196 status = psa_generate_random(local_iv, default_iv_length);
4197 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004198 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 }
Ronald Cron5618a392021-03-26 09:52:26 +01004200
Gilles Peskine449bd832023-01-11 14:50:10 +01004201 status = psa_driver_wrapper_cipher_set_iv(operation,
4202 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004203
4204exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004205 if (status == PSA_SUCCESS) {
4206 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004207 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004208 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004210 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004211 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004212 }
Ronald Cron6d051732020-10-01 14:10:20 +02004213
Gilles Peskine449bd832023-01-11 14:50:10 +01004214 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004215}
4216
Gilles Peskine449bd832023-01-11 14:50:10 +01004217psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4218 const uint8_t *iv,
4219 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004220{
Ronald Cron6d051732020-10-01 14:10:20 +02004221 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004222
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004224 status = PSA_ERROR_BAD_STATE;
4225 goto exit;
4226 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004227
Gilles Peskine449bd832023-01-11 14:50:10 +01004228 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004229 status = PSA_ERROR_BAD_STATE;
4230 goto exit;
4231 }
Ronald Crona0d68172021-03-26 10:15:08 +01004232
Gilles Peskine449bd832023-01-11 14:50:10 +01004233 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004234 status = PSA_ERROR_INVALID_ARGUMENT;
4235 goto exit;
4236 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004237
Gilles Peskine449bd832023-01-11 14:50:10 +01004238 status = psa_driver_wrapper_cipher_set_iv(operation,
4239 iv,
4240 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004241
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004242exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004243 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004244 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004245 } else {
4246 psa_cipher_abort(operation);
4247 }
4248 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004249}
4250
Gilles Peskine449bd832023-01-11 14:50:10 +01004251psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4252 const uint8_t *input,
4253 size_t input_length,
4254 uint8_t *output,
4255 size_t output_size,
4256 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004257{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004258 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004259
Gilles Peskine449bd832023-01-11 14:50:10 +01004260 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004261 status = PSA_ERROR_BAD_STATE;
4262 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004263 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004264
Gilles Peskine449bd832023-01-11 14:50:10 +01004265 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004266 status = PSA_ERROR_BAD_STATE;
4267 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004268 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004269
Gilles Peskine449bd832023-01-11 14:50:10 +01004270 status = psa_driver_wrapper_cipher_update(operation,
4271 input,
4272 input_length,
4273 output,
4274 output_size,
4275 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004276
4277exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004278 if (status != PSA_SUCCESS) {
4279 psa_cipher_abort(operation);
4280 }
Ronald Cron6d051732020-10-01 14:10:20 +02004281
Gilles Peskine449bd832023-01-11 14:50:10 +01004282 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004283}
4284
Gilles Peskine449bd832023-01-11 14:50:10 +01004285psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4286 uint8_t *output,
4287 size_t output_size,
4288 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004289{
David Saadab4ecc272019-02-14 13:48:10 +02004290 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004291
Gilles Peskine449bd832023-01-11 14:50:10 +01004292 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004293 status = PSA_ERROR_BAD_STATE;
4294 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004295 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004296
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004298 status = PSA_ERROR_BAD_STATE;
4299 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004300 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004301
Gilles Peskine449bd832023-01-11 14:50:10 +01004302 status = psa_driver_wrapper_cipher_finish(operation,
4303 output,
4304 output_size,
4305 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004306
4307exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004308 if (status == PSA_SUCCESS) {
4309 return psa_cipher_abort(operation);
4310 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004311 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004313
Gilles Peskine449bd832023-01-11 14:50:10 +01004314 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004315 }
mohammad1603503973b2018-03-12 15:59:30 +02004316}
4317
Gilles Peskine449bd832023-01-11 14:50:10 +01004318psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004319{
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004321 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004322 * in use. It's ok to call abort on such an object, and there's
4323 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004324 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004325 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004326
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004328
Ronald Cron49fafa92021-03-10 08:34:23 +01004329 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004330 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004331 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004332
Gilles Peskine449bd832023-01-11 14:50:10 +01004333 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004334}
4335
Gilles Peskine449bd832023-01-11 14:50:10 +01004336psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4337 psa_algorithm_t alg,
4338 const uint8_t *input,
4339 size_t input_length,
4340 uint8_t *output,
4341 size_t output_size,
4342 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004343{
4344 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004345 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004346 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004347 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4348 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004349
Gilles Peskine449bd832023-01-11 14:50:10 +01004350 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004351 status = PSA_ERROR_INVALID_ARGUMENT;
4352 goto exit;
4353 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004354
Gilles Peskine449bd832023-01-11 14:50:10 +01004355 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4356 PSA_KEY_USAGE_ENCRYPT,
4357 alg);
4358 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004359 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004361
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004362 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004364 };
4365
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4367 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004368 status = PSA_ERROR_GENERIC_ERROR;
4369 goto exit;
4370 }
4371
Gilles Peskine449bd832023-01-11 14:50:10 +01004372 if (default_iv_length > 0) {
4373 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004374 status = PSA_ERROR_BUFFER_TOO_SMALL;
4375 goto exit;
4376 }
4377
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 status = psa_generate_random(local_iv, default_iv_length);
4379 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004380 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004381 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004382 }
4383
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004384 status = psa_driver_wrapper_cipher_encrypt(
4385 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004386 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004387 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004388 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004389
4390exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004391 unlock_status = psa_unlock_key_slot(slot);
4392 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004393 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004394 }
Ronald Cron23919522021-07-08 19:00:07 +02004395
Gilles Peskine449bd832023-01-11 14:50:10 +01004396 if (status == PSA_SUCCESS) {
4397 if (default_iv_length > 0) {
4398 memcpy(output, local_iv, default_iv_length);
4399 }
4400 *output_length += default_iv_length;
4401 } else {
4402 *output_length = 0;
4403 }
4404
4405 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004406}
4407
Gilles Peskine449bd832023-01-11 14:50:10 +01004408psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4409 psa_algorithm_t alg,
4410 const uint8_t *input,
4411 size_t input_length,
4412 uint8_t *output,
4413 size_t output_size,
4414 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004415{
4416 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004417 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004418 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004419
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004421 status = PSA_ERROR_INVALID_ARGUMENT;
4422 goto exit;
4423 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004424
Gilles Peskine449bd832023-01-11 14:50:10 +01004425 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4426 PSA_KEY_USAGE_DECRYPT,
4427 alg);
4428 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004429 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004431
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004432 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004434 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004435
Gilles Peskine449bd832023-01-11 14:50:10 +01004436 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4437 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004438 status = PSA_ERROR_INVALID_ARGUMENT;
4439 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004441 status = PSA_ERROR_INVALID_ARGUMENT;
4442 goto exit;
4443 }
4444
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004445 status = psa_driver_wrapper_cipher_decrypt(
4446 &attributes, slot->key.data, slot->key.bytes,
4447 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004449
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004450exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 unlock_status = psa_unlock_key_slot(slot);
4452 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004453 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004455
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004457 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004458 }
Ronald Cron23919522021-07-08 19:00:07 +02004459
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004461}
4462
4463
mohammad16035955c982018-04-26 00:53:03 +03004464/****************************************************************/
4465/* AEAD */
4466/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004467
Paul Elliottbaff51c2021-09-28 17:44:45 +01004468/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004469static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004470{
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004472}
4473
4474/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004475static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4476 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004477{
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004479
Gilles Peskine449bd832023-01-11 14:50:10 +01004480 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004481#if defined(PSA_WANT_ALG_GCM)
4482 case PSA_ALG_GCM:
4483 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 * arbitrarily large nonces. Please note that we do not generally
4485 * recommend the usage of nonces of greater length than
4486 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4487 * size, which can then lead to collisions if you encrypt a very
4488 * large number of messages.*/
4489 if (nonce_length != 0) {
4490 return PSA_SUCCESS;
4491 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004492 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004493#endif /* PSA_WANT_ALG_GCM */
4494#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004495 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004496 if (nonce_length >= 7 && nonce_length <= 13) {
4497 return PSA_SUCCESS;
4498 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004499 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004500#endif /* PSA_WANT_ALG_CCM */
4501#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004502 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 if (nonce_length == 12) {
4504 return PSA_SUCCESS;
4505 } else if (nonce_length == 8) {
4506 return PSA_ERROR_NOT_SUPPORTED;
4507 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004508 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004509#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004510 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004511 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004513 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004514
Gilles Peskine449bd832023-01-11 14:50:10 +01004515 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004516}
4517
Gilles Peskine449bd832023-01-11 14:50:10 +01004518static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004519{
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4521 return PSA_ERROR_INVALID_ARGUMENT;
4522 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004523
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004525}
4526
Gilles Peskine449bd832023-01-11 14:50:10 +01004527psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4528 psa_algorithm_t alg,
4529 const uint8_t *nonce,
4530 size_t nonce_length,
4531 const uint8_t *additional_data,
4532 size_t additional_data_length,
4533 const uint8_t *plaintext,
4534 size_t plaintext_length,
4535 uint8_t *ciphertext,
4536 size_t ciphertext_size,
4537 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004538{
Ronald Cron215633c2021-03-16 17:15:37 +01004539 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4540 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004541
mohammad1603f08a5502018-06-03 15:05:47 +03004542 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004543
Gilles Peskine449bd832023-01-11 14:50:10 +01004544 status = psa_aead_check_algorithm(alg);
4545 if (status != PSA_SUCCESS) {
4546 return status;
4547 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004548
Ronald Cron9a986162021-03-26 12:40:07 +01004549 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4551 if (status != PSA_SUCCESS) {
4552 return status;
4553 }
mohammad16035955c982018-04-26 00:53:03 +03004554
Ronald Cron215633c2021-03-16 17:15:37 +01004555 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004557 };
mohammad16035955c982018-04-26 00:53:03 +03004558
Gilles Peskine449bd832023-01-11 14:50:10 +01004559 status = psa_aead_check_nonce_length(alg, nonce_length);
4560 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004561 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004563
Ronald Cronde822812021-03-17 16:08:20 +01004564 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004565 &attributes, slot->key.data, slot->key.bytes,
4566 alg,
4567 nonce, nonce_length,
4568 additional_data, additional_data_length,
4569 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004571
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4573 memset(ciphertext, 0, ciphertext_size);
4574 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004575
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004576exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004578
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004580}
4581
Gilles Peskine449bd832023-01-11 14:50:10 +01004582psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4583 psa_algorithm_t alg,
4584 const uint8_t *nonce,
4585 size_t nonce_length,
4586 const uint8_t *additional_data,
4587 size_t additional_data_length,
4588 const uint8_t *ciphertext,
4589 size_t ciphertext_length,
4590 uint8_t *plaintext,
4591 size_t plaintext_size,
4592 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004593{
Ronald Cron215633c2021-03-16 17:15:37 +01004594 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4595 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004596
Gilles Peskineee652a32018-06-01 19:23:52 +02004597 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004598
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 status = psa_aead_check_algorithm(alg);
4600 if (status != PSA_SUCCESS) {
4601 return status;
4602 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004603
Ronald Cron9a986162021-03-26 12:40:07 +01004604 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4606 if (status != PSA_SUCCESS) {
4607 return status;
4608 }
mohammad16035955c982018-04-26 00:53:03 +03004609
Ronald Cron215633c2021-03-16 17:15:37 +01004610 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004612 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004613
Gilles Peskine449bd832023-01-11 14:50:10 +01004614 status = psa_aead_check_nonce_length(alg, nonce_length);
4615 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004616 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004617 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004618
Ronald Cronde822812021-03-17 16:08:20 +01004619 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004620 &attributes, slot->key.data, slot->key.bytes,
4621 alg,
4622 nonce, nonce_length,
4623 additional_data, additional_data_length,
4624 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004626
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 if (status != PSA_SUCCESS && plaintext_size != 0) {
4628 memset(plaintext, 0, plaintext_size);
4629 }
mohammad160360a64d02018-06-03 17:20:42 +03004630
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004631exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004633
Gilles Peskine449bd832023-01-11 14:50:10 +01004634 return status;
mohammad16035955c982018-04-26 00:53:03 +03004635}
4636
Gilles Peskine449bd832023-01-11 14:50:10 +01004637static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4638{
4639 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004640
Gilles Peskine449bd832023-01-11 14:50:10 +01004641 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004642#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004643 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004644 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004645 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4646 return PSA_ERROR_INVALID_ARGUMENT;
4647 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004648 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004649#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004650
Przemek Stekiel86679c72022-10-06 17:06:56 +02004651#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004653 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4655 return PSA_ERROR_INVALID_ARGUMENT;
4656 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004657 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004658#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004659
Przemek Stekiel86679c72022-10-06 17:06:56 +02004660#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004662 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004663 if (tag_len != 16) {
4664 return PSA_ERROR_INVALID_ARGUMENT;
4665 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004666 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004667#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004668
4669 default:
4670 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004672 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004674}
4675
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004676/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004677static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4678 int is_encrypt,
4679 mbedtls_svc_key_id_t key,
4680 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004681{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004682 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4683 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4684 psa_key_slot_t *slot = NULL;
4685 psa_key_usage_t key_usage = 0;
4686
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 status = psa_aead_check_algorithm(alg);
4688 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004689 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004691
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004693 status = PSA_ERROR_BAD_STATE;
4694 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004695 }
4696
Gilles Peskine449bd832023-01-11 14:50:10 +01004697 if (operation->nonce_set || operation->lengths_set ||
4698 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004699 status = PSA_ERROR_BAD_STATE;
4700 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004701 }
4702
Gilles Peskine449bd832023-01-11 14:50:10 +01004703 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004704 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004706 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004707 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004708
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4710 alg);
4711 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004712 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004713 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004714
4715 psa_key_attributes_t attributes = {
4716 .core = slot->attr
4717 };
4718
Gilles Peskine449bd832023-01-11 14:50:10 +01004719 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004720 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004722
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 if (is_encrypt) {
4724 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4725 &attributes,
4726 slot->key.data,
4727 slot->key.bytes,
4728 alg);
4729 } else {
4730 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4731 &attributes,
4732 slot->key.data,
4733 slot->key.bytes,
4734 alg);
4735 }
4736 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004737 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004739
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004741
4742exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004743 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004744
Gilles Peskine449bd832023-01-11 14:50:10 +01004745 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004746 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004747 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004748 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 } else {
4750 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004751 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004752
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004754}
4755
Paul Elliott302ff6b2021-04-20 18:10:30 +01004756/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004757psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4758 mbedtls_svc_key_id_t key,
4759 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004760{
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004762}
4763
4764/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004765psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4766 mbedtls_svc_key_id_t key,
4767 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004768{
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004770}
4771
4772/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004773psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4774 uint8_t *nonce,
4775 size_t nonce_size,
4776 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004777{
4778 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004779 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004780 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004781
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004782 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004783
Gilles Peskine449bd832023-01-11 14:50:10 +01004784 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004785 status = PSA_ERROR_BAD_STATE;
4786 goto exit;
4787 }
4788
Gilles Peskine449bd832023-01-11 14:50:10 +01004789 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004790 status = PSA_ERROR_BAD_STATE;
4791 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004792 }
4793
Paul Elliotte193ea82021-10-01 13:00:16 +01004794 /* For CCM, this size may not be correct according to the PSA
4795 * specification. The PSA Crypto 1.0.1 specification states:
4796 *
4797 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4798 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4799 *
4800 * However this restriction that L has to be the smallest integer is not
4801 * applied in practice, and it is not implementable here since the
4802 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4804 operation->alg);
4805 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004806 status = PSA_ERROR_BUFFER_TOO_SMALL;
4807 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004808 }
4809
Gilles Peskine449bd832023-01-11 14:50:10 +01004810 status = psa_generate_random(local_nonce, required_nonce_size);
4811 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004812 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004813 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004814
Gilles Peskine449bd832023-01-11 14:50:10 +01004815 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004816
Paul Elliott39dc6b82021-05-11 19:16:09 +01004817exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 if (status == PSA_SUCCESS) {
4819 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004820 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 } else {
4822 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004823 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004824
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004826}
4827
4828/* Set the nonce for a multipart authenticated encryption or decryption
4829 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004830psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4831 const uint8_t *nonce,
4832 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004833{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004834 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4835
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004837 status = PSA_ERROR_BAD_STATE;
4838 goto exit;
4839 }
4840
Gilles Peskine449bd832023-01-11 14:50:10 +01004841 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004842 status = PSA_ERROR_BAD_STATE;
4843 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004844 }
4845
Gilles Peskine449bd832023-01-11 14:50:10 +01004846 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4847 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004848 status = PSA_ERROR_INVALID_ARGUMENT;
4849 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004850 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004851
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4853 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004854
Paul Elliott39dc6b82021-05-11 19:16:09 +01004855exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004856 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004857 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004858 } else {
4859 psa_aead_abort(operation);
4860 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004861
Gilles Peskine449bd832023-01-11 14:50:10 +01004862 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004863}
4864
4865/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004866psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4867 size_t ad_length,
4868 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004869{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004870 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4871
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004873 status = PSA_ERROR_BAD_STATE;
4874 goto exit;
4875 }
4876
Gilles Peskine449bd832023-01-11 14:50:10 +01004877 if (operation->lengths_set || operation->ad_started ||
4878 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004879 status = PSA_ERROR_BAD_STATE;
4880 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004881 }
4882
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004884#if defined(PSA_WANT_ALG_GCM)
4885 case PSA_ALG_GCM:
4886 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 * bits. Without the guard this code will generate warnings on 32bit
4888 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004889#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004890 if (((uint64_t) ad_length) >> 61 != 0 ||
4891 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004892 status = PSA_ERROR_INVALID_ARGUMENT;
4893 goto exit;
4894 }
Paul Elliott325d3742021-09-27 17:56:28 +01004895#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004896 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004897#endif /* PSA_WANT_ALG_GCM */
4898#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004899 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004900 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004901 status = PSA_ERROR_INVALID_ARGUMENT;
4902 goto exit;
4903 }
4904 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004905#endif /* PSA_WANT_ALG_CCM */
4906#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004907 case PSA_ALG_CHACHA20_POLY1305:
4908 /* No length restrictions for ChaChaPoly. */
4909 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004910#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004911 default:
4912 break;
4913 }
Paul Elliott325d3742021-09-27 17:56:28 +01004914
Gilles Peskine449bd832023-01-11 14:50:10 +01004915 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4916 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004917
Paul Elliott39dc6b82021-05-11 19:16:09 +01004918exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004919 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004920 operation->ad_remaining = ad_length;
4921 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004922 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004923 } else {
4924 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004925 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004926
Gilles Peskine449bd832023-01-11 14:50:10 +01004927 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004928}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004929
4930/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004931psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4932 const uint8_t *input,
4933 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004934{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004935 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4936
Gilles Peskine449bd832023-01-11 14:50:10 +01004937 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004938 status = PSA_ERROR_BAD_STATE;
4939 goto exit;
4940 }
4941
Gilles Peskine449bd832023-01-11 14:50:10 +01004942 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004943 status = PSA_ERROR_BAD_STATE;
4944 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004945 }
4946
Gilles Peskine449bd832023-01-11 14:50:10 +01004947 if (operation->lengths_set) {
4948 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004949 status = PSA_ERROR_INVALID_ARGUMENT;
4950 goto exit;
4951 }
4952
4953 operation->ad_remaining -= input_length;
4954 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004955#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004956 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004957 status = PSA_ERROR_BAD_STATE;
4958 goto exit;
4959 }
4960#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004961
Gilles Peskine449bd832023-01-11 14:50:10 +01004962 status = psa_driver_wrapper_aead_update_ad(operation, input,
4963 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004964
Paul Elliott39dc6b82021-05-11 19:16:09 +01004965exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004966 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004967 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 } else {
4969 psa_aead_abort(operation);
4970 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004971
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004973}
4974
4975/* Encrypt or decrypt a message fragment in an active multipart AEAD
4976 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004977psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4978 const uint8_t *input,
4979 size_t input_length,
4980 uint8_t *output,
4981 size_t output_size,
4982 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004983{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004984 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004985
4986 *output_length = 0;
4987
Gilles Peskine449bd832023-01-11 14:50:10 +01004988 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004989 status = PSA_ERROR_BAD_STATE;
4990 goto exit;
4991 }
4992
Gilles Peskine449bd832023-01-11 14:50:10 +01004993 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004994 status = PSA_ERROR_BAD_STATE;
4995 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004996 }
4997
Gilles Peskine449bd832023-01-11 14:50:10 +01004998 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004999 /* Additional data length was supplied, but not all the additional
5000 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005001 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005002 status = PSA_ERROR_INVALID_ARGUMENT;
5003 goto exit;
5004 }
5005
5006 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005007 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005008 status = PSA_ERROR_INVALID_ARGUMENT;
5009 goto exit;
5010 }
5011
5012 operation->body_remaining -= input_length;
5013 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005014#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005015 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005016 status = PSA_ERROR_BAD_STATE;
5017 goto exit;
5018 }
5019#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005020
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5022 output, output_size,
5023 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005024
Paul Elliott39dc6b82021-05-11 19:16:09 +01005025exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005026 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005027 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005028 } else {
5029 psa_aead_abort(operation);
5030 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005031
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005033}
5034
Gilles Peskine449bd832023-01-11 14:50:10 +01005035static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005036{
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 if (operation->id == 0 || !operation->nonce_set) {
5038 return PSA_ERROR_BAD_STATE;
5039 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005040
Gilles Peskine449bd832023-01-11 14:50:10 +01005041 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5042 operation->body_remaining != 0)) {
5043 return PSA_ERROR_INVALID_ARGUMENT;
5044 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005045
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005047}
5048
Paul Elliott302ff6b2021-04-20 18:10:30 +01005049/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005050psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5051 uint8_t *ciphertext,
5052 size_t ciphertext_size,
5053 size_t *ciphertext_length,
5054 uint8_t *tag,
5055 size_t tag_size,
5056 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005057{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005058 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5059
Paul Elliott302ff6b2021-04-20 18:10:30 +01005060 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005061 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005062
Gilles Peskine449bd832023-01-11 14:50:10 +01005063 status = psa_aead_final_checks(operation);
5064 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005065 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005067
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005069 status = PSA_ERROR_BAD_STATE;
5070 goto exit;
5071 }
5072
Gilles Peskine449bd832023-01-11 14:50:10 +01005073 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5074 ciphertext_size,
5075 ciphertext_length,
5076 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005077
Paul Elliott39dc6b82021-05-11 19:16:09 +01005078exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005079
5080
Paul Elliottf47b0952021-05-21 18:02:33 +01005081 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005082 * the zero tag size, make sure the tag is set to something implausible.
5083 * Even if the operation succeeds, make sure we clear the rest of the
5084 * buffer to prevent potential leakage of anything previously placed in
5085 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005086 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005087
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005089
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005091}
5092
5093/* Finish authenticating and decrypting a message in a multipart AEAD
5094 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005095psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5096 uint8_t *plaintext,
5097 size_t plaintext_size,
5098 size_t *plaintext_length,
5099 const uint8_t *tag,
5100 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005101{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005102 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5103
Paul Elliott302ff6b2021-04-20 18:10:30 +01005104 *plaintext_length = 0;
5105
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 status = psa_aead_final_checks(operation);
5107 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005108 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005110
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005112 status = PSA_ERROR_BAD_STATE;
5113 goto exit;
5114 }
5115
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5117 plaintext_size,
5118 plaintext_length,
5119 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005120
5121exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005123
Gilles Peskine449bd832023-01-11 14:50:10 +01005124 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005125}
5126
5127/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005128psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005129{
5130 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5131
Gilles Peskine449bd832023-01-11 14:50:10 +01005132 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005133 /* The object has (apparently) been initialized but it is not (yet)
5134 * in use. It's ok to call abort on such an object, and there's
5135 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005137 }
5138
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005140
Gilles Peskine449bd832023-01-11 14:50:10 +01005141 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005142
Gilles Peskine449bd832023-01-11 14:50:10 +01005143 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005144}
5145
Gilles Peskinee59236f2018-01-27 23:32:46 +01005146/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005147/* Generators */
5148/****************************************************************/
5149
Przemek Stekiel69c46792022-06-10 12:59:51 +02005150#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005151 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005152 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
5153 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08005154#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005155#endif /* At least one builtin KDF */
5156
Przemek Stekiel69c46792022-06-10 12:59:51 +02005157#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005158 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5159 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5160static psa_status_t psa_key_derivation_start_hmac(
5161 psa_mac_operation_t *operation,
5162 psa_algorithm_t hash_alg,
5163 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005165{
5166 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5167 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005168 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5169 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5170 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005171
Steven Cooreman72f736a2021-05-07 14:14:37 +02005172 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005174
Gilles Peskine449bd832023-01-11 14:50:10 +01005175 status = psa_driver_wrapper_mac_sign_setup(operation,
5176 &attributes,
5177 hmac_key, hmac_key_length,
5178 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005179
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 psa_reset_key_attributes(&attributes);
5181 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005182}
5183#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005184
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005185#define HKDF_STATE_INIT 0 /* no input yet */
5186#define HKDF_STATE_STARTED 1 /* got salt */
5187#define HKDF_STATE_KEYED 2 /* got key */
5188#define HKDF_STATE_OUTPUT 3 /* output started */
5189
Gilles Peskinecbe66502019-05-16 16:59:18 +02005190static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005191 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005192{
Gilles Peskine449bd832023-01-11 14:50:10 +01005193 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5194 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5195 } else {
5196 return operation->alg;
5197 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005198}
5199
Gilles Peskine449bd832023-01-11 14:50:10 +01005200psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005201{
5202 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5204 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005205 /* The object has (apparently) been initialized but it is not
5206 * in use. It's ok to call abort on such an object, and there's
5207 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005208 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005209#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5211 mbedtls_free(operation->ctx.hkdf.info);
5212 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5213 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005214#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005215#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5216 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005217 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5218 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5219 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5220 if (operation->ctx.tls12_prf.secret != NULL) {
5221 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5222 operation->ctx.tls12_prf.secret_length);
5223 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005224 }
5225
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 if (operation->ctx.tls12_prf.seed != NULL) {
5227 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5228 operation->ctx.tls12_prf.seed_length);
5229 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005230 }
5231
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 if (operation->ctx.tls12_prf.label != NULL) {
5233 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5234 operation->ctx.tls12_prf.label_length);
5235 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005236 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005237#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005238 if (operation->ctx.tls12_prf.other_secret != NULL) {
5239 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5240 operation->ctx.tls12_prf.other_secret_length);
5241 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005242 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005243#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005244 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005245
5246 /* We leave the fields Ai and output_block to be erased safely by the
5247 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005248 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005249#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5250 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005251#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005252 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5253 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5254 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5255 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005256#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005257 {
5258 status = PSA_ERROR_BAD_STATE;
5259 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005260 mbedtls_platform_zeroize(operation, sizeof(*operation));
5261 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005262}
5263
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005264psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005266{
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005268 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005269 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005270 }
5271
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005272 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005274}
5275
Gilles Peskine449bd832023-01-11 14:50:10 +01005276psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5277 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005278{
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 if (operation->alg == 0) {
5280 return PSA_ERROR_BAD_STATE;
5281 }
5282 if (capacity > operation->capacity) {
5283 return PSA_ERROR_INVALID_ARGUMENT;
5284 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005285 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005287}
5288
Przemek Stekiel69c46792022-06-10 12:59:51 +02005289#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005290/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005291static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5292 psa_algorithm_t kdf_alg,
5293 uint8_t *output,
5294 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005295{
Gilles Peskine449bd832023-01-11 14:50:10 +01005296 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5297 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005298 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005299 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005300#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005302#else
5303 const uint8_t last_block = 0xff;
5304#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005305
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 if (hkdf->state < HKDF_STATE_KEYED ||
5307 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005308#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005309 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005310#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 )) {
5312 return PSA_ERROR_BAD_STATE;
5313 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005314 hkdf->state = HKDF_STATE_OUTPUT;
5315
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005317 /* Copy what remains of the current block */
5318 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005320 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005321 }
5322 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005323 output += n;
5324 output_length -= n;
5325 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005327 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005329 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005330 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005331 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005332 * object was corrupted or if this function is called directly
5333 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 if (hkdf->block_number == last_block) {
5335 return PSA_ERROR_BAD_STATE;
5336 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005337
5338 /* We need a new block */
5339 ++hkdf->block_number;
5340 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005341
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5343 hash_alg,
5344 hkdf->prk,
5345 hash_length);
5346 if (status != PSA_SUCCESS) {
5347 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005348 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005349
5350 if (hkdf->block_number != 1) {
5351 status = psa_mac_update(&hkdf->hmac,
5352 hkdf->output_block,
5353 hash_length);
5354 if (status != PSA_SUCCESS) {
5355 return status;
5356 }
5357 }
5358 status = psa_mac_update(&hkdf->hmac,
5359 hkdf->info,
5360 hkdf->info_length);
5361 if (status != PSA_SUCCESS) {
5362 return status;
5363 }
5364 status = psa_mac_update(&hkdf->hmac,
5365 &hkdf->block_number, 1);
5366 if (status != PSA_SUCCESS) {
5367 return status;
5368 }
5369 status = psa_mac_sign_finish(&hkdf->hmac,
5370 hkdf->output_block,
5371 sizeof(hkdf->output_block),
5372 &hmac_output_length);
5373 if (status != PSA_SUCCESS) {
5374 return status;
5375 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005376 }
5377
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005379}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005380#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005381
John Durkop07cc04a2020-11-16 22:08:34 -08005382#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5383 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005384static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5385 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005387{
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5389 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005390 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5391 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005392 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005393
Janos Follath7742fee2019-06-17 12:58:10 +01005394 /* We can't be wanting more output after block 0xff, otherwise
5395 * the capacity check in psa_key_derivation_output_bytes() would have
5396 * prevented this call. It could happen only if the operation
5397 * object was corrupted or if this function is called directly
5398 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005399 if (tls12_prf->block_number == 0xff) {
5400 return PSA_ERROR_CORRUPTION_DETECTED;
5401 }
Janos Follath7742fee2019-06-17 12:58:10 +01005402
5403 /* We need a new block */
5404 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005405 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005406
5407 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5408 *
5409 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5410 *
5411 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5412 * HMAC_hash(secret, A(2) + seed) +
5413 * HMAC_hash(secret, A(3) + seed) + ...
5414 *
5415 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005416 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005417 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005418 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005419 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005420 * is currently extracted as `output_block` and where i is
5421 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005422 */
5423
Gilles Peskine449bd832023-01-11 14:50:10 +01005424 status = psa_key_derivation_start_hmac(&hmac,
5425 hash_alg,
5426 tls12_prf->secret,
5427 tls12_prf->secret_length);
5428 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005429 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005430 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005431
5432 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005433 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005434 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5435 * the variable seed and in this instance means it in the context of the
5436 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 status = psa_mac_update(&hmac,
5438 tls12_prf->label,
5439 tls12_prf->label_length);
5440 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005441 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 }
5443 status = psa_mac_update(&hmac,
5444 tls12_prf->seed,
5445 tls12_prf->seed_length);
5446 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005447 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 }
5449 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005450 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5452 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005453 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005455 }
5456
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 status = psa_mac_sign_finish(&hmac,
5458 tls12_prf->Ai, hash_length,
5459 &hmac_output_length);
5460 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005461 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 }
5463 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005464 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005466
5467 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005468 status = psa_key_derivation_start_hmac(&hmac,
5469 hash_alg,
5470 tls12_prf->secret,
5471 tls12_prf->secret_length);
5472 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005473 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 }
5475 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5476 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005477 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 }
5479 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5480 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005481 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 }
5483 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5484 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005485 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 }
5487 status = psa_mac_sign_finish(&hmac,
5488 tls12_prf->output_block, hash_length,
5489 &hmac_output_length);
5490 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005491 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005493
Janos Follath7742fee2019-06-17 12:58:10 +01005494
5495cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 cleanup_status = psa_mac_abort(&hmac);
5497 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005498 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005500
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005502}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005503
Janos Follath844eb0e2019-06-19 12:10:49 +01005504static psa_status_t psa_key_derivation_tls12_prf_read(
5505 psa_tls12_prf_key_derivation_t *tls12_prf,
5506 psa_algorithm_t alg,
5507 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005509{
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5511 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005512 psa_status_t status;
5513 uint8_t offset, length;
5514
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005516 case PSA_TLS12_PRF_STATE_LABEL_SET:
5517 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5518 break;
5519 case PSA_TLS12_PRF_STATE_OUTPUT:
5520 break;
5521 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005523 }
5524
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005526 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 if (tls12_prf->left_in_block == 0) {
5528 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5529 alg);
5530 if (status != PSA_SUCCESS) {
5531 return status;
5532 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005533
5534 continue;
5535 }
5536
Gilles Peskine449bd832023-01-11 14:50:10 +01005537 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005538 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005539 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005540 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005541 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005542
5543 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005545 output += length;
5546 output_length -= length;
5547 tls12_prf->left_in_block -= length;
5548 }
5549
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005551}
John Durkop07cc04a2020-11-16 22:08:34 -08005552#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5553 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005554
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005555#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5556static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5557 psa_tls12_ecjpake_to_pms_t *ecjpake,
5558 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005560{
5561 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005562 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005563
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 if (output_length != 32) {
5565 return PSA_ERROR_INVALID_ARGUMENT;
5566 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005567
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5569 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5570 &output_size);
5571 if (status != PSA_SUCCESS) {
5572 return status;
5573 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005574
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 if (output_size != output_length) {
5576 return PSA_ERROR_GENERIC_ERROR;
5577 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005578
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005580}
5581#endif
5582
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005583psa_status_t psa_key_derivation_output_bytes(
5584 psa_key_derivation_operation_t *operation,
5585 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005587{
5588 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005590
Gilles Peskine449bd832023-01-11 14:50:10 +01005591 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005592 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005593 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005594 }
5595
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005597 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005598 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005599 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005600 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005601 goto exit;
5602 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005603 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005604 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005605 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005606 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5607 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005608 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005609 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005611 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005612 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005613
Przemek Stekiel69c46792022-06-10 12:59:51 +02005614#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5616 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5617 output, output_length);
5618 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005619#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005620#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5621 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005622 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5623 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5624 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5625 kdf_alg, output,
5626 output_length);
5627 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005628#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5629 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005630#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005631 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005632 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005633 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5634 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005635#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5636
Gilles Peskineeab56e42018-07-12 17:12:33 +02005637 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005638 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005640 }
5641
5642exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005643 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005644 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005645 * This allows us to differentiate between exhausted operations and
5646 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5647 * operations. */
5648 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005649 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005650 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005651 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005652 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005653 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005654}
5655
David Brown7807bf72021-02-09 16:28:23 -07005656#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005657static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005658{
Gilles Peskine449bd832023-01-11 14:50:10 +01005659 if (data_size >= 8) {
5660 mbedtls_des_key_set_parity(data);
5661 }
5662 if (data_size >= 16) {
5663 mbedtls_des_key_set_parity(data + 8);
5664 }
5665 if (data_size >= 24) {
5666 mbedtls_des_key_set_parity(data + 16);
5667 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005668}
David Brown7807bf72021-02-09 16:28:23 -07005669#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005670
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005671/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 * ECC keys on a Weierstrass elliptic curve require the generation
5673 * of a private key which is an integer
5674 * in the range [1, N - 1], where N is the boundary of the private key domain:
5675 * N is the prime p for Diffie-Hellman, or the order of the
5676 * curve’s base point for ECC.
5677 *
5678 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5679 * This function generates the private key using the following process:
5680 *
5681 * 1. Draw a byte string of length ceiling(m/8) bytes.
5682 * 2. If m is not a multiple of 8, set the most significant
5683 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5684 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5685 * 4. If k > N - 2, discard the result and return to step 1.
5686 * 5. Output k + 1 as the private key.
5687 *
5688 * This method allows compliance to NIST standards, specifically the methods titled
5689 * Key-Pair Generation by Testing Candidates in the following publications:
5690 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5691 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5692 * Diffie-Hellman keys.
5693 *
5694 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5695 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5696 *
5697 * Note: Function allocates memory for *data buffer, so given *data should be
5698 * always NULL.
5699 */
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005700#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5701 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005702 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5703 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5704 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005705static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005706 psa_key_slot_t *slot,
5707 size_t bits,
5708 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005709 uint8_t **data
5710 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005711{
Valerio Setti52789862023-04-07 12:13:11 +02005712#if defined(MBEDTLS_ECP_LIGHT)
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005713 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005714 mbedtls_mpi k;
5715 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005716 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005717 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005718
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 mbedtls_mpi_init(&k);
5720 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005721
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005722 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005723 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005724 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005725 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005726
Gilles Peskine449bd832023-01-11 14:50:10 +01005727 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005728 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005729 goto cleanup;
5730 }
5731
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005732 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005734
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005736
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005737 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005738 /* Let m be the bit size of N. */
5739 size_t m = ecp_group.nbits;
5740
Gilles Peskine449bd832023-01-11 14:50:10 +01005741 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005742
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005743 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005745
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005746 /* Note: This function is always called with *data == NULL and it
5747 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005748 *data = mbedtls_calloc(1, m_bytes);
5749 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005750 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005751 goto cleanup;
5752 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005753
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005755 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005757 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005759
5760 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005762 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005763 * (8 * ceiling(m/8) - m) bits of the first byte in
5764 * the string to zero.
5765 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005766 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005767 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005768 }
5769
5770 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 * big-endian byte string.
5772 */
5773 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005774
5775 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005776 * Result of comparison is returned. When it indicates error
5777 * then this function is called again.
5778 */
5779 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005780 }
5781
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005782 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5784 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005785cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 if (ret != 0) {
5787 status = mbedtls_to_psa_error(ret);
5788 }
5789 if (status != PSA_SUCCESS) {
5790 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005791 *data = NULL;
5792 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005793 mbedtls_mpi_free(&k);
5794 mbedtls_mpi_free(&diff_N_2);
5795 return status;
Valerio Setti52789862023-04-07 12:13:11 +02005796#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005797 (void) slot;
5798 (void) bits;
5799 (void) operation;
5800 (void) data;
5801 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti52789862023-04-07 12:13:11 +02005802#endif /* MBEDTLS_ECP_LIGHT */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005803}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005804
5805/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5806 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5807 *
5808 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5809 * draw a 32-byte string and process it as specified in
5810 * Elliptic Curves for Security [RFC7748] §5.
5811 *
5812 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5813 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5814 *
5815 * Note: Function allocates memory for *data buffer, so given *data should be
5816 * always NULL.
5817 */
5818
5819static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5820 size_t bits,
5821 psa_key_derivation_operation_t *operation,
5822 uint8_t **data
5823 )
5824{
5825 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005826 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005827
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005829 case 255:
5830 output_length = 32;
5831 break;
5832 case 448:
5833 output_length = 56;
5834 break;
5835 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005836 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005837 break;
5838 }
5839
Gilles Peskine449bd832023-01-11 14:50:10 +01005840 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005841
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 if (*data == NULL) {
5843 return PSA_ERROR_INSUFFICIENT_MEMORY;
5844 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005845
Gilles Peskine449bd832023-01-11 14:50:10 +01005846 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005847
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005849 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005851
Gilles Peskine449bd832023-01-11 14:50:10 +01005852 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005853 case 255:
5854 (*data)[0] &= 248;
5855 (*data)[31] &= 127;
5856 (*data)[31] |= 64;
5857 break;
5858 case 448:
5859 (*data)[0] &= 252;
5860 (*data)[55] |= 128;
5861 break;
5862 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005864 break;
5865 }
5866
5867 return status;
5868}
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005869#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5870 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005871 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5872 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5873 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005874
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005875static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005876 psa_key_slot_t *slot,
5877 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005879{
5880 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305882 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005883 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005884
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5886 return PSA_ERROR_INVALID_ARGUMENT;
5887 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005888
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005889#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5890 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005891 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5892 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5893 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5895 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5896 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005897 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005898 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5899 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005900 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005901 }
5902 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005903 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005904 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5905 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005906 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005907 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005908 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005909 } else
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005910#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5911 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005912 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5913 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5914 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005915 if (key_type_is_raw_bytes(slot->attr.type)) {
5916 if (bits % 8 != 0) {
5917 return PSA_ERROR_INVALID_ARGUMENT;
5918 }
5919 data = mbedtls_calloc(1, bytes);
5920 if (data == NULL) {
5921 return PSA_ERROR_INSUFFICIENT_MEMORY;
5922 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005923
Gilles Peskine449bd832023-01-11 14:50:10 +01005924 status = psa_key_derivation_output_bytes(operation, data, bytes);
5925 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005926 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005927 }
David Brown12ca5032021-02-11 11:02:00 -07005928#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005929 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5930 psa_des_set_key_parity(data, bytes);
5931 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005932#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 } else {
5934 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005935 }
Ronald Crondd04d422020-11-28 15:14:42 +01005936
Ronald Cronbf33c932020-11-28 18:06:53 +01005937 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005938 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005940 };
5941
Gilles Peskine449bd832023-01-11 14:50:10 +01005942 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5943 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5944 &storage_size);
5945 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305946 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 }
Archanad8a83dc2021-06-14 10:04:16 +05305948 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005949 status = psa_allocate_buffer_to_slot(slot, storage_size);
5950 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305951 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 }
Archanad8a83dc2021-06-14 10:04:16 +05305953
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 status = psa_driver_wrapper_import_key(&attributes,
5955 data, bytes,
5956 slot->key.data,
5957 slot->key.bytes,
5958 &slot->key.bytes, &bits);
5959 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005960 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005961 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005962
5963exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005964 mbedtls_free(data);
5965 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005966}
5967
Gilles Peskine449bd832023-01-11 14:50:10 +01005968psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5969 psa_key_derivation_operation_t *operation,
5970 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005971{
5972 psa_status_t status;
5973 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005974 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005975
Ronald Cron81709fc2020-11-14 12:10:32 +01005976 *key = MBEDTLS_SVC_KEY_ID_INIT;
5977
Gilles Peskine0f84d622019-09-12 19:03:13 +02005978 /* Reject any attempt to create a zero-length key so that we don't
5979 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 if (psa_get_key_bits(attributes) == 0) {
5981 return PSA_ERROR_INVALID_ARGUMENT;
5982 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005983
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 if (operation->alg == PSA_ALG_NONE) {
5985 return PSA_ERROR_BAD_STATE;
5986 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005987
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 if (!operation->can_output_key) {
5989 return PSA_ERROR_NOT_PERMITTED;
5990 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005991
Gilles Peskine449bd832023-01-11 14:50:10 +01005992 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5993 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005994#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005996 /* Deriving a key in a secure element is not implemented yet. */
5997 status = PSA_ERROR_NOT_SUPPORTED;
5998 }
5999#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 if (status == PSA_SUCCESS) {
6001 status = psa_generate_derived_key_internal(slot,
6002 attributes->core.bits,
6003 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006004 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006005 if (status == PSA_SUCCESS) {
6006 status = psa_finish_key_creation(slot, driver, key);
6007 }
6008 if (status != PSA_SUCCESS) {
6009 psa_fail_key_creation(slot, driver);
6010 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006011
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006013}
6014
Gilles Peskineeab56e42018-07-12 17:12:33 +02006015
6016
6017/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006018/* Key derivation */
6019/****************************************************************/
6020
Steven Cooreman932ffb72021-02-15 12:14:32 +01006021#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006022static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006023{
6024#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6026 return 1;
6027 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006028#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006029#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006030 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6031 return 1;
6032 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006033#endif
6034#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 1;
6037 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006038#endif
6039#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006040 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6041 return 1;
6042 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006043#endif
6044#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6046 return 1;
6047 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006048#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006049#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006050 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6051 return 1;
6052 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006053#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006055}
6056
Gilles Peskine449bd832023-01-11 14:50:10 +01006057static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006058{
6059 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 psa_status_t status = psa_hash_setup(&operation, alg);
6061 psa_hash_abort(&operation);
6062 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006063}
6064
Gilles Peskine969c5d62019-01-16 15:53:06 +01006065static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006066 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006068{
Janos Follath5fe19732019-06-20 15:09:30 +01006069 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6070 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006071 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006072
Gilles Peskine969c5d62019-01-16 15:53:06 +01006073 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 if (!is_kdf_alg_supported(kdf_alg)) {
6075 return PSA_ERROR_NOT_SUPPORTED;
6076 }
John Durkop07cc04a2020-11-16 22:08:34 -08006077
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006078 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04006079 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6081 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
6082 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6083 if (hash_size == 0) {
6084 return PSA_ERROR_NOT_SUPPORTED;
6085 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006086
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006087 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6088 * we might fail later, which is somewhat unfriendly and potentially
6089 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006090 psa_status_t status = psa_hash_try_support(hash_alg);
6091 if (status != PSA_SUCCESS) {
6092 return status;
6093 }
6094 } else {
6095 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006096 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006097
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6099 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6100 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6101 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006102 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006103#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006104 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6106 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006107 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006109#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6110 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 operation->capacity = 255 * hash_size;
6112 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006113}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006114
Gilles Peskine449bd832023-01-11 14:50:10 +01006115static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006116{
6117#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 if (alg == PSA_ALG_ECDH) {
6119 return PSA_SUCCESS;
6120 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006121#endif
6122 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006124}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006125
6126static int psa_key_derivation_allows_free_form_secret_input(
6127 psa_algorithm_t kdf_alg)
6128{
6129#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6130 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6131 return 0;
6132 }
6133#endif
6134 (void) kdf_alg;
6135 return 1;
6136}
John Durkop07cc04a2020-11-16 22:08:34 -08006137#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006138
Gilles Peskine449bd832023-01-11 14:50:10 +01006139psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6140 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006141{
6142 psa_status_t status;
6143
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 if (operation->alg != 0) {
6145 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006146 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006147
Gilles Peskine449bd832023-01-11 14:50:10 +01006148 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6149 return PSA_ERROR_INVALID_ARGUMENT;
6150 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6151#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6152 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6153 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6154 status = psa_key_agreement_try_support(ka_alg);
6155 if (status != PSA_SUCCESS) {
6156 return status;
6157 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006158 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6159 return PSA_ERROR_INVALID_ARGUMENT;
6160 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6162#else
6163 return PSA_ERROR_NOT_SUPPORTED;
6164#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6165 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6166#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6167 status = psa_key_derivation_setup_kdf(operation, alg);
6168#else
6169 return PSA_ERROR_NOT_SUPPORTED;
6170#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6171 } else {
6172 return PSA_ERROR_INVALID_ARGUMENT;
6173 }
6174
6175 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006176 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006177 }
6178 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006179}
6180
Przemek Stekiel69c46792022-06-10 12:59:51 +02006181#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006182static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6183 psa_algorithm_t kdf_alg,
6184 psa_key_derivation_step_t step,
6185 const uint8_t *data,
6186 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006187{
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006189 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006191 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006192#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6194 return PSA_ERROR_INVALID_ARGUMENT;
6195 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006196#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 if (hkdf->state != HKDF_STATE_INIT) {
6198 return PSA_ERROR_BAD_STATE;
6199 } else {
6200 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6201 hash_alg,
6202 data, data_length);
6203 if (status != PSA_SUCCESS) {
6204 return status;
6205 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006206 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006208 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006209 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006210#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006212 /* We shouldn't be in different state as HKDF_EXPAND only allows
6213 * two inputs: SECRET (this case) and INFO which does not modify
6214 * the state. It could happen only if the hkdf
6215 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 if (hkdf->state != HKDF_STATE_INIT) {
6217 return PSA_ERROR_BAD_STATE;
6218 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006219
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006220 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6222 return PSA_ERROR_INVALID_ARGUMENT;
6223 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006224
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 memcpy(hkdf->prk, data, data_length);
6226 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006227#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006228 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006229 /* HKDF: If no salt was provided, use an empty salt.
6230 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006232#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006233 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6234 return PSA_ERROR_BAD_STATE;
6235 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006236#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6238 hash_alg,
6239 NULL, 0);
6240 if (status != PSA_SUCCESS) {
6241 return status;
6242 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006243 hkdf->state = HKDF_STATE_STARTED;
6244 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 if (hkdf->state != HKDF_STATE_STARTED) {
6246 return PSA_ERROR_BAD_STATE;
6247 }
6248 status = psa_mac_update(&hkdf->hmac,
6249 data, data_length);
6250 if (status != PSA_SUCCESS) {
6251 return status;
6252 }
6253 status = psa_mac_sign_finish(&hkdf->hmac,
6254 hkdf->prk,
6255 sizeof(hkdf->prk),
6256 &data_length);
6257 if (status != PSA_SUCCESS) {
6258 return status;
6259 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006260 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006261
Gilles Peskine2b522db2019-04-12 15:11:49 +02006262 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006263 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006264#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006265 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006266 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006268 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006270#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006271 {
6272 /* Block 0 is empty, and the next block will be
6273 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006275 }
6276
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006278 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006279#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6281 return PSA_ERROR_INVALID_ARGUMENT;
6282 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006283#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006284#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6286 hkdf->state == HKDF_STATE_INIT) {
6287 return PSA_ERROR_BAD_STATE;
6288 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006289#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006290 if (hkdf->state == HKDF_STATE_OUTPUT) {
6291 return PSA_ERROR_BAD_STATE;
6292 }
6293 if (hkdf->info_set) {
6294 return PSA_ERROR_BAD_STATE;
6295 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006296 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 if (data_length != 0) {
6298 hkdf->info = mbedtls_calloc(1, data_length);
6299 if (hkdf->info == NULL) {
6300 return PSA_ERROR_INSUFFICIENT_MEMORY;
6301 }
6302 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006303 }
6304 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006306 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006308 }
6309}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006310#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006311
John Durkop07cc04a2020-11-16 22:08:34 -08006312#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6313 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006314static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6315 const uint8_t *data,
6316 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006317{
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6319 return PSA_ERROR_BAD_STATE;
6320 }
Janos Follathf08e2652019-06-13 09:05:41 +01006321
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 if (data_length != 0) {
6323 prf->seed = mbedtls_calloc(1, data_length);
6324 if (prf->seed == NULL) {
6325 return PSA_ERROR_INSUFFICIENT_MEMORY;
6326 }
Janos Follathf08e2652019-06-13 09:05:41 +01006327
Gilles Peskine449bd832023-01-11 14:50:10 +01006328 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006329 prf->seed_length = data_length;
6330 }
Janos Follathf08e2652019-06-13 09:05:41 +01006331
Gilles Peskine43f958b2020-12-13 14:55:14 +01006332 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006333
Gilles Peskine449bd832023-01-11 14:50:10 +01006334 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006335}
6336
Gilles Peskine449bd832023-01-11 14:50:10 +01006337static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6338 const uint8_t *data,
6339 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006340{
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6342 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6343 return PSA_ERROR_BAD_STATE;
6344 }
Janos Follath81550542019-06-13 14:26:34 +01006345
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 if (data_length != 0) {
6347 prf->secret = mbedtls_calloc(1, data_length);
6348 if (prf->secret == NULL) {
6349 return PSA_ERROR_INSUFFICIENT_MEMORY;
6350 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006351
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006353 prf->secret_length = data_length;
6354 }
Janos Follath81550542019-06-13 14:26:34 +01006355
Gilles Peskine43f958b2020-12-13 14:55:14 +01006356 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006357
Gilles Peskine449bd832023-01-11 14:50:10 +01006358 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006359}
6360
Gilles Peskine449bd832023-01-11 14:50:10 +01006361static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6362 const uint8_t *data,
6363 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006364{
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6366 return PSA_ERROR_BAD_STATE;
6367 }
Janos Follath63028dd2019-06-13 09:15:47 +01006368
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 if (data_length != 0) {
6370 prf->label = mbedtls_calloc(1, data_length);
6371 if (prf->label == NULL) {
6372 return PSA_ERROR_INSUFFICIENT_MEMORY;
6373 }
Janos Follath63028dd2019-06-13 09:15:47 +01006374
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006376 prf->label_length = data_length;
6377 }
Janos Follath63028dd2019-06-13 09:15:47 +01006378
Gilles Peskine43f958b2020-12-13 14:55:14 +01006379 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006380
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006382}
6383
Gilles Peskine449bd832023-01-11 14:50:10 +01006384static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6385 psa_key_derivation_step_t step,
6386 const uint8_t *data,
6387 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006388{
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006390 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006391 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006392 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006394 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006396 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006398 }
6399}
John Durkop07cc04a2020-11-16 22:08:34 -08006400#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6401 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6402
6403#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6404static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6405 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006406 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006408{
6409 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6411 4 + data_length + prf->other_secret_length :
6412 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006413
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6415 return PSA_ERROR_INVALID_ARGUMENT;
6416 }
John Durkop07cc04a2020-11-16 22:08:34 -08006417
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 uint8_t *pms = mbedtls_calloc(1, pms_len);
6419 if (pms == NULL) {
6420 return PSA_ERROR_INSUFFICIENT_MEMORY;
6421 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006422 uint8_t *cur = pms;
6423
6424 /* pure-PSK:
6425 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006426 *
6427 * The premaster secret is formed as follows: if the PSK is N octets
6428 * long, concatenate a uint16 with the value N, N zero octets, a second
6429 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006430 *
6431 * mixed-PSK:
6432 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6433 * follows: concatenate a uint16 with the length of the other secret,
6434 * the other secret itself, uint16 with the length of PSK, and the
6435 * PSK itself.
6436 * For details please check:
6437 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6438 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6439 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006440 */
6441
Gilles Peskine449bd832023-01-11 14:50:10 +01006442 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6443 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6444 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6445 if (prf->other_secret_length != 0) {
6446 memcpy(cur, prf->other_secret, prf->other_secret_length);
6447 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006448 cur += prf->other_secret_length;
6449 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 } else {
6451 *cur++ = MBEDTLS_BYTE_1(data_length);
6452 *cur++ = MBEDTLS_BYTE_0(data_length);
6453 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006454 cur += data_length;
6455 }
6456
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 *cur++ = MBEDTLS_BYTE_1(data_length);
6458 *cur++ = MBEDTLS_BYTE_0(data_length);
6459 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006460 cur += data_length;
6461
Gilles Peskine449bd832023-01-11 14:50:10 +01006462 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006463
Gilles Peskine449bd832023-01-11 14:50:10 +01006464 mbedtls_platform_zeroize(pms, pms_len);
6465 mbedtls_free(pms);
6466 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006467}
Janos Follath6660f0e2019-06-17 08:44:03 +01006468
Przemek Stekiele47201b2022-04-19 13:53:28 +02006469static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6470 psa_tls12_prf_key_derivation_t *prf,
6471 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006472 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006473{
Gilles Peskine449bd832023-01-11 14:50:10 +01006474 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6475 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006476 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006477
6478 if (data_length != 0) {
6479 prf->other_secret = mbedtls_calloc(1, data_length);
6480 if (prf->other_secret == NULL) {
6481 return PSA_ERROR_INSUFFICIENT_MEMORY;
6482 }
6483
6484 memcpy(prf->other_secret, data, data_length);
6485 prf->other_secret_length = data_length;
6486 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006487 prf->other_secret_length = 0;
6488 }
6489
6490 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6491
Gilles Peskine449bd832023-01-11 14:50:10 +01006492 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006493}
6494
Janos Follath6660f0e2019-06-17 08:44:03 +01006495static psa_status_t psa_tls12_prf_psk_to_ms_input(
6496 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006497 psa_key_derivation_step_t step,
6498 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006499 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006500{
Gilles Peskine449bd832023-01-11 14:50:10 +01006501 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006502 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006503 return psa_tls12_prf_psk_to_ms_set_key(prf,
6504 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006505 break;
6506 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006507 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6508 data,
6509 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006510 break;
6511 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006512 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006513 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006514
Przemek Stekiele47201b2022-04-19 13:53:28 +02006515 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006516}
John Durkop07cc04a2020-11-16 22:08:34 -08006517#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006518
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006519#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6520static psa_status_t psa_tls12_ecjpake_to_pms_input(
6521 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006522 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006523 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006524 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006525{
Gilles Peskine449bd832023-01-11 14:50:10 +01006526 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6527 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6528 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006529 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006530
6531 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006532 if (data[0] != 0x04) {
6533 return PSA_ERROR_INVALID_ARGUMENT;
6534 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006535
6536 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006537 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006538
Gilles Peskine449bd832023-01-11 14:50:10 +01006539 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006540}
6541#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006542/** Check whether the given key type is acceptable for the given
6543 * input step of a key derivation.
6544 *
6545 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6546 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6547 * Both secret and non-secret inputs can alternatively have the type
6548 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6549 * that the input was passed as a buffer rather than via a key object.
6550 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006551static int psa_key_derivation_check_input_type(
6552 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006553 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006554{
Gilles Peskine449bd832023-01-11 14:50:10 +01006555 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006556 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 if (key_type == PSA_KEY_TYPE_DERIVE) {
6558 return PSA_SUCCESS;
6559 }
6560 if (key_type == PSA_KEY_TYPE_NONE) {
6561 return PSA_SUCCESS;
6562 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006563 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006564 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006565 if (key_type == PSA_KEY_TYPE_DERIVE) {
6566 return PSA_SUCCESS;
6567 }
6568 if (key_type == PSA_KEY_TYPE_NONE) {
6569 return PSA_SUCCESS;
6570 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006571 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006572 case PSA_KEY_DERIVATION_INPUT_LABEL:
6573 case PSA_KEY_DERIVATION_INPUT_SALT:
6574 case PSA_KEY_DERIVATION_INPUT_INFO:
6575 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006576 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6577 return PSA_SUCCESS;
6578 }
6579 if (key_type == PSA_KEY_TYPE_NONE) {
6580 return PSA_SUCCESS;
6581 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006582 break;
6583 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006584 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006585}
6586
Janos Follathb80a94e2019-06-12 15:54:46 +01006587static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006588 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006589 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006590 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006591 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006592 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006593{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006594 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006595 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006596
Gilles Peskine449bd832023-01-11 14:50:10 +01006597 status = psa_key_derivation_check_input_type(step, key_type);
6598 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006599 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006600 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006601
Przemek Stekiel69c46792022-06-10 12:59:51 +02006602#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006603 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6604 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6605 step, data, data_length);
6606 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006607#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006608#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006609 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6610 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6611 step, data, data_length);
6612 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006613#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6614#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006615 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6616 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6617 step, data, data_length);
6618 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006619#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006620#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006621 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006622 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006623 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6624 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006625#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006626 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006627 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006628 (void) data;
6629 (void) data_length;
6630 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006631 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006632 }
6633
Gilles Peskine224b0d62019-09-23 18:13:17 +02006634exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006635 if (status != PSA_SUCCESS) {
6636 psa_key_derivation_abort(operation);
6637 }
6638 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006639}
6640
Janos Follath51f4a0f2019-06-14 11:35:55 +01006641psa_status_t psa_key_derivation_input_bytes(
6642 psa_key_derivation_operation_t *operation,
6643 psa_key_derivation_step_t step,
6644 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006645 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006646{
Gilles Peskine449bd832023-01-11 14:50:10 +01006647 return psa_key_derivation_input_internal(operation, step,
6648 PSA_KEY_TYPE_NONE,
6649 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006650}
6651
Janos Follath51f4a0f2019-06-14 11:35:55 +01006652psa_status_t psa_key_derivation_input_key(
6653 psa_key_derivation_operation_t *operation,
6654 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006655 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006656{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006657 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006658 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006659 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006660
Ronald Cron5c522922020-11-14 16:35:34 +01006661 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006662 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6663 if (status != PSA_SUCCESS) {
6664 psa_key_derivation_abort(operation);
6665 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006666 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006667
6668 /* Passing a key object as a SECRET input unlocks the permission
6669 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006670 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006671 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006672 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006673
Gilles Peskine449bd832023-01-11 14:50:10 +01006674 status = psa_key_derivation_input_internal(operation,
6675 step, slot->attr.type,
6676 slot->key.data,
6677 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006678
Gilles Peskine449bd832023-01-11 14:50:10 +01006679 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006680
Gilles Peskine449bd832023-01-11 14:50:10 +01006681 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006682}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006683
6684
6685
6686/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006687/* Key agreement */
6688/****************************************************************/
6689
Gilles Peskine449bd832023-01-11 14:50:10 +01006690psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6691 const uint8_t *key_buffer,
6692 size_t key_buffer_size,
6693 psa_algorithm_t alg,
6694 const uint8_t *peer_key,
6695 size_t peer_key_length,
6696 uint8_t *shared_secret,
6697 size_t shared_secret_size,
6698 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006699{
Gilles Peskine449bd832023-01-11 14:50:10 +01006700 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006701#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006702 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006703 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6704 key_buffer_size, alg,
6705 peer_key, peer_key_length,
6706 shared_secret,
6707 shared_secret_size,
6708 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006709#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006710 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006711 (void) attributes;
6712 (void) key_buffer;
6713 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006714 (void) peer_key;
6715 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006716 (void) shared_secret;
6717 (void) shared_secret_size;
6718 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006719 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006720 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006721}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006722
Aditya Deshpande17845b82022-10-13 17:21:01 +01006723/** Internal function for raw key agreement
6724 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006725 * to the driver's implementation if a driver is present.
6726 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006727 * (psa_key_agreement_raw_builtin).
6728 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006729static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6730 psa_key_slot_t *private_key,
6731 const uint8_t *peer_key,
6732 size_t peer_key_length,
6733 uint8_t *shared_secret,
6734 size_t shared_secret_size,
6735 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006736{
Gilles Peskine449bd832023-01-11 14:50:10 +01006737 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6738 return PSA_ERROR_NOT_SUPPORTED;
6739 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006740
6741 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006742 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006743 };
6744
Gilles Peskine449bd832023-01-11 14:50:10 +01006745 return psa_driver_wrapper_key_agreement(&attributes,
6746 private_key->key.data,
6747 private_key->key.bytes, alg,
6748 peer_key, peer_key_length,
6749 shared_secret,
6750 shared_secret_size,
6751 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006752}
6753
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006754/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006755 * to potentially free embedded data structures and wipe confidential data.
6756 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006757static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6758 psa_key_derivation_step_t step,
6759 psa_key_slot_t *private_key,
6760 const uint8_t *peer_key,
6761 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006762{
6763 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006764 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006765 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006766 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006767
6768 /* Step 1: run the secret agreement algorithm to generate the shared
6769 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006770 status = psa_key_agreement_raw_internal(ka_alg,
6771 private_key,
6772 peer_key, peer_key_length,
6773 shared_secret,
6774 sizeof(shared_secret),
6775 &shared_secret_length);
6776 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006777 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006778 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006779
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006780 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006781 * the shared secret. A shared secret is permitted wherever a key
6782 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006783 status = psa_key_derivation_input_internal(operation, step,
6784 PSA_KEY_TYPE_DERIVE,
6785 shared_secret,
6786 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006787exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006788 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6789 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006790}
6791
Gilles Peskine449bd832023-01-11 14:50:10 +01006792psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6793 psa_key_derivation_step_t step,
6794 mbedtls_svc_key_id_t private_key,
6795 const uint8_t *peer_key,
6796 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006797{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006798 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006799 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006800 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006801
Gilles Peskine449bd832023-01-11 14:50:10 +01006802 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6803 return PSA_ERROR_INVALID_ARGUMENT;
6804 }
Ronald Cron5c522922020-11-14 16:35:34 +01006805 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006806 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6807 if (status != PSA_SUCCESS) {
6808 return status;
6809 }
6810 status = psa_key_agreement_internal(operation, step,
6811 slot,
6812 peer_key, peer_key_length);
6813 if (status != PSA_SUCCESS) {
6814 psa_key_derivation_abort(operation);
6815 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006816 /* If a private key has been added as SECRET, we allow the derived
6817 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006818 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006819 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006820 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006821 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006822
Gilles Peskine449bd832023-01-11 14:50:10 +01006823 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006824
Gilles Peskine449bd832023-01-11 14:50:10 +01006825 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006826}
6827
Gilles Peskine449bd832023-01-11 14:50:10 +01006828psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6829 mbedtls_svc_key_id_t private_key,
6830 const uint8_t *peer_key,
6831 size_t peer_key_length,
6832 uint8_t *output,
6833 size_t output_size,
6834 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006835{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006836 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006837 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006838 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006839
Gilles Peskine449bd832023-01-11 14:50:10 +01006840 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006841 status = PSA_ERROR_INVALID_ARGUMENT;
6842 goto exit;
6843 }
Ronald Cron5c522922020-11-14 16:35:34 +01006844 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006845 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6846 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006847 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006848 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006849
Gilles Peskine3e561302022-04-14 00:17:15 +02006850 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6851 * for the output size. The PSA specification only guarantees that this
6852 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6853 * but it might be nice to allow smaller buffers if the output fits.
6854 * At the time of writing this comment, with only ECDH implemented,
6855 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6856 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6857 * be exact for it as well. */
6858 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006859 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6860 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006861 status = PSA_ERROR_BUFFER_TOO_SMALL;
6862 goto exit;
6863 }
6864
Gilles Peskine449bd832023-01-11 14:50:10 +01006865 status = psa_key_agreement_raw_internal(alg, slot,
6866 peer_key, peer_key_length,
6867 output, output_size,
6868 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006869
6870exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006871 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006872 /* If an error happens and is not handled properly, the output
6873 * may be used as a key to protect sensitive data. Arrange for such
6874 * a key to be random, which is likely to result in decryption or
6875 * verification errors. This is better than filling the buffer with
6876 * some constant data such as zeros, which would result in the data
6877 * being protected with a reproducible, easily knowable key.
6878 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006880 *output_length = output_size;
6881 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006882
Gilles Peskine449bd832023-01-11 14:50:10 +01006883 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006884
Gilles Peskine449bd832023-01-11 14:50:10 +01006885 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006886}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006887
6888
Gilles Peskine30524eb2020-11-13 17:02:26 +01006889
Gilles Peskine86a440b2018-11-12 18:39:40 +01006890/****************************************************************/
6891/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006892/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006893
Gilles Peskine30524eb2020-11-13 17:02:26 +01006894/** Initialize the PSA random generator.
6895 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006896static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006897{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006898#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006899 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006900#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6901
Gilles Peskine30524eb2020-11-13 17:02:26 +01006902 /* Set default configuration if
6903 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006904 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006905 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006906 }
6907 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006908 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006909 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006910
Gilles Peskine449bd832023-01-11 14:50:10 +01006911 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006912#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6913 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6914 /* The PSA entropy injection feature depends on using NV seed as an entropy
6915 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006916 mbedtls_entropy_add_source(&rng->entropy,
6917 mbedtls_nv_seed_poll, NULL,
6918 MBEDTLS_ENTROPY_BLOCK_SIZE,
6919 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006920#endif
6921
Gilles Peskine449bd832023-01-11 14:50:10 +01006922 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006923#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006924}
6925
6926/** Deinitialize the PSA random generator.
6927 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006928static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006929{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006930#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006931 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006932#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01006933 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
6934 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006935#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006936}
6937
6938/** Seed the PSA random generator.
6939 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006940static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006941{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006942#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6943 /* Do nothing: the external RNG seeds itself. */
6944 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006946#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006947 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01006948 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
6949 drbg_seed, sizeof(drbg_seed) - 1);
6950 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006951#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006952}
6953
Gilles Peskine449bd832023-01-11 14:50:10 +01006954psa_status_t psa_generate_random(uint8_t *output,
6955 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006956{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006957 GUARD_MODULE_INITIALIZED;
6958
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006959#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6960
6961 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006962 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6963 output, output_size,
6964 &output_length);
6965 if (status != PSA_SUCCESS) {
6966 return status;
6967 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006968 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006969 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006970 if (output_length != output_size) {
6971 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6972 }
6973 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006974
6975#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6976
Gilles Peskine449bd832023-01-11 14:50:10 +01006977 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006978 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006979 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6980 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6981 output_size);
6982 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6983 output, request_size);
6984 if (ret != 0) {
6985 return mbedtls_to_psa_error(ret);
6986 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006987 output_size -= request_size;
6988 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006989 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006990 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006991#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006992}
6993
Gilles Peskine8814fc42020-12-14 15:33:44 +01006994/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006995 *
6996 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6997 * `psa_generate_random(...)`. The state parameter is ignored since the
6998 * PSA API doesn't support passing an explicit state.
6999 *
7000 * In the non-external case, psa_generate_random() calls an
7001 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7002 * and semantics as mbedtls_psa_get_random(). As an optimization,
7003 * instead of doing this back-and-forth between the PSA API and the
7004 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7005 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007006 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007007#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7008int mbedtls_psa_get_random(void *p_rng,
7009 unsigned char *output,
7010 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007011{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007012 /* This function takes a pointer to the RNG state because that's what
7013 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7014 * its own state internally and doesn't let the caller access that state.
7015 * So we just ignore the state parameter, and in practice we'll pass
7016 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007017 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007018 psa_status_t status = psa_generate_random(output, output_size);
7019 if (status == PSA_SUCCESS) {
7020 return 0;
7021 } else {
7022 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7023 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007024}
7025#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7026
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007027#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00007028#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007029
Gilles Peskine449bd832023-01-11 14:50:10 +01007030psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7031 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007032{
Gilles Peskine449bd832023-01-11 14:50:10 +01007033 if (global_data.initialized) {
7034 return PSA_ERROR_NOT_PERMITTED;
7035 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007036
Gilles Peskine449bd832023-01-11 14:50:10 +01007037 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7038 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7039 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7040 return PSA_ERROR_INVALID_ARGUMENT;
7041 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007042
Gilles Peskine449bd832023-01-11 14:50:10 +01007043 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007044}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007045#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007046
Ronald Cron3772afe2021-02-08 16:10:05 +01007047/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007048 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007049 * \param type The key type
7050 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007051 *
7052 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007053 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007054 * \retval #PSA_ERROR_INVALID_ARGUMENT
7055 * The size in bits of the key is not valid.
7056 * \retval #PSA_ERROR_NOT_SUPPORTED
7057 * The type and/or the size in bits of the key or the combination of
7058 * the two is not supported.
7059 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007060static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007061 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007062{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007063 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007064
Gilles Peskine449bd832023-01-11 14:50:10 +01007065 if (key_type_is_raw_bytes(type)) {
7066 status = psa_validate_unstructured_key_bit_size(type, bits);
7067 if (status != PSA_SUCCESS) {
7068 return status;
7069 }
7070 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007071#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007072 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7073 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7074 return PSA_ERROR_NOT_SUPPORTED;
7075 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007076
Ronald Cron01b2aba2020-10-05 09:42:02 +02007077 /* Accept only byte-aligned keys, for the same reasons as
7078 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007079 if (bits % 8 != 0) {
7080 return PSA_ERROR_NOT_SUPPORTED;
7081 }
7082 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007083#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007084
Ronald Cron5c4d3862020-12-07 11:07:24 +01007085#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007087 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007088 return PSA_SUCCESS;
7089 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007090#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007091 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007092 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007093 }
7094
Gilles Peskine449bd832023-01-11 14:50:10 +01007095 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007096}
7097
Ronald Cron55ed0592020-10-05 10:30:40 +02007098psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007099 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007100 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007101{
7102 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007103 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007104
Gilles Peskine449bd832023-01-11 14:50:10 +01007105 if ((attributes->domain_parameters == NULL) &&
7106 (attributes->domain_parameters_size != 0)) {
7107 return PSA_ERROR_INVALID_ARGUMENT;
7108 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007109
Gilles Peskine449bd832023-01-11 14:50:10 +01007110 if (key_type_is_raw_bytes(type)) {
7111 status = psa_generate_random(key_buffer, key_buffer_size);
7112 if (status != PSA_SUCCESS) {
7113 return status;
7114 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007115
David Brown12ca5032021-02-11 11:02:00 -07007116#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007117 if (type == PSA_KEY_TYPE_DES) {
7118 psa_des_set_key_parity(key_buffer, key_buffer_size);
7119 }
David Brown8107e312021-02-12 08:08:46 -07007120#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007121 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007122
Jaeden Amero424fa932021-05-14 08:34:32 +01007123#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
7124 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01007125 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7126 return mbedtls_psa_rsa_generate_key(attributes,
7127 key_buffer,
7128 key_buffer_size,
7129 key_buffer_length);
7130 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01007131#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
7132 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007133
John Durkop9814fa22020-11-04 12:28:15 -08007134#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007135 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7136 return mbedtls_psa_ecp_generate_key(attributes,
7137 key_buffer,
7138 key_buffer_size,
7139 key_buffer_length);
7140 } else
John Durkop9814fa22020-11-04 12:28:15 -08007141#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02007142 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007143 (void) key_buffer_length;
7144 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007145 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007146
Gilles Peskine449bd832023-01-11 14:50:10 +01007147 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007148}
Darryl Green0c6575a2018-11-07 16:05:30 +00007149
Gilles Peskine449bd832023-01-11 14:50:10 +01007150psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7151 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007152{
7153 psa_status_t status;
7154 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007155 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007156 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007157
Ronald Cron81709fc2020-11-14 12:10:32 +01007158 *key = MBEDTLS_SVC_KEY_ID_INIT;
7159
Gilles Peskine0f84d622019-09-12 19:03:13 +02007160 /* Reject any attempt to create a zero-length key so that we don't
7161 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007162 if (psa_get_key_bits(attributes) == 0) {
7163 return PSA_ERROR_INVALID_ARGUMENT;
7164 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007165
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007166 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007167 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7168 return PSA_ERROR_INVALID_ARGUMENT;
7169 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007170
Gilles Peskine449bd832023-01-11 14:50:10 +01007171 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7172 &slot, &driver);
7173 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007174 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007175 }
Gilles Peskine11792082019-08-06 18:36:36 +02007176
Ronald Cron977c2472020-10-13 08:32:21 +02007177 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307178 * storage ( thus not in the case of generating a key in a secure element
7179 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7180 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007181 if (slot->key.data == NULL) {
7182 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7183 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007184 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007185 attributes->core.type, attributes->core.bits);
7186 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007187 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007188 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007189
7190 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007191 attributes->core.type,
7192 attributes->core.bits);
7193 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007194 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007195 attributes, &key_buffer_size);
7196 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007197 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007198 }
Ronald Cron977c2472020-10-13 08:32:21 +02007199 }
Ronald Cron977c2472020-10-13 08:32:21 +02007200
Gilles Peskine449bd832023-01-11 14:50:10 +01007201 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7202 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007203 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007204 }
Ronald Cron977c2472020-10-13 08:32:21 +02007205 }
7206
Gilles Peskine449bd832023-01-11 14:50:10 +01007207 status = psa_driver_wrapper_generate_key(attributes,
7208 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007209
Gilles Peskine449bd832023-01-11 14:50:10 +01007210 if (status != PSA_SUCCESS) {
7211 psa_remove_key_data_from_memory(slot);
7212 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007213
Gilles Peskine11792082019-08-06 18:36:36 +02007214exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007215 if (status == PSA_SUCCESS) {
7216 status = psa_finish_key_creation(slot, driver, key);
7217 }
7218 if (status != PSA_SUCCESS) {
7219 psa_fail_key_creation(slot, driver);
7220 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007221
Gilles Peskine449bd832023-01-11 14:50:10 +01007222 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007223}
7224
Gilles Peskinee59236f2018-01-27 23:32:46 +01007225/****************************************************************/
7226/* Module setup */
7227/****************************************************************/
7228
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007229#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007230psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007231 void (* entropy_init)(mbedtls_entropy_context *ctx),
7232 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007233{
Gilles Peskine449bd832023-01-11 14:50:10 +01007234 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7235 return PSA_ERROR_BAD_STATE;
7236 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007237 global_data.rng.entropy_init = entropy_init;
7238 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007239 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007240}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007241#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007242
Gilles Peskine449bd832023-01-11 14:50:10 +01007243void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007244{
Gilles Peskine449bd832023-01-11 14:50:10 +01007245 psa_wipe_all_key_slots();
7246 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7247 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007248 }
7249 /* Wipe all remaining data, including configuration.
7250 * In particular, this sets all state indicator to the value
7251 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007252 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007253
7254 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007255 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007256}
7257
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007258#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7259/** Recover a transaction that was interrupted by a power failure.
7260 *
7261 * This function is called during initialization, before psa_crypto_init()
7262 * returns. If this function returns a failure status, the initialization
7263 * fails.
7264 */
7265static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007266 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007267{
Gilles Peskine449bd832023-01-11 14:50:10 +01007268 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007269 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7270 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007271 /* TODO - fall through to the failure case until this
7272 * is implemented.
7273 * https://github.com/ARMmbed/mbed-crypto/issues/218
7274 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007275 default:
7276 /* We found an unsupported transaction in the storage.
7277 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007278 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007279 }
7280}
7281#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7282
Gilles Peskine449bd832023-01-11 14:50:10 +01007283psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007284{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007285 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007286
Gilles Peskinec6b69072018-11-20 21:42:52 +01007287 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007288 if (global_data.initialized != 0) {
7289 return PSA_SUCCESS;
7290 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007291
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007292 /* Init drivers */
7293 status = psa_driver_wrapper_init();
7294 if (status != PSA_SUCCESS) {
7295 goto exit;
7296 }
7297 global_data.drivers_initialized = 1;
7298
Gilles Peskine30524eb2020-11-13 17:02:26 +01007299 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007300 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007301 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007302 status = mbedtls_psa_random_seed(&global_data.rng);
7303 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007304 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007305 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007306 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007307
Gilles Peskine449bd832023-01-11 14:50:10 +01007308 status = psa_initialize_key_slots();
7309 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007310 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007311 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007312
Gilles Peskine4b734222019-07-24 15:56:31 +02007313#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007314 status = psa_crypto_load_transaction();
7315 if (status == PSA_SUCCESS) {
7316 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7317 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007318 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007319 }
7320 status = psa_crypto_stop_transaction();
7321 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007322 /* There's no transaction to complete. It's all good. */
7323 status = PSA_SUCCESS;
7324 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007325#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007326
Gilles Peskinec6b69072018-11-20 21:42:52 +01007327 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007328 global_data.initialized = 1;
7329
7330exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007331 if (status != PSA_SUCCESS) {
7332 mbedtls_psa_crypto_free();
7333 }
7334 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007335}
7336
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007337psa_status_t psa_crypto_driver_pake_get_password_len(
7338 const psa_crypto_driver_pake_inputs_t *inputs,
7339 size_t *password_len)
7340{
7341 if (inputs->password_len == 0) {
7342 return PSA_ERROR_BAD_STATE;
7343 }
7344
7345 *password_len = inputs->password_len;
7346
7347 return PSA_SUCCESS;
7348}
7349
7350psa_status_t psa_crypto_driver_pake_get_password(
7351 const psa_crypto_driver_pake_inputs_t *inputs,
7352 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7353{
7354 if (inputs->password_len == 0) {
7355 return PSA_ERROR_BAD_STATE;
7356 }
7357
7358 if (buffer_size < inputs->password_len) {
7359 return PSA_ERROR_BUFFER_TOO_SMALL;
7360 }
7361
7362 memcpy(buffer, inputs->password, inputs->password_len);
7363 *buffer_length = inputs->password_len;
7364
7365 return PSA_SUCCESS;
7366}
7367
7368psa_status_t psa_crypto_driver_pake_get_role(
7369 const psa_crypto_driver_pake_inputs_t *inputs,
7370 psa_pake_role_t *role)
7371{
7372 if (inputs->role == PSA_PAKE_ROLE_NONE) {
7373 return PSA_ERROR_BAD_STATE;
7374 }
7375
7376 *role = inputs->role;
7377
7378 return PSA_SUCCESS;
7379}
7380
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007381psa_status_t psa_crypto_driver_pake_get_user_len(
7382 const psa_crypto_driver_pake_inputs_t *inputs,
7383 size_t *user_len)
7384{
7385 if (inputs->user_len == 0) {
7386 return PSA_ERROR_BAD_STATE;
7387 }
7388
7389 *user_len = inputs->user_len;
7390
7391 return PSA_SUCCESS;
7392}
7393
7394psa_status_t psa_crypto_driver_pake_get_user(
7395 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007396 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007397{
7398 if (inputs->user_len == 0) {
7399 return PSA_ERROR_BAD_STATE;
7400 }
7401
Przemek Stekielc0e62502023-03-14 11:49:36 +01007402 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007403 return PSA_ERROR_BUFFER_TOO_SMALL;
7404 }
7405
Przemek Stekielc0e62502023-03-14 11:49:36 +01007406 memcpy(user_id, inputs->user, inputs->user_len);
7407 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007408
7409 return PSA_SUCCESS;
7410}
7411
7412psa_status_t psa_crypto_driver_pake_get_peer_len(
7413 const psa_crypto_driver_pake_inputs_t *inputs,
7414 size_t *peer_len)
7415{
7416 if (inputs->peer_len == 0) {
7417 return PSA_ERROR_BAD_STATE;
7418 }
7419
7420 *peer_len = inputs->peer_len;
7421
7422 return PSA_SUCCESS;
7423}
7424
7425psa_status_t psa_crypto_driver_pake_get_peer(
7426 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007427 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007428{
7429 if (inputs->peer_len == 0) {
7430 return PSA_ERROR_BAD_STATE;
7431 }
7432
Przemek Stekielc0e62502023-03-14 11:49:36 +01007433 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007434 return PSA_ERROR_BUFFER_TOO_SMALL;
7435 }
7436
Przemek Stekielc0e62502023-03-14 11:49:36 +01007437 memcpy(peer_id, inputs->peer, inputs->peer_len);
7438 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007439
7440 return PSA_SUCCESS;
7441}
7442
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007443psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7444 const psa_crypto_driver_pake_inputs_t *inputs,
7445 psa_pake_cipher_suite_t *cipher_suite)
7446{
7447 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7448 return PSA_ERROR_BAD_STATE;
7449 }
7450
7451 *cipher_suite = inputs->cipher_suite;
7452
7453 return PSA_SUCCESS;
7454}
7455
Neil Armstronga7d08c32022-06-01 18:21:20 +02007456psa_status_t psa_pake_setup(
7457 psa_pake_operation_t *operation,
7458 const psa_pake_cipher_suite_t *cipher_suite)
7459{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007460 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007461
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007462 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007463 status = PSA_ERROR_BAD_STATE;
7464 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007465 }
7466
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007467 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007468 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007469 status = PSA_ERROR_INVALID_ARGUMENT;
7470 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007471 }
7472
Przemek Stekiel51eac532022-12-07 11:04:51 +01007473 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7474
Przemek Stekiele12ed362022-12-21 12:54:46 +01007475 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007476 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7477 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007478 operation->data.inputs.cipher_suite = *cipher_suite;
7479
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007480#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007481 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007482 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007483 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007484
Przemek Stekiele12ed362022-12-21 12:54:46 +01007485 computation_stage->state = PSA_PAKE_STATE_SETUP;
7486 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7487 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7488 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007489 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007490#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007491 {
7492 status = PSA_ERROR_NOT_SUPPORTED;
7493 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007494 }
7495
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007496 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7497
Przemek Stekiel51eac532022-12-07 11:04:51 +01007498 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007499exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007500 psa_pake_abort(operation);
7501 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007502}
7503
7504psa_status_t psa_pake_set_password_key(
7505 psa_pake_operation_t *operation,
7506 mbedtls_svc_key_id_t password)
7507{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007508 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007509 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7510 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007511
Przemek Stekiel51eac532022-12-07 11:04:51 +01007512 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007513 status = PSA_ERROR_BAD_STATE;
7514 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007515 }
7516
Przemek Stekiel6c764412022-11-22 14:05:12 +01007517 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7518 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007519 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007520 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007521 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007522 }
7523
Przemek Stekiel6c764412022-11-22 14:05:12 +01007524 psa_key_attributes_t attributes = {
7525 .core = slot->attr
7526 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007527
Przemek Stekiel51eac532022-12-07 11:04:51 +01007528 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007529
Przemek Stekiel51eac532022-12-07 11:04:51 +01007530 if (type != PSA_KEY_TYPE_PASSWORD &&
7531 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7532 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007533 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007534 }
7535
Przemek Stekiel51eac532022-12-07 11:04:51 +01007536 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7537 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007538 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007539 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007540 }
7541
7542 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7543 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007544 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007545exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007546 if (status != PSA_SUCCESS) {
7547 psa_pake_abort(operation);
7548 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007549 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007550 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007551}
7552
7553psa_status_t psa_pake_set_user(
7554 psa_pake_operation_t *operation,
7555 const uint8_t *user_id,
7556 size_t user_id_len)
7557{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007558 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007559
7560 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007561 status = PSA_ERROR_BAD_STATE;
7562 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007563 }
7564
Przemek Stekiel51eac532022-12-07 11:04:51 +01007565 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007566 status = PSA_ERROR_INVALID_ARGUMENT;
7567 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007568 }
7569
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007570 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007571 status = PSA_ERROR_BAD_STATE;
7572 goto exit;
7573 }
7574
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007575 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007576 if ((user_id_len != sizeof(jpake_server_id) ||
7577 memcmp(user_id, jpake_server_id, user_id_len) != 0) &&
7578 (user_id_len != sizeof(jpake_client_id) ||
7579 memcmp(user_id, jpake_client_id, user_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007580 status = PSA_ERROR_NOT_SUPPORTED;
7581 goto exit;
7582 }
7583
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007584 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7585 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007586 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7587 goto exit;
7588 }
7589
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007590 memcpy(operation->data.inputs.user, user_id, user_id_len);
7591 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007592
7593 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007594exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007595 psa_pake_abort(operation);
7596 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007597}
7598
7599psa_status_t psa_pake_set_peer(
7600 psa_pake_operation_t *operation,
7601 const uint8_t *peer_id,
7602 size_t peer_id_len)
7603{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007604 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007605
7606 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007607 status = PSA_ERROR_BAD_STATE;
7608 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007609 }
7610
Przemek Stekiel51eac532022-12-07 11:04:51 +01007611 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007612 status = PSA_ERROR_INVALID_ARGUMENT;
7613 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007614 }
7615
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007616 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007617 status = PSA_ERROR_BAD_STATE;
7618 goto exit;
7619 }
7620
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007621 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007622 if ((peer_id_len != sizeof(jpake_server_id) ||
7623 memcmp(peer_id, jpake_server_id, peer_id_len) != 0) &&
7624 (peer_id_len != sizeof(jpake_client_id) ||
7625 memcmp(peer_id, jpake_client_id, peer_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007626 status = PSA_ERROR_NOT_SUPPORTED;
7627 goto exit;
7628 }
7629
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007630 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7631 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007632 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7633 goto exit;
7634 }
7635
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007636 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7637 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007638
7639 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007640exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007641 psa_pake_abort(operation);
7642 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007643}
7644
7645psa_status_t psa_pake_set_role(
7646 psa_pake_operation_t *operation,
7647 psa_pake_role_t role)
7648{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007649 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007650
Przemek Stekiel51eac532022-12-07 11:04:51 +01007651 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007652 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007653 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007654 }
7655
Przemek Stekiel09104b82023-03-06 14:11:51 +01007656 switch (operation->alg) {
7657#if defined(PSA_WANT_ALG_JPAKE)
7658 case PSA_ALG_JPAKE:
7659 if (role == PSA_PAKE_ROLE_NONE) {
7660 return PSA_SUCCESS;
7661 }
7662 status = PSA_ERROR_INVALID_ARGUMENT;
7663 break;
7664#endif
7665 default:
7666 (void) role;
7667 status = PSA_ERROR_NOT_SUPPORTED;
7668 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007669 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007670exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007671 psa_pake_abort(operation);
7672 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007673}
7674
Przemek Stekielb09c4872023-01-17 12:05:38 +01007675/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007676#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007677static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007678 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007679{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007680 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007681 case PSA_PAKE_OUTPUT_X1_X2:
7682 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007683 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007684 case PSA_PAKE_X1_STEP_KEY_SHARE:
7685 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007686 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7687 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007688 case PSA_PAKE_X1_STEP_ZK_PROOF:
7689 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007690 case PSA_PAKE_X2_STEP_KEY_SHARE:
7691 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007692 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7693 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007694 case PSA_PAKE_X2_STEP_ZK_PROOF:
7695 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007696 default:
7697 return PSA_JPAKE_STEP_INVALID;
7698 }
7699 break;
7700 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007701 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007702 case PSA_PAKE_X1_STEP_KEY_SHARE:
7703 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007704 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7705 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007706 case PSA_PAKE_X1_STEP_ZK_PROOF:
7707 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007708 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007709 return PSA_JPAKE_STEP_INVALID;
7710 }
7711 break;
7712 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007713 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007714 case PSA_PAKE_X1_STEP_KEY_SHARE:
7715 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007716 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7717 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007718 case PSA_PAKE_X1_STEP_ZK_PROOF:
7719 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007720 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007721 return PSA_JPAKE_STEP_INVALID;
7722 }
7723 break;
7724 default:
7725 return PSA_JPAKE_STEP_INVALID;
7726 }
7727 return PSA_JPAKE_STEP_INVALID;
7728}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007729#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007730
Przemek Stekiel2797d372022-12-22 11:19:22 +01007731static psa_status_t psa_pake_complete_inputs(
7732 psa_pake_operation_t *operation)
7733{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007734 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007735 /* Create copy of the inputs on stack as inputs share memory
7736 with the driver context which will be setup by the driver. */
7737 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007738
Przemek Stekielfde11282023-03-13 16:06:09 +01007739 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007740 return PSA_ERROR_BAD_STATE;
7741 }
7742
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007743 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007744 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7745 return PSA_ERROR_BAD_STATE;
7746 }
7747 if (memcmp(inputs.user, jpake_client_id, inputs.user_len) == 0 &&
7748 memcmp(inputs.peer, jpake_server_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007749 inputs.role = PSA_PAKE_ROLE_CLIENT;
7750 } else
Przemek Stekielfde11282023-03-13 16:06:09 +01007751 if (memcmp(inputs.user, jpake_server_id, inputs.user_len) == 0 &&
7752 memcmp(inputs.peer, jpake_client_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007753 inputs.role = PSA_PAKE_ROLE_SERVER;
7754 }
7755
7756 if (inputs.role != PSA_PAKE_ROLE_CLIENT &&
7757 inputs.role != PSA_PAKE_ROLE_SERVER) {
7758 return PSA_ERROR_NOT_SUPPORTED;
7759 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007760 }
7761
Przemek Stekiel18620a32023-01-17 16:34:52 +01007762 /* Clear driver context */
7763 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7764
7765 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007766
7767 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007768 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7769 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007770
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007771 /* User and peer are translated to role. */
7772 mbedtls_free(inputs.user);
7773 mbedtls_free(inputs.peer);
7774
Przemek Stekiel2797d372022-12-22 11:19:22 +01007775 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007776#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007777 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007778 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007779 psa_jpake_computation_stage_t *computation_stage =
7780 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007781 computation_stage->state = PSA_PAKE_STATE_READY;
7782 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7783 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7784 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007785 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007786#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007787 {
7788 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007789 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007790 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007791 return status;
7792}
7793
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007794#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007795static psa_status_t psa_jpake_output_prologue(
7796 psa_pake_operation_t *operation,
7797 psa_pake_step_t step)
7798{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007799 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7800 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7801 step != PSA_PAKE_STEP_ZK_PROOF) {
7802 return PSA_ERROR_INVALID_ARGUMENT;
7803 }
7804
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007805 psa_jpake_computation_stage_t *computation_stage =
7806 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007807
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007808 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7809 return PSA_ERROR_BAD_STATE;
7810 }
7811
7812 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7813 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
7814 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
7815 return PSA_ERROR_BAD_STATE;
7816 }
7817
7818 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7819 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007820 return PSA_ERROR_BAD_STATE;
7821 }
7822
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007823 switch (computation_stage->output_step) {
7824 case PSA_PAKE_STEP_X1_X2:
7825 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007826 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007827 case PSA_PAKE_STEP_X2S:
7828 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007829 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007830 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007831 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007832 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007833
7834 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7835 }
7836
7837 /* Check if step matches current sequence */
7838 switch (computation_stage->sequence) {
7839 case PSA_PAKE_X1_STEP_KEY_SHARE:
7840 case PSA_PAKE_X2_STEP_KEY_SHARE:
7841 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7842 return PSA_ERROR_BAD_STATE;
7843 }
7844 break;
7845
7846 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7847 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7848 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7849 return PSA_ERROR_BAD_STATE;
7850 }
7851 break;
7852
7853 case PSA_PAKE_X1_STEP_ZK_PROOF:
7854 case PSA_PAKE_X2_STEP_ZK_PROOF:
7855 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7856 return PSA_ERROR_BAD_STATE;
7857 }
7858 break;
7859
7860 default:
7861 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007862 }
7863
7864 return PSA_SUCCESS;
7865}
7866
7867static psa_status_t psa_jpake_output_epilogue(
7868 psa_pake_operation_t *operation)
7869{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007870 psa_jpake_computation_stage_t *computation_stage =
7871 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007872
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007873 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007874 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007875 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007876 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007877 computation_stage->state = PSA_PAKE_STATE_READY;
7878 computation_stage->output_step++;
7879 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7880 } else {
7881 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007882 }
7883
7884 return PSA_SUCCESS;
7885}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007886#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007887
Neil Armstronga7d08c32022-06-01 18:21:20 +02007888psa_status_t psa_pake_output(
7889 psa_pake_operation_t *operation,
7890 psa_pake_step_t step,
7891 uint8_t *output,
7892 size_t output_size,
7893 size_t *output_length)
7894{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007895 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007896 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007897 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007898
7899 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007900 status = psa_pake_complete_inputs(operation);
7901 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007902 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007903 }
7904 }
7905
7906 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007907 status = PSA_ERROR_BAD_STATE;
7908 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007909 }
7910
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007911 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007912 status = PSA_ERROR_INVALID_ARGUMENT;
7913 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007914 }
7915
Przemek Stekiele12ed362022-12-21 12:54:46 +01007916 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007917#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007918 case PSA_ALG_JPAKE:
7919 status = psa_jpake_output_prologue(operation, step);
7920 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007921 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007922 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007923 driver_step = convert_jpake_computation_stage_to_driver_step(
7924 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007925 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007926#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007927 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007928 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007929 status = PSA_ERROR_NOT_SUPPORTED;
7930 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007931 }
7932
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007933 status = psa_driver_wrapper_pake_output(operation, driver_step,
7934 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007935
7936 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007937 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007938 }
7939
7940 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007941#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007942 case PSA_ALG_JPAKE:
7943 status = psa_jpake_output_epilogue(operation);
7944 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007945 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007946 }
7947 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007948#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007949 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007950 status = PSA_ERROR_NOT_SUPPORTED;
7951 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007952 }
7953
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007954 return PSA_SUCCESS;
7955exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007956 psa_pake_abort(operation);
7957 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007958}
7959
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007960#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007961static psa_status_t psa_jpake_input_prologue(
7962 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007963 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007964{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007965 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7966 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7967 step != PSA_PAKE_STEP_ZK_PROOF) {
7968 return PSA_ERROR_INVALID_ARGUMENT;
7969 }
7970
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007971 psa_jpake_computation_stage_t *computation_stage =
7972 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007973
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007974 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7975 return PSA_ERROR_BAD_STATE;
7976 }
7977
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007978 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7979 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
7980 computation_stage->state != PSA_PAKE_INPUT_X4S) {
7981 return PSA_ERROR_BAD_STATE;
7982 }
7983
7984 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7985 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007986 return PSA_ERROR_BAD_STATE;
7987 }
7988
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007989 switch (computation_stage->input_step) {
7990 case PSA_PAKE_STEP_X1_X2:
7991 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007992 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007993 case PSA_PAKE_STEP_X2S:
7994 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007995 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007996 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007997 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007998 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007999
8000 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
8001 }
8002
8003 /* Check if step matches current sequence */
8004 switch (computation_stage->sequence) {
8005 case PSA_PAKE_X1_STEP_KEY_SHARE:
8006 case PSA_PAKE_X2_STEP_KEY_SHARE:
8007 if (step != PSA_PAKE_STEP_KEY_SHARE) {
8008 return PSA_ERROR_BAD_STATE;
8009 }
8010 break;
8011
8012 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
8013 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
8014 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
8015 return PSA_ERROR_BAD_STATE;
8016 }
8017 break;
8018
8019 case PSA_PAKE_X1_STEP_ZK_PROOF:
8020 case PSA_PAKE_X2_STEP_ZK_PROOF:
8021 if (step != PSA_PAKE_STEP_ZK_PROOF) {
8022 return PSA_ERROR_BAD_STATE;
8023 }
8024 break;
8025
8026 default:
8027 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008028 }
8029
8030 return PSA_SUCCESS;
8031}
8032
Przemek Stekiele12ed362022-12-21 12:54:46 +01008033static psa_status_t psa_jpake_input_epilogue(
8034 psa_pake_operation_t *operation)
8035{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008036 psa_jpake_computation_stage_t *computation_stage =
8037 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008038
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008039 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01008040 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008041 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01008042 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008043 computation_stage->state = PSA_PAKE_STATE_READY;
8044 computation_stage->input_step++;
8045 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
8046 } else {
8047 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008048 }
8049
8050 return PSA_SUCCESS;
8051}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008052#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008053
Neil Armstronga7d08c32022-06-01 18:21:20 +02008054psa_status_t psa_pake_input(
8055 psa_pake_operation_t *operation,
8056 psa_pake_step_t step,
8057 const uint8_t *input,
8058 size_t input_length)
8059{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008060 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008061 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008062 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8063 operation->primitive,
8064 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008065
8066 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008067 status = psa_pake_complete_inputs(operation);
8068 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008069 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008070 }
8071 }
8072
8073 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008074 status = PSA_ERROR_BAD_STATE;
8075 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008076 }
8077
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008078 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008079 status = PSA_ERROR_INVALID_ARGUMENT;
8080 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008081 }
8082
Przemek Stekiele12ed362022-12-21 12:54:46 +01008083 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008084#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008085 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008086 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008087 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008088 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008089 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008090 driver_step = convert_jpake_computation_stage_to_driver_step(
8091 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008092 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008093#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008094 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008095 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008096 status = PSA_ERROR_NOT_SUPPORTED;
8097 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008098 }
8099
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008100 status = psa_driver_wrapper_pake_input(operation, driver_step,
8101 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008102
8103 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008104 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008105 }
8106
8107 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008108#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008109 case PSA_ALG_JPAKE:
8110 status = psa_jpake_input_epilogue(operation);
8111 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008112 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008113 }
8114 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008115#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008116 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008117 status = PSA_ERROR_NOT_SUPPORTED;
8118 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008119 }
8120
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008121 return PSA_SUCCESS;
8122exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008123 psa_pake_abort(operation);
8124 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008125}
8126
8127psa_status_t psa_pake_get_implicit_key(
8128 psa_pake_operation_t *operation,
8129 psa_key_derivation_operation_t *output)
8130{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008131 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008132 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008133 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008134 size_t shared_key_len = 0;
8135
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008136 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008137 status = PSA_ERROR_BAD_STATE;
8138 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008139 }
8140
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008141#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008142 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008143 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008144 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008145 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
8146 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008147 status = PSA_ERROR_BAD_STATE;
8148 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008149 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008150 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008151#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008152 {
8153 status = PSA_ERROR_NOT_SUPPORTED;
8154 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008155 }
8156
Przemek Stekiel0c781802022-11-29 14:53:13 +01008157 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8158 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008159 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008160 &shared_key_len);
8161
8162 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008163 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008164 }
8165
8166 status = psa_key_derivation_input_bytes(output,
8167 PSA_KEY_DERIVATION_INPUT_SECRET,
8168 shared_key,
8169 shared_key_len);
8170
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008171 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008172exit:
8173 abort_status = psa_pake_abort(operation);
8174 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008175}
8176
8177psa_status_t psa_pake_abort(
8178 psa_pake_operation_t *operation)
8179{
Przemek Stekield69dca92023-01-31 19:59:20 +01008180 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008181
Przemek Stekield69dca92023-01-31 19:59:20 +01008182 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008183 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008184 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008185
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008186 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8187 if (operation->data.inputs.password != NULL) {
8188 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008189 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008190 mbedtls_free(operation->data.inputs.password);
8191 }
8192 if (operation->data.inputs.user != NULL) {
8193 mbedtls_free(operation->data.inputs.user);
8194 }
8195 if (operation->data.inputs.peer != NULL) {
8196 mbedtls_free(operation->data.inputs.peer);
8197 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008198 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008199 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008200
Przemek Stekield69dca92023-01-31 19:59:20 +01008201 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008202}
Neil Armstronga7d08c32022-06-01 18:21:20 +02008203
Gilles Peskinee59236f2018-01-27 23:32:46 +01008204#endif /* MBEDTLS_PSA_CRYPTO_C */