blob: d0cdd626e88ca4061c3f051a6b2bd57ee17abeb5 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
John Durkop07cc04a2020-11-16 22:08:34 -080025#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
26#include "check_crypto_config.h"
27#endif
28
Gilles Peskinee59236f2018-01-27 23:32:46 +010029#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010030#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031
Ronald Crond6d28882020-12-14 14:56:02 +010032#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020035#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010036#include "psa_crypto_ecp.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010037#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010038#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010039#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010040#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020041#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020042#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020043#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010044#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010045/* Include internal declarations that are useful for implementing persistently
46 * stored keys. */
47#include "psa_crypto_storage.h"
48
Gilles Peskineb2b64d32020-12-14 16:43:58 +010049#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010050
Gilles Peskineb46bef22019-07-30 21:32:04 +020051#include <assert.h>
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"
Gilles Peskinea5905292018-02-07 20:59:33 +010085
Gilles Peskine449bd832023-01-11 14:50:10 +010086#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020087
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020088#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
89 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
90 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020091#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020092#endif
93
Przemek Stekielfde11282023-03-13 16:06:09 +010094/* The only two JPAKE user/peer identifiers supported for the time being. */
95static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
96static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
Przemek Stekiel18cd6c92023-03-06 15:40:35 +010097
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010098/****************************************************************/
99/* Global data, support functions and library management */
100/****************************************************************/
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200103{
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105}
106
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100107/* Values for psa_global_data_t::rng_state */
108#define RNG_NOT_INITIALIZED 0
109#define RNG_INITIALIZED 1
110#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100113 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100114 unsigned rng_state : 2;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100115 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100116} psa_global_data_t;
117
118static psa_global_data_t global_data;
119
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100120#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
121mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
122 &global_data.rng.drbg;
123#endif
124
itayzafrir0adf0fc2018-09-06 16:24:41 +0300125#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 if (global_data.initialized == 0) \
127 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100130{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100131 /* Mbed TLS error codes can combine a high-level error code and a
132 * low-level error code. The low-level error usually reflects the
133 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 int low_level_ret = -(-ret & 0x007f);
135 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100136 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100138
139 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
140 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200142 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
143 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
144 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
145 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
146 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200148 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200150 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200152
Jaeden Amero93e21112019-02-20 13:57:28 +0000153#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000154 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000155#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100156 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100158
159 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100161 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100163
Gilles Peskine26869f22019-05-06 15:25:00 +0200164 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200166
167 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200169 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200171
Gilles Peskinea5905292018-02-07 20:59:33 +0100172 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100178 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100180 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100182 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100184 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
188 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100189 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
190 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100191 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
194 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100196 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100198#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100199
200 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100202
Gilles Peskinee59236f2018-01-27 23:32:46 +0100203 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
204 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
205 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100207
208 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200210 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100212 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100214
Gilles Peskine82e57d12020-11-13 21:31:17 +0100215#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100217 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
218 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100219 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100221 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
222 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100224 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100226#endif
227
Gilles Peskinea5905292018-02-07 20:59:33 +0100228 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100230 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100232 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100234 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100236
Gilles Peskinef76aa772018-10-29 19:24:33 +0100237 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100239 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100241 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100243 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100245 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100247 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
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_NOT_ACCEPTABLE:
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_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100253
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100254 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100256 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
257 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100259 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100261 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
262 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100264 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100266 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
267 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100269 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_INVALID_ALG:
272 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
273 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100275 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200277 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100279
Gilles Peskineff2d2002019-05-06 15:26:23 +0200280 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200282 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200284
Gilles Peskinea5905292018-02-07 20:59:33 +0100285 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100287 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100289 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100291 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100293 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
294 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100298 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100300 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200302
itayzafrir5c753392018-05-08 11:18:38 +0300303 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300304 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300306 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300308 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300310 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
311 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300313 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100315 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100317
Paul Elliott588f8ed2022-12-02 18:10:26 +0000318 case MBEDTLS_ERR_ECP_IN_PROGRESS:
319 return PSA_OPERATION_INCOMPLETE;
320
Janos Follatha13b9052019-11-22 12:48:59 +0000321 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300323
Gilles Peskinee59236f2018-01-27 23:32:46 +0100324 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100326 }
327}
328
Paul Elliottdc42ca82023-02-24 18:11:59 +0000329/**
330 * \brief For output buffers which contain "tags"
331 * (outputs that may be checked for validity like
332 * hashes, MACs and signatures), fill the unused
333 * part of the output buffer (the whole buffer on
334 * error, the trailing part on success) with
335 * something that isn't a valid tag (barring an
336 * attack on the tag and deliberately-crafted
337 * input), in case the caller doesn't check the
338 * return status properly.
339 *
340 * \param output_buffer Pointer to buffer to wipe. May not be NULL
341 * unless \p output_buffer_size is zero.
342 * \param status Status of function called to generate
343 * output_buffer originally
344 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
345 * could be NULL.
346 * \param output_buffer_length Length of data written to output_buffer, must be
347 * less than \p output_buffer_size
348 */
349static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000350 size_t output_buffer_size, size_t output_buffer_length)
351{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000352 size_t offset = 0;
353
354 if (output_buffer_size == 0) {
355 /* If output_buffer_size is 0 then we have nothing to do. We must not
356 call memset because output_buffer may be NULL in this case */
357 return;
358 }
359
360 if (status == PSA_SUCCESS) {
361 offset = output_buffer_length;
362 }
363
364 memset(output_buffer + offset, '!', output_buffer_size - offset);
365}
366
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200367
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200368
369
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100370/****************************************************************/
371/* Key management */
372/****************************************************************/
373
John Durkop9814fa22020-11-04 12:28:15 -0800374#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800375 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cronf467d632021-11-19 18:02:34 +0100376 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
377 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
378 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100379mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
380 size_t bits,
381 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200382{
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100384 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100386#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100387 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100389#endif
390#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100391 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100393#endif
394#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100395 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100397#endif
398#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100399 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100401#endif
402#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100403 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100405 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 if (bits_is_sloppy) {
407 return MBEDTLS_ECP_DP_SECP521R1;
408 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100409 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100410#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100411 }
412 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100413
Paul Elliott8ff510a2020-06-02 17:19:28 +0100414 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100416#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100417 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100419#endif
420#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100421 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100423#endif
424#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100425 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100427#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100428 }
429 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100430
Paul Elliott8ff510a2020-06-02 17:19:28 +0100431 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100433#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100434 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100436 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if (bits_is_sloppy) {
438 return MBEDTLS_ECP_DP_CURVE25519;
439 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100440 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100441#endif
442#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100443 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100445#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100446 }
447 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100448
Paul Elliott8ff510a2020-06-02 17:19:28 +0100449 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100451#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100452 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100454#endif
455#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100456 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100458#endif
459#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100460 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100462#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100463 }
464 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200465 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100466
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100467 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200469}
John Durkop9814fa22020-11-04 12:28:15 -0800470#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cronf467d632021-11-19 18:02:34 +0100471 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
472 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
473 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
474 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
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)) {
John Durkop9814fa22020-11-04 12:28:15 -0800632#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
634 if (PSA_KEY_TYPE_IS_ECC(type)) {
635 return mbedtls_psa_ecp_import_key(attributes,
636 data, data_length,
637 key_buffer, key_buffer_size,
638 key_buffer_length,
639 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100640 }
John Durkop9814fa22020-11-04 12:28:15 -0800641#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
642 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700643#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
645 if (PSA_KEY_TYPE_IS_RSA(type)) {
646 return mbedtls_psa_rsa_import_key(attributes,
647 data, data_length,
648 key_buffer, key_buffer_size,
649 key_buffer_length,
650 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200651 }
John Durkop9814fa22020-11-04 12:28:15 -0800652#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
653 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100654 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100657}
658
Gilles Peskinef603c712019-01-19 13:40:11 +0100659/** Calculate the intersection of two algorithm usage policies.
660 *
661 * Return 0 (which allows no operation) on incompatibility.
662 */
663static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100664 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100665 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100667{
Gilles Peskine549ea862019-05-22 11:45:59 +0200668 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 if (alg1 == alg2) {
670 return alg1;
671 }
Steven Cooreman03488022021-02-18 12:07:20 +0100672 /* If the policies are from the same hash-and-sign family, check
673 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
675 PSA_ALG_IS_SIGN_HASH(alg2) &&
676 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
677 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
678 return alg2;
679 }
680 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
681 return alg1;
682 }
Steven Cooreman03488022021-02-18 12:07:20 +0100683 }
684 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100685 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100686 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
688 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
689 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
690 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
691 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100692 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100693
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100694 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
696 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
697 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
698 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100699 }
700 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
702 (alg1_len <= alg2_len)) {
703 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100704 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
706 (alg2_len <= alg1_len)) {
707 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100708 }
709 }
710 /* If the policies are from the same MAC family, check whether one
711 * of them is a minimum-MAC-length policy. Calculate the most
712 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
714 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
715 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100716 /* Validate the combination of key type and algorithm. Since the base
717 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
719 return 0;
720 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100721
Steven Cooreman31a876d2021-03-03 20:47:40 +0100722 /* Get the (exact or at-least) output lengths for both sides of the
723 * requested intersection. None of the currently supported algorithms
724 * have an output length dependent on the actual key size, so setting it
725 * to a bogus value of 0 is currently OK.
726 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100727 * Note that for at-least-this-length wildcard algorithms, the output
728 * length is set to the shortest allowed length, which allows us to
729 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
731 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100732 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100733
Steven Cooremana1d83222021-02-25 10:20:29 +0100734 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
736 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
737 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100738 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100739
740 /* If only one is an at-least-this-length policy, the intersection would
741 * be the other (fixed-length) policy as long as said fixed length is
742 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
744 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100745 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
747 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100748 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100749
Steven Cooremancd640932021-03-08 12:00:27 +0100750 /* If none of them are wildcards, check whether they define the same tag
751 * length. This is still possible here when one is default-length and
752 * the other specific-length. Ensure to always return the
753 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 if (alg1_len == alg2_len) {
755 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
756 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100757 }
758 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100760}
761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762static int psa_key_algorithm_permits(psa_key_type_t key_type,
763 psa_algorithm_t policy_alg,
764 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200765{
Gilles Peskine549ea862019-05-22 11:45:59 +0200766 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 if (requested_alg == policy_alg) {
768 return 1;
769 }
Steven Cooreman03488022021-02-18 12:07:20 +0100770 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
771 * and requested_alg is the same hash-and-sign family with any hash,
772 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
774 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
775 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
776 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100777 }
778 /* If policy_alg is a wildcard AEAD algorithm of the same base as
779 * the requested algorithm, check the requested tag length to be
780 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if (PSA_ALG_IS_AEAD(policy_alg) &&
782 PSA_ALG_IS_AEAD(requested_alg) &&
783 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
784 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
785 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
786 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
787 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100788 }
Steven Cooremancd640932021-03-08 12:00:27 +0100789 /* If policy_alg is a MAC algorithm of the same base as the requested
790 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 if (PSA_ALG_IS_MAC(policy_alg) &&
792 PSA_ALG_IS_MAC(requested_alg) &&
793 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
794 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100795 /* Validate the combination of key type and algorithm. Since the policy
796 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
798 return 0;
799 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100800
Steven Cooreman31a876d2021-03-03 20:47:40 +0100801 /* Get both the requested output length for the algorithm which is to be
802 * verified, and the default output length for the base algorithm.
803 * Note that none of the currently supported algorithms have an output
804 * length dependent on actual key size, so setting it to a bogus value
805 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100806 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100808 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 key_type, 0,
810 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100811
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100812 /* If the policy is default-length, only allow an algorithm with
813 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
815 return requested_output_length == default_output_length;
816 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100817
818 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100819 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
821 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
822 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100823 }
824
Steven Cooremancd640932021-03-08 12:00:27 +0100825 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
826 * check for the requested MAC length to be equal to or longer than the
827 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
829 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
830 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100831 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200832 }
Steven Cooremance48e852020-10-05 16:02:45 +0200833 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200834 * a key derivation with that key agreement should also be allowed. This
835 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
837 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
838 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
839 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200840 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100841 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200843}
844
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100845/** Test whether a policy permits an algorithm.
846 *
847 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100848 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100849 * \note This function requires providing the key type for which the policy is
850 * being validated, since some algorithm policy definitions (e.g. MAC)
851 * have different properties depending on what kind of cipher it is
852 * combined with.
853 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100854 * \retval PSA_SUCCESS When \p alg is a specific algorithm
855 * allowed by the \p policy.
856 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
857 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
858 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100859 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100860static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
861 psa_key_type_t key_type,
862 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100863{
Steven Cooremand788fab2021-02-25 11:29:17 +0100864 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (alg == 0) {
866 return PSA_ERROR_INVALID_ARGUMENT;
867 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100868
Steven Cooreman7e39f052021-02-23 14:18:32 +0100869 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if (PSA_ALG_IS_WILDCARD(alg)) {
871 return PSA_ERROR_INVALID_ARGUMENT;
872 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
875 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
876 return PSA_SUCCESS;
877 } else {
878 return PSA_ERROR_NOT_PERMITTED;
879 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100880}
881
Gilles Peskinef603c712019-01-19 13:40:11 +0100882/** Restrict a key policy based on a constraint.
883 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100884 * \note This function requires providing the key type for which the policy is
885 * being restricted, since some algorithm policy definitions (e.g. MAC)
886 * have different properties depending on what kind of cipher it is
887 * combined with.
888 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100889 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100890 * \param[in,out] policy The policy to restrict.
891 * \param[in] constraint The policy constraint to apply.
892 *
893 * \retval #PSA_SUCCESS
894 * \c *policy contains the intersection of the original value of
895 * \c *policy and \c *constraint.
896 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100897 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100898 * \c *policy is unchanged.
899 */
900static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100901 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100902 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100904{
905 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 psa_key_policy_algorithm_intersection(key_type, policy->alg,
907 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200908 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
910 constraint->alg2);
911 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
912 return PSA_ERROR_INVALID_ARGUMENT;
913 }
914 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
915 return PSA_ERROR_INVALID_ARGUMENT;
916 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100917 policy->usage &= constraint->usage;
918 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200919 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100921}
922
Przemek Stekiel061f6942022-11-30 10:51:35 +0100923/** Get the description of a key given its identifier and policy constraints
924 * and lock it.
925 *
926 * The key must have allow all the usage flags set in \p usage. If \p alg is
927 * nonzero, the key must allow operations with this algorithm. If \p alg is
928 * zero, the algorithm is not checked.
929 *
930 * In case of a persistent key, the function loads the description of the key
931 * into a key slot if not already done.
932 *
933 * On success, the returned key slot is locked. It is the responsibility of
934 * the caller to unlock the key slot when it does not access it anymore.
935 */
936static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100937 mbedtls_svc_key_id_t key,
938 psa_key_slot_t **p_slot,
939 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100941{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200942 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100943 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 status = psa_get_and_lock_key_slot(key, p_slot);
946 if (status != PSA_SUCCESS) {
947 return status;
948 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200949 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100950
951 /* Enforce that usage policy for the key slot contains all the flags
952 * required by the usage parameter. There is one exception: public
953 * keys can always be exported, so we treat public key objects as
954 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100956 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200958
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100960 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200961 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100962 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100963
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800964 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 if (alg != 0) {
966 status = psa_key_policy_permits(&slot->attr.policy,
967 slot->attr.type,
968 alg);
969 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100970 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100972 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200975
976error:
977 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200979
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100981}
Darryl Green940d72c2018-07-13 13:18:51 +0100982
Ronald Cron5c522922020-11-14 16:35:34 +0100983/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200984 *
985 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200986 * available, as opposed to a key in a secure element and/or to be used
987 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200988 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200989 * This is a temporary function that may be used instead of
990 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
991 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200992 *
Ronald Cron5c522922020-11-14 16:35:34 +0100993 * On success, the returned key slot is locked. It is the responsibility of the
994 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200995 */
Ronald Cron5c522922020-11-14 16:35:34 +0100996static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
997 mbedtls_svc_key_id_t key,
998 psa_key_slot_t **p_slot,
999 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001001{
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1003 usage, alg);
1004 if (status != PSA_SUCCESS) {
1005 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001006 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001007
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1009 psa_unlock_key_slot(*p_slot);
1010 *p_slot = NULL;
1011 return PSA_ERROR_NOT_SUPPORTED;
1012 }
1013
1014 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001015}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001016
Gilles Peskine449bd832023-01-11 14:50:10 +01001017psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001018{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001019 /* Data pointer will always be either a valid pointer or NULL in an
1020 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 if (slot->key.data != NULL) {
1022 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1023 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001024
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001026 slot->key.data = NULL;
1027 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001030}
1031
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001032/** Completely wipe a slot in memory, including its policy.
1033 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001034psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001035{
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001037
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 /*
1039 * As the return error code may not be handled in case of multiple errors,
1040 * do our best to report an unexpected lock counter. Assert with
1041 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1042 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1043 * function is called as part of the execution of a test suite, the
1044 * execution of the test suite is stopped in error if the assertion fails.
1045 */
1046 if (slot->lock_count != 1) {
1047 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001048 status = PSA_ERROR_CORRUPTION_DETECTED;
1049 }
1050
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001051 /* Multipart operations may still be using the key. This is safe
1052 * because all multipart operation objects are independent from
1053 * the key slot: if they need to access the key after the setup
1054 * phase, they have a copy of the key. Note that this means that
1055 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001056 /* At this point, key material and other type-specific content has
1057 * been wiped. Clear remaining metadata. We can call memset and not
1058 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 memset(slot, 0, sizeof(*slot));
1060 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001061}
1062
Gilles Peskine449bd832023-01-11 14:50:10 +01001063psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001064{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001065 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001066 psa_status_t status; /* status of the last operation */
1067 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001068#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1069 psa_se_drv_table_entry_t *driver;
1070#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if (mbedtls_svc_key_id_is_null(key)) {
1073 return PSA_SUCCESS;
1074 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001075
Ronald Cronf2911112020-10-29 17:51:10 +01001076 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001077 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001078 * key, this will load the key description from persistent memory if not
1079 * done yet. We cannot avoid this loading as without it we don't know if
1080 * the key is operated by an SE or not and this information is needed by
1081 * the current implementation.
1082 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 status = psa_get_and_lock_key_slot(key, &slot);
1084 if (status != PSA_SUCCESS) {
1085 return status;
1086 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001087
Ronald Cronf2911112020-10-29 17:51:10 +01001088 /*
1089 * If the key slot containing the key description is under access by the
1090 * library (apart from the present access), the key cannot be destroyed
1091 * yet. For the time being, just return in error. Eventually (to be
1092 * implemented), the key should be destroyed when all accesses have
1093 * stopped.
1094 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if (slot->lock_count > 1) {
1096 psa_unlock_key_slot(slot);
1097 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001098 }
1099
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001101 /* Refuse the destruction of a read-only key (which may or may not work
1102 * if we attempt it, depending on whether the key is merely read-only
1103 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001104 * Just do the best we can, which is to wipe the copy in memory
1105 * (done in this function's cleanup code). */
1106 overall_status = PSA_ERROR_NOT_PERMITTED;
1107 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001108 }
1109
Gilles Peskine354f7672019-07-12 23:46:38 +02001110#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1112 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001113 /* For a key in a secure element, we need to do three things:
1114 * remove the key file in internal storage, destroy the
1115 * key inside the secure element, and update the driver's
1116 * persistent data. Start a transaction that will encompass these
1117 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001119 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001121 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 status = psa_crypto_save_transaction();
1123 if (status != PSA_SUCCESS) {
1124 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001125 /* We should still try to destroy the key in the secure
1126 * element and the key metadata in storage. This is especially
1127 * important if the error is that the storage is full.
1128 * But how to do it exactly without risking an inconsistent
1129 * state after a reset?
1130 * https://github.com/ARMmbed/mbed-crypto/issues/215
1131 */
1132 overall_status = status;
1133 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001134 }
1135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 status = psa_destroy_se_key(driver,
1137 psa_key_slot_get_slot_number(slot));
1138 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001139 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001141 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001142#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1143
Darryl Greend49a4992018-06-18 17:27:26 +01001144#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1146 status = psa_destroy_persistent_key(slot->attr.id);
1147 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001148 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001150
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001151 /* TODO: other slots may have a copy of the same key. We should
1152 * invalidate them.
1153 * https://github.com/ARMmbed/mbed-crypto/issues/214
1154 */
Darryl Greend49a4992018-06-18 17:27:26 +01001155 }
1156#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001157
Gilles Peskinefc762652019-07-22 19:30:34 +02001158#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 if (driver != NULL) {
1160 status = psa_save_se_persistent_data(driver);
1161 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001162 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 }
1164 status = psa_crypto_stop_transaction();
1165 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001166 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001168 }
1169#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1170
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001171exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001173 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001175 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 }
1177 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001178}
1179
John Durkop07cc04a2020-11-16 22:08:34 -08001180#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001181 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001182static psa_status_t psa_get_rsa_public_exponent(
1183 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001185{
1186 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001187 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001188 uint8_t *buffer = NULL;
1189 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001191
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1193 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001194 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 }
1196 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001197 /* It's the default value, which is reported as an empty string,
1198 * so there's nothing to do. */
1199 goto exit;
1200 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 buflen = mbedtls_mpi_size(&mpi);
1203 buffer = mbedtls_calloc(1, buflen);
1204 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001205 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1206 goto exit;
1207 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1209 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001210 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001212 attributes->domain_parameters = buffer;
1213 attributes->domain_parameters_size = buflen;
1214
1215exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 mbedtls_mpi_free(&mpi);
1217 if (ret != 0) {
1218 mbedtls_free(buffer);
1219 }
1220 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001221}
John Durkop07cc04a2020-11-16 22:08:34 -08001222#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001223 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001224
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001225/** Retrieve all the publicly-accessible attributes of a key.
1226 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001227psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1228 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001229{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001230 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001231 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001232 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001233
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001235
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1237 if (status != PSA_SUCCESS) {
1238 return status;
1239 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001240
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001241 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1243 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001244
1245#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1247 psa_set_key_slot_number(attributes,
1248 psa_key_slot_get_slot_number(slot));
1249 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001250#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001253#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001254 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001255 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001256 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001257 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001258 * is not yet implemented.
1259 * https://github.com/ARMmbed/mbed-crypto/issues/216
1260 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001262 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001263
Ronald Cron90857082020-11-25 14:58:33 +01001264 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 slot->attr.type,
1266 slot->key.data,
1267 slot->key.bytes,
1268 &rsa);
1269 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001270 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 status = psa_get_rsa_public_exponent(rsa,
1274 attributes);
1275 mbedtls_rsa_free(rsa);
1276 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001277 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001278 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001279#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001280 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001281 default:
1282 /* Nothing else to do. */
1283 break;
1284 }
1285
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 if (status != PSA_SUCCESS) {
1287 psa_reset_key_attributes(attributes);
1288 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001289
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001293}
1294
Gilles Peskinec8000c02019-08-02 20:15:51 +02001295#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1296psa_status_t psa_get_key_slot_number(
1297 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001299{
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001301 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 return PSA_SUCCESS;
1303 } else {
1304 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001305 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001306}
1307#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1310 size_t key_buffer_size,
1311 uint8_t *data,
1312 size_t data_size,
1313 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001314{
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 if (key_buffer_size > data_size) {
1316 return PSA_ERROR_BUFFER_TOO_SMALL;
1317 }
1318 memcpy(data, key_buffer, key_buffer_size);
1319 memset(data + key_buffer_size, 0,
1320 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001321 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001323}
1324
Ronald Cron7285cda2020-11-26 14:46:37 +01001325psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001326 const psa_key_attributes_t *attributes,
1327 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001329{
Ronald Crond18b5f82020-11-25 16:46:05 +01001330 psa_key_type_t type = attributes->core.type;
1331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 if (key_type_is_raw_bytes(type) ||
1333 PSA_KEY_TYPE_IS_RSA(type) ||
1334 PSA_KEY_TYPE_IS_ECC(type)) {
1335 return psa_export_key_buffer_internal(
1336 key_buffer, key_buffer_size,
1337 data, data_size, data_length);
1338 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001339 /* This shouldn't happen in the reference implementation, but
1340 it is valid for a special-purpose implementation to omit
1341 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001343 }
1344}
1345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1347 uint8_t *data,
1348 size_t data_size,
1349 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001350{
1351 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1352 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1353 psa_key_slot_t *slot;
1354
Ronald Crone907e552021-01-18 13:22:38 +01001355 /* Reject a zero-length output buffer now, since this can never be a
1356 * valid key representation. This way we know that data must be a valid
1357 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 if (data_size == 0) {
1359 return PSA_ERROR_BUFFER_TOO_SMALL;
1360 }
Ronald Crone907e552021-01-18 13:22:38 +01001361
Ronald Cron9486f9d2020-11-26 13:36:23 +01001362 /* Set the key to empty now, so that even when there are errors, we always
1363 * set data_length to a value between 0 and data_size. On error, setting
1364 * the key to empty is a good choice because an empty key representation is
1365 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001366 *data_length = 0;
1367
Ronald Cron9486f9d2020-11-26 13:36:23 +01001368 /* Export requires the EXPORT flag. There is an exception for public keys,
1369 * which don't require any flag, but
1370 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1371 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1373 PSA_KEY_USAGE_EXPORT, 0);
1374 if (status != PSA_SUCCESS) {
1375 return status;
1376 }
mohammad160306e79202018-03-28 13:17:44 +03001377
Ronald Crond18b5f82020-11-25 16:46:05 +01001378 psa_key_attributes_t attributes = {
1379 .core = slot->attr
1380 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 status = psa_driver_wrapper_export_key(&attributes,
1382 slot->key.data, slot->key.bytes,
1383 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001386
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001388}
1389
Ronald Cron7285cda2020-11-26 14:46:37 +01001390psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001391 const psa_key_attributes_t *attributes,
1392 const uint8_t *key_buffer,
1393 size_t key_buffer_size,
1394 uint8_t *data,
1395 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001397{
Ronald Crond18b5f82020-11-25 16:46:05 +01001398 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001399
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1401 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001402 /* Exporting public -> public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 return psa_export_key_buffer_internal(
1404 key_buffer, key_buffer_size,
1405 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001406 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001407
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001409#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1411 return mbedtls_psa_rsa_export_public_key(attributes,
1412 key_buffer,
1413 key_buffer_size,
1414 data,
1415 data_size,
1416 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001417#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001418 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001420#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1421 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001423#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1425 return mbedtls_psa_ecp_export_public_key(attributes,
1426 key_buffer,
1427 key_buffer_size,
1428 data,
1429 data_size,
1430 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001431#else
1432 /* We don't know how to convert a private ECC key to public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001434#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1435 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001436 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001438 /* This shouldn't happen in the reference implementation, but
1439 it is valid for a special-purpose implementation to omit
1440 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001442 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001443}
Ronald Crond18b5f82020-11-25 16:46:05 +01001444
Gilles Peskine449bd832023-01-11 14:50:10 +01001445psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1446 uint8_t *data,
1447 size_t data_size,
1448 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001449{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001450 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001451 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001452 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001453
Ronald Crone907e552021-01-18 13:22:38 +01001454 /* Reject a zero-length output buffer now, since this can never be a
1455 * valid key representation. This way we know that data must be a valid
1456 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 if (data_size == 0) {
1458 return PSA_ERROR_BUFFER_TOO_SMALL;
1459 }
Ronald Crone907e552021-01-18 13:22:38 +01001460
Darryl Greendd8fb772018-11-07 16:00:44 +00001461 /* Set the key to empty now, so that even when there are errors, we always
1462 * set data_length to a value between 0 and data_size. On error, setting
1463 * the key to empty is a good choice because an empty key representation is
1464 * unlikely to be accepted anywhere. */
1465 *data_length = 0;
1466
1467 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1469 if (status != PSA_SUCCESS) {
1470 return status;
1471 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001472
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1474 status = PSA_ERROR_INVALID_ARGUMENT;
1475 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001476 }
1477
Ronald Crond18b5f82020-11-25 16:46:05 +01001478 psa_key_attributes_t attributes = {
1479 .core = slot->attr
1480 };
Ronald Cron67227982020-11-26 15:16:05 +01001481 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001482 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001484
1485exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001489}
1490
Gilles Peskine91e8c332019-08-02 19:19:39 +02001491#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001492static_assert((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1493 "One or more key attribute flag is listed as both external-only and dual-use");
1494static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1495 "One or more key attribute flag is listed as both internal-only and dual-use");
1496static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1497 "One or more key attribute flag is listed as both internal-only and external-only");
Gilles Peskine91e8c332019-08-02 19:19:39 +02001498#endif
1499
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001500/** Validate that a key policy is internally well-formed.
1501 *
1502 * This function only rejects invalid policies. It does not validate the
1503 * consistency of the policy with respect to other attributes of the key
1504 * such as the key type.
1505 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001506static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001507{
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1509 PSA_KEY_USAGE_COPY |
1510 PSA_KEY_USAGE_ENCRYPT |
1511 PSA_KEY_USAGE_DECRYPT |
1512 PSA_KEY_USAGE_SIGN_MESSAGE |
1513 PSA_KEY_USAGE_VERIFY_MESSAGE |
1514 PSA_KEY_USAGE_SIGN_HASH |
1515 PSA_KEY_USAGE_VERIFY_HASH |
1516 PSA_KEY_USAGE_VERIFY_DERIVATION |
1517 PSA_KEY_USAGE_DERIVE)) != 0) {
1518 return PSA_ERROR_INVALID_ARGUMENT;
1519 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001522}
1523
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001524/** Validate the internal consistency of key attributes.
1525 *
1526 * This function only rejects invalid attribute values. If does not
1527 * validate the consistency of the attributes with any key data that may
1528 * be involved in the creation of the key.
1529 *
1530 * Call this function early in the key creation process.
1531 *
1532 * \param[in] attributes Key attributes for the new key.
1533 * \param[out] p_drv On any return, the driver for the key, if any.
1534 * NULL for a transparent key.
1535 *
1536 */
1537static psa_status_t psa_validate_key_attributes(
1538 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001540{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001541 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1543 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 status = psa_validate_key_location(lifetime, p_drv);
1546 if (status != PSA_SUCCESS) {
1547 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001548 }
1549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 status = psa_validate_key_persistence(lifetime);
1551 if (status != PSA_SUCCESS) {
1552 return status;
1553 }
1554
1555 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1556 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1557 return PSA_ERROR_INVALID_ARGUMENT;
1558 }
1559 } else {
1560 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1561 return PSA_ERROR_INVALID_ARGUMENT;
1562 }
1563 }
1564
1565 status = psa_validate_key_policy(&attributes->core.policy);
1566 if (status != PSA_SUCCESS) {
1567 return status;
1568 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001569
1570 /* Refuse to create overly large keys.
1571 * Note that this doesn't trigger on import if the attributes don't
1572 * explicitly specify a size (so psa_get_key_bits returns 0), so
1573 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1575 return PSA_ERROR_NOT_SUPPORTED;
1576 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001577
Gilles Peskine91e8c332019-08-02 19:19:39 +02001578 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1580 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1581 return PSA_ERROR_INVALID_ARGUMENT;
1582 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001583
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001585}
1586
Gilles Peskine4747d192019-04-17 15:05:45 +02001587/** Prepare a key slot to receive key material.
1588 *
1589 * This function allocates a key slot and sets its metadata.
1590 *
1591 * If this function fails, call psa_fail_key_creation().
1592 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001593 * This function is intended to be used as follows:
1594 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001595 * it with the specified attributes, and in case of a volatile key assign it
1596 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001597 * -# Populate the slot with the key material.
1598 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1599 * In case of failure at any step, stop the sequence and call
1600 * psa_fail_key_creation().
1601 *
Ronald Cron5c522922020-11-14 16:35:34 +01001602 * On success, the key slot is locked. It is the responsibility of the caller
1603 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001604 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001605 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001606 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001607 * \param[out] p_slot On success, a pointer to the prepared slot.
1608 * \param[out] p_drv On any return, the driver for the key, if any.
1609 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001610 *
1611 * \retval #PSA_SUCCESS
1612 * The key slot is ready to receive key material.
1613 * \return If this function fails, the key slot is an invalid state.
1614 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001615 */
1616static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001617 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001618 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001619 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001621{
1622 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001623 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001624 psa_key_slot_t *slot;
1625
Gilles Peskinedf179142019-07-15 22:02:14 +02001626 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001627 *p_drv = NULL;
1628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 status = psa_validate_key_attributes(attributes, p_drv);
1630 if (status != PSA_SUCCESS) {
1631 return status;
1632 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1635 if (status != PSA_SUCCESS) {
1636 return status;
1637 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001638 slot = *p_slot;
1639
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001640 /* We're storing the declared bit-size of the key. It's up to each
1641 * creation mechanism to verify that this information is correct.
1642 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001643 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001644 * is optional (import, copy). In case of a volatile key, assign it the
1645 * volatile key identifier associated to the slot returned to contain its
1646 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001647
1648 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001650#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1651 slot->attr.id = volatile_key_id;
1652#else
1653 slot->attr.id.key_id = volatile_key_id;
1654#endif
1655 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001656
Gilles Peskine91e8c332019-08-02 19:19:39 +02001657 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001658 * external-only flags, query `attributes`. Thanks to the check
1659 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001660 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001661 * may have set. */
1662 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001663
Gilles Peskinecbaff462019-07-12 23:46:04 +02001664#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001665 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001666 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001667 * create the key file in internal storage, create the
1668 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001669 * persistent data. This is done by starting a transaction that will
1670 * encompass these three actions.
1671 * For registering a volatile key, we just need to find an appropriate
1672 * slot number inside the SE. Since the key is designated volatile, creating
1673 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001674 /* The first thing to do is to find a slot number for the new key.
1675 * We save the slot number in persistent storage as part of the
1676 * transaction data. It will be needed to recover if the power
1677 * fails during the key creation process, to clean up on the secure
1678 * element side after restarting. Obtaining a slot number from the
1679 * secure element driver updates its persistent state, but we do not yet
1680 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001681 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001683 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1685 &slot_number);
1686 if (status != PSA_SUCCESS) {
1687 return status;
1688 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1691 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001692 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001693 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001694 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 status = psa_crypto_save_transaction();
1696 if (status != PSA_SUCCESS) {
1697 (void) psa_crypto_stop_transaction();
1698 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001699 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001700 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001701
1702 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001704 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001705
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001707 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001709 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001710#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1711
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001713}
Gilles Peskine4747d192019-04-17 15:05:45 +02001714
1715/** Finalize the creation of a key once its key material has been set.
1716 *
1717 * This entails writing the key to persistent storage.
1718 *
1719 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001720 * See the documentation of psa_start_key_creation() for the intended use
1721 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001722 *
Ronald Cron5c522922020-11-14 16:35:34 +01001723 * If the finalization succeeds, the function unlocks the key slot (it was
1724 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1725 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001726 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001727 * \param[in,out] slot Pointer to the slot with key material.
1728 * \param[in] driver The secure element driver for the key,
1729 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001730 * \param[out] key On success, identifier of the key. Note that the
1731 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001732 *
1733 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001734 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001735 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1736 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1737 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1738 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1739 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1740 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001741 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001742 * \return If this function fails, the key slot is an invalid state.
1743 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001744 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001745static psa_status_t psa_finish_key_creation(
1746 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001747 psa_se_drv_table_entry_t *driver,
1748 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001749{
1750 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001751 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001752 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001753
1754#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001756#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001758 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001759 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001761
Gilles Peskineb46bef22019-07-30 21:32:04 +02001762#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 static_assert(sizeof(slot_number) ==
1764 sizeof(data.slot_number),
1765 "Slot number size does not match psa_se_key_data_storage_t");
Gilles Peskineb46bef22019-07-30 21:32:04 +02001766#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1768 status = psa_save_persistent_key(&slot->attr,
1769 (uint8_t *) &data,
1770 sizeof(data));
1771 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001772#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1773 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001774 /* Key material is saved in export representation in the slot, so
1775 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 status = psa_save_persistent_key(&slot->attr,
1777 slot->key.data,
1778 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001779 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001780 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001781#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1782
Gilles Peskinecbaff462019-07-12 23:46:04 +02001783#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001784 /* Finish the transaction for a key creation. This does not
1785 * happen when registering an existing key. Detect this case
1786 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001787 * creation of a persistent key in a secure element requires a transaction,
1788 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 if (driver != NULL &&
1790 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1791 status = psa_save_se_persistent_data(driver);
1792 if (status != PSA_SUCCESS) {
1793 psa_destroy_persistent_key(slot->attr.id);
1794 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001795 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001797 }
1798#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1799
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001801 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 status = psa_unlock_key_slot(slot);
1803 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001804 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001806 }
Ronald Cron50972942020-11-14 11:28:25 +01001807
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001809}
1810
1811/** Abort the creation of a key.
1812 *
1813 * You may call this function after calling psa_start_key_creation(),
1814 * or after psa_finish_key_creation() fails. In other circumstances, this
1815 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001816 * See the documentation of psa_start_key_creation() for the intended use
1817 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001818 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001819 * \param[in,out] slot Pointer to the slot with key material.
1820 * \param[in] driver The secure element driver for the key,
1821 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001822 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001823static void psa_fail_key_creation(psa_key_slot_t *slot,
1824 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001825{
Gilles Peskine011e4282019-06-26 18:34:38 +02001826 (void) driver;
1827
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001829 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001831
1832#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001833 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001834 * element, and the failure happened later (when saving metadata
1835 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001836 * element.
1837 * https://github.com/ARMmbed/mbed-crypto/issues/217
1838 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001839
Gilles Peskined7729582019-08-05 15:55:54 +02001840 /* Abort the ongoing transaction if any (there may not be one if
1841 * the creation process failed before starting one, or if the
1842 * key creation is a registration of a key in a secure element).
1843 * Earlier functions must already have done what it takes to undo any
1844 * partial creation. All that's left is to update the transaction data
1845 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001847#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1848
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001850}
1851
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001852/** Validate optional attributes during key creation.
1853 *
1854 * Some key attributes are optional during key creation. If they are
1855 * specified in the attributes structure, check that they are consistent
1856 * with the data in the slot.
1857 *
1858 * This function should be called near the end of key creation, after
1859 * the slot in memory is fully populated but before saving persistent data.
1860 */
1861static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001862 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001864{
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 if (attributes->core.type != 0) {
1866 if (attributes->core.type != slot->attr.type) {
1867 return PSA_ERROR_INVALID_ARGUMENT;
1868 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001869 }
1870
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001872#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1874 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001875 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001876 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001877 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001878
Ronald Cron90857082020-11-25 14:58:33 +01001879 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 slot->attr.type,
1881 slot->key.data,
1882 slot->key.bytes,
1883 &rsa);
1884 if (status != PSA_SUCCESS) {
1885 return status;
1886 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001887
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 mbedtls_mpi_init(&actual);
1889 mbedtls_mpi_init(&required);
1890 ret = mbedtls_rsa_export(rsa,
1891 NULL, NULL, NULL, NULL, &actual);
1892 mbedtls_rsa_free(rsa);
1893 mbedtls_free(rsa);
1894 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001895 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001896 }
1897 ret = mbedtls_mpi_read_binary(&required,
1898 attributes->domain_parameters,
1899 attributes->domain_parameters_size);
1900 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001901 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 }
1903 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001904 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 }
1906rsa_exit:
1907 mbedtls_mpi_free(&actual);
1908 mbedtls_mpi_free(&required);
1909 if (ret != 0) {
1910 return mbedtls_to_psa_error(ret);
1911 }
1912 } else
John Durkop9814fa22020-11-04 12:28:15 -08001913#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1914 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001915 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001917 }
1918 }
1919
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 if (attributes->core.bits != 0) {
1921 if (attributes->core.bits != slot->attr.bits) {
1922 return PSA_ERROR_INVALID_ARGUMENT;
1923 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001924 }
1925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001927}
1928
Gilles Peskine449bd832023-01-11 14:50:10 +01001929psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1930 const uint8_t *data,
1931 size_t data_length,
1932 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001933{
1934 psa_status_t status;
1935 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001936 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001937 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301938 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001939
Ronald Cron81709fc2020-11-14 12:10:32 +01001940 *key = MBEDTLS_SVC_KEY_ID_INIT;
1941
Gilles Peskine0f84d622019-09-12 19:03:13 +02001942 /* Reject zero-length symmetric keys (including raw data key objects).
1943 * This also rejects any key which might be encoded as an empty string,
1944 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 if (data_length == 0) {
1946 return PSA_ERROR_INVALID_ARGUMENT;
1947 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001948
Archana449608b2021-09-08 15:36:05 +05301949 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 if (data_length > SIZE_MAX / 8) {
1951 return PSA_ERROR_NOT_SUPPORTED;
1952 }
Archana6ed4bda2021-08-04 10:47:15 +05301953
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1955 &slot, &driver);
1956 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001957 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001959
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001960 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301961 * storage ( thus not in the case of importing a key in a secure element
1962 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1963 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 if (slot->key.data == NULL) {
1965 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301966 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 attributes, data, data_length, &storage_size);
1968 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301969 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 }
Archanad8a83dc2021-06-14 10:04:16 +05301971 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 status = psa_allocate_buffer_to_slot(slot, storage_size);
1973 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001974 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001976 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001977
1978 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 status = psa_driver_wrapper_import_key(attributes,
1980 data, data_length,
1981 slot->key.data,
1982 slot->key.bytes,
1983 &slot->key.bytes, &bits);
1984 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001985 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001987
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001989 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001991 status = PSA_ERROR_INVALID_ARGUMENT;
1992 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001993 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001994
Archana6ed4bda2021-08-04 10:47:15 +05301995 /* Enforce a size limit, and in particular ensure that the bit
1996 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05301998 status = PSA_ERROR_NOT_SUPPORTED;
1999 goto exit;
2000 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 status = psa_validate_optional_attributes(slot, attributes);
2002 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002003 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002005
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002007exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 if (status != PSA_SUCCESS) {
2009 psa_fail_key_creation(slot, driver);
2010 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002011
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002013}
2014
Gilles Peskined7729582019-08-05 15:55:54 +02002015#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2016psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002018{
2019 psa_status_t status;
2020 psa_key_slot_t *slot = NULL;
2021 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002022 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002023
2024 /* Leaving attributes unspecified is not currently supported.
2025 * It could make sense to query the key type and size from the
2026 * secure element, but not all secure elements support this
2027 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2029 return PSA_ERROR_NOT_SUPPORTED;
2030 }
2031 if (psa_get_key_bits(attributes) == 0) {
2032 return PSA_ERROR_NOT_SUPPORTED;
2033 }
Gilles Peskined7729582019-08-05 15:55:54 +02002034
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2036 &slot, &driver);
2037 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002038 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 }
Gilles Peskined7729582019-08-05 15:55:54 +02002040
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002042
2043exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 if (status != PSA_SUCCESS) {
2045 psa_fail_key_creation(slot, driver);
2046 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002047
Gilles Peskined7729582019-08-05 15:55:54 +02002048 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 psa_close_key(key);
2050 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002051}
2052#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2053
Gilles Peskine449bd832023-01-11 14:50:10 +01002054psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2055 const psa_key_attributes_t *specified_attributes,
2056 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002057{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002058 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002059 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002060 psa_key_slot_t *source_slot = NULL;
2061 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002062 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002063 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302064 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002065
Ronald Cron81709fc2020-11-14 12:10:32 +01002066 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2067
Archana8a180362021-07-05 02:18:48 +05302068 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2070 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002071 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002073
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 status = psa_validate_optional_attributes(source_slot,
2075 specified_attributes);
2076 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002077 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002079
Archana9d17bf42021-09-10 06:22:44 +05302080 /* The target key type and number of bits have been validated by
2081 * psa_validate_optional_attributes() to be either equal to zero or
2082 * equal to the ones of the source key. So it is safe to inherit
2083 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302084 * */
Archana9d17bf42021-09-10 06:22:44 +05302085 actual_attributes.core.bits = source_slot->attr.bits;
2086 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302087
2088
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 status = psa_restrict_key_policy(source_slot->attr.type,
2090 &actual_attributes.core.policy,
2091 &source_slot->attr.policy);
2092 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002093 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002095
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2097 &target_slot, &driver);
2098 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002099 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 }
2101 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2102 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302103 /*
Archana9d17bf42021-09-10 06:22:44 +05302104 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302105 * the source key would need to be exported as plaintext and re-imported
2106 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302107 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302108 * appropriate API invocations from the application, if needed.
2109 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002110 status = PSA_ERROR_NOT_SUPPORTED;
2111 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002112 }
Archana8a180362021-07-05 02:18:48 +05302113 /*
2114 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302115 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302116 * - For opaque keys this translates to an invocation of the drivers'
2117 * copy_key entry point through the dispatch layer.
2118 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2120 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2121 &storage_size);
2122 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302123 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 }
Archana374fe5b2021-09-08 15:50:28 +05302125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2127 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302128 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 }
Archana374fe5b2021-09-08 15:50:28 +05302130
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 status = psa_driver_wrapper_copy_key(&actual_attributes,
2132 source_slot->key.data,
2133 source_slot->key.bytes,
2134 target_slot->key.data,
2135 target_slot->key.bytes,
2136 &target_slot->key.bytes);
2137 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302138 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 }
2140 } else {
2141 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302142 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 source_slot->key.bytes);
2144 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302145 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 }
Archana8a180362021-07-05 02:18:48 +05302147 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002149exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 if (status != PSA_SUCCESS) {
2151 psa_fail_key_creation(target_slot, driver);
2152 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002155
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002157}
2158
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002159
2160
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002161/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002162/* Message digests */
2163/****************************************************************/
2164
Gilles Peskine449bd832023-01-11 14:50:10 +01002165psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002166{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002167 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 if (operation->id == 0) {
2169 return PSA_SUCCESS;
2170 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002173 operation->id = 0;
2174
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002176}
2177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2179 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002180{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002181 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002182
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002183 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002185 status = PSA_ERROR_BAD_STATE;
2186 goto exit;
2187 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002188
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002190 status = PSA_ERROR_INVALID_ARGUMENT;
2191 goto exit;
2192 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002193
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002194 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2195 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002197
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002199
2200exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 if (status != PSA_SUCCESS) {
2202 psa_hash_abort(operation);
2203 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002204
2205 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002206}
2207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2209 const uint8_t *input,
2210 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002211{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002212 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002215 status = PSA_ERROR_BAD_STATE;
2216 goto exit;
2217 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002218
Steven Cooremanc8288352021-03-04 14:02:19 +01002219 /* Don't require hash implementations to behave correctly on a
2220 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 if (input_length == 0) {
2222 return PSA_SUCCESS;
2223 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002226
2227exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 if (status != PSA_SUCCESS) {
2229 psa_hash_abort(operation);
2230 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002231
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002233}
2234
Gilles Peskine449bd832023-01-11 14:50:10 +01002235psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2236 uint8_t *hash,
2237 size_t hash_size,
2238 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002239{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002240 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 if (operation->id == 0) {
2242 return PSA_ERROR_BAD_STATE;
2243 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002244
Steven Cooreman1e582352021-02-18 17:24:37 +01002245 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 operation, hash, hash_size, hash_length);
2247 psa_hash_abort(operation);
2248 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002249}
2250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2252 const uint8_t *hash,
2253 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002254{
Ronald Cron69a63422021-10-18 09:47:58 +02002255 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002256 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002257 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 operation,
2259 actual_hash, sizeof(actual_hash),
2260 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002261
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002263 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002265
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002267 status = PSA_ERROR_INVALID_SIGNATURE;
2268 goto exit;
2269 }
2270
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002272 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002274
2275exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2277 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002278 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002280
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002282}
2283
Gilles Peskine449bd832023-01-11 14:50:10 +01002284psa_status_t psa_hash_compute(psa_algorithm_t alg,
2285 const uint8_t *input, size_t input_length,
2286 uint8_t *hash, size_t hash_size,
2287 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002288{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002289 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 if (!PSA_ALG_IS_HASH(alg)) {
2291 return PSA_ERROR_INVALID_ARGUMENT;
2292 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002293
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2295 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002296}
2297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298psa_status_t psa_hash_compare(psa_algorithm_t alg,
2299 const uint8_t *input, size_t input_length,
2300 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002301{
Ronald Cron69a63422021-10-18 09:47:58 +02002302 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002303 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002304
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 if (!PSA_ALG_IS_HASH(alg)) {
2306 return PSA_ERROR_INVALID_ARGUMENT;
2307 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002308
Steven Cooreman1e582352021-02-18 17:24:37 +01002309 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 alg, input, input_length,
2311 actual_hash, sizeof(actual_hash),
2312 &actual_hash_length);
2313 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002314 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 }
2316 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002317 status = PSA_ERROR_INVALID_SIGNATURE;
2318 goto exit;
2319 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002321 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002323
2324exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2326 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002327}
2328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2330 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002331{
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 if (source_operation->id == 0 ||
2333 target_operation->id != 0) {
2334 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002335 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002336
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2338 target_operation);
2339 if (status != PSA_SUCCESS) {
2340 psa_hash_abort(target_operation);
2341 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002344}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002345
2346
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002347/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002348/* MAC */
2349/****************************************************************/
2350
Gilles Peskine449bd832023-01-11 14:50:10 +01002351psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002352{
Steven Cooremane6804192021-03-19 18:28:56 +01002353 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 if (operation->id == 0) {
2355 return PSA_SUCCESS;
2356 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002357
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002359 operation->mac_size = 0;
2360 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002361 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002362
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002364}
2365
Ronald Cron2dff3b22021-06-17 16:33:22 +02002366static psa_status_t psa_mac_finalize_alg_and_key_validation(
2367 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002368 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002370{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002371 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 psa_key_type_t key_type = psa_get_key_type(attributes);
2373 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002374
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 if (!PSA_ALG_IS_MAC(alg)) {
2376 return PSA_ERROR_INVALID_ARGUMENT;
2377 }
Steven Cooremane6804192021-03-19 18:28:56 +01002378
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002379 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 status = psa_mac_key_can_do(alg, key_type);
2381 if (status != PSA_SUCCESS) {
2382 return status;
2383 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002384
Steven Cooremand1a68f12021-05-10 11:13:41 +02002385 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002387
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002389 /* A very short MAC is too short for security since it can be
2390 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2391 * so we make this our minimum, even though 32 bits is still
2392 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002394 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002395
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2397 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002398 /* It's impossible to "truncate" to a larger length than the full length
2399 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002401 }
2402
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002404 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2405 * that is disabled in the compile-time configuration. The result can
2406 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2407 * configuration into account. In this case, force a return of
2408 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2409 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2410 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2411 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2412 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002414 }
2415
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002417}
2418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2420 mbedtls_svc_key_id_t key,
2421 psa_algorithm_t alg,
2422 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002423{
2424 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2425 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002426 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002427
2428 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002430 status = PSA_ERROR_BAD_STATE;
2431 goto exit;
2432 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002433
2434 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 key,
2436 &slot,
2437 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2438 alg);
2439 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002440 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002442
2443 psa_key_attributes_t attributes = {
2444 .core = slot->attr
2445 };
2446
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2448 &operation->mac_size);
2449 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002450 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002452
2453 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002454 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 if (is_sign) {
2456 status = psa_driver_wrapper_mac_sign_setup(operation,
2457 &attributes,
2458 slot->key.data,
2459 slot->key.bytes,
2460 alg);
2461 } else {
2462 status = psa_driver_wrapper_mac_verify_setup(operation,
2463 &attributes,
2464 slot->key.data,
2465 slot->key.bytes,
2466 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002467 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002468
Gilles Peskinefbfac682018-07-08 20:51:54 +02002469exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 if (status != PSA_SUCCESS) {
2471 psa_mac_abort(operation);
2472 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002473
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002475
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002477}
2478
Gilles Peskine449bd832023-01-11 14:50:10 +01002479psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2480 mbedtls_svc_key_id_t key,
2481 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002482{
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002484}
2485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2487 mbedtls_svc_key_id_t key,
2488 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002489{
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002491}
2492
Gilles Peskine449bd832023-01-11 14:50:10 +01002493psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2494 const uint8_t *input,
2495 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002496{
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 if (operation->id == 0) {
2498 return PSA_ERROR_BAD_STATE;
2499 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002500
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002501 /* Don't require hash implementations to behave correctly on a
2502 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 if (input_length == 0) {
2504 return PSA_SUCCESS;
2505 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002506
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2508 input, input_length);
2509 if (status != PSA_SUCCESS) {
2510 psa_mac_abort(operation);
2511 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002514}
2515
Gilles Peskine449bd832023-01-11 14:50:10 +01002516psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2517 uint8_t *mac,
2518 size_t mac_size,
2519 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002520{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002521 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2522 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002525 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002526 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002527 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002528
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002530 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002531 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002532 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002533
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002534 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2535 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002537 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002538 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002539 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002540
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002542 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002543 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002544 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 status = psa_driver_wrapper_mac_sign_finish(operation,
2547 mac, operation->mac_size,
2548 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002549
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002550exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002551 /* In case of success, set the potential excess room in the output buffer
2552 * to an invalid value, to avoid potentially leaking a longer MAC.
2553 * In case of error, set the output length and content to a safe default,
2554 * such that in case the caller misses an error check, the output would be
2555 * an unachievable MAC.
2556 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002558 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002559 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002560 }
mohammad16036df908f2018-04-02 08:34:15 -07002561
Paul Elliottdc42ca82023-02-24 18:11:59 +00002562 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002565
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002567}
2568
Gilles Peskine449bd832023-01-11 14:50:10 +01002569psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2570 const uint8_t *mac,
2571 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002572{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002573 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2574 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002577 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002578 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002579 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002580
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002582 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002583 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002584 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002585
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002587 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002588 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002589 }
2590
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 status = psa_driver_wrapper_mac_verify_finish(operation,
2592 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002593
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002594exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002596
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002598}
2599
Gilles Peskine449bd832023-01-11 14:50:10 +01002600static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2601 psa_algorithm_t alg,
2602 const uint8_t *input,
2603 size_t input_length,
2604 uint8_t *mac,
2605 size_t mac_size,
2606 size_t *mac_length,
2607 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002608{
2609 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002610 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2611 psa_key_slot_t *slot;
2612 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002613
Ronald Cron51131b52021-06-17 17:17:20 +02002614 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 key,
2616 &slot,
2617 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2618 alg);
2619 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002620 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002622
Ronald Cron51131b52021-06-17 17:17:20 +02002623 psa_key_attributes_t attributes = {
2624 .core = slot->attr
2625 };
2626
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2628 &operation_mac_size);
2629 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002630 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002632
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002634 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002635 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002636 }
2637
2638 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 &attributes,
2640 slot->key.data, slot->key.bytes,
2641 alg,
2642 input, input_length,
2643 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002644
2645exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002646 /* In case of success, set the potential excess room in the output buffer
2647 * to an invalid value, to avoid potentially leaking a longer MAC.
2648 * In case of error, set the output length and content to a safe default,
2649 * such that in case the caller misses an error check, the output would be
2650 * an unachievable MAC.
2651 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002653 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002654 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002655 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002656
2657 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002658
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002660
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002662}
2663
Gilles Peskine449bd832023-01-11 14:50:10 +01002664psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002665 psa_algorithm_t alg,
2666 const uint8_t *input,
2667 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 uint8_t *mac,
2669 size_t mac_size,
2670 size_t *mac_length)
2671{
2672 return psa_mac_compute_internal(key, alg,
2673 input, input_length,
2674 mac, mac_size, mac_length, 1);
2675}
2676
2677psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2678 psa_algorithm_t alg,
2679 const uint8_t *input,
2680 size_t input_length,
2681 const uint8_t *mac,
2682 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002683{
2684 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002685 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2686 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002687
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 status = psa_mac_compute_internal(key, alg,
2689 input, input_length,
2690 actual_mac, sizeof(actual_mac),
2691 &actual_mac_length, 0);
2692 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002693 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002697 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002698 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002699 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002701 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002702 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002703 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002704
2705exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002707
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002709}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002710
Gilles Peskinee59236f2018-01-27 23:32:46 +01002711/****************************************************************/
2712/* Asymmetric cryptography */
2713/****************************************************************/
2714
Gilles Peskine449bd832023-01-11 14:50:10 +01002715static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2716 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002717{
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 if (input_is_message) {
2719 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2720 return PSA_ERROR_INVALID_ARGUMENT;
2721 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002722
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2724 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2725 return PSA_ERROR_INVALID_ARGUMENT;
2726 }
2727 }
2728 } else {
2729 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2730 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002731 }
2732 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002733
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002735}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002736
Gilles Peskine449bd832023-01-11 14:50:10 +01002737static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2738 int input_is_message,
2739 psa_algorithm_t alg,
2740 const uint8_t *input,
2741 size_t input_length,
2742 uint8_t *signature,
2743 size_t signature_size,
2744 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002745{
2746 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2747 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2748 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002749
2750 *signature_length = 0;
2751
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 status = psa_sign_verify_check_alg(input_is_message, alg);
2753 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002754 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002756
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002757 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002758 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002759 * buffer can in principle be empty since it doesn't actually have
2760 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 if (signature_size == 0) {
2762 return PSA_ERROR_BUFFER_TOO_SMALL;
2763 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002764
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002765 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 key, &slot,
2767 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2768 PSA_KEY_USAGE_SIGN_HASH,
2769 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002770
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002772 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002774
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002776 status = PSA_ERROR_INVALID_ARGUMENT;
2777 goto exit;
2778 }
2779
2780 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002782 };
2783
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002785 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002786 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002787 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 signature, signature_size, signature_length);
2789 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002790
2791 status = psa_driver_wrapper_sign_hash(
2792 &attributes, slot->key.data, slot->key.bytes,
2793 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002795 }
2796
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002797
2798exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002799 psa_wipe_tag_output_buffer(signature, status, signature_size,
2800 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002801
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002803
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002805}
2806
Gilles Peskine449bd832023-01-11 14:50:10 +01002807static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2808 int input_is_message,
2809 psa_algorithm_t alg,
2810 const uint8_t *input,
2811 size_t input_length,
2812 const uint8_t *signature,
2813 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002814{
2815 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2816 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2817 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002818
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 status = psa_sign_verify_check_alg(input_is_message, alg);
2820 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002821 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002823
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002824 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002825 key, &slot,
2826 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2827 PSA_KEY_USAGE_VERIFY_HASH,
2828 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002829
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 if (status != PSA_SUCCESS) {
2831 return status;
2832 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002833
2834 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002836 };
2837
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002839 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002840 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002841 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 signature, signature_length);
2843 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002844 status = psa_driver_wrapper_verify_hash(
2845 &attributes, slot->key.data, slot->key.bytes,
2846 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002847 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002848 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002849
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002851
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002853
2854}
2855
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002856psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002857 const psa_key_attributes_t *attributes,
2858 const uint8_t *key_buffer,
2859 size_t key_buffer_size,
2860 psa_algorithm_t alg,
2861 const uint8_t *input,
2862 size_t input_length,
2863 uint8_t *signature,
2864 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002866{
2867 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002868
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002870 size_t hash_length;
2871 uint8_t hash[PSA_HASH_MAX_SIZE];
2872
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002873 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 PSA_ALG_SIGN_GET_HASH(alg),
2875 input, input_length,
2876 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002877
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002879 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002881
2882 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 attributes, key_buffer, key_buffer_size,
2884 alg, hash, hash_length,
2885 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002886 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002887
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002889}
2890
Gilles Peskine449bd832023-01-11 14:50:10 +01002891psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2892 psa_algorithm_t alg,
2893 const uint8_t *input,
2894 size_t input_length,
2895 uint8_t *signature,
2896 size_t signature_size,
2897 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002898{
2899 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002900 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002902}
2903
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002904psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002905 const psa_key_attributes_t *attributes,
2906 const uint8_t *key_buffer,
2907 size_t key_buffer_size,
2908 psa_algorithm_t alg,
2909 const uint8_t *input,
2910 size_t input_length,
2911 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002913{
2914 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002915
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002917 size_t hash_length;
2918 uint8_t hash[PSA_HASH_MAX_SIZE];
2919
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002920 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 PSA_ALG_SIGN_GET_HASH(alg),
2922 input, input_length,
2923 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002924
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002926 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002928
2929 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 attributes, key_buffer, key_buffer_size,
2931 alg, hash, hash_length,
2932 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002933 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002934
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002936}
2937
Gilles Peskine449bd832023-01-11 14:50:10 +01002938psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2939 psa_algorithm_t alg,
2940 const uint8_t *input,
2941 size_t input_length,
2942 const uint8_t *signature,
2943 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002944{
2945 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002946 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002948}
2949
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002950psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002951 const psa_key_attributes_t *attributes,
2952 const uint8_t *key_buffer, size_t key_buffer_size,
2953 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002955{
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2957 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2958 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002959#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2961 return mbedtls_psa_rsa_sign_hash(
2962 attributes,
2963 key_buffer, key_buffer_size,
2964 alg, hash, hash_length,
2965 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002966#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2967 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 } else {
2969 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002970 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2972 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002973#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2975 return mbedtls_psa_ecdsa_sign_hash(
2976 attributes,
2977 key_buffer, key_buffer_size,
2978 alg, hash, hash_length,
2979 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002980#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2981 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 } else {
2983 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002984 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002985 }
Ronald Cron4501c982021-03-19 20:09:32 +01002986
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 (void) key_buffer;
2988 (void) key_buffer_size;
2989 (void) hash;
2990 (void) hash_length;
2991 (void) signature;
2992 (void) signature_size;
2993 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002994
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002996}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002997
Gilles Peskine449bd832023-01-11 14:50:10 +01002998psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2999 psa_algorithm_t alg,
3000 const uint8_t *hash,
3001 size_t hash_length,
3002 uint8_t *signature,
3003 size_t signature_size,
3004 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003005{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003006 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003007 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003009}
3010
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003011psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003012 const psa_key_attributes_t *attributes,
3013 const uint8_t *key_buffer, size_t key_buffer_size,
3014 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003016{
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3018 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3019 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003020#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3022 return mbedtls_psa_rsa_verify_hash(
3023 attributes,
3024 key_buffer, key_buffer_size,
3025 alg, hash, hash_length,
3026 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003027#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3028 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 } else {
3030 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003031 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3033 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003034#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3036 return mbedtls_psa_ecdsa_verify_hash(
3037 attributes,
3038 key_buffer, key_buffer_size,
3039 alg, hash, hash_length,
3040 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003041#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3042 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 } else {
3044 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003045 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003046 }
Ronald Cron4501c982021-03-19 20:09:32 +01003047
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 (void) key_buffer;
3049 (void) key_buffer_size;
3050 (void) hash;
3051 (void) hash_length;
3052 (void) signature;
3053 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003054
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003056}
3057
Gilles Peskine449bd832023-01-11 14:50:10 +01003058psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3059 psa_algorithm_t alg,
3060 const uint8_t *hash,
3061 size_t hash_length,
3062 const uint8_t *signature,
3063 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003064{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003065 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003066 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003068}
3069
Gilles Peskine449bd832023-01-11 14:50:10 +01003070psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3071 psa_algorithm_t alg,
3072 const uint8_t *input,
3073 size_t input_length,
3074 const uint8_t *salt,
3075 size_t salt_length,
3076 uint8_t *output,
3077 size_t output_size,
3078 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003079{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003080 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003081 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003082 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003083
Darryl Green5cc689a2018-07-24 15:34:10 +01003084 (void) input;
3085 (void) input_length;
3086 (void) salt;
3087 (void) output;
3088 (void) output_size;
3089
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003090 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003091
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3093 return PSA_ERROR_INVALID_ARGUMENT;
3094 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003095
Ronald Cron5c522922020-11-14 16:35:34 +01003096 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3098 if (status != PSA_SUCCESS) {
3099 return status;
3100 }
3101 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3102 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003103 status = PSA_ERROR_INVALID_ARGUMENT;
3104 goto exit;
3105 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003106
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003107 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003109 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003110
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003111 status = psa_driver_wrapper_asymmetric_encrypt(
3112 &attributes, slot->key.data, slot->key.bytes,
3113 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003115exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003117
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003119}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003120
Gilles Peskine449bd832023-01-11 14:50:10 +01003121psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3122 psa_algorithm_t alg,
3123 const uint8_t *input,
3124 size_t input_length,
3125 const uint8_t *salt,
3126 size_t salt_length,
3127 uint8_t *output,
3128 size_t output_size,
3129 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003130{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003131 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003132 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003133 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003134
Darryl Green5cc689a2018-07-24 15:34:10 +01003135 (void) input;
3136 (void) input_length;
3137 (void) salt;
3138 (void) output;
3139 (void) output_size;
3140
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003141 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003142
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3144 return PSA_ERROR_INVALID_ARGUMENT;
3145 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003146
Ronald Cron5c522922020-11-14 16:35:34 +01003147 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3149 if (status != PSA_SUCCESS) {
3150 return status;
3151 }
3152 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003153 status = PSA_ERROR_INVALID_ARGUMENT;
3154 goto exit;
3155 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003156
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003157 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003159 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003160
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003161 status = psa_driver_wrapper_asymmetric_decrypt(
3162 &attributes, slot->key.data, slot->key.bytes,
3163 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003165
3166exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003168
Gilles Peskine449bd832023-01-11 14:50:10 +01003169 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003170}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003171
Paul Elliott9fe12f62022-11-30 19:16:02 +00003172/****************************************************************/
3173/* Asymmetric interruptible cryptography */
3174/****************************************************************/
3175
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003176static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3177
Paul Elliott9fe12f62022-11-30 19:16:02 +00003178void psa_interruptible_set_max_ops(uint32_t max_ops)
3179{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003180 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003181}
3182
3183uint32_t psa_interruptible_get_max_ops(void)
3184{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003185 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003186}
3187
Paul Elliott9fe12f62022-11-30 19:16:02 +00003188uint32_t psa_sign_hash_get_num_ops(
3189 const psa_sign_hash_interruptible_operation_t *operation)
3190{
Paul Elliott296ede92022-12-15 17:00:30 +00003191 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003192}
3193
3194uint32_t psa_verify_hash_get_num_ops(
3195 const psa_verify_hash_interruptible_operation_t *operation)
3196{
Paul Elliott296ede92022-12-15 17:00:30 +00003197 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003198}
3199
Paul Elliott59ad9452022-12-18 15:09:02 +00003200static psa_status_t psa_sign_hash_abort_internal(
3201 psa_sign_hash_interruptible_operation_t *operation)
3202{
3203 if (operation->id == 0) {
3204 /* The object has (apparently) been initialized but it is not (yet)
3205 * in use. It's ok to call abort on such an object, and there's
3206 * nothing to do. */
3207 return PSA_SUCCESS;
3208 }
3209
3210 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3211
3212 status = psa_driver_wrapper_sign_hash_abort(operation);
3213
3214 operation->id = 0;
3215
Paul Elliotte6145dc2023-02-07 12:51:21 +00003216 /* Do not clear either the error_occurred or num_ops elements here as they
3217 * only want to be cleared by the application calling abort, not by abort
3218 * being called at completion of an operation. */
3219
Paul Elliott59ad9452022-12-18 15:09:02 +00003220 return status;
3221}
3222
Paul Elliott9fe12f62022-11-30 19:16:02 +00003223psa_status_t psa_sign_hash_start(
3224 psa_sign_hash_interruptible_operation_t *operation,
3225 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3226 const uint8_t *hash, size_t hash_length)
3227{
3228 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3229 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3230 psa_key_slot_t *slot;
3231
Paul Elliottc9774412023-02-06 15:14:07 +00003232 /* Check that start has not been previously called, or operation has not
3233 * previously errored. */
3234 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003235 return PSA_ERROR_BAD_STATE;
3236 }
3237
Paul Elliott9fe12f62022-11-30 19:16:02 +00003238 status = psa_sign_verify_check_alg(0, alg);
3239 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003240 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003241 return status;
3242 }
3243
3244 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3245 PSA_KEY_USAGE_SIGN_HASH,
3246 alg);
3247
3248 if (status != PSA_SUCCESS) {
3249 goto exit;
3250 }
3251
3252 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3253 status = PSA_ERROR_INVALID_ARGUMENT;
3254 goto exit;
3255 }
3256
3257 psa_key_attributes_t attributes = {
3258 .core = slot->attr
3259 };
3260
Paul Elliott296ede92022-12-15 17:00:30 +00003261 /* Ensure ops count gets reset, in case of operation re-use. */
3262 operation->num_ops = 0;
3263
Paul Elliott9fe12f62022-11-30 19:16:02 +00003264 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3265 slot->key.data,
3266 slot->key.bytes, alg,
3267 hash, hash_length);
3268exit:
3269
3270 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003271 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003272 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003273 }
3274
3275 unlock_status = psa_unlock_key_slot(slot);
3276
Paul Elliottc9774412023-02-06 15:14:07 +00003277 if (unlock_status != PSA_SUCCESS) {
3278 operation->error_occurred = 1;
3279 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003280
Paul Elliottc9774412023-02-06 15:14:07 +00003281 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003282}
3283
3284
3285psa_status_t psa_sign_hash_complete(
3286 psa_sign_hash_interruptible_operation_t *operation,
3287 uint8_t *signature, size_t signature_size,
3288 size_t *signature_length)
3289{
3290 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3291
3292 *signature_length = 0;
3293
Paul Elliottc9774412023-02-06 15:14:07 +00003294 /* Check that start has been called first, and that operation has not
3295 * previously errored. */
3296 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003297 status = PSA_ERROR_BAD_STATE;
3298 goto exit;
3299 }
3300
Paul Elliott096abc42023-02-03 18:33:23 +00003301 /* Immediately reject a zero-length signature buffer. This guarantees that
3302 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003303 if (signature_size == 0) {
3304 status = PSA_ERROR_BUFFER_TOO_SMALL;
3305 goto exit;
3306 }
3307
3308 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3309 signature_size,
3310 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003311
Paul Elliott296ede92022-12-15 17:00:30 +00003312 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003313 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003314
Paul Elliottebe225c2023-02-10 14:32:53 +00003315exit:
3316
Paul Elliott59200a22023-02-21 15:07:40 +00003317 psa_wipe_tag_output_buffer(signature, status, signature_size,
3318 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003319
Paul Elliott53bb3122023-02-10 14:22:22 +00003320 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003321 if (status != PSA_SUCCESS) {
3322 operation->error_occurred = 1;
3323 }
3324
Paul Elliott59ad9452022-12-18 15:09:02 +00003325 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003326 }
3327
3328 return status;
3329}
3330
3331psa_status_t psa_sign_hash_abort(
3332 psa_sign_hash_interruptible_operation_t *operation)
3333{
Paul Elliott59ad9452022-12-18 15:09:02 +00003334 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3335
3336 status = psa_sign_hash_abort_internal(operation);
3337
3338 /* We clear the number of ops done here, so that it is not cleared when
3339 * the operation fails or succeeds, only on manual abort. */
3340 operation->num_ops = 0;
3341
Paul Elliottc9774412023-02-06 15:14:07 +00003342 /* Likewise, failure state. */
3343 operation->error_occurred = 0;
3344
Paul Elliott59ad9452022-12-18 15:09:02 +00003345 return status;
3346}
3347
3348static psa_status_t psa_verify_hash_abort_internal(
3349 psa_verify_hash_interruptible_operation_t *operation)
3350{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003351 if (operation->id == 0) {
3352 /* The object has (apparently) been initialized but it is not (yet)
3353 * in use. It's ok to call abort on such an object, and there's
3354 * nothing to do. */
3355 return PSA_SUCCESS;
3356 }
3357
Paul Elliott59ad9452022-12-18 15:09:02 +00003358 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3359
3360 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003361
3362 operation->id = 0;
3363
Paul Elliotte6145dc2023-02-07 12:51:21 +00003364 /* Do not clear either the error_occurred or num_ops elements here as they
3365 * only want to be cleared by the application calling abort, not by abort
3366 * being called at completion of an operation. */
3367
Paul Elliott59ad9452022-12-18 15:09:02 +00003368 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003369}
3370
3371psa_status_t psa_verify_hash_start(
3372 psa_verify_hash_interruptible_operation_t *operation,
3373 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3374 const uint8_t *hash, size_t hash_length,
3375 const uint8_t *signature, size_t signature_length)
3376{
3377 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3378 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3379 psa_key_slot_t *slot;
3380
Paul Elliottc9774412023-02-06 15:14:07 +00003381 /* Check that start has not been previously called, or operation has not
3382 * previously errored. */
3383 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003384 return PSA_ERROR_BAD_STATE;
3385 }
3386
3387 status = psa_sign_verify_check_alg(0, alg);
3388 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003389 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003390 return status;
3391 }
3392
3393 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3394 PSA_KEY_USAGE_VERIFY_HASH,
3395 alg);
3396
3397 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003398 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003399 return status;
3400 }
3401
3402 psa_key_attributes_t attributes = {
3403 .core = slot->attr
3404 };
3405
Paul Elliott296ede92022-12-15 17:00:30 +00003406 /* Ensure ops count gets reset, in case of operation re-use. */
3407 operation->num_ops = 0;
3408
Paul Elliott9fe12f62022-11-30 19:16:02 +00003409 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3410 slot->key.data,
3411 slot->key.bytes,
3412 alg, hash, hash_length,
3413 signature, signature_length);
3414
3415 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003416 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003417 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003418 }
3419
3420 unlock_status = psa_unlock_key_slot(slot);
3421
Paul Elliottc9774412023-02-06 15:14:07 +00003422 if (unlock_status != PSA_SUCCESS) {
3423 operation->error_occurred = 1;
3424 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003425
Paul Elliottc9774412023-02-06 15:14:07 +00003426 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003427}
3428
3429psa_status_t psa_verify_hash_complete(
3430 psa_verify_hash_interruptible_operation_t *operation)
3431{
3432 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3433
Paul Elliottc9774412023-02-06 15:14:07 +00003434 /* Check that start has been called first, and that operation has not
3435 * previously errored. */
3436 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003437 status = PSA_ERROR_BAD_STATE;
3438 goto exit;
3439 }
3440
3441 status = psa_driver_wrapper_verify_hash_complete(operation);
3442
Paul Elliott296ede92022-12-15 17:00:30 +00003443 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003444 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003445 operation);
3446
Paul Elliottebe225c2023-02-10 14:32:53 +00003447exit:
3448
Paul Elliott9fe12f62022-11-30 19:16:02 +00003449 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003450 if (status != PSA_SUCCESS) {
3451 operation->error_occurred = 1;
3452 }
3453
Paul Elliott59ad9452022-12-18 15:09:02 +00003454 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003455 }
3456
3457 return status;
3458}
3459
3460psa_status_t psa_verify_hash_abort(
3461 psa_verify_hash_interruptible_operation_t *operation)
3462{
Paul Elliott59ad9452022-12-18 15:09:02 +00003463 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003464
Paul Elliott59ad9452022-12-18 15:09:02 +00003465 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003466
Paul Elliott59ad9452022-12-18 15:09:02 +00003467 /* We clear the number of ops done here, so that it is not cleared when
3468 * the operation fails or succeeds, only on manual abort. */
3469 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003470
Paul Elliottc9774412023-02-06 15:14:07 +00003471 /* Likewise, failure state. */
3472 operation->error_occurred = 0;
3473
Paul Elliott59ad9452022-12-18 15:09:02 +00003474 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003475}
3476
Paul Elliott588f8ed2022-12-02 18:10:26 +00003477/****************************************************************/
3478/* Asymmetric interruptible cryptography internal */
3479/* implementations */
3480/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003481
Paul Elliott588f8ed2022-12-02 18:10:26 +00003482void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3483{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003484
Paul Elliott588f8ed2022-12-02 18:10:26 +00003485#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3486 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3487 defined(MBEDTLS_ECP_RESTARTABLE)
3488
3489 /* Internal implementation uses zero to indicate infinite number max ops,
3490 * therefore avoid this value, and set to minimum possible. */
3491 if (max_ops == 0) {
3492 max_ops = 1;
3493 }
3494
Paul Elliott588f8ed2022-12-02 18:10:26 +00003495 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003496#else
3497 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003498#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3499 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3500 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3501}
3502
Paul Elliott588f8ed2022-12-02 18:10:26 +00003503uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003504 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003505{
3506#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3507 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3508 defined(MBEDTLS_ECP_RESTARTABLE)
3509
Paul Elliott93d9ca82023-02-15 18:14:21 +00003510 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003511#else
3512 (void) operation;
3513 return 0;
3514#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3515 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3516 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3517}
3518
3519uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003520 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003521{
3522 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3523 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3524 defined(MBEDTLS_ECP_RESTARTABLE)
3525
Paul Elliott93d9ca82023-02-15 18:14:21 +00003526 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003527#else
3528 (void) operation;
3529 return 0;
3530#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3531 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3532 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3533}
3534
3535psa_status_t mbedtls_psa_sign_hash_start(
3536 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3537 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3538 size_t key_buffer_size, psa_algorithm_t alg,
3539 const uint8_t *hash, size_t hash_length)
3540{
3541 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003542 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003543
Paul Elliott068fe072023-01-16 13:59:15 +00003544 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3545 return PSA_ERROR_NOT_SUPPORTED;
3546 }
3547
3548 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003549 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003550 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003551
3552#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003553 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3554 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003555
Paul Elliotta1c94092023-02-15 16:38:04 +00003556 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3557
Paul Elliott93d9ca82023-02-15 18:14:21 +00003558 /* Ensure num_ops is zero'ed in case of context re-use. */
3559 operation->num_ops = 0;
3560
Paul Elliott068fe072023-01-16 13:59:15 +00003561 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3562 attributes->core.bits,
3563 key_buffer,
3564 key_buffer_size,
3565 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003566
Paul Elliott068fe072023-01-16 13:59:15 +00003567 if (status != PSA_SUCCESS) {
3568 return status;
3569 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003570
Paul Elliott1bc59df2023-02-05 13:41:57 +00003571 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003572 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003573
Paul Elliott068fe072023-01-16 13:59:15 +00003574 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3575 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3576 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003577
Paul Elliott0290a762023-02-09 14:30:24 +00003578 /* We only need to store the same length of hash as the private key size
3579 * here, it would be truncated by the internal implementation anyway. */
3580 required_hash_length = (hash_length < operation->coordinate_bytes ?
3581 hash_length : operation->coordinate_bytes);
3582
Paul Elliottba70ad42023-02-15 18:23:53 +00003583 if (required_hash_length > sizeof(operation->hash)) {
3584 /* Shouldn't happen, but better safe than sorry. */
3585 return PSA_ERROR_CORRUPTION_DETECTED;
3586 }
3587
Paul Elliott0290a762023-02-09 14:30:24 +00003588 memcpy(operation->hash, hash, required_hash_length);
3589 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003590
3591 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003592
3593#else
Paul Elliott068fe072023-01-16 13:59:15 +00003594 (void) operation;
3595 (void) key_buffer;
3596 (void) key_buffer_size;
3597 (void) alg;
3598 (void) hash;
3599 (void) hash_length;
3600 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003601 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003602
Paul Elliott068fe072023-01-16 13:59:15 +00003603 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003604#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3605 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3606 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003607}
3608
3609psa_status_t mbedtls_psa_sign_hash_complete(
3610 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3611 uint8_t *signature, size_t signature_size,
3612 size_t *signature_length)
3613{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003614#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3615 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3616 defined(MBEDTLS_ECP_RESTARTABLE)
3617
Paul Elliotta1c94092023-02-15 16:38:04 +00003618 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003619 mbedtls_mpi r;
3620 mbedtls_mpi s;
3621
Paul Elliott46845252023-02-03 14:59:11 +00003622 mbedtls_mpi_init(&r);
3623 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003624
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003625 /* Ensure max_ops is set to the current value (or default). */
3626 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3627
Paul Elliotta1c94092023-02-15 16:38:04 +00003628 if (signature_size < 2 * operation->coordinate_bytes) {
3629 status = PSA_ERROR_BUFFER_TOO_SMALL;
3630 goto exit;
3631 }
3632
Paul Elliott588f8ed2022-12-02 18:10:26 +00003633 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003634
Paul Elliott588f8ed2022-12-02 18:10:26 +00003635#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3636 status = mbedtls_to_psa_error(
3637 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003638 &r,
3639 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003640 &operation->ctx->d,
3641 operation->hash,
3642 operation->hash_length,
3643 operation->md_alg,
3644 mbedtls_psa_get_random,
3645 MBEDTLS_PSA_RANDOM_STATE,
3646 &operation->restart_ctx));
3647#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003648 status = PSA_ERROR_NOT_SUPPORTED;
3649 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003650#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3651 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003652 status = mbedtls_to_psa_error(
3653 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003654 &r,
3655 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003656 &operation->ctx->d,
3657 operation->hash,
3658 operation->hash_length,
3659 mbedtls_psa_get_random,
3660 MBEDTLS_PSA_RANDOM_STATE,
3661 mbedtls_psa_get_random,
3662 MBEDTLS_PSA_RANDOM_STATE,
3663 &operation->restart_ctx));
3664 }
3665
Paul Elliottf8e5b562023-02-19 18:43:45 +00003666 /* Hide the fact that the restart context only holds a delta of number of
3667 * ops done during the last operation, not an absolute value. */
3668 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3669
Paul Elliott724bd252023-02-08 12:35:08 +00003670 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003671 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003672 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003673 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003674 operation->coordinate_bytes)
3675 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003676
3677 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003678 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003679 }
3680
3681 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003682 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003683 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003684 operation->coordinate_bytes,
3685 operation->coordinate_bytes)
3686 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003687
3688 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003689 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003690 }
3691
Paul Elliott1bc59df2023-02-05 13:41:57 +00003692 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003693
Paul Elliott724bd252023-02-08 12:35:08 +00003694 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003695 }
Paul Elliott724bd252023-02-08 12:35:08 +00003696
3697exit:
3698
3699 mbedtls_mpi_free(&r);
3700 mbedtls_mpi_free(&s);
3701 return status;
3702
Paul Elliott588f8ed2022-12-02 18:10:26 +00003703 #else
3704
3705 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003706 (void) signature;
3707 (void) signature_size;
3708 (void) signature_length;
3709
3710 return PSA_ERROR_NOT_SUPPORTED;
3711
3712#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3713 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3714 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3715}
3716
3717psa_status_t mbedtls_psa_sign_hash_abort(
3718 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3719{
3720
3721#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3722 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3723 defined(MBEDTLS_ECP_RESTARTABLE)
3724
3725 if (operation->ctx) {
3726 mbedtls_ecdsa_free(operation->ctx);
3727 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003728 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003729 }
3730
3731 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3732
Paul Elliott93d9ca82023-02-15 18:14:21 +00003733 operation->num_ops = 0;
3734
Paul Elliott588f8ed2022-12-02 18:10:26 +00003735 return PSA_SUCCESS;
3736
3737#else
3738
3739 (void) operation;
3740
3741 return PSA_ERROR_NOT_SUPPORTED;
3742
3743#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3744 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3745 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3746}
3747
3748psa_status_t mbedtls_psa_verify_hash_start(
3749 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3750 const psa_key_attributes_t *attributes,
3751 const uint8_t *key_buffer, size_t key_buffer_size,
3752 psa_algorithm_t alg,
3753 const uint8_t *hash, size_t hash_length,
3754 const uint8_t *signature, size_t signature_length)
3755{
3756 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003757 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003758 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003759
Paul Elliott068fe072023-01-16 13:59:15 +00003760 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3761 return PSA_ERROR_NOT_SUPPORTED;
3762 }
3763
3764 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003765 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003766 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003767
3768#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003769 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3770 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003771
Paul Elliotta1c94092023-02-15 16:38:04 +00003772 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3773 mbedtls_mpi_init(&operation->r);
3774 mbedtls_mpi_init(&operation->s);
3775
Paul Elliott93d9ca82023-02-15 18:14:21 +00003776 /* Ensure num_ops is zero'ed in case of context re-use. */
3777 operation->num_ops = 0;
3778
Paul Elliott068fe072023-01-16 13:59:15 +00003779 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3780 attributes->core.bits,
3781 key_buffer,
3782 key_buffer_size,
3783 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003784
Paul Elliott068fe072023-01-16 13:59:15 +00003785 if (status != PSA_SUCCESS) {
3786 return status;
3787 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003788
Paul Elliottc569fc22023-02-10 13:02:54 +00003789 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003790
Paul Elliott1bc59df2023-02-05 13:41:57 +00003791 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003792 return PSA_ERROR_INVALID_SIGNATURE;
3793 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003794
Paul Elliott068fe072023-01-16 13:59:15 +00003795 status = mbedtls_to_psa_error(
3796 mbedtls_mpi_read_binary(&operation->r,
3797 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003798 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003799
Paul Elliott068fe072023-01-16 13:59:15 +00003800 if (status != PSA_SUCCESS) {
3801 return status;
3802 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003803
Paul Elliott068fe072023-01-16 13:59:15 +00003804 status = mbedtls_to_psa_error(
3805 mbedtls_mpi_read_binary(&operation->s,
3806 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003807 coordinate_bytes,
3808 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003809
Paul Elliott068fe072023-01-16 13:59:15 +00003810 if (status != PSA_SUCCESS) {
3811 return status;
3812 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003813
Paul Elliott2c9843f2023-02-15 17:32:42 +00003814 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003815
Paul Elliott2c9843f2023-02-15 17:32:42 +00003816 if (status != PSA_SUCCESS) {
3817 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003818 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003819
Paul Elliott0290a762023-02-09 14:30:24 +00003820 /* We only need to store the same length of hash as the private key size
3821 * here, it would be truncated by the internal implementation anyway. */
3822 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3823 coordinate_bytes);
3824
Paul Elliottba70ad42023-02-15 18:23:53 +00003825 if (required_hash_length > sizeof(operation->hash)) {
3826 /* Shouldn't happen, but better safe than sorry. */
3827 return PSA_ERROR_CORRUPTION_DETECTED;
3828 }
3829
Paul Elliott0290a762023-02-09 14:30:24 +00003830 memcpy(operation->hash, hash, required_hash_length);
3831 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003832
3833 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003834#else
Paul Elliott068fe072023-01-16 13:59:15 +00003835 (void) operation;
3836 (void) key_buffer;
3837 (void) key_buffer_size;
3838 (void) alg;
3839 (void) hash;
3840 (void) hash_length;
3841 (void) signature;
3842 (void) signature_length;
3843 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003844 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003845 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003846
Paul Elliott068fe072023-01-16 13:59:15 +00003847 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003848#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3849 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3850 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003851}
3852
3853psa_status_t mbedtls_psa_verify_hash_complete(
3854 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3855{
3856
3857#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3858 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3859 defined(MBEDTLS_ECP_RESTARTABLE)
3860
Paul Elliottf8e5b562023-02-19 18:43:45 +00003861 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3862
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003863 /* Ensure max_ops is set to the current value (or default). */
3864 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3865
Paul Elliottf8e5b562023-02-19 18:43:45 +00003866 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003867 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3868 operation->hash,
3869 operation->hash_length,
3870 &operation->ctx->Q,
3871 &operation->r,
3872 &operation->s,
3873 &operation->restart_ctx));
3874
Paul Elliottf8e5b562023-02-19 18:43:45 +00003875 /* Hide the fact that the restart context only holds a delta of number of
3876 * ops done during the last operation, not an absolute value. */
3877 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3878
3879 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003880#else
3881 (void) operation;
3882
3883 return PSA_ERROR_NOT_SUPPORTED;
3884
3885#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3886 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3887 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3888}
3889
3890psa_status_t mbedtls_psa_verify_hash_abort(
3891 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3892{
3893
3894#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3895 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3896 defined(MBEDTLS_ECP_RESTARTABLE)
3897
3898 if (operation->ctx) {
3899 mbedtls_ecdsa_free(operation->ctx);
3900 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003901 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003902 }
3903
3904 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3905
Paul Elliott93d9ca82023-02-15 18:14:21 +00003906 operation->num_ops = 0;
3907
Paul Elliott588f8ed2022-12-02 18:10:26 +00003908 mbedtls_mpi_free(&operation->r);
3909 mbedtls_mpi_free(&operation->s);
3910
3911 return PSA_SUCCESS;
3912
3913#else
3914 (void) operation;
3915
3916 return PSA_ERROR_NOT_SUPPORTED;
3917
3918#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3919 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3920 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3921}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003922
mohammad1603503973b2018-03-12 15:59:30 +02003923/****************************************************************/
3924/* Symmetric cryptography */
3925/****************************************************************/
3926
Gilles Peskine449bd832023-01-11 14:50:10 +01003927static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3928 mbedtls_svc_key_id_t key,
3929 psa_algorithm_t alg,
3930 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003931{
3932 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3933 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003934 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003935 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3936 PSA_KEY_USAGE_ENCRYPT :
3937 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003938
3939 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003940 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003941 status = PSA_ERROR_BAD_STATE;
3942 goto exit;
3943 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003944
Gilles Peskine449bd832023-01-11 14:50:10 +01003945 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003946 status = PSA_ERROR_INVALID_ARGUMENT;
3947 goto exit;
3948 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003949
Gilles Peskine449bd832023-01-11 14:50:10 +01003950 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3951 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003952 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003953 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003954
Ronald Cron49fafa92021-03-10 08:34:23 +01003955 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003956 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003957 * so we only set it (in the driver wrapper) after resources have been
3958 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003959 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003960 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003961 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003962 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003963 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003964 }
3965 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003966
Ronald Crona4af55f2020-12-14 14:36:06 +01003967 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003968 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003969 };
3970
Ronald Cronab99ac22020-10-01 16:38:28 +02003971 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003972 if (cipher_operation == MBEDTLS_ENCRYPT) {
3973 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3974 &attributes,
3975 slot->key.data,
3976 slot->key.bytes,
3977 alg);
3978 } else {
3979 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3980 &attributes,
3981 slot->key.data,
3982 slot->key.bytes,
3983 alg);
3984 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003985
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003986exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003987 if (status != PSA_SUCCESS) {
3988 psa_cipher_abort(operation);
3989 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003990
Gilles Peskine449bd832023-01-11 14:50:10 +01003991 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003992
Gilles Peskine449bd832023-01-11 14:50:10 +01003993 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003994}
3995
Gilles Peskine449bd832023-01-11 14:50:10 +01003996psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3997 mbedtls_svc_key_id_t key,
3998 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003999{
Gilles Peskine449bd832023-01-11 14:50:10 +01004000 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004001}
4002
Gilles Peskine449bd832023-01-11 14:50:10 +01004003psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4004 mbedtls_svc_key_id_t key,
4005 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004006{
Gilles Peskine449bd832023-01-11 14:50:10 +01004007 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004008}
4009
Gilles Peskine449bd832023-01-11 14:50:10 +01004010psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4011 uint8_t *iv,
4012 size_t iv_size,
4013 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004014{
Ronald Cron6d051732020-10-01 14:10:20 +02004015 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004016 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4017 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004018
Gilles Peskine449bd832023-01-11 14:50:10 +01004019 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004020 status = PSA_ERROR_BAD_STATE;
4021 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004022 }
4023
Gilles Peskine449bd832023-01-11 14:50:10 +01004024 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004025 status = PSA_ERROR_BAD_STATE;
4026 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004027 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004028
Ronald Cron2fb90522021-07-05 12:31:44 +02004029 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004030 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004031 status = PSA_ERROR_BUFFER_TOO_SMALL;
4032 goto exit;
4033 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004034
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004036 status = PSA_ERROR_GENERIC_ERROR;
4037 goto exit;
4038 }
4039
Gilles Peskine449bd832023-01-11 14:50:10 +01004040 status = psa_generate_random(local_iv, default_iv_length);
4041 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004042 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004043 }
Ronald Cron5618a392021-03-26 09:52:26 +01004044
Gilles Peskine449bd832023-01-11 14:50:10 +01004045 status = psa_driver_wrapper_cipher_set_iv(operation,
4046 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004047
4048exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004049 if (status == PSA_SUCCESS) {
4050 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004051 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004052 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004053 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004054 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004055 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004056 }
Ronald Cron6d051732020-10-01 14:10:20 +02004057
Gilles Peskine449bd832023-01-11 14:50:10 +01004058 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004059}
4060
Gilles Peskine449bd832023-01-11 14:50:10 +01004061psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4062 const uint8_t *iv,
4063 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004064{
Ronald Cron6d051732020-10-01 14:10:20 +02004065 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004066
Gilles Peskine449bd832023-01-11 14:50:10 +01004067 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004068 status = PSA_ERROR_BAD_STATE;
4069 goto exit;
4070 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004071
Gilles Peskine449bd832023-01-11 14:50:10 +01004072 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004073 status = PSA_ERROR_BAD_STATE;
4074 goto exit;
4075 }
Ronald Crona0d68172021-03-26 10:15:08 +01004076
Gilles Peskine449bd832023-01-11 14:50:10 +01004077 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004078 status = PSA_ERROR_INVALID_ARGUMENT;
4079 goto exit;
4080 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004081
Gilles Peskine449bd832023-01-11 14:50:10 +01004082 status = psa_driver_wrapper_cipher_set_iv(operation,
4083 iv,
4084 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004085
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004086exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004087 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004088 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004089 } else {
4090 psa_cipher_abort(operation);
4091 }
4092 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004093}
4094
Gilles Peskine449bd832023-01-11 14:50:10 +01004095psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4096 const uint8_t *input,
4097 size_t input_length,
4098 uint8_t *output,
4099 size_t output_size,
4100 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004101{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004102 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004103
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004105 status = PSA_ERROR_BAD_STATE;
4106 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004107 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004108
Gilles Peskine449bd832023-01-11 14:50:10 +01004109 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004110 status = PSA_ERROR_BAD_STATE;
4111 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004112 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004113
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 status = psa_driver_wrapper_cipher_update(operation,
4115 input,
4116 input_length,
4117 output,
4118 output_size,
4119 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004120
4121exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 if (status != PSA_SUCCESS) {
4123 psa_cipher_abort(operation);
4124 }
Ronald Cron6d051732020-10-01 14:10:20 +02004125
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004127}
4128
Gilles Peskine449bd832023-01-11 14:50:10 +01004129psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4130 uint8_t *output,
4131 size_t output_size,
4132 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004133{
David Saadab4ecc272019-02-14 13:48:10 +02004134 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004135
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004137 status = PSA_ERROR_BAD_STATE;
4138 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004139 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004140
Gilles Peskine449bd832023-01-11 14:50:10 +01004141 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004142 status = PSA_ERROR_BAD_STATE;
4143 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004144 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004145
Gilles Peskine449bd832023-01-11 14:50:10 +01004146 status = psa_driver_wrapper_cipher_finish(operation,
4147 output,
4148 output_size,
4149 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004150
4151exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004152 if (status == PSA_SUCCESS) {
4153 return psa_cipher_abort(operation);
4154 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004155 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004156 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004157
Gilles Peskine449bd832023-01-11 14:50:10 +01004158 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004159 }
mohammad1603503973b2018-03-12 15:59:30 +02004160}
4161
Gilles Peskine449bd832023-01-11 14:50:10 +01004162psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004163{
Gilles Peskine449bd832023-01-11 14:50:10 +01004164 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004165 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004166 * in use. It's ok to call abort on such an object, and there's
4167 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004168 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004169 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004170
Gilles Peskine449bd832023-01-11 14:50:10 +01004171 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004172
Ronald Cron49fafa92021-03-10 08:34:23 +01004173 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004174 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004175 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004176
Gilles Peskine449bd832023-01-11 14:50:10 +01004177 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004178}
4179
Gilles Peskine449bd832023-01-11 14:50:10 +01004180psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4181 psa_algorithm_t alg,
4182 const uint8_t *input,
4183 size_t input_length,
4184 uint8_t *output,
4185 size_t output_size,
4186 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004187{
4188 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004189 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004190 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004191 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4192 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004193
Gilles Peskine449bd832023-01-11 14:50:10 +01004194 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004195 status = PSA_ERROR_INVALID_ARGUMENT;
4196 goto exit;
4197 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004198
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4200 PSA_KEY_USAGE_ENCRYPT,
4201 alg);
4202 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004203 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004204 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004205
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004206 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004208 };
4209
Gilles Peskine449bd832023-01-11 14:50:10 +01004210 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4211 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004212 status = PSA_ERROR_GENERIC_ERROR;
4213 goto exit;
4214 }
4215
Gilles Peskine449bd832023-01-11 14:50:10 +01004216 if (default_iv_length > 0) {
4217 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004218 status = PSA_ERROR_BUFFER_TOO_SMALL;
4219 goto exit;
4220 }
4221
Gilles Peskine449bd832023-01-11 14:50:10 +01004222 status = psa_generate_random(local_iv, default_iv_length);
4223 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004224 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004225 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004226 }
4227
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004228 status = psa_driver_wrapper_cipher_encrypt(
4229 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004230 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 mbedtls_buffer_offset(output, default_iv_length),
4232 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004233
4234exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004235 unlock_status = psa_unlock_key_slot(slot);
4236 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004237 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004238 }
Ronald Cron23919522021-07-08 19:00:07 +02004239
Gilles Peskine449bd832023-01-11 14:50:10 +01004240 if (status == PSA_SUCCESS) {
4241 if (default_iv_length > 0) {
4242 memcpy(output, local_iv, default_iv_length);
4243 }
4244 *output_length += default_iv_length;
4245 } else {
4246 *output_length = 0;
4247 }
4248
4249 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004250}
4251
Gilles Peskine449bd832023-01-11 14:50:10 +01004252psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4253 psa_algorithm_t alg,
4254 const uint8_t *input,
4255 size_t input_length,
4256 uint8_t *output,
4257 size_t output_size,
4258 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004259{
4260 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004261 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004262 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004263
Gilles Peskine449bd832023-01-11 14:50:10 +01004264 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004265 status = PSA_ERROR_INVALID_ARGUMENT;
4266 goto exit;
4267 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004268
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4270 PSA_KEY_USAGE_DECRYPT,
4271 alg);
4272 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004273 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004274 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004275
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004276 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004277 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004278 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004279
Gilles Peskine449bd832023-01-11 14:50:10 +01004280 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4281 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004282 status = PSA_ERROR_INVALID_ARGUMENT;
4283 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004284 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004285 status = PSA_ERROR_INVALID_ARGUMENT;
4286 goto exit;
4287 }
4288
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004289 status = psa_driver_wrapper_cipher_decrypt(
4290 &attributes, slot->key.data, slot->key.bytes,
4291 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004292 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004293
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004294exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004295 unlock_status = psa_unlock_key_slot(slot);
4296 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004297 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004299
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004301 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004302 }
Ronald Cron23919522021-07-08 19:00:07 +02004303
Gilles Peskine449bd832023-01-11 14:50:10 +01004304 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004305}
4306
4307
mohammad16035955c982018-04-26 00:53:03 +03004308/****************************************************************/
4309/* AEAD */
4310/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004311
Paul Elliottbaff51c2021-09-28 17:44:45 +01004312/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004313static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004314{
Gilles Peskine449bd832023-01-11 14:50:10 +01004315 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004316}
4317
4318/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004319static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4320 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004321{
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004323
Gilles Peskine449bd832023-01-11 14:50:10 +01004324 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004325#if defined(PSA_WANT_ALG_GCM)
4326 case PSA_ALG_GCM:
4327 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 * arbitrarily large nonces. Please note that we do not generally
4329 * recommend the usage of nonces of greater length than
4330 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4331 * size, which can then lead to collisions if you encrypt a very
4332 * large number of messages.*/
4333 if (nonce_length != 0) {
4334 return PSA_SUCCESS;
4335 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004336 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004337#endif /* PSA_WANT_ALG_GCM */
4338#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004339 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004340 if (nonce_length >= 7 && nonce_length <= 13) {
4341 return PSA_SUCCESS;
4342 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004343 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004344#endif /* PSA_WANT_ALG_CCM */
4345#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004346 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004347 if (nonce_length == 12) {
4348 return PSA_SUCCESS;
4349 } else if (nonce_length == 8) {
4350 return PSA_ERROR_NOT_SUPPORTED;
4351 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004352 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004353#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004354 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004355 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004356 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004357 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004358
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004360}
4361
Gilles Peskine449bd832023-01-11 14:50:10 +01004362static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004363{
Gilles Peskine449bd832023-01-11 14:50:10 +01004364 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4365 return PSA_ERROR_INVALID_ARGUMENT;
4366 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004367
Gilles Peskine449bd832023-01-11 14:50:10 +01004368 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004369}
4370
Gilles Peskine449bd832023-01-11 14:50:10 +01004371psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4372 psa_algorithm_t alg,
4373 const uint8_t *nonce,
4374 size_t nonce_length,
4375 const uint8_t *additional_data,
4376 size_t additional_data_length,
4377 const uint8_t *plaintext,
4378 size_t plaintext_length,
4379 uint8_t *ciphertext,
4380 size_t ciphertext_size,
4381 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004382{
Ronald Cron215633c2021-03-16 17:15:37 +01004383 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4384 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004385
mohammad1603f08a5502018-06-03 15:05:47 +03004386 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004387
Gilles Peskine449bd832023-01-11 14:50:10 +01004388 status = psa_aead_check_algorithm(alg);
4389 if (status != PSA_SUCCESS) {
4390 return status;
4391 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004392
Ronald Cron9a986162021-03-26 12:40:07 +01004393 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4395 if (status != PSA_SUCCESS) {
4396 return status;
4397 }
mohammad16035955c982018-04-26 00:53:03 +03004398
Ronald Cron215633c2021-03-16 17:15:37 +01004399 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004401 };
mohammad16035955c982018-04-26 00:53:03 +03004402
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 status = psa_aead_check_nonce_length(alg, nonce_length);
4404 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004405 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004407
Ronald Cronde822812021-03-17 16:08:20 +01004408 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004409 &attributes, slot->key.data, slot->key.bytes,
4410 alg,
4411 nonce, nonce_length,
4412 additional_data, additional_data_length,
4413 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004414 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004415
Gilles Peskine449bd832023-01-11 14:50:10 +01004416 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4417 memset(ciphertext, 0, ciphertext_size);
4418 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004419
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004420exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004421 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004422
Gilles Peskine449bd832023-01-11 14:50:10 +01004423 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004424}
4425
Gilles Peskine449bd832023-01-11 14:50:10 +01004426psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4427 psa_algorithm_t alg,
4428 const uint8_t *nonce,
4429 size_t nonce_length,
4430 const uint8_t *additional_data,
4431 size_t additional_data_length,
4432 const uint8_t *ciphertext,
4433 size_t ciphertext_length,
4434 uint8_t *plaintext,
4435 size_t plaintext_size,
4436 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004437{
Ronald Cron215633c2021-03-16 17:15:37 +01004438 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4439 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004440
Gilles Peskineee652a32018-06-01 19:23:52 +02004441 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004442
Gilles Peskine449bd832023-01-11 14:50:10 +01004443 status = psa_aead_check_algorithm(alg);
4444 if (status != PSA_SUCCESS) {
4445 return status;
4446 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004447
Ronald Cron9a986162021-03-26 12:40:07 +01004448 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004449 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4450 if (status != PSA_SUCCESS) {
4451 return status;
4452 }
mohammad16035955c982018-04-26 00:53:03 +03004453
Ronald Cron215633c2021-03-16 17:15:37 +01004454 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004456 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004457
Gilles Peskine449bd832023-01-11 14:50:10 +01004458 status = psa_aead_check_nonce_length(alg, nonce_length);
4459 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004460 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004462
Ronald Cronde822812021-03-17 16:08:20 +01004463 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004464 &attributes, slot->key.data, slot->key.bytes,
4465 alg,
4466 nonce, nonce_length,
4467 additional_data, additional_data_length,
4468 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004470
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 if (status != PSA_SUCCESS && plaintext_size != 0) {
4472 memset(plaintext, 0, plaintext_size);
4473 }
mohammad160360a64d02018-06-03 17:20:42 +03004474
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004475exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004476 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004477
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 return status;
mohammad16035955c982018-04-26 00:53:03 +03004479}
4480
Gilles Peskine449bd832023-01-11 14:50:10 +01004481static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4482{
4483 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004484
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004486#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004488 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004489 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4490 return PSA_ERROR_INVALID_ARGUMENT;
4491 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004492 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004493#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004494
Przemek Stekiel86679c72022-10-06 17:06:56 +02004495#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004496 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004497 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004498 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4499 return PSA_ERROR_INVALID_ARGUMENT;
4500 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004501 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004502#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004503
Przemek Stekiel86679c72022-10-06 17:06:56 +02004504#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004506 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 if (tag_len != 16) {
4508 return PSA_ERROR_INVALID_ARGUMENT;
4509 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004510 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004511#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004512
4513 default:
4514 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004515 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004516 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004518}
4519
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004520/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004521static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4522 int is_encrypt,
4523 mbedtls_svc_key_id_t key,
4524 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004525{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004526 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4527 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4528 psa_key_slot_t *slot = NULL;
4529 psa_key_usage_t key_usage = 0;
4530
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 status = psa_aead_check_algorithm(alg);
4532 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004533 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004535
Gilles Peskine449bd832023-01-11 14:50:10 +01004536 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004537 status = PSA_ERROR_BAD_STATE;
4538 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004539 }
4540
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 if (operation->nonce_set || operation->lengths_set ||
4542 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004543 status = PSA_ERROR_BAD_STATE;
4544 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004545 }
4546
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004548 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004550 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004551 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004552
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4554 alg);
4555 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004556 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004557 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004558
4559 psa_key_attributes_t attributes = {
4560 .core = slot->attr
4561 };
4562
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004564 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004566
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 if (is_encrypt) {
4568 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4569 &attributes,
4570 slot->key.data,
4571 slot->key.bytes,
4572 alg);
4573 } else {
4574 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4575 &attributes,
4576 slot->key.data,
4577 slot->key.bytes,
4578 alg);
4579 }
4580 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004581 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004583
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004585
4586exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004588
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004590 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004592 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 } else {
4594 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004595 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004596
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004598}
4599
Paul Elliott302ff6b2021-04-20 18:10:30 +01004600/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004601psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4602 mbedtls_svc_key_id_t key,
4603 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004604{
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004606}
4607
4608/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004609psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4610 mbedtls_svc_key_id_t key,
4611 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004612{
Gilles Peskine449bd832023-01-11 14:50:10 +01004613 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004614}
4615
4616/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004617psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4618 uint8_t *nonce,
4619 size_t nonce_size,
4620 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004621{
4622 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004623 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004624 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004625
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004626 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004627
Gilles Peskine449bd832023-01-11 14:50:10 +01004628 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004629 status = PSA_ERROR_BAD_STATE;
4630 goto exit;
4631 }
4632
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004634 status = PSA_ERROR_BAD_STATE;
4635 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004636 }
4637
Paul Elliotte193ea82021-10-01 13:00:16 +01004638 /* For CCM, this size may not be correct according to the PSA
4639 * specification. The PSA Crypto 1.0.1 specification states:
4640 *
4641 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4642 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4643 *
4644 * However this restriction that L has to be the smallest integer is not
4645 * applied in practice, and it is not implementable here since the
4646 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4648 operation->alg);
4649 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004650 status = PSA_ERROR_BUFFER_TOO_SMALL;
4651 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004652 }
4653
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 status = psa_generate_random(local_nonce, required_nonce_size);
4655 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004656 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004658
Gilles Peskine449bd832023-01-11 14:50:10 +01004659 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004660
Paul Elliott39dc6b82021-05-11 19:16:09 +01004661exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004662 if (status == PSA_SUCCESS) {
4663 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004664 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004665 } else {
4666 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004667 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004668
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004670}
4671
4672/* Set the nonce for a multipart authenticated encryption or decryption
4673 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004674psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4675 const uint8_t *nonce,
4676 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004677{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004678 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4679
Gilles Peskine449bd832023-01-11 14:50:10 +01004680 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004681 status = PSA_ERROR_BAD_STATE;
4682 goto exit;
4683 }
4684
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004686 status = PSA_ERROR_BAD_STATE;
4687 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004688 }
4689
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4691 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004692 status = PSA_ERROR_INVALID_ARGUMENT;
4693 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004694 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004695
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4697 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004698
Paul Elliott39dc6b82021-05-11 19:16:09 +01004699exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004700 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004701 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 } else {
4703 psa_aead_abort(operation);
4704 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004705
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004707}
4708
4709/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004710psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4711 size_t ad_length,
4712 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004713{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004714 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4715
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004717 status = PSA_ERROR_BAD_STATE;
4718 goto exit;
4719 }
4720
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 if (operation->lengths_set || operation->ad_started ||
4722 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004723 status = PSA_ERROR_BAD_STATE;
4724 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004725 }
4726
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004728#if defined(PSA_WANT_ALG_GCM)
4729 case PSA_ALG_GCM:
4730 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004731 * bits. Without the guard this code will generate warnings on 32bit
4732 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004733#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 if (((uint64_t) ad_length) >> 61 != 0 ||
4735 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004736 status = PSA_ERROR_INVALID_ARGUMENT;
4737 goto exit;
4738 }
Paul Elliott325d3742021-09-27 17:56:28 +01004739#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004740 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004741#endif /* PSA_WANT_ALG_GCM */
4742#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004743 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004745 status = PSA_ERROR_INVALID_ARGUMENT;
4746 goto exit;
4747 }
4748 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004749#endif /* PSA_WANT_ALG_CCM */
4750#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004751 case PSA_ALG_CHACHA20_POLY1305:
4752 /* No length restrictions for ChaChaPoly. */
4753 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004754#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004755 default:
4756 break;
4757 }
Paul Elliott325d3742021-09-27 17:56:28 +01004758
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4760 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004761
Paul Elliott39dc6b82021-05-11 19:16:09 +01004762exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004764 operation->ad_remaining = ad_length;
4765 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004766 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 } else {
4768 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004769 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004770
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004772}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004773
4774/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004775psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4776 const uint8_t *input,
4777 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004778{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004779 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4780
Gilles Peskine449bd832023-01-11 14:50:10 +01004781 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004782 status = PSA_ERROR_BAD_STATE;
4783 goto exit;
4784 }
4785
Gilles Peskine449bd832023-01-11 14:50:10 +01004786 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004787 status = PSA_ERROR_BAD_STATE;
4788 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004789 }
4790
Gilles Peskine449bd832023-01-11 14:50:10 +01004791 if (operation->lengths_set) {
4792 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004793 status = PSA_ERROR_INVALID_ARGUMENT;
4794 goto exit;
4795 }
4796
4797 operation->ad_remaining -= input_length;
4798 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004799#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004800 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004801 status = PSA_ERROR_BAD_STATE;
4802 goto exit;
4803 }
4804#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004805
Gilles Peskine449bd832023-01-11 14:50:10 +01004806 status = psa_driver_wrapper_aead_update_ad(operation, input,
4807 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004808
Paul Elliott39dc6b82021-05-11 19:16:09 +01004809exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004810 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004811 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 } else {
4813 psa_aead_abort(operation);
4814 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004815
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004817}
4818
4819/* Encrypt or decrypt a message fragment in an active multipart AEAD
4820 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004821psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4822 const uint8_t *input,
4823 size_t input_length,
4824 uint8_t *output,
4825 size_t output_size,
4826 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004827{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004828 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004829
4830 *output_length = 0;
4831
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004833 status = PSA_ERROR_BAD_STATE;
4834 goto exit;
4835 }
4836
Gilles Peskine449bd832023-01-11 14:50:10 +01004837 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004838 status = PSA_ERROR_BAD_STATE;
4839 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004840 }
4841
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004843 /* Additional data length was supplied, but not all the additional
4844 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004845 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004846 status = PSA_ERROR_INVALID_ARGUMENT;
4847 goto exit;
4848 }
4849
4850 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004852 status = PSA_ERROR_INVALID_ARGUMENT;
4853 goto exit;
4854 }
4855
4856 operation->body_remaining -= input_length;
4857 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004858#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004859 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004860 status = PSA_ERROR_BAD_STATE;
4861 goto exit;
4862 }
4863#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004864
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4866 output, output_size,
4867 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004868
Paul Elliott39dc6b82021-05-11 19:16:09 +01004869exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004870 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004871 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 } else {
4873 psa_aead_abort(operation);
4874 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004875
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004877}
4878
Gilles Peskine449bd832023-01-11 14:50:10 +01004879static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004880{
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 if (operation->id == 0 || !operation->nonce_set) {
4882 return PSA_ERROR_BAD_STATE;
4883 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004884
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4886 operation->body_remaining != 0)) {
4887 return PSA_ERROR_INVALID_ARGUMENT;
4888 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004889
Gilles Peskine449bd832023-01-11 14:50:10 +01004890 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004891}
4892
Paul Elliott302ff6b2021-04-20 18:10:30 +01004893/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004894psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4895 uint8_t *ciphertext,
4896 size_t ciphertext_size,
4897 size_t *ciphertext_length,
4898 uint8_t *tag,
4899 size_t tag_size,
4900 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004901{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004902 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4903
Paul Elliott302ff6b2021-04-20 18:10:30 +01004904 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004905 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004906
Gilles Peskine449bd832023-01-11 14:50:10 +01004907 status = psa_aead_final_checks(operation);
4908 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004909 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004911
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004913 status = PSA_ERROR_BAD_STATE;
4914 goto exit;
4915 }
4916
Gilles Peskine449bd832023-01-11 14:50:10 +01004917 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4918 ciphertext_size,
4919 ciphertext_length,
4920 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004921
Paul Elliott39dc6b82021-05-11 19:16:09 +01004922exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004923
4924
Paul Elliottf47b0952021-05-21 18:02:33 +01004925 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004926 * the zero tag size, make sure the tag is set to something implausible.
4927 * Even if the operation succeeds, make sure we clear the rest of the
4928 * buffer to prevent potential leakage of anything previously placed in
4929 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004930 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004931
Gilles Peskine449bd832023-01-11 14:50:10 +01004932 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004933
Gilles Peskine449bd832023-01-11 14:50:10 +01004934 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004935}
4936
4937/* Finish authenticating and decrypting a message in a multipart AEAD
4938 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004939psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4940 uint8_t *plaintext,
4941 size_t plaintext_size,
4942 size_t *plaintext_length,
4943 const uint8_t *tag,
4944 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004945{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004946 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4947
Paul Elliott302ff6b2021-04-20 18:10:30 +01004948 *plaintext_length = 0;
4949
Gilles Peskine449bd832023-01-11 14:50:10 +01004950 status = psa_aead_final_checks(operation);
4951 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004952 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004954
Gilles Peskine449bd832023-01-11 14:50:10 +01004955 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004956 status = PSA_ERROR_BAD_STATE;
4957 goto exit;
4958 }
4959
Gilles Peskine449bd832023-01-11 14:50:10 +01004960 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4961 plaintext_size,
4962 plaintext_length,
4963 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004964
4965exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004966 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004967
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004969}
4970
4971/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004972psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004973{
4974 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4975
Gilles Peskine449bd832023-01-11 14:50:10 +01004976 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004977 /* The object has (apparently) been initialized but it is not (yet)
4978 * in use. It's ok to call abort on such an object, and there's
4979 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004980 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004981 }
4982
Gilles Peskine449bd832023-01-11 14:50:10 +01004983 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004984
Gilles Peskine449bd832023-01-11 14:50:10 +01004985 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004986
Gilles Peskine449bd832023-01-11 14:50:10 +01004987 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004988}
4989
Gilles Peskinee59236f2018-01-27 23:32:46 +01004990/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004991/* Generators */
4992/****************************************************************/
4993
Przemek Stekiel69c46792022-06-10 12:59:51 +02004994#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004995 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004996 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
4997 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08004998#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004999#endif /* At least one builtin KDF */
5000
Przemek Stekiel69c46792022-06-10 12:59:51 +02005001#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005002 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5003 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5004static psa_status_t psa_key_derivation_start_hmac(
5005 psa_mac_operation_t *operation,
5006 psa_algorithm_t hash_alg,
5007 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005009{
5010 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5011 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005012 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5013 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5014 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005015
Steven Cooreman72f736a2021-05-07 14:14:37 +02005016 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005017 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005018
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 status = psa_driver_wrapper_mac_sign_setup(operation,
5020 &attributes,
5021 hmac_key, hmac_key_length,
5022 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005023
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 psa_reset_key_attributes(&attributes);
5025 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005026}
5027#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005028
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005029#define HKDF_STATE_INIT 0 /* no input yet */
5030#define HKDF_STATE_STARTED 1 /* got salt */
5031#define HKDF_STATE_KEYED 2 /* got key */
5032#define HKDF_STATE_OUTPUT 3 /* output started */
5033
Gilles Peskinecbe66502019-05-16 16:59:18 +02005034static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005036{
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5038 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5039 } else {
5040 return operation->alg;
5041 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005042}
5043
Gilles Peskine449bd832023-01-11 14:50:10 +01005044psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005045{
5046 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5048 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005049 /* The object has (apparently) been initialized but it is not
5050 * in use. It's ok to call abort on such an object, and there's
5051 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005052 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005053#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005054 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5055 mbedtls_free(operation->ctx.hkdf.info);
5056 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5057 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005058#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005059#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5060 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005061 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5062 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5063 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5064 if (operation->ctx.tls12_prf.secret != NULL) {
5065 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5066 operation->ctx.tls12_prf.secret_length);
5067 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005068 }
5069
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 if (operation->ctx.tls12_prf.seed != NULL) {
5071 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5072 operation->ctx.tls12_prf.seed_length);
5073 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005074 }
5075
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 if (operation->ctx.tls12_prf.label != NULL) {
5077 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5078 operation->ctx.tls12_prf.label_length);
5079 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005080 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005081#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005082 if (operation->ctx.tls12_prf.other_secret != NULL) {
5083 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5084 operation->ctx.tls12_prf.other_secret_length);
5085 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005086 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005087#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005088 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005089
5090 /* We leave the fields Ai and output_block to be erased safely by the
5091 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005092 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005093#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5094 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005095#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5097 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5098 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5099 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005100#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005101 {
5102 status = PSA_ERROR_BAD_STATE;
5103 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005104 mbedtls_platform_zeroize(operation, sizeof(*operation));
5105 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005106}
5107
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005108psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005110{
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005112 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005113 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005114 }
5115
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005116 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005118}
5119
Gilles Peskine449bd832023-01-11 14:50:10 +01005120psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5121 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005122{
Gilles Peskine449bd832023-01-11 14:50:10 +01005123 if (operation->alg == 0) {
5124 return PSA_ERROR_BAD_STATE;
5125 }
5126 if (capacity > operation->capacity) {
5127 return PSA_ERROR_INVALID_ARGUMENT;
5128 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005129 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005130 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005131}
5132
Przemek Stekiel69c46792022-06-10 12:59:51 +02005133#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005134/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005135static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5136 psa_algorithm_t kdf_alg,
5137 uint8_t *output,
5138 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005139{
Gilles Peskine449bd832023-01-11 14:50:10 +01005140 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5141 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005142 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005143 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005144#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005145 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005146#else
5147 const uint8_t last_block = 0xff;
5148#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005149
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 if (hkdf->state < HKDF_STATE_KEYED ||
5151 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005152#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005154#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 )) {
5156 return PSA_ERROR_BAD_STATE;
5157 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005158 hkdf->state = HKDF_STATE_OUTPUT;
5159
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005161 /* Copy what remains of the current block */
5162 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005164 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005165 }
5166 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005167 output += n;
5168 output_length -= n;
5169 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005170 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005171 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005173 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005174 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005175 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005176 * object was corrupted or if this function is called directly
5177 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 if (hkdf->block_number == last_block) {
5179 return PSA_ERROR_BAD_STATE;
5180 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005181
5182 /* We need a new block */
5183 ++hkdf->block_number;
5184 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005185
Gilles Peskine449bd832023-01-11 14:50:10 +01005186 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5187 hash_alg,
5188 hkdf->prk,
5189 hash_length);
5190 if (status != PSA_SUCCESS) {
5191 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005192 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005193
5194 if (hkdf->block_number != 1) {
5195 status = psa_mac_update(&hkdf->hmac,
5196 hkdf->output_block,
5197 hash_length);
5198 if (status != PSA_SUCCESS) {
5199 return status;
5200 }
5201 }
5202 status = psa_mac_update(&hkdf->hmac,
5203 hkdf->info,
5204 hkdf->info_length);
5205 if (status != PSA_SUCCESS) {
5206 return status;
5207 }
5208 status = psa_mac_update(&hkdf->hmac,
5209 &hkdf->block_number, 1);
5210 if (status != PSA_SUCCESS) {
5211 return status;
5212 }
5213 status = psa_mac_sign_finish(&hkdf->hmac,
5214 hkdf->output_block,
5215 sizeof(hkdf->output_block),
5216 &hmac_output_length);
5217 if (status != PSA_SUCCESS) {
5218 return status;
5219 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005220 }
5221
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005223}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005224#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005225
John Durkop07cc04a2020-11-16 22:08:34 -08005226#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5227 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005228static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5229 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005230 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005231{
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5233 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005234 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5235 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005236 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005237
Janos Follath7742fee2019-06-17 12:58:10 +01005238 /* We can't be wanting more output after block 0xff, otherwise
5239 * the capacity check in psa_key_derivation_output_bytes() would have
5240 * prevented this call. It could happen only if the operation
5241 * object was corrupted or if this function is called directly
5242 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 if (tls12_prf->block_number == 0xff) {
5244 return PSA_ERROR_CORRUPTION_DETECTED;
5245 }
Janos Follath7742fee2019-06-17 12:58:10 +01005246
5247 /* We need a new block */
5248 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005249 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005250
5251 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5252 *
5253 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5254 *
5255 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5256 * HMAC_hash(secret, A(2) + seed) +
5257 * HMAC_hash(secret, A(3) + seed) + ...
5258 *
5259 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005260 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005261 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005262 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005263 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005264 * is currently extracted as `output_block` and where i is
5265 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005266 */
5267
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 status = psa_key_derivation_start_hmac(&hmac,
5269 hash_alg,
5270 tls12_prf->secret,
5271 tls12_prf->secret_length);
5272 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005273 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005275
5276 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005278 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5279 * the variable seed and in this instance means it in the context of the
5280 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 status = psa_mac_update(&hmac,
5282 tls12_prf->label,
5283 tls12_prf->label_length);
5284 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005285 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 }
5287 status = psa_mac_update(&hmac,
5288 tls12_prf->seed,
5289 tls12_prf->seed_length);
5290 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005291 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 }
5293 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005294 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005295 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5296 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005297 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005299 }
5300
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 status = psa_mac_sign_finish(&hmac,
5302 tls12_prf->Ai, hash_length,
5303 &hmac_output_length);
5304 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005305 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 }
5307 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005308 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005309 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005310
5311 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005312 status = psa_key_derivation_start_hmac(&hmac,
5313 hash_alg,
5314 tls12_prf->secret,
5315 tls12_prf->secret_length);
5316 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005317 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 }
5319 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5320 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005321 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 }
5323 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5324 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005325 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 }
5327 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5328 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005329 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 }
5331 status = psa_mac_sign_finish(&hmac,
5332 tls12_prf->output_block, hash_length,
5333 &hmac_output_length);
5334 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005335 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005337
Janos Follath7742fee2019-06-17 12:58:10 +01005338
5339cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005340 cleanup_status = psa_mac_abort(&hmac);
5341 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005342 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005344
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005346}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005347
Janos Follath844eb0e2019-06-19 12:10:49 +01005348static psa_status_t psa_key_derivation_tls12_prf_read(
5349 psa_tls12_prf_key_derivation_t *tls12_prf,
5350 psa_algorithm_t alg,
5351 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005353{
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5355 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005356 psa_status_t status;
5357 uint8_t offset, length;
5358
Gilles Peskine449bd832023-01-11 14:50:10 +01005359 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005360 case PSA_TLS12_PRF_STATE_LABEL_SET:
5361 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5362 break;
5363 case PSA_TLS12_PRF_STATE_OUTPUT:
5364 break;
5365 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005366 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005367 }
5368
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005370 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005371 if (tls12_prf->left_in_block == 0) {
5372 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5373 alg);
5374 if (status != PSA_SUCCESS) {
5375 return status;
5376 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005377
5378 continue;
5379 }
5380
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005382 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005384 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005386
5387 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005389 output += length;
5390 output_length -= length;
5391 tls12_prf->left_in_block -= length;
5392 }
5393
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005395}
John Durkop07cc04a2020-11-16 22:08:34 -08005396#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5397 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005398
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005399#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5400static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5401 psa_tls12_ecjpake_to_pms_t *ecjpake,
5402 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005404{
5405 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005406 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005407
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 if (output_length != 32) {
5409 return PSA_ERROR_INVALID_ARGUMENT;
5410 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005411
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5413 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5414 &output_size);
5415 if (status != PSA_SUCCESS) {
5416 return status;
5417 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005418
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 if (output_size != output_length) {
5420 return PSA_ERROR_GENERIC_ERROR;
5421 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005422
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005424}
5425#endif
5426
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005427psa_status_t psa_key_derivation_output_bytes(
5428 psa_key_derivation_operation_t *operation,
5429 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005430 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005431{
5432 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005433 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005434
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005436 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005438 }
5439
Gilles Peskine449bd832023-01-11 14:50:10 +01005440 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005441 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005442 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005443 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005444 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005445 goto exit;
5446 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005448 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005449 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005450 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5451 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005452 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005453 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005455 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005456 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005457
Przemek Stekiel69c46792022-06-10 12:59:51 +02005458#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5460 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5461 output, output_length);
5462 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005463#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005464#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5465 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5467 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5468 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5469 kdf_alg, output,
5470 output_length);
5471 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005472#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5473 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005474#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005476 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5478 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005479#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5480
Gilles Peskineeab56e42018-07-12 17:12:33 +02005481 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005482 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005484 }
5485
5486exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005488 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005489 * This allows us to differentiate between exhausted operations and
5490 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5491 * operations. */
5492 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005494 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005496 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005498}
5499
David Brown7807bf72021-02-09 16:28:23 -07005500#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005501static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005502{
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 if (data_size >= 8) {
5504 mbedtls_des_key_set_parity(data);
5505 }
5506 if (data_size >= 16) {
5507 mbedtls_des_key_set_parity(data + 8);
5508 }
5509 if (data_size >= 24) {
5510 mbedtls_des_key_set_parity(data + 16);
5511 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005512}
David Brown7807bf72021-02-09 16:28:23 -07005513#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005514
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005515/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 * ECC keys on a Weierstrass elliptic curve require the generation
5517 * of a private key which is an integer
5518 * in the range [1, N - 1], where N is the boundary of the private key domain:
5519 * N is the prime p for Diffie-Hellman, or the order of the
5520 * curve’s base point for ECC.
5521 *
5522 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5523 * This function generates the private key using the following process:
5524 *
5525 * 1. Draw a byte string of length ceiling(m/8) bytes.
5526 * 2. If m is not a multiple of 8, set the most significant
5527 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5528 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5529 * 4. If k > N - 2, discard the result and return to step 1.
5530 * 5. Output k + 1 as the private key.
5531 *
5532 * This method allows compliance to NIST standards, specifically the methods titled
5533 * Key-Pair Generation by Testing Candidates in the following publications:
5534 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5535 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5536 * Diffie-Hellman keys.
5537 *
5538 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5539 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5540 *
5541 * Note: Function allocates memory for *data buffer, so given *data should be
5542 * always NULL.
5543 */
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005544#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
5545 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
5546 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5547 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5548 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005549static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005550 psa_key_slot_t *slot,
5551 size_t bits,
5552 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005553 uint8_t **data
5554 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005555{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005556 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005557 mbedtls_mpi k;
5558 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005559 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005560 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005561
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 mbedtls_mpi_init(&k);
5563 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005564
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005565 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005567 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005569
Gilles Peskine449bd832023-01-11 14:50:10 +01005570 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005571 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005572 goto cleanup;
5573 }
5574
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005575 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005577
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005579
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005580 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005581 /* Let m be the bit size of N. */
5582 size_t m = ecp_group.nbits;
5583
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005585
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005586 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005588
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005589 /* Note: This function is always called with *data == NULL and it
5590 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005591 *data = mbedtls_calloc(1, m_bytes);
5592 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005593 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005594 goto cleanup;
5595 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005596
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005598 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005599 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005600 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005602
5603 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005605 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005606 * (8 * ceiling(m/8) - m) bits of the first byte in
5607 * the string to zero.
5608 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005609 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005610 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005611 }
5612
5613 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005614 * big-endian byte string.
5615 */
5616 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005617
5618 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 * Result of comparison is returned. When it indicates error
5620 * then this function is called again.
5621 */
5622 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005623 }
5624
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005625 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005626 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5627 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005628cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 if (ret != 0) {
5630 status = mbedtls_to_psa_error(ret);
5631 }
5632 if (status != PSA_SUCCESS) {
5633 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005634 *data = NULL;
5635 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005636 mbedtls_mpi_free(&k);
5637 mbedtls_mpi_free(&diff_N_2);
5638 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005639}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005640
5641/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5642 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5643 *
5644 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5645 * draw a 32-byte string and process it as specified in
5646 * Elliptic Curves for Security [RFC7748] §5.
5647 *
5648 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5649 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5650 *
5651 * Note: Function allocates memory for *data buffer, so given *data should be
5652 * always NULL.
5653 */
5654
5655static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5656 size_t bits,
5657 psa_key_derivation_operation_t *operation,
5658 uint8_t **data
5659 )
5660{
5661 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005662 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005663
Gilles Peskine449bd832023-01-11 14:50:10 +01005664 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005665 case 255:
5666 output_length = 32;
5667 break;
5668 case 448:
5669 output_length = 56;
5670 break;
5671 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005673 break;
5674 }
5675
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005677
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 if (*data == NULL) {
5679 return PSA_ERROR_INSUFFICIENT_MEMORY;
5680 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005681
Gilles Peskine449bd832023-01-11 14:50:10 +01005682 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005683
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005685 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005687
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005689 case 255:
5690 (*data)[0] &= 248;
5691 (*data)[31] &= 127;
5692 (*data)[31] |= 64;
5693 break;
5694 case 448:
5695 (*data)[0] &= 252;
5696 (*data)[55] |= 128;
5697 break;
5698 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005700 break;
5701 }
5702
5703 return status;
5704}
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005705#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
5706 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
5707 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5708 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5709 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005710
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005711static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005712 psa_key_slot_t *slot,
5713 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005714 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005715{
5716 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305718 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005719 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005720
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5722 return PSA_ERROR_INVALID_ARGUMENT;
5723 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005724
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005725#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
5726 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
5727 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5728 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5729 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005730 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5731 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5732 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005733 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5735 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005736 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 }
5738 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005739 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005740 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5741 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005742 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005744 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005745 } else
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005746#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
5747 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
5748 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5749 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5750 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005751 if (key_type_is_raw_bytes(slot->attr.type)) {
5752 if (bits % 8 != 0) {
5753 return PSA_ERROR_INVALID_ARGUMENT;
5754 }
5755 data = mbedtls_calloc(1, bytes);
5756 if (data == NULL) {
5757 return PSA_ERROR_INSUFFICIENT_MEMORY;
5758 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005759
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 status = psa_key_derivation_output_bytes(operation, data, bytes);
5761 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005762 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005763 }
David Brown12ca5032021-02-11 11:02:00 -07005764#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5766 psa_des_set_key_parity(data, bytes);
5767 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005768#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 } else {
5770 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005771 }
Ronald Crondd04d422020-11-28 15:14:42 +01005772
Ronald Cronbf33c932020-11-28 18:06:53 +01005773 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005774 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005776 };
5777
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5779 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5780 &storage_size);
5781 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305782 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 }
Archanad8a83dc2021-06-14 10:04:16 +05305784 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 status = psa_allocate_buffer_to_slot(slot, storage_size);
5786 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305787 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 }
Archanad8a83dc2021-06-14 10:04:16 +05305789
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 status = psa_driver_wrapper_import_key(&attributes,
5791 data, bytes,
5792 slot->key.data,
5793 slot->key.bytes,
5794 &slot->key.bytes, &bits);
5795 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005796 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005797 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005798
5799exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 mbedtls_free(data);
5801 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005802}
5803
Gilles Peskine449bd832023-01-11 14:50:10 +01005804psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5805 psa_key_derivation_operation_t *operation,
5806 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005807{
5808 psa_status_t status;
5809 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005810 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005811
Ronald Cron81709fc2020-11-14 12:10:32 +01005812 *key = MBEDTLS_SVC_KEY_ID_INIT;
5813
Gilles Peskine0f84d622019-09-12 19:03:13 +02005814 /* Reject any attempt to create a zero-length key so that we don't
5815 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 if (psa_get_key_bits(attributes) == 0) {
5817 return PSA_ERROR_INVALID_ARGUMENT;
5818 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005819
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 if (operation->alg == PSA_ALG_NONE) {
5821 return PSA_ERROR_BAD_STATE;
5822 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005823
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 if (!operation->can_output_key) {
5825 return PSA_ERROR_NOT_PERMITTED;
5826 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005827
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5829 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005830#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005832 /* Deriving a key in a secure element is not implemented yet. */
5833 status = PSA_ERROR_NOT_SUPPORTED;
5834 }
5835#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005836 if (status == PSA_SUCCESS) {
5837 status = psa_generate_derived_key_internal(slot,
5838 attributes->core.bits,
5839 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005840 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 if (status == PSA_SUCCESS) {
5842 status = psa_finish_key_creation(slot, driver, key);
5843 }
5844 if (status != PSA_SUCCESS) {
5845 psa_fail_key_creation(slot, driver);
5846 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005847
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005849}
5850
Gilles Peskineeab56e42018-07-12 17:12:33 +02005851
5852
5853/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005854/* Key derivation */
5855/****************************************************************/
5856
Steven Cooreman932ffb72021-02-15 12:14:32 +01005857#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005858static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005859{
5860#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005861 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5862 return 1;
5863 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005864#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005865#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5867 return 1;
5868 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005869#endif
5870#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5872 return 1;
5873 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005874#endif
5875#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5877 return 1;
5878 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005879#endif
5880#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5882 return 1;
5883 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005884#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005885#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5887 return 1;
5888 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005889#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005890 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005891}
5892
Gilles Peskine449bd832023-01-11 14:50:10 +01005893static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005894{
5895 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 psa_status_t status = psa_hash_setup(&operation, alg);
5897 psa_hash_abort(&operation);
5898 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005899}
5900
Gilles Peskine969c5d62019-01-16 15:53:06 +01005901static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005902 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005903 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005904{
Janos Follath5fe19732019-06-20 15:09:30 +01005905 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5906 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005907 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005908
Gilles Peskine969c5d62019-01-16 15:53:06 +01005909 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005910 if (!is_kdf_alg_supported(kdf_alg)) {
5911 return PSA_ERROR_NOT_SUPPORTED;
5912 }
John Durkop07cc04a2020-11-16 22:08:34 -08005913
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005914 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005915 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5917 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5918 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5919 if (hash_size == 0) {
5920 return PSA_ERROR_NOT_SUPPORTED;
5921 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005922
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005923 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5924 * we might fail later, which is somewhat unfriendly and potentially
5925 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 psa_status_t status = psa_hash_try_support(hash_alg);
5927 if (status != PSA_SUCCESS) {
5928 return status;
5929 }
5930 } else {
5931 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005932 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005933
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5935 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
5936 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
5937 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005938 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005939#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005940 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
5942 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005943 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005944 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005945#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5946 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 operation->capacity = 255 * hash_size;
5948 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005949}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005950
Gilles Peskine449bd832023-01-11 14:50:10 +01005951static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005952{
5953#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 if (alg == PSA_ALG_ECDH) {
5955 return PSA_SUCCESS;
5956 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005957#endif
5958 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005959 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005960}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005961
5962static int psa_key_derivation_allows_free_form_secret_input(
5963 psa_algorithm_t kdf_alg)
5964{
5965#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
5966 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5967 return 0;
5968 }
5969#endif
5970 (void) kdf_alg;
5971 return 1;
5972}
John Durkop07cc04a2020-11-16 22:08:34 -08005973#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005974
Gilles Peskine449bd832023-01-11 14:50:10 +01005975psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
5976 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005977{
5978 psa_status_t status;
5979
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 if (operation->alg != 0) {
5981 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005982 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005983
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
5985 return PSA_ERROR_INVALID_ARGUMENT;
5986 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
5987#if defined(AT_LEAST_ONE_BUILTIN_KDF)
5988 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
5989 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
5990 status = psa_key_agreement_try_support(ka_alg);
5991 if (status != PSA_SUCCESS) {
5992 return status;
5993 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005994 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
5995 return PSA_ERROR_INVALID_ARGUMENT;
5996 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
5998#else
5999 return PSA_ERROR_NOT_SUPPORTED;
6000#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6001 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6002#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6003 status = psa_key_derivation_setup_kdf(operation, alg);
6004#else
6005 return PSA_ERROR_NOT_SUPPORTED;
6006#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6007 } else {
6008 return PSA_ERROR_INVALID_ARGUMENT;
6009 }
6010
6011 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006012 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006013 }
6014 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006015}
6016
Przemek Stekiel69c46792022-06-10 12:59:51 +02006017#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006018static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6019 psa_algorithm_t kdf_alg,
6020 psa_key_derivation_step_t step,
6021 const uint8_t *data,
6022 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006023{
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006025 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006026 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006027 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006028#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006029 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6030 return PSA_ERROR_INVALID_ARGUMENT;
6031 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006032#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 if (hkdf->state != HKDF_STATE_INIT) {
6034 return PSA_ERROR_BAD_STATE;
6035 } else {
6036 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6037 hash_alg,
6038 data, data_length);
6039 if (status != PSA_SUCCESS) {
6040 return status;
6041 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006042 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006044 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006045 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006046#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006047 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006048 /* We shouldn't be in different state as HKDF_EXPAND only allows
6049 * two inputs: SECRET (this case) and INFO which does not modify
6050 * the state. It could happen only if the hkdf
6051 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 if (hkdf->state != HKDF_STATE_INIT) {
6053 return PSA_ERROR_BAD_STATE;
6054 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006055
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006056 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6058 return PSA_ERROR_INVALID_ARGUMENT;
6059 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006060
Gilles Peskine449bd832023-01-11 14:50:10 +01006061 memcpy(hkdf->prk, data, data_length);
6062 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006063#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006064 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006065 /* HKDF: If no salt was provided, use an empty salt.
6066 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006068#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6070 return PSA_ERROR_BAD_STATE;
6071 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006072#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6074 hash_alg,
6075 NULL, 0);
6076 if (status != PSA_SUCCESS) {
6077 return status;
6078 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006079 hkdf->state = HKDF_STATE_STARTED;
6080 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006081 if (hkdf->state != HKDF_STATE_STARTED) {
6082 return PSA_ERROR_BAD_STATE;
6083 }
6084 status = psa_mac_update(&hkdf->hmac,
6085 data, data_length);
6086 if (status != PSA_SUCCESS) {
6087 return status;
6088 }
6089 status = psa_mac_sign_finish(&hkdf->hmac,
6090 hkdf->prk,
6091 sizeof(hkdf->prk),
6092 &data_length);
6093 if (status != PSA_SUCCESS) {
6094 return status;
6095 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006096 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006097
Gilles Peskine2b522db2019-04-12 15:11:49 +02006098 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006099 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006100#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006102 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006104 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006106#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006107 {
6108 /* Block 0 is empty, and the next block will be
6109 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006111 }
6112
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006114 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006115#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006116 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6117 return PSA_ERROR_INVALID_ARGUMENT;
6118 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006119#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006120#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6122 hkdf->state == HKDF_STATE_INIT) {
6123 return PSA_ERROR_BAD_STATE;
6124 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006125#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 if (hkdf->state == HKDF_STATE_OUTPUT) {
6127 return PSA_ERROR_BAD_STATE;
6128 }
6129 if (hkdf->info_set) {
6130 return PSA_ERROR_BAD_STATE;
6131 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006132 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 if (data_length != 0) {
6134 hkdf->info = mbedtls_calloc(1, data_length);
6135 if (hkdf->info == NULL) {
6136 return PSA_ERROR_INSUFFICIENT_MEMORY;
6137 }
6138 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006139 }
6140 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006142 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006144 }
6145}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006146#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006147
John Durkop07cc04a2020-11-16 22:08:34 -08006148#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6149 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006150static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6151 const uint8_t *data,
6152 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006153{
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6155 return PSA_ERROR_BAD_STATE;
6156 }
Janos Follathf08e2652019-06-13 09:05:41 +01006157
Gilles Peskine449bd832023-01-11 14:50:10 +01006158 if (data_length != 0) {
6159 prf->seed = mbedtls_calloc(1, data_length);
6160 if (prf->seed == NULL) {
6161 return PSA_ERROR_INSUFFICIENT_MEMORY;
6162 }
Janos Follathf08e2652019-06-13 09:05:41 +01006163
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006165 prf->seed_length = data_length;
6166 }
Janos Follathf08e2652019-06-13 09:05:41 +01006167
Gilles Peskine43f958b2020-12-13 14:55:14 +01006168 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006169
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006171}
6172
Gilles Peskine449bd832023-01-11 14:50:10 +01006173static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6174 const uint8_t *data,
6175 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006176{
Gilles Peskine449bd832023-01-11 14:50:10 +01006177 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6178 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6179 return PSA_ERROR_BAD_STATE;
6180 }
Janos Follath81550542019-06-13 14:26:34 +01006181
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 if (data_length != 0) {
6183 prf->secret = mbedtls_calloc(1, data_length);
6184 if (prf->secret == NULL) {
6185 return PSA_ERROR_INSUFFICIENT_MEMORY;
6186 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006187
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006189 prf->secret_length = data_length;
6190 }
Janos Follath81550542019-06-13 14:26:34 +01006191
Gilles Peskine43f958b2020-12-13 14:55:14 +01006192 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006193
Gilles Peskine449bd832023-01-11 14:50:10 +01006194 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006195}
6196
Gilles Peskine449bd832023-01-11 14:50:10 +01006197static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6198 const uint8_t *data,
6199 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006200{
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6202 return PSA_ERROR_BAD_STATE;
6203 }
Janos Follath63028dd2019-06-13 09:15:47 +01006204
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 if (data_length != 0) {
6206 prf->label = mbedtls_calloc(1, data_length);
6207 if (prf->label == NULL) {
6208 return PSA_ERROR_INSUFFICIENT_MEMORY;
6209 }
Janos Follath63028dd2019-06-13 09:15:47 +01006210
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006212 prf->label_length = data_length;
6213 }
Janos Follath63028dd2019-06-13 09:15:47 +01006214
Gilles Peskine43f958b2020-12-13 14:55:14 +01006215 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006216
Gilles Peskine449bd832023-01-11 14:50:10 +01006217 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006218}
6219
Gilles Peskine449bd832023-01-11 14:50:10 +01006220static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6221 psa_key_derivation_step_t step,
6222 const uint8_t *data,
6223 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006224{
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006226 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006228 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006230 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006232 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006233 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006234 }
6235}
John Durkop07cc04a2020-11-16 22:08:34 -08006236#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6237 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6238
6239#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6240static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6241 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006242 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006243 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006244{
6245 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6247 4 + data_length + prf->other_secret_length :
6248 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006249
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6251 return PSA_ERROR_INVALID_ARGUMENT;
6252 }
John Durkop07cc04a2020-11-16 22:08:34 -08006253
Gilles Peskine449bd832023-01-11 14:50:10 +01006254 uint8_t *pms = mbedtls_calloc(1, pms_len);
6255 if (pms == NULL) {
6256 return PSA_ERROR_INSUFFICIENT_MEMORY;
6257 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006258 uint8_t *cur = pms;
6259
6260 /* pure-PSK:
6261 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006262 *
6263 * The premaster secret is formed as follows: if the PSK is N octets
6264 * long, concatenate a uint16 with the value N, N zero octets, a second
6265 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006266 *
6267 * mixed-PSK:
6268 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6269 * follows: concatenate a uint16 with the length of the other secret,
6270 * the other secret itself, uint16 with the length of PSK, and the
6271 * PSK itself.
6272 * For details please check:
6273 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6274 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6275 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006276 */
6277
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6279 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6280 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6281 if (prf->other_secret_length != 0) {
6282 memcpy(cur, prf->other_secret, prf->other_secret_length);
6283 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006284 cur += prf->other_secret_length;
6285 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 } else {
6287 *cur++ = MBEDTLS_BYTE_1(data_length);
6288 *cur++ = MBEDTLS_BYTE_0(data_length);
6289 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006290 cur += data_length;
6291 }
6292
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 *cur++ = MBEDTLS_BYTE_1(data_length);
6294 *cur++ = MBEDTLS_BYTE_0(data_length);
6295 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006296 cur += data_length;
6297
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006299
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 mbedtls_platform_zeroize(pms, pms_len);
6301 mbedtls_free(pms);
6302 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006303}
Janos Follath6660f0e2019-06-17 08:44:03 +01006304
Przemek Stekiele47201b2022-04-19 13:53:28 +02006305static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6306 psa_tls12_prf_key_derivation_t *prf,
6307 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006308 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006309{
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6311 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006312 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006313
6314 if (data_length != 0) {
6315 prf->other_secret = mbedtls_calloc(1, data_length);
6316 if (prf->other_secret == NULL) {
6317 return PSA_ERROR_INSUFFICIENT_MEMORY;
6318 }
6319
6320 memcpy(prf->other_secret, data, data_length);
6321 prf->other_secret_length = data_length;
6322 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006323 prf->other_secret_length = 0;
6324 }
6325
6326 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6327
Gilles Peskine449bd832023-01-11 14:50:10 +01006328 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006329}
6330
Janos Follath6660f0e2019-06-17 08:44:03 +01006331static psa_status_t psa_tls12_prf_psk_to_ms_input(
6332 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006333 psa_key_derivation_step_t step,
6334 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006336{
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006338 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006339 return psa_tls12_prf_psk_to_ms_set_key(prf,
6340 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006341 break;
6342 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6344 data,
6345 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006346 break;
6347 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006348 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006349 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006350
Przemek Stekiele47201b2022-04-19 13:53:28 +02006351 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006352}
John Durkop07cc04a2020-11-16 22:08:34 -08006353#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006354
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006355#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6356static psa_status_t psa_tls12_ecjpake_to_pms_input(
6357 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006358 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006359 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006361{
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6363 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6364 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006365 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006366
6367 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 if (data[0] != 0x04) {
6369 return PSA_ERROR_INVALID_ARGUMENT;
6370 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006371
6372 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006374
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006376}
6377#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006378/** Check whether the given key type is acceptable for the given
6379 * input step of a key derivation.
6380 *
6381 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6382 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6383 * Both secret and non-secret inputs can alternatively have the type
6384 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6385 * that the input was passed as a buffer rather than via a key object.
6386 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006387static int psa_key_derivation_check_input_type(
6388 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006390{
Gilles Peskine449bd832023-01-11 14:50:10 +01006391 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006392 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 if (key_type == PSA_KEY_TYPE_DERIVE) {
6394 return PSA_SUCCESS;
6395 }
6396 if (key_type == PSA_KEY_TYPE_NONE) {
6397 return PSA_SUCCESS;
6398 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006399 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006400 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 if (key_type == PSA_KEY_TYPE_DERIVE) {
6402 return PSA_SUCCESS;
6403 }
6404 if (key_type == PSA_KEY_TYPE_NONE) {
6405 return PSA_SUCCESS;
6406 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006407 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006408 case PSA_KEY_DERIVATION_INPUT_LABEL:
6409 case PSA_KEY_DERIVATION_INPUT_SALT:
6410 case PSA_KEY_DERIVATION_INPUT_INFO:
6411 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6413 return PSA_SUCCESS;
6414 }
6415 if (key_type == PSA_KEY_TYPE_NONE) {
6416 return PSA_SUCCESS;
6417 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006418 break;
6419 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006421}
6422
Janos Follathb80a94e2019-06-12 15:54:46 +01006423static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006424 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006425 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006426 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006427 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006428 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006429{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006430 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006432
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 status = psa_key_derivation_check_input_type(step, key_type);
6434 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006435 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006437
Przemek Stekiel69c46792022-06-10 12:59:51 +02006438#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006439 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6440 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6441 step, data, data_length);
6442 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006443#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006444#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006445 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6446 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6447 step, data, data_length);
6448 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006449#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6450#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6452 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6453 step, data, data_length);
6454 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006455#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006456#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006458 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6460 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006461#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006462 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006463 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006464 (void) data;
6465 (void) data_length;
6466 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006467 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006468 }
6469
Gilles Peskine224b0d62019-09-23 18:13:17 +02006470exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 if (status != PSA_SUCCESS) {
6472 psa_key_derivation_abort(operation);
6473 }
6474 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006475}
6476
Janos Follath51f4a0f2019-06-14 11:35:55 +01006477psa_status_t psa_key_derivation_input_bytes(
6478 psa_key_derivation_operation_t *operation,
6479 psa_key_derivation_step_t step,
6480 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006481 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006482{
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 return psa_key_derivation_input_internal(operation, step,
6484 PSA_KEY_TYPE_NONE,
6485 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006486}
6487
Janos Follath51f4a0f2019-06-14 11:35:55 +01006488psa_status_t psa_key_derivation_input_key(
6489 psa_key_derivation_operation_t *operation,
6490 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006492{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006493 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006494 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006495 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006496
Ronald Cron5c522922020-11-14 16:35:34 +01006497 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6499 if (status != PSA_SUCCESS) {
6500 psa_key_derivation_abort(operation);
6501 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006502 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006503
6504 /* Passing a key object as a SECRET input unlocks the permission
6505 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006506 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006507 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006509
Gilles Peskine449bd832023-01-11 14:50:10 +01006510 status = psa_key_derivation_input_internal(operation,
6511 step, slot->attr.type,
6512 slot->key.data,
6513 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006514
Gilles Peskine449bd832023-01-11 14:50:10 +01006515 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006516
Gilles Peskine449bd832023-01-11 14:50:10 +01006517 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006518}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006519
6520
6521
6522/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006523/* Key agreement */
6524/****************************************************************/
6525
Gilles Peskine449bd832023-01-11 14:50:10 +01006526psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6527 const uint8_t *key_buffer,
6528 size_t key_buffer_size,
6529 psa_algorithm_t alg,
6530 const uint8_t *peer_key,
6531 size_t peer_key_length,
6532 uint8_t *shared_secret,
6533 size_t shared_secret_size,
6534 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006535{
Gilles Peskine449bd832023-01-11 14:50:10 +01006536 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006537#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006538 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006539 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6540 key_buffer_size, alg,
6541 peer_key, peer_key_length,
6542 shared_secret,
6543 shared_secret_size,
6544 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006545#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006546 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006547 (void) attributes;
6548 (void) key_buffer;
6549 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006550 (void) peer_key;
6551 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006552 (void) shared_secret;
6553 (void) shared_secret_size;
6554 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006555 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006556 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006557}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006558
Aditya Deshpande17845b82022-10-13 17:21:01 +01006559/** Internal function for raw key agreement
6560 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006561 * to the driver's implementation if a driver is present.
6562 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006563 * (psa_key_agreement_raw_builtin).
6564 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006565static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6566 psa_key_slot_t *private_key,
6567 const uint8_t *peer_key,
6568 size_t peer_key_length,
6569 uint8_t *shared_secret,
6570 size_t shared_secret_size,
6571 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006572{
Gilles Peskine449bd832023-01-11 14:50:10 +01006573 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6574 return PSA_ERROR_NOT_SUPPORTED;
6575 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006576
6577 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006578 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006579 };
6580
Gilles Peskine449bd832023-01-11 14:50:10 +01006581 return psa_driver_wrapper_key_agreement(&attributes,
6582 private_key->key.data,
6583 private_key->key.bytes, alg,
6584 peer_key, peer_key_length,
6585 shared_secret,
6586 shared_secret_size,
6587 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006588}
6589
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006590/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006591 * to potentially free embedded data structures and wipe confidential data.
6592 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006593static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6594 psa_key_derivation_step_t step,
6595 psa_key_slot_t *private_key,
6596 const uint8_t *peer_key,
6597 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006598{
6599 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006600 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006601 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006602 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006603
6604 /* Step 1: run the secret agreement algorithm to generate the shared
6605 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006606 status = psa_key_agreement_raw_internal(ka_alg,
6607 private_key,
6608 peer_key, peer_key_length,
6609 shared_secret,
6610 sizeof(shared_secret),
6611 &shared_secret_length);
6612 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006613 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006614 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006615
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006616 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006617 * the shared secret. A shared secret is permitted wherever a key
6618 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006619 status = psa_key_derivation_input_internal(operation, step,
6620 PSA_KEY_TYPE_DERIVE,
6621 shared_secret,
6622 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006623exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006624 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6625 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006626}
6627
Gilles Peskine449bd832023-01-11 14:50:10 +01006628psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6629 psa_key_derivation_step_t step,
6630 mbedtls_svc_key_id_t private_key,
6631 const uint8_t *peer_key,
6632 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006633{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006634 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006635 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006636 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006637
Gilles Peskine449bd832023-01-11 14:50:10 +01006638 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6639 return PSA_ERROR_INVALID_ARGUMENT;
6640 }
Ronald Cron5c522922020-11-14 16:35:34 +01006641 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006642 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6643 if (status != PSA_SUCCESS) {
6644 return status;
6645 }
6646 status = psa_key_agreement_internal(operation, step,
6647 slot,
6648 peer_key, peer_key_length);
6649 if (status != PSA_SUCCESS) {
6650 psa_key_derivation_abort(operation);
6651 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006652 /* If a private key has been added as SECRET, we allow the derived
6653 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006654 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006655 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006656 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006657 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006658
Gilles Peskine449bd832023-01-11 14:50:10 +01006659 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006660
Gilles Peskine449bd832023-01-11 14:50:10 +01006661 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006662}
6663
Gilles Peskine449bd832023-01-11 14:50:10 +01006664psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6665 mbedtls_svc_key_id_t private_key,
6666 const uint8_t *peer_key,
6667 size_t peer_key_length,
6668 uint8_t *output,
6669 size_t output_size,
6670 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006671{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006672 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006673 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006674 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006675
Gilles Peskine449bd832023-01-11 14:50:10 +01006676 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006677 status = PSA_ERROR_INVALID_ARGUMENT;
6678 goto exit;
6679 }
Ronald Cron5c522922020-11-14 16:35:34 +01006680 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006681 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6682 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006683 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006684 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006685
Gilles Peskine3e561302022-04-14 00:17:15 +02006686 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6687 * for the output size. The PSA specification only guarantees that this
6688 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6689 * but it might be nice to allow smaller buffers if the output fits.
6690 * At the time of writing this comment, with only ECDH implemented,
6691 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6692 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6693 * be exact for it as well. */
6694 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006695 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6696 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006697 status = PSA_ERROR_BUFFER_TOO_SMALL;
6698 goto exit;
6699 }
6700
Gilles Peskine449bd832023-01-11 14:50:10 +01006701 status = psa_key_agreement_raw_internal(alg, slot,
6702 peer_key, peer_key_length,
6703 output, output_size,
6704 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006705
6706exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006707 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006708 /* If an error happens and is not handled properly, the output
6709 * may be used as a key to protect sensitive data. Arrange for such
6710 * a key to be random, which is likely to result in decryption or
6711 * verification errors. This is better than filling the buffer with
6712 * some constant data such as zeros, which would result in the data
6713 * being protected with a reproducible, easily knowable key.
6714 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006715 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006716 *output_length = output_size;
6717 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006718
Gilles Peskine449bd832023-01-11 14:50:10 +01006719 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006720
Gilles Peskine449bd832023-01-11 14:50:10 +01006721 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006722}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006723
6724
Gilles Peskine30524eb2020-11-13 17:02:26 +01006725
Gilles Peskine86a440b2018-11-12 18:39:40 +01006726/****************************************************************/
6727/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006728/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006729
Gilles Peskine30524eb2020-11-13 17:02:26 +01006730/** Initialize the PSA random generator.
6731 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006732static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006733{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006734#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006735 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006736#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6737
Gilles Peskine30524eb2020-11-13 17:02:26 +01006738 /* Set default configuration if
6739 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006740 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006741 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006742 }
6743 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006744 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006745 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006746
Gilles Peskine449bd832023-01-11 14:50:10 +01006747 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006748#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6749 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6750 /* The PSA entropy injection feature depends on using NV seed as an entropy
6751 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006752 mbedtls_entropy_add_source(&rng->entropy,
6753 mbedtls_nv_seed_poll, NULL,
6754 MBEDTLS_ENTROPY_BLOCK_SIZE,
6755 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006756#endif
6757
Gilles Peskine449bd832023-01-11 14:50:10 +01006758 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006759#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006760}
6761
6762/** Deinitialize the PSA random generator.
6763 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006764static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006765{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006766#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006767 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006768#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01006769 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
6770 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006771#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006772}
6773
6774/** Seed the PSA random generator.
6775 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006776static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006777{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006778#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6779 /* Do nothing: the external RNG seeds itself. */
6780 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006781 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006782#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006783 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01006784 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
6785 drbg_seed, sizeof(drbg_seed) - 1);
6786 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006787#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006788}
6789
Gilles Peskine449bd832023-01-11 14:50:10 +01006790psa_status_t psa_generate_random(uint8_t *output,
6791 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006792{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006793 GUARD_MODULE_INITIALIZED;
6794
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006795#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6796
6797 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006798 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6799 output, output_size,
6800 &output_length);
6801 if (status != PSA_SUCCESS) {
6802 return status;
6803 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006804 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006805 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006806 if (output_length != output_size) {
6807 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6808 }
6809 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006810
6811#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6812
Gilles Peskine449bd832023-01-11 14:50:10 +01006813 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006814 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006815 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6816 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6817 output_size);
6818 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6819 output, request_size);
6820 if (ret != 0) {
6821 return mbedtls_to_psa_error(ret);
6822 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006823 output_size -= request_size;
6824 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006825 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006826 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006827#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006828}
6829
Gilles Peskine8814fc42020-12-14 15:33:44 +01006830/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006831 *
6832 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6833 * `psa_generate_random(...)`. The state parameter is ignored since the
6834 * PSA API doesn't support passing an explicit state.
6835 *
6836 * In the non-external case, psa_generate_random() calls an
6837 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6838 * and semantics as mbedtls_psa_get_random(). As an optimization,
6839 * instead of doing this back-and-forth between the PSA API and the
6840 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6841 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006842 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006843#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6844int mbedtls_psa_get_random(void *p_rng,
6845 unsigned char *output,
6846 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01006847{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006848 /* This function takes a pointer to the RNG state because that's what
6849 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6850 * its own state internally and doesn't let the caller access that state.
6851 * So we just ignore the state parameter, and in practice we'll pass
6852 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006853 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006854 psa_status_t status = psa_generate_random(output, output_size);
6855 if (status == PSA_SUCCESS) {
6856 return 0;
6857 } else {
6858 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
6859 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01006860}
6861#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6862
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006863#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006864#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006865
Gilles Peskine449bd832023-01-11 14:50:10 +01006866psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
6867 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006868{
Gilles Peskine449bd832023-01-11 14:50:10 +01006869 if (global_data.initialized) {
6870 return PSA_ERROR_NOT_PERMITTED;
6871 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006872
Gilles Peskine449bd832023-01-11 14:50:10 +01006873 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
6874 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
6875 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
6876 return PSA_ERROR_INVALID_ARGUMENT;
6877 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006878
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006880}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006881#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006882
Ronald Cron3772afe2021-02-08 16:10:05 +01006883/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006884 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006885 * \param type The key type
6886 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006887 *
6888 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006889 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006890 * \retval #PSA_ERROR_INVALID_ARGUMENT
6891 * The size in bits of the key is not valid.
6892 * \retval #PSA_ERROR_NOT_SUPPORTED
6893 * The type and/or the size in bits of the key or the combination of
6894 * the two is not supported.
6895 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006896static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006897 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006898{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006899 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006900
Gilles Peskine449bd832023-01-11 14:50:10 +01006901 if (key_type_is_raw_bytes(type)) {
6902 status = psa_validate_unstructured_key_bit_size(type, bits);
6903 if (status != PSA_SUCCESS) {
6904 return status;
6905 }
6906 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006907#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006908 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6909 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
6910 return PSA_ERROR_NOT_SUPPORTED;
6911 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006912
Ronald Cron01b2aba2020-10-05 09:42:02 +02006913 /* Accept only byte-aligned keys, for the same reasons as
6914 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006915 if (bits % 8 != 0) {
6916 return PSA_ERROR_NOT_SUPPORTED;
6917 }
6918 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006919#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006920
Ronald Cron5c4d3862020-12-07 11:07:24 +01006921#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006922 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01006923 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006924 return PSA_SUCCESS;
6925 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006926#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006927 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006928 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006929 }
6930
Gilles Peskine449bd832023-01-11 14:50:10 +01006931 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006932}
6933
Ronald Cron55ed0592020-10-05 10:30:40 +02006934psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006935 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01006936 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006937{
6938 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006939 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006940
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 if ((attributes->domain_parameters == NULL) &&
6942 (attributes->domain_parameters_size != 0)) {
6943 return PSA_ERROR_INVALID_ARGUMENT;
6944 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02006945
Gilles Peskine449bd832023-01-11 14:50:10 +01006946 if (key_type_is_raw_bytes(type)) {
6947 status = psa_generate_random(key_buffer, key_buffer_size);
6948 if (status != PSA_SUCCESS) {
6949 return status;
6950 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006951
David Brown12ca5032021-02-11 11:02:00 -07006952#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006953 if (type == PSA_KEY_TYPE_DES) {
6954 psa_des_set_key_parity(key_buffer, key_buffer_size);
6955 }
David Brown8107e312021-02-12 08:08:46 -07006956#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01006957 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01006958
Jaeden Amero424fa932021-05-14 08:34:32 +01006959#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
6960 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01006961 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
6962 return mbedtls_psa_rsa_generate_key(attributes,
6963 key_buffer,
6964 key_buffer_size,
6965 key_buffer_length);
6966 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01006967#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
6968 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006969
John Durkop9814fa22020-11-04 12:28:15 -08006970#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006971 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6972 return mbedtls_psa_ecp_generate_key(attributes,
6973 key_buffer,
6974 key_buffer_size,
6975 key_buffer_length);
6976 } else
John Durkop9814fa22020-11-04 12:28:15 -08006977#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006978 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006979 (void) key_buffer_length;
6980 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02006981 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006982
Gilles Peskine449bd832023-01-11 14:50:10 +01006983 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006984}
Darryl Green0c6575a2018-11-07 16:05:30 +00006985
Gilles Peskine449bd832023-01-11 14:50:10 +01006986psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
6987 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006988{
6989 psa_status_t status;
6990 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006991 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006992 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006993
Ronald Cron81709fc2020-11-14 12:10:32 +01006994 *key = MBEDTLS_SVC_KEY_ID_INIT;
6995
Gilles Peskine0f84d622019-09-12 19:03:13 +02006996 /* Reject any attempt to create a zero-length key so that we don't
6997 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006998 if (psa_get_key_bits(attributes) == 0) {
6999 return PSA_ERROR_INVALID_ARGUMENT;
7000 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007001
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007002 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7004 return PSA_ERROR_INVALID_ARGUMENT;
7005 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007006
Gilles Peskine449bd832023-01-11 14:50:10 +01007007 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7008 &slot, &driver);
7009 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007010 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007011 }
Gilles Peskine11792082019-08-06 18:36:36 +02007012
Ronald Cron977c2472020-10-13 08:32:21 +02007013 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307014 * storage ( thus not in the case of generating a key in a secure element
7015 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7016 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007017 if (slot->key.data == NULL) {
7018 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7019 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007020 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007021 attributes->core.type, attributes->core.bits);
7022 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007023 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007024 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007025
7026 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007027 attributes->core.type,
7028 attributes->core.bits);
7029 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007030 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007031 attributes, &key_buffer_size);
7032 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007033 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 }
Ronald Cron977c2472020-10-13 08:32:21 +02007035 }
Ronald Cron977c2472020-10-13 08:32:21 +02007036
Gilles Peskine449bd832023-01-11 14:50:10 +01007037 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7038 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007039 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007040 }
Ronald Cron977c2472020-10-13 08:32:21 +02007041 }
7042
Gilles Peskine449bd832023-01-11 14:50:10 +01007043 status = psa_driver_wrapper_generate_key(attributes,
7044 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007045
Gilles Peskine449bd832023-01-11 14:50:10 +01007046 if (status != PSA_SUCCESS) {
7047 psa_remove_key_data_from_memory(slot);
7048 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007049
Gilles Peskine11792082019-08-06 18:36:36 +02007050exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007051 if (status == PSA_SUCCESS) {
7052 status = psa_finish_key_creation(slot, driver, key);
7053 }
7054 if (status != PSA_SUCCESS) {
7055 psa_fail_key_creation(slot, driver);
7056 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007057
Gilles Peskine449bd832023-01-11 14:50:10 +01007058 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007059}
7060
Gilles Peskinee59236f2018-01-27 23:32:46 +01007061/****************************************************************/
7062/* Module setup */
7063/****************************************************************/
7064
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007065#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007066psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007067 void (* entropy_init)(mbedtls_entropy_context *ctx),
7068 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007069{
Gilles Peskine449bd832023-01-11 14:50:10 +01007070 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7071 return PSA_ERROR_BAD_STATE;
7072 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007073 global_data.rng.entropy_init = entropy_init;
7074 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007075 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007076}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007077#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007078
Gilles Peskine449bd832023-01-11 14:50:10 +01007079void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007080{
Gilles Peskine449bd832023-01-11 14:50:10 +01007081 psa_wipe_all_key_slots();
7082 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7083 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007084 }
7085 /* Wipe all remaining data, including configuration.
7086 * In particular, this sets all state indicator to the value
7087 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007088 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007089
7090 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007091 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007092}
7093
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007094#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7095/** Recover a transaction that was interrupted by a power failure.
7096 *
7097 * This function is called during initialization, before psa_crypto_init()
7098 * returns. If this function returns a failure status, the initialization
7099 * fails.
7100 */
7101static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007102 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007103{
Gilles Peskine449bd832023-01-11 14:50:10 +01007104 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007105 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7106 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007107 /* TODO - fall through to the failure case until this
7108 * is implemented.
7109 * https://github.com/ARMmbed/mbed-crypto/issues/218
7110 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007111 default:
7112 /* We found an unsupported transaction in the storage.
7113 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007114 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007115 }
7116}
7117#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7118
Gilles Peskine449bd832023-01-11 14:50:10 +01007119psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007120{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007121 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007122
Gilles Peskinec6b69072018-11-20 21:42:52 +01007123 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007124 if (global_data.initialized != 0) {
7125 return PSA_SUCCESS;
7126 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007127
Gilles Peskine30524eb2020-11-13 17:02:26 +01007128 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007129 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007130 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007131 status = mbedtls_psa_random_seed(&global_data.rng);
7132 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007133 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007134 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007135 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007136
Gilles Peskine449bd832023-01-11 14:50:10 +01007137 status = psa_initialize_key_slots();
7138 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007139 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007140 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007141
Ronald Cron9ba76912021-04-10 16:57:30 +02007142 /* Init drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007143 status = psa_driver_wrapper_init();
7144 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02007145 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007146 }
Gilles Peskined9348f22019-10-01 15:22:29 +02007147
Gilles Peskine4b734222019-07-24 15:56:31 +02007148#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007149 status = psa_crypto_load_transaction();
7150 if (status == PSA_SUCCESS) {
7151 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7152 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007153 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007154 }
7155 status = psa_crypto_stop_transaction();
7156 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007157 /* There's no transaction to complete. It's all good. */
7158 status = PSA_SUCCESS;
7159 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007160#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007161
Gilles Peskinec6b69072018-11-20 21:42:52 +01007162 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007163 global_data.initialized = 1;
7164
7165exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007166 if (status != PSA_SUCCESS) {
7167 mbedtls_psa_crypto_free();
7168 }
7169 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007170}
7171
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007172psa_status_t psa_crypto_driver_pake_get_password_len(
7173 const psa_crypto_driver_pake_inputs_t *inputs,
7174 size_t *password_len)
7175{
7176 if (inputs->password_len == 0) {
7177 return PSA_ERROR_BAD_STATE;
7178 }
7179
7180 *password_len = inputs->password_len;
7181
7182 return PSA_SUCCESS;
7183}
7184
7185psa_status_t psa_crypto_driver_pake_get_password(
7186 const psa_crypto_driver_pake_inputs_t *inputs,
7187 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7188{
7189 if (inputs->password_len == 0) {
7190 return PSA_ERROR_BAD_STATE;
7191 }
7192
7193 if (buffer_size < inputs->password_len) {
7194 return PSA_ERROR_BUFFER_TOO_SMALL;
7195 }
7196
7197 memcpy(buffer, inputs->password, inputs->password_len);
7198 *buffer_length = inputs->password_len;
7199
7200 return PSA_SUCCESS;
7201}
7202
7203psa_status_t psa_crypto_driver_pake_get_role(
7204 const psa_crypto_driver_pake_inputs_t *inputs,
7205 psa_pake_role_t *role)
7206{
7207 if (inputs->role == PSA_PAKE_ROLE_NONE) {
7208 return PSA_ERROR_BAD_STATE;
7209 }
7210
7211 *role = inputs->role;
7212
7213 return PSA_SUCCESS;
7214}
7215
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007216psa_status_t psa_crypto_driver_pake_get_user_len(
7217 const psa_crypto_driver_pake_inputs_t *inputs,
7218 size_t *user_len)
7219{
7220 if (inputs->user_len == 0) {
7221 return PSA_ERROR_BAD_STATE;
7222 }
7223
7224 *user_len = inputs->user_len;
7225
7226 return PSA_SUCCESS;
7227}
7228
7229psa_status_t psa_crypto_driver_pake_get_user(
7230 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007231 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007232{
7233 if (inputs->user_len == 0) {
7234 return PSA_ERROR_BAD_STATE;
7235 }
7236
Przemek Stekielc0e62502023-03-14 11:49:36 +01007237 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007238 return PSA_ERROR_BUFFER_TOO_SMALL;
7239 }
7240
Przemek Stekielc0e62502023-03-14 11:49:36 +01007241 memcpy(user_id, inputs->user, inputs->user_len);
7242 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007243
7244 return PSA_SUCCESS;
7245}
7246
7247psa_status_t psa_crypto_driver_pake_get_peer_len(
7248 const psa_crypto_driver_pake_inputs_t *inputs,
7249 size_t *peer_len)
7250{
7251 if (inputs->peer_len == 0) {
7252 return PSA_ERROR_BAD_STATE;
7253 }
7254
7255 *peer_len = inputs->peer_len;
7256
7257 return PSA_SUCCESS;
7258}
7259
7260psa_status_t psa_crypto_driver_pake_get_peer(
7261 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007262 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007263{
7264 if (inputs->peer_len == 0) {
7265 return PSA_ERROR_BAD_STATE;
7266 }
7267
Przemek Stekielc0e62502023-03-14 11:49:36 +01007268 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007269 return PSA_ERROR_BUFFER_TOO_SMALL;
7270 }
7271
Przemek Stekielc0e62502023-03-14 11:49:36 +01007272 memcpy(peer_id, inputs->peer, inputs->peer_len);
7273 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007274
7275 return PSA_SUCCESS;
7276}
7277
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007278psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7279 const psa_crypto_driver_pake_inputs_t *inputs,
7280 psa_pake_cipher_suite_t *cipher_suite)
7281{
7282 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7283 return PSA_ERROR_BAD_STATE;
7284 }
7285
7286 *cipher_suite = inputs->cipher_suite;
7287
7288 return PSA_SUCCESS;
7289}
7290
Neil Armstronga7d08c32022-06-01 18:21:20 +02007291psa_status_t psa_pake_setup(
7292 psa_pake_operation_t *operation,
7293 const psa_pake_cipher_suite_t *cipher_suite)
7294{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007295 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007296
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007297 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007298 status = PSA_ERROR_BAD_STATE;
7299 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007300 }
7301
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007302 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007303 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007304 status = PSA_ERROR_INVALID_ARGUMENT;
7305 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007306 }
7307
Przemek Stekiel51eac532022-12-07 11:04:51 +01007308 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7309
Przemek Stekiele12ed362022-12-21 12:54:46 +01007310 operation->alg = cipher_suite->algorithm;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007311 operation->data.inputs.cipher_suite = *cipher_suite;
7312
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007313#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007314 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007315 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007316 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007317
Przemek Stekiele12ed362022-12-21 12:54:46 +01007318 computation_stage->state = PSA_PAKE_STATE_SETUP;
7319 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7320 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7321 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007322 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007323#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007324 {
7325 status = PSA_ERROR_NOT_SUPPORTED;
7326 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007327 }
7328
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007329 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7330
Przemek Stekiel51eac532022-12-07 11:04:51 +01007331 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007332exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007333 psa_pake_abort(operation);
7334 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007335}
7336
7337psa_status_t psa_pake_set_password_key(
7338 psa_pake_operation_t *operation,
7339 mbedtls_svc_key_id_t password)
7340{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007341 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007342 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7343 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007344
Przemek Stekiel51eac532022-12-07 11:04:51 +01007345 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007346 status = PSA_ERROR_BAD_STATE;
7347 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007348 }
7349
Przemek Stekiel6c764412022-11-22 14:05:12 +01007350 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7351 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007352 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007353 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007354 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007355 }
7356
Przemek Stekiel6c764412022-11-22 14:05:12 +01007357 psa_key_attributes_t attributes = {
7358 .core = slot->attr
7359 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007360
Przemek Stekiel51eac532022-12-07 11:04:51 +01007361 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007362
Przemek Stekiel51eac532022-12-07 11:04:51 +01007363 if (type != PSA_KEY_TYPE_PASSWORD &&
7364 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7365 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007366 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007367 }
7368
Przemek Stekiel51eac532022-12-07 11:04:51 +01007369 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7370 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007371 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007372 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007373 }
7374
7375 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7376 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007377 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007378exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007379 if (status != PSA_SUCCESS) {
7380 psa_pake_abort(operation);
7381 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007382 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007383 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007384}
7385
7386psa_status_t psa_pake_set_user(
7387 psa_pake_operation_t *operation,
7388 const uint8_t *user_id,
7389 size_t user_id_len)
7390{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007391 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007392
7393 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007394 status = PSA_ERROR_BAD_STATE;
7395 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007396 }
7397
Przemek Stekiel51eac532022-12-07 11:04:51 +01007398 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007399 status = PSA_ERROR_INVALID_ARGUMENT;
7400 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007401 }
7402
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007403 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007404 status = PSA_ERROR_BAD_STATE;
7405 goto exit;
7406 }
7407
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007408 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007409 if ((user_id_len != sizeof(jpake_server_id) ||
7410 memcmp(user_id, jpake_server_id, user_id_len) != 0) &&
7411 (user_id_len != sizeof(jpake_client_id) ||
7412 memcmp(user_id, jpake_client_id, user_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007413 status = PSA_ERROR_NOT_SUPPORTED;
7414 goto exit;
7415 }
7416
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007417 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7418 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007419 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7420 goto exit;
7421 }
7422
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007423 memcpy(operation->data.inputs.user, user_id, user_id_len);
7424 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007425
7426 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007427exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007428 psa_pake_abort(operation);
7429 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007430}
7431
7432psa_status_t psa_pake_set_peer(
7433 psa_pake_operation_t *operation,
7434 const uint8_t *peer_id,
7435 size_t peer_id_len)
7436{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007437 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007438
7439 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007440 status = PSA_ERROR_BAD_STATE;
7441 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007442 }
7443
Przemek Stekiel51eac532022-12-07 11:04:51 +01007444 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007445 status = PSA_ERROR_INVALID_ARGUMENT;
7446 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007447 }
7448
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007449 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007450 status = PSA_ERROR_BAD_STATE;
7451 goto exit;
7452 }
7453
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007454 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007455 if ((peer_id_len != sizeof(jpake_server_id) ||
7456 memcmp(peer_id, jpake_server_id, peer_id_len) != 0) &&
7457 (peer_id_len != sizeof(jpake_client_id) ||
7458 memcmp(peer_id, jpake_client_id, peer_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007459 status = PSA_ERROR_NOT_SUPPORTED;
7460 goto exit;
7461 }
7462
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007463 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7464 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007465 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7466 goto exit;
7467 }
7468
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007469 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7470 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007471
7472 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007473exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007474 psa_pake_abort(operation);
7475 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007476}
7477
7478psa_status_t psa_pake_set_role(
7479 psa_pake_operation_t *operation,
7480 psa_pake_role_t role)
7481{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007482 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007483
Przemek Stekiel51eac532022-12-07 11:04:51 +01007484 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007485 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007486 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007487 }
7488
Przemek Stekiel09104b82023-03-06 14:11:51 +01007489 switch (operation->alg) {
7490#if defined(PSA_WANT_ALG_JPAKE)
7491 case PSA_ALG_JPAKE:
7492 if (role == PSA_PAKE_ROLE_NONE) {
7493 return PSA_SUCCESS;
7494 }
7495 status = PSA_ERROR_INVALID_ARGUMENT;
7496 break;
7497#endif
7498 default:
7499 (void) role;
7500 status = PSA_ERROR_NOT_SUPPORTED;
7501 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007502 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007503exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007504 psa_pake_abort(operation);
7505 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007506}
7507
Przemek Stekielb09c4872023-01-17 12:05:38 +01007508/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007509#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007510static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007511 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007512{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007513 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007514 case PSA_PAKE_OUTPUT_X1_X2:
7515 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007516 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007517 case PSA_PAKE_X1_STEP_KEY_SHARE:
7518 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007519 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7520 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007521 case PSA_PAKE_X1_STEP_ZK_PROOF:
7522 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007523 case PSA_PAKE_X2_STEP_KEY_SHARE:
7524 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007525 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7526 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007527 case PSA_PAKE_X2_STEP_ZK_PROOF:
7528 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007529 default:
7530 return PSA_JPAKE_STEP_INVALID;
7531 }
7532 break;
7533 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007534 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007535 case PSA_PAKE_X1_STEP_KEY_SHARE:
7536 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007537 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7538 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007539 case PSA_PAKE_X1_STEP_ZK_PROOF:
7540 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007541 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007542 return PSA_JPAKE_STEP_INVALID;
7543 }
7544 break;
7545 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007546 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007547 case PSA_PAKE_X1_STEP_KEY_SHARE:
7548 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007549 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7550 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007551 case PSA_PAKE_X1_STEP_ZK_PROOF:
7552 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007553 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007554 return PSA_JPAKE_STEP_INVALID;
7555 }
7556 break;
7557 default:
7558 return PSA_JPAKE_STEP_INVALID;
7559 }
7560 return PSA_JPAKE_STEP_INVALID;
7561}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007562#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007563
Przemek Stekiel2797d372022-12-22 11:19:22 +01007564static psa_status_t psa_pake_complete_inputs(
7565 psa_pake_operation_t *operation)
7566{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007567 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007568 /* Create copy of the inputs on stack as inputs share memory
7569 with the driver context which will be setup by the driver. */
7570 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007571
Przemek Stekielfde11282023-03-13 16:06:09 +01007572 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007573 return PSA_ERROR_BAD_STATE;
7574 }
7575
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007576 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007577 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7578 return PSA_ERROR_BAD_STATE;
7579 }
7580 if (memcmp(inputs.user, jpake_client_id, inputs.user_len) == 0 &&
7581 memcmp(inputs.peer, jpake_server_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007582 inputs.role = PSA_PAKE_ROLE_CLIENT;
7583 } else
Przemek Stekielfde11282023-03-13 16:06:09 +01007584 if (memcmp(inputs.user, jpake_server_id, inputs.user_len) == 0 &&
7585 memcmp(inputs.peer, jpake_client_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007586 inputs.role = PSA_PAKE_ROLE_SERVER;
7587 }
7588
7589 if (inputs.role != PSA_PAKE_ROLE_CLIENT &&
7590 inputs.role != PSA_PAKE_ROLE_SERVER) {
7591 return PSA_ERROR_NOT_SUPPORTED;
7592 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007593 }
7594
Przemek Stekiel18620a32023-01-17 16:34:52 +01007595 /* Clear driver context */
7596 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7597
7598 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007599
7600 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007601 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7602 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007603
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007604 /* User and peer are translated to role. */
7605 mbedtls_free(inputs.user);
7606 mbedtls_free(inputs.peer);
7607
Przemek Stekiel2797d372022-12-22 11:19:22 +01007608 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007609#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007610 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007611 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007612 psa_jpake_computation_stage_t *computation_stage =
7613 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007614 computation_stage->state = PSA_PAKE_STATE_READY;
7615 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7616 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7617 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007618 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007619#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007620 {
7621 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007622 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007623 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007624 return status;
7625}
7626
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007627#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007628static psa_status_t psa_jpake_output_prologue(
7629 psa_pake_operation_t *operation,
7630 psa_pake_step_t step)
7631{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007632 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7633 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7634 step != PSA_PAKE_STEP_ZK_PROOF) {
7635 return PSA_ERROR_INVALID_ARGUMENT;
7636 }
7637
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007638 psa_jpake_computation_stage_t *computation_stage =
7639 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007640
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007641 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7642 return PSA_ERROR_BAD_STATE;
7643 }
7644
7645 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7646 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
7647 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
7648 return PSA_ERROR_BAD_STATE;
7649 }
7650
7651 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7652 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007653 return PSA_ERROR_BAD_STATE;
7654 }
7655
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007656 switch (computation_stage->output_step) {
7657 case PSA_PAKE_STEP_X1_X2:
7658 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007659 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007660 case PSA_PAKE_STEP_X2S:
7661 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007662 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007663 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007664 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007665 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007666
7667 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7668 }
7669
7670 /* Check if step matches current sequence */
7671 switch (computation_stage->sequence) {
7672 case PSA_PAKE_X1_STEP_KEY_SHARE:
7673 case PSA_PAKE_X2_STEP_KEY_SHARE:
7674 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7675 return PSA_ERROR_BAD_STATE;
7676 }
7677 break;
7678
7679 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7680 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7681 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7682 return PSA_ERROR_BAD_STATE;
7683 }
7684 break;
7685
7686 case PSA_PAKE_X1_STEP_ZK_PROOF:
7687 case PSA_PAKE_X2_STEP_ZK_PROOF:
7688 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7689 return PSA_ERROR_BAD_STATE;
7690 }
7691 break;
7692
7693 default:
7694 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007695 }
7696
7697 return PSA_SUCCESS;
7698}
7699
7700static psa_status_t psa_jpake_output_epilogue(
7701 psa_pake_operation_t *operation)
7702{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007703 psa_jpake_computation_stage_t *computation_stage =
7704 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007705
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007706 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007707 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007708 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007709 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007710 computation_stage->state = PSA_PAKE_STATE_READY;
7711 computation_stage->output_step++;
7712 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7713 } else {
7714 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007715 }
7716
7717 return PSA_SUCCESS;
7718}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007719#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007720
Neil Armstronga7d08c32022-06-01 18:21:20 +02007721psa_status_t psa_pake_output(
7722 psa_pake_operation_t *operation,
7723 psa_pake_step_t step,
7724 uint8_t *output,
7725 size_t output_size,
7726 size_t *output_length)
7727{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007728 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007729 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007730 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007731
7732 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007733 status = psa_pake_complete_inputs(operation);
7734 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007735 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007736 }
7737 }
7738
7739 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007740 status = PSA_ERROR_BAD_STATE;
7741 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007742 }
7743
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007744 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007745 status = PSA_ERROR_INVALID_ARGUMENT;
7746 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007747 }
7748
Przemek Stekiele12ed362022-12-21 12:54:46 +01007749 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007750#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007751 case PSA_ALG_JPAKE:
7752 status = psa_jpake_output_prologue(operation, step);
7753 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007754 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007755 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007756 driver_step = convert_jpake_computation_stage_to_driver_step(
7757 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007758 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007759#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007760 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007761 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007762 status = PSA_ERROR_NOT_SUPPORTED;
7763 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007764 }
7765
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007766 status = psa_driver_wrapper_pake_output(operation, driver_step,
7767 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007768
7769 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007770 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007771 }
7772
7773 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007774#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007775 case PSA_ALG_JPAKE:
7776 status = psa_jpake_output_epilogue(operation);
7777 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007778 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007779 }
7780 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007781#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007782 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007783 status = PSA_ERROR_NOT_SUPPORTED;
7784 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007785 }
7786
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007787 return PSA_SUCCESS;
7788exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007789 psa_pake_abort(operation);
7790 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007791}
7792
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007793#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007794static psa_status_t psa_jpake_input_prologue(
7795 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007796 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007797{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007798 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7799 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7800 step != PSA_PAKE_STEP_ZK_PROOF) {
7801 return PSA_ERROR_INVALID_ARGUMENT;
7802 }
7803
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007804 psa_jpake_computation_stage_t *computation_stage =
7805 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007806
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007807 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7808 return PSA_ERROR_BAD_STATE;
7809 }
7810
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007811 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7812 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
7813 computation_stage->state != PSA_PAKE_INPUT_X4S) {
7814 return PSA_ERROR_BAD_STATE;
7815 }
7816
7817 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7818 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007819 return PSA_ERROR_BAD_STATE;
7820 }
7821
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007822 switch (computation_stage->input_step) {
7823 case PSA_PAKE_STEP_X1_X2:
7824 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007825 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007826 case PSA_PAKE_STEP_X2S:
7827 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007828 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007829 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007830 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007831 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007832
7833 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7834 }
7835
7836 /* Check if step matches current sequence */
7837 switch (computation_stage->sequence) {
7838 case PSA_PAKE_X1_STEP_KEY_SHARE:
7839 case PSA_PAKE_X2_STEP_KEY_SHARE:
7840 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7841 return PSA_ERROR_BAD_STATE;
7842 }
7843 break;
7844
7845 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7846 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7847 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7848 return PSA_ERROR_BAD_STATE;
7849 }
7850 break;
7851
7852 case PSA_PAKE_X1_STEP_ZK_PROOF:
7853 case PSA_PAKE_X2_STEP_ZK_PROOF:
7854 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7855 return PSA_ERROR_BAD_STATE;
7856 }
7857 break;
7858
7859 default:
7860 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007861 }
7862
7863 return PSA_SUCCESS;
7864}
7865
Przemek Stekiele12ed362022-12-21 12:54:46 +01007866static psa_status_t psa_jpake_input_epilogue(
7867 psa_pake_operation_t *operation)
7868{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007869 psa_jpake_computation_stage_t *computation_stage =
7870 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007871
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007872 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007873 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007874 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007875 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007876 computation_stage->state = PSA_PAKE_STATE_READY;
7877 computation_stage->input_step++;
7878 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7879 } else {
7880 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007881 }
7882
7883 return PSA_SUCCESS;
7884}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007885#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007886
Neil Armstronga7d08c32022-06-01 18:21:20 +02007887psa_status_t psa_pake_input(
7888 psa_pake_operation_t *operation,
7889 psa_pake_step_t step,
7890 const uint8_t *input,
7891 size_t input_length)
7892{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007893 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007894 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007895
7896 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007897 status = psa_pake_complete_inputs(operation);
7898 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007899 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007900 }
7901 }
7902
7903 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007904 status = PSA_ERROR_BAD_STATE;
7905 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007906 }
7907
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007908 if (input_length == 0 || input_length > PSA_PAKE_INPUT_MAX_SIZE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007909 status = PSA_ERROR_INVALID_ARGUMENT;
7910 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007911 }
7912
Przemek Stekiele12ed362022-12-21 12:54:46 +01007913 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007914#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007915 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007916 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007917 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007918 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007919 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007920 driver_step = convert_jpake_computation_stage_to_driver_step(
7921 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007922 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007923#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007924 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007925 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007926 status = PSA_ERROR_NOT_SUPPORTED;
7927 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007928 }
7929
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007930 status = psa_driver_wrapper_pake_input(operation, driver_step,
7931 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007932
7933 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007934 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007935 }
7936
7937 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007938#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007939 case PSA_ALG_JPAKE:
7940 status = psa_jpake_input_epilogue(operation);
7941 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007942 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007943 }
7944 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007945#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007946 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007947 status = PSA_ERROR_NOT_SUPPORTED;
7948 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007949 }
7950
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007951 return PSA_SUCCESS;
7952exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007953 psa_pake_abort(operation);
7954 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007955}
7956
7957psa_status_t psa_pake_get_implicit_key(
7958 psa_pake_operation_t *operation,
7959 psa_key_derivation_operation_t *output)
7960{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007961 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007962 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007963 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01007964 size_t shared_key_len = 0;
7965
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007966 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007967 status = PSA_ERROR_BAD_STATE;
7968 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007969 }
7970
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007971#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007972 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007973 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01007974 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007975 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
7976 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007977 status = PSA_ERROR_BAD_STATE;
7978 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007979 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01007980 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007981#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007982 {
7983 status = PSA_ERROR_NOT_SUPPORTED;
7984 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007985 }
7986
Przemek Stekiel0c781802022-11-29 14:53:13 +01007987 status = psa_driver_wrapper_pake_get_implicit_key(operation,
7988 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01007989 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01007990 &shared_key_len);
7991
7992 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007993 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01007994 }
7995
7996 status = psa_key_derivation_input_bytes(output,
7997 PSA_KEY_DERIVATION_INPUT_SECRET,
7998 shared_key,
7999 shared_key_len);
8000
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008001 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008002exit:
8003 abort_status = psa_pake_abort(operation);
8004 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008005}
8006
8007psa_status_t psa_pake_abort(
8008 psa_pake_operation_t *operation)
8009{
Przemek Stekield69dca92023-01-31 19:59:20 +01008010 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008011
Przemek Stekield69dca92023-01-31 19:59:20 +01008012 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008013 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008014 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008015
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008016 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8017 if (operation->data.inputs.password != NULL) {
8018 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008019 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008020 mbedtls_free(operation->data.inputs.password);
8021 }
8022 if (operation->data.inputs.user != NULL) {
8023 mbedtls_free(operation->data.inputs.user);
8024 }
8025 if (operation->data.inputs.peer != NULL) {
8026 mbedtls_free(operation->data.inputs.peer);
8027 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008028 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008029 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008030
Przemek Stekield69dca92023-01-31 19:59:20 +01008031 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008032}
Neil Armstronga7d08c32022-06-01 18:21:20 +02008033
Gilles Peskinee59236f2018-01-27 23:32:46 +01008034#endif /* MBEDTLS_PSA_CRYPTO_C */