blob: 76af6b66df5d948189d383f6f7c16f1e51bbc60f [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
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/****************************************************************/
95/* Global data, support functions and library management */
96/****************************************************************/
97
Gilles Peskine449bd832023-01-11 14:50:10 +010098static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020099{
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200101}
102
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100103/* Values for psa_global_data_t::rng_state */
104#define RNG_NOT_INITIALIZED 0
105#define RNG_INITIALIZED 1
106#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100109 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100110 unsigned rng_state : 2;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100111 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100112} psa_global_data_t;
113
114static psa_global_data_t global_data;
115
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100116#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
117mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
118 &global_data.rng.drbg;
119#endif
120
itayzafrir0adf0fc2018-09-06 16:24:41 +0300121#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 if (global_data.initialized == 0) \
123 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100126{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100127 /* Mbed TLS error codes can combine a high-level error code and a
128 * low-level error code. The low-level error usually reflects the
129 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 int low_level_ret = -(-ret & 0x007f);
131 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100132 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100134
135 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
136 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200138 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
139 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
140 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
141 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
142 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200144 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200146 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200148
Jaeden Amero93e21112019-02-20 13:57:28 +0000149#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000150 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000151#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100152 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100154
155 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100157 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100159
Gilles Peskine26869f22019-05-06 15:25:00 +0200160 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200162
163 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200165 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200167
Gilles Peskinea5905292018-02-07 20:59:33 +0100168 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100170 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100172 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100178 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100180 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
184 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100185 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
186 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100187 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100189 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
190 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100192 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100194#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100195
196 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100198
Gilles Peskinee59236f2018-01-27 23:32:46 +0100199 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
200 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
201 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100203
204 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200206 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100208 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100210
Gilles Peskine82e57d12020-11-13 21:31:17 +0100211#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100213 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
214 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100215 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100217 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
218 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100220 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100222#endif
223
Gilles Peskinea5905292018-02-07 20:59:33 +0100224 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100226 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100228 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100230 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100232
Gilles Peskinef76aa772018-10-29 19:24:33 +0100233 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100235 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100237 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100239 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100241 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
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_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100245 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
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_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100249
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100250 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100252 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
253 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100255 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100257 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
258 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100260 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100262 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
263 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100265 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100267 case MBEDTLS_ERR_PK_INVALID_ALG:
268 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
269 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200273 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100275
Gilles Peskineff2d2002019-05-06 15:26:23 +0200276 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200278 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200280
Gilles Peskinea5905292018-02-07 20:59:33 +0100281 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100283 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100285 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100287 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100289 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
290 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100292 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200298
itayzafrir5c753392018-05-08 11:18:38 +0300299 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300300 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300302 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300304 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300306 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
307 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300309 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100311 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100313
Paul Elliott588f8ed2022-12-02 18:10:26 +0000314 case MBEDTLS_ERR_ECP_IN_PROGRESS:
315 return PSA_OPERATION_INCOMPLETE;
316
Janos Follatha13b9052019-11-22 12:48:59 +0000317 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300319
Gilles Peskinee59236f2018-01-27 23:32:46 +0100320 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100322 }
323}
324
Paul Elliottdc42ca82023-02-24 18:11:59 +0000325/**
326 * \brief For output buffers which contain "tags"
327 * (outputs that may be checked for validity like
328 * hashes, MACs and signatures), fill the unused
329 * part of the output buffer (the whole buffer on
330 * error, the trailing part on success) with
331 * something that isn't a valid tag (barring an
332 * attack on the tag and deliberately-crafted
333 * input), in case the caller doesn't check the
334 * return status properly.
335 *
336 * \param output_buffer Pointer to buffer to wipe. May not be NULL
337 * unless \p output_buffer_size is zero.
338 * \param status Status of function called to generate
339 * output_buffer originally
340 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
341 * could be NULL.
342 * \param output_buffer_length Length of data written to output_buffer, must be
343 * less than \p output_buffer_size
344 */
345static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000346 size_t output_buffer_size, size_t output_buffer_length)
347{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000348 size_t offset = 0;
349
350 if (output_buffer_size == 0) {
351 /* If output_buffer_size is 0 then we have nothing to do. We must not
352 call memset because output_buffer may be NULL in this case */
353 return;
354 }
355
356 if (status == PSA_SUCCESS) {
357 offset = output_buffer_length;
358 }
359
360 memset(output_buffer + offset, '!', output_buffer_size - offset);
361}
362
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200363
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200364
365
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100366/****************************************************************/
367/* Key management */
368/****************************************************************/
369
John Durkop9814fa22020-11-04 12:28:15 -0800370#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800371 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cronf467d632021-11-19 18:02:34 +0100372 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
373 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
374 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100375mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
376 size_t bits,
377 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200378{
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100380 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100382#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100383 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100385#endif
386#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100387 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100389#endif
390#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100391 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100393#endif
394#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100395 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100397#endif
398#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100399 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100401 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if (bits_is_sloppy) {
403 return MBEDTLS_ECP_DP_SECP521R1;
404 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100405 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100406#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100407 }
408 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100409
Paul Elliott8ff510a2020-06-02 17:19:28 +0100410 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100412#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100413 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100415#endif
416#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100417 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100419#endif
420#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100421 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100423#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100424 }
425 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100426
Paul Elliott8ff510a2020-06-02 17:19:28 +0100427 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100429#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100430 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100432 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if (bits_is_sloppy) {
434 return MBEDTLS_ECP_DP_CURVE25519;
435 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100436 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100437#endif
438#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100439 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100441#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100442 }
443 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100444
Paul Elliott8ff510a2020-06-02 17:19:28 +0100445 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100447#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100448 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100450#endif
451#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100452 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100454#endif
455#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100456 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100458#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100459 }
460 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200461 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100462
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100463 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200465}
John Durkop9814fa22020-11-04 12:28:15 -0800466#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cronf467d632021-11-19 18:02:34 +0100467 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
468 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
469 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
470 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
473 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200474{
475 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200477 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200478 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200479 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200480 case PSA_KEY_TYPE_PASSWORD:
481 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200482 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100483#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200484 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if (bits != 128 && bits != 192 && bits != 256) {
486 return PSA_ERROR_INVALID_ARGUMENT;
487 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200488 break;
489#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200490#if defined(PSA_WANT_KEY_TYPE_ARIA)
491 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 if (bits != 128 && bits != 192 && bits != 256) {
493 return PSA_ERROR_INVALID_ARGUMENT;
494 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200495 break;
496#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100497#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200498 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 if (bits != 128 && bits != 192 && bits != 256) {
500 return PSA_ERROR_INVALID_ARGUMENT;
501 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200502 break;
503#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100504#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200505 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 if (bits != 64 && bits != 128 && bits != 192) {
507 return PSA_ERROR_INVALID_ARGUMENT;
508 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200509 break;
510#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100511#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200512 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 if (bits != 256) {
514 return PSA_ERROR_INVALID_ARGUMENT;
515 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200516 break;
517#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200518 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200520 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 if (bits % 8 != 0) {
522 return PSA_ERROR_INVALID_ARGUMENT;
523 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200526}
527
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100528/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100529 *
Steven Cooremancd640932021-03-08 12:00:27 +0100530 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
531 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100532 *
533 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100534 * \param[in] key_type The key type of the key to be used with the
535 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100536 *
537 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100538 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100539 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100540 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100541 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100542MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100543 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100545{
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (PSA_ALG_IS_HMAC(algorithm)) {
547 if (key_type == PSA_KEY_TYPE_HMAC) {
548 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100549 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100550 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
553 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
554 * key. */
555 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
556 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
557 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
558 * the block length (larger than 1) for block ciphers. */
559 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
560 return PSA_SUCCESS;
561 }
562 }
563 }
564
565 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100566}
567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
569 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200570{
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 if (slot->key.data != NULL) {
572 return PSA_ERROR_ALREADY_EXISTS;
573 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 slot->key.data = mbedtls_calloc(1, buffer_length);
576 if (slot->key.data == NULL) {
577 return PSA_ERROR_INSUFFICIENT_MEMORY;
578 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200579
Ronald Cronea0f8a62020-11-25 17:52:23 +0100580 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200582}
583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
585 const uint8_t *data,
586 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200587{
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 psa_status_t status = psa_allocate_buffer_to_slot(slot,
589 data_length);
590 if (status != PSA_SUCCESS) {
591 return status;
592 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 memcpy(slot->key.data, data, data_length);
595 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200596}
597
Ronald Crona0fe59f2020-11-29 11:14:53 +0100598psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100599 const psa_key_attributes_t *attributes,
600 const uint8_t *data, size_t data_length,
601 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100603{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100604 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
605 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100606
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200607 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (data_length == 0) {
609 return PSA_ERROR_NOT_SUPPORTED;
610 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 if (key_type_is_raw_bytes(type)) {
613 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
616 *bits);
617 if (status != PSA_SUCCESS) {
618 return status;
619 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200620
Ronald Crondd04d422020-11-28 15:14:42 +0100621 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100623 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 return PSA_SUCCESS;
627 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800628#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
630 if (PSA_KEY_TYPE_IS_ECC(type)) {
631 return mbedtls_psa_ecp_import_key(attributes,
632 data, data_length,
633 key_buffer, key_buffer_size,
634 key_buffer_length,
635 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100636 }
John Durkop9814fa22020-11-04 12:28:15 -0800637#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
638 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700639#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
641 if (PSA_KEY_TYPE_IS_RSA(type)) {
642 return mbedtls_psa_rsa_import_key(attributes,
643 data, data_length,
644 key_buffer, key_buffer_size,
645 key_buffer_length,
646 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200647 }
John Durkop9814fa22020-11-04 12:28:15 -0800648#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
649 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100650 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100653}
654
Gilles Peskinef603c712019-01-19 13:40:11 +0100655/** Calculate the intersection of two algorithm usage policies.
656 *
657 * Return 0 (which allows no operation) on incompatibility.
658 */
659static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100660 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100661 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100663{
Gilles Peskine549ea862019-05-22 11:45:59 +0200664 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (alg1 == alg2) {
666 return alg1;
667 }
Steven Cooreman03488022021-02-18 12:07:20 +0100668 /* If the policies are from the same hash-and-sign family, check
669 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
671 PSA_ALG_IS_SIGN_HASH(alg2) &&
672 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
673 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
674 return alg2;
675 }
676 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
677 return alg1;
678 }
Steven Cooreman03488022021-02-18 12:07:20 +0100679 }
680 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100681 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100682 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
684 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
685 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
686 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
687 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100688 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100689
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100690 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
692 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
693 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
694 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100695 }
696 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
698 (alg1_len <= alg2_len)) {
699 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100700 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
702 (alg2_len <= alg1_len)) {
703 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100704 }
705 }
706 /* If the policies are from the same MAC family, check whether one
707 * of them is a minimum-MAC-length policy. Calculate the most
708 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
710 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
711 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100712 /* Validate the combination of key type and algorithm. Since the base
713 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
715 return 0;
716 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100717
Steven Cooreman31a876d2021-03-03 20:47:40 +0100718 /* Get the (exact or at-least) output lengths for both sides of the
719 * requested intersection. None of the currently supported algorithms
720 * have an output length dependent on the actual key size, so setting it
721 * to a bogus value of 0 is currently OK.
722 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100723 * Note that for at-least-this-length wildcard algorithms, the output
724 * length is set to the shortest allowed length, which allows us to
725 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
727 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100728 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100729
Steven Cooremana1d83222021-02-25 10:20:29 +0100730 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
732 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
733 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100734 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100735
736 /* If only one is an at-least-this-length policy, the intersection would
737 * be the other (fixed-length) policy as long as said fixed length is
738 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
740 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100741 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
743 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100744 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100745
Steven Cooremancd640932021-03-08 12:00:27 +0100746 /* If none of them are wildcards, check whether they define the same tag
747 * length. This is still possible here when one is default-length and
748 * the other specific-length. Ensure to always return the
749 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 if (alg1_len == alg2_len) {
751 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
752 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100753 }
754 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100756}
757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758static int psa_key_algorithm_permits(psa_key_type_t key_type,
759 psa_algorithm_t policy_alg,
760 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200761{
Gilles Peskine549ea862019-05-22 11:45:59 +0200762 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 if (requested_alg == policy_alg) {
764 return 1;
765 }
Steven Cooreman03488022021-02-18 12:07:20 +0100766 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
767 * and requested_alg is the same hash-and-sign family with any hash,
768 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
770 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
771 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
772 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100773 }
774 /* If policy_alg is a wildcard AEAD algorithm of the same base as
775 * the requested algorithm, check the requested tag length to be
776 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 if (PSA_ALG_IS_AEAD(policy_alg) &&
778 PSA_ALG_IS_AEAD(requested_alg) &&
779 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
780 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
781 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
782 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
783 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100784 }
Steven Cooremancd640932021-03-08 12:00:27 +0100785 /* If policy_alg is a MAC algorithm of the same base as the requested
786 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 if (PSA_ALG_IS_MAC(policy_alg) &&
788 PSA_ALG_IS_MAC(requested_alg) &&
789 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
790 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100791 /* Validate the combination of key type and algorithm. Since the policy
792 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
794 return 0;
795 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100796
Steven Cooreman31a876d2021-03-03 20:47:40 +0100797 /* Get both the requested output length for the algorithm which is to be
798 * verified, and the default output length for the base algorithm.
799 * Note that none of the currently supported algorithms have an output
800 * length dependent on actual key size, so setting it to a bogus value
801 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100802 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100804 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 key_type, 0,
806 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100807
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100808 /* If the policy is default-length, only allow an algorithm with
809 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
811 return requested_output_length == default_output_length;
812 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100813
814 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100815 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
817 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
818 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100819 }
820
Steven Cooremancd640932021-03-08 12:00:27 +0100821 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
822 * check for the requested MAC length to be equal to or longer than the
823 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
825 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
826 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100827 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200828 }
Steven Cooremance48e852020-10-05 16:02:45 +0200829 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200830 * a key derivation with that key agreement should also be allowed. This
831 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
833 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
834 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
835 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200836 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100837 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200839}
840
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100841/** Test whether a policy permits an algorithm.
842 *
843 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100844 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100845 * \note This function requires providing the key type for which the policy is
846 * being validated, since some algorithm policy definitions (e.g. MAC)
847 * have different properties depending on what kind of cipher it is
848 * combined with.
849 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100850 * \retval PSA_SUCCESS When \p alg is a specific algorithm
851 * allowed by the \p policy.
852 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
853 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
854 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100855 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100856static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
857 psa_key_type_t key_type,
858 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100859{
Steven Cooremand788fab2021-02-25 11:29:17 +0100860 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 if (alg == 0) {
862 return PSA_ERROR_INVALID_ARGUMENT;
863 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100864
Steven Cooreman7e39f052021-02-23 14:18:32 +0100865 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if (PSA_ALG_IS_WILDCARD(alg)) {
867 return PSA_ERROR_INVALID_ARGUMENT;
868 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
871 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
872 return PSA_SUCCESS;
873 } else {
874 return PSA_ERROR_NOT_PERMITTED;
875 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100876}
877
Gilles Peskinef603c712019-01-19 13:40:11 +0100878/** Restrict a key policy based on a constraint.
879 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100880 * \note This function requires providing the key type for which the policy is
881 * being restricted, since some algorithm policy definitions (e.g. MAC)
882 * have different properties depending on what kind of cipher it is
883 * combined with.
884 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100885 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100886 * \param[in,out] policy The policy to restrict.
887 * \param[in] constraint The policy constraint to apply.
888 *
889 * \retval #PSA_SUCCESS
890 * \c *policy contains the intersection of the original value of
891 * \c *policy and \c *constraint.
892 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100893 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100894 * \c *policy is unchanged.
895 */
896static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100897 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100898 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100900{
901 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 psa_key_policy_algorithm_intersection(key_type, policy->alg,
903 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200904 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
906 constraint->alg2);
907 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
908 return PSA_ERROR_INVALID_ARGUMENT;
909 }
910 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
911 return PSA_ERROR_INVALID_ARGUMENT;
912 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100913 policy->usage &= constraint->usage;
914 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200915 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100917}
918
Przemek Stekiel061f6942022-11-30 10:51:35 +0100919/** Get the description of a key given its identifier and policy constraints
920 * and lock it.
921 *
922 * The key must have allow all the usage flags set in \p usage. If \p alg is
923 * nonzero, the key must allow operations with this algorithm. If \p alg is
924 * zero, the algorithm is not checked.
925 *
926 * In case of a persistent key, the function loads the description of the key
927 * into a key slot if not already done.
928 *
929 * On success, the returned key slot is locked. It is the responsibility of
930 * the caller to unlock the key slot when it does not access it anymore.
931 */
932static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100933 mbedtls_svc_key_id_t key,
934 psa_key_slot_t **p_slot,
935 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100937{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200938 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100939 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 status = psa_get_and_lock_key_slot(key, p_slot);
942 if (status != PSA_SUCCESS) {
943 return status;
944 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200945 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100946
947 /* Enforce that usage policy for the key slot contains all the flags
948 * required by the usage parameter. There is one exception: public
949 * keys can always be exported, so we treat public key objects as
950 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100952 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200954
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100956 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200957 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100958 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100959
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800960 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 if (alg != 0) {
962 status = psa_key_policy_permits(&slot->attr.policy,
963 slot->attr.type,
964 alg);
965 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100966 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100968 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200971
972error:
973 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200975
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100977}
Darryl Green940d72c2018-07-13 13:18:51 +0100978
Ronald Cron5c522922020-11-14 16:35:34 +0100979/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200980 *
981 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200982 * available, as opposed to a key in a secure element and/or to be used
983 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200984 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200985 * This is a temporary function that may be used instead of
986 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
987 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200988 *
Ronald Cron5c522922020-11-14 16:35:34 +0100989 * On success, the returned key slot is locked. It is the responsibility of the
990 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200991 */
Ronald Cron5c522922020-11-14 16:35:34 +0100992static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
993 mbedtls_svc_key_id_t key,
994 psa_key_slot_t **p_slot,
995 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +0200997{
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
999 usage, alg);
1000 if (status != PSA_SUCCESS) {
1001 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001002 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001003
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1005 psa_unlock_key_slot(*p_slot);
1006 *p_slot = NULL;
1007 return PSA_ERROR_NOT_SUPPORTED;
1008 }
1009
1010 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001011}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001012
Gilles Peskine449bd832023-01-11 14:50:10 +01001013psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001014{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001015 /* Data pointer will always be either a valid pointer or NULL in an
1016 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 if (slot->key.data != NULL) {
1018 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1019 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001022 slot->key.data = NULL;
1023 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001024
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001026}
1027
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001028/** Completely wipe a slot in memory, including its policy.
1029 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001030psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001031{
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001033
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 /*
1035 * As the return error code may not be handled in case of multiple errors,
1036 * do our best to report an unexpected lock counter. Assert with
1037 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1038 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1039 * function is called as part of the execution of a test suite, the
1040 * execution of the test suite is stopped in error if the assertion fails.
1041 */
1042 if (slot->lock_count != 1) {
1043 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001044 status = PSA_ERROR_CORRUPTION_DETECTED;
1045 }
1046
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001047 /* Multipart operations may still be using the key. This is safe
1048 * because all multipart operation objects are independent from
1049 * the key slot: if they need to access the key after the setup
1050 * phase, they have a copy of the key. Note that this means that
1051 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001052 /* At this point, key material and other type-specific content has
1053 * been wiped. Clear remaining metadata. We can call memset and not
1054 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 memset(slot, 0, sizeof(*slot));
1056 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001057}
1058
Gilles Peskine449bd832023-01-11 14:50:10 +01001059psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001060{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001061 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001062 psa_status_t status; /* status of the last operation */
1063 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001064#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1065 psa_se_drv_table_entry_t *driver;
1066#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001067
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 if (mbedtls_svc_key_id_is_null(key)) {
1069 return PSA_SUCCESS;
1070 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001071
Ronald Cronf2911112020-10-29 17:51:10 +01001072 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001073 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001074 * key, this will load the key description from persistent memory if not
1075 * done yet. We cannot avoid this loading as without it we don't know if
1076 * the key is operated by an SE or not and this information is needed by
1077 * the current implementation.
1078 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 status = psa_get_and_lock_key_slot(key, &slot);
1080 if (status != PSA_SUCCESS) {
1081 return status;
1082 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001083
Ronald Cronf2911112020-10-29 17:51:10 +01001084 /*
1085 * If the key slot containing the key description is under access by the
1086 * library (apart from the present access), the key cannot be destroyed
1087 * yet. For the time being, just return in error. Eventually (to be
1088 * implemented), the key should be destroyed when all accesses have
1089 * stopped.
1090 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (slot->lock_count > 1) {
1092 psa_unlock_key_slot(slot);
1093 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001094 }
1095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001097 /* Refuse the destruction of a read-only key (which may or may not work
1098 * if we attempt it, depending on whether the key is merely read-only
1099 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001100 * Just do the best we can, which is to wipe the copy in memory
1101 * (done in this function's cleanup code). */
1102 overall_status = PSA_ERROR_NOT_PERMITTED;
1103 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001104 }
1105
Gilles Peskine354f7672019-07-12 23:46:38 +02001106#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1108 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001109 /* For a key in a secure element, we need to do three things:
1110 * remove the key file in internal storage, destroy the
1111 * key inside the secure element, and update the driver's
1112 * persistent data. Start a transaction that will encompass these
1113 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001115 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001117 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 status = psa_crypto_save_transaction();
1119 if (status != PSA_SUCCESS) {
1120 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001121 /* We should still try to destroy the key in the secure
1122 * element and the key metadata in storage. This is especially
1123 * important if the error is that the storage is full.
1124 * But how to do it exactly without risking an inconsistent
1125 * state after a reset?
1126 * https://github.com/ARMmbed/mbed-crypto/issues/215
1127 */
1128 overall_status = status;
1129 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001130 }
1131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 status = psa_destroy_se_key(driver,
1133 psa_key_slot_get_slot_number(slot));
1134 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001135 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001137 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001138#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1139
Darryl Greend49a4992018-06-18 17:27:26 +01001140#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1142 status = psa_destroy_persistent_key(slot->attr.id);
1143 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001144 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001146
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001147 /* TODO: other slots may have a copy of the same key. We should
1148 * invalidate them.
1149 * https://github.com/ARMmbed/mbed-crypto/issues/214
1150 */
Darryl Greend49a4992018-06-18 17:27:26 +01001151 }
1152#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001153
Gilles Peskinefc762652019-07-22 19:30:34 +02001154#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 if (driver != NULL) {
1156 status = psa_save_se_persistent_data(driver);
1157 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001158 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 }
1160 status = psa_crypto_stop_transaction();
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 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001164 }
1165#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1166
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001167exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001169 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001171 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 }
1173 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001174}
1175
John Durkop07cc04a2020-11-16 22:08:34 -08001176#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001177 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001178static psa_status_t psa_get_rsa_public_exponent(
1179 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001181{
1182 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001184 uint8_t *buffer = NULL;
1185 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001187
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1189 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001190 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 }
1192 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001193 /* It's the default value, which is reported as an empty string,
1194 * so there's nothing to do. */
1195 goto exit;
1196 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001197
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 buflen = mbedtls_mpi_size(&mpi);
1199 buffer = mbedtls_calloc(1, buflen);
1200 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001201 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1202 goto exit;
1203 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1205 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001206 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001208 attributes->domain_parameters = buffer;
1209 attributes->domain_parameters_size = buflen;
1210
1211exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 mbedtls_mpi_free(&mpi);
1213 if (ret != 0) {
1214 mbedtls_free(buffer);
1215 }
1216 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001217}
John Durkop07cc04a2020-11-16 22:08:34 -08001218#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001219 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001220
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001221/** Retrieve all the publicly-accessible attributes of a key.
1222 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001223psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1224 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001225{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001226 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001227 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001228 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001229
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1233 if (status != PSA_SUCCESS) {
1234 return status;
1235 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001236
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001237 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1239 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001240
1241#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1243 psa_set_key_slot_number(attributes,
1244 psa_key_slot_get_slot_number(slot));
1245 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001246#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001247
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001249#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001250 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001251 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001252 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001253 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001254 * is not yet implemented.
1255 * https://github.com/ARMmbed/mbed-crypto/issues/216
1256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001258 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001259
Ronald Cron90857082020-11-25 14:58:33 +01001260 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 slot->attr.type,
1262 slot->key.data,
1263 slot->key.bytes,
1264 &rsa);
1265 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001266 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001268
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 status = psa_get_rsa_public_exponent(rsa,
1270 attributes);
1271 mbedtls_rsa_free(rsa);
1272 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001273 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001274 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001275#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001276 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001277 default:
1278 /* Nothing else to do. */
1279 break;
1280 }
1281
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 if (status != PSA_SUCCESS) {
1283 psa_reset_key_attributes(attributes);
1284 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001285
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001289}
1290
Gilles Peskinec8000c02019-08-02 20:15:51 +02001291#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1292psa_status_t psa_get_key_slot_number(
1293 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001295{
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001297 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 return PSA_SUCCESS;
1299 } else {
1300 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001301 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001302}
1303#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1306 size_t key_buffer_size,
1307 uint8_t *data,
1308 size_t data_size,
1309 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001310{
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 if (key_buffer_size > data_size) {
1312 return PSA_ERROR_BUFFER_TOO_SMALL;
1313 }
1314 memcpy(data, key_buffer, key_buffer_size);
1315 memset(data + key_buffer_size, 0,
1316 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001317 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001319}
1320
Ronald Cron7285cda2020-11-26 14:46:37 +01001321psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001322 const psa_key_attributes_t *attributes,
1323 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001325{
Ronald Crond18b5f82020-11-25 16:46:05 +01001326 psa_key_type_t type = attributes->core.type;
1327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 if (key_type_is_raw_bytes(type) ||
1329 PSA_KEY_TYPE_IS_RSA(type) ||
1330 PSA_KEY_TYPE_IS_ECC(type)) {
1331 return psa_export_key_buffer_internal(
1332 key_buffer, key_buffer_size,
1333 data, data_size, data_length);
1334 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001335 /* This shouldn't happen in the reference implementation, but
1336 it is valid for a special-purpose implementation to omit
1337 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001339 }
1340}
1341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1343 uint8_t *data,
1344 size_t data_size,
1345 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001346{
1347 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1348 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1349 psa_key_slot_t *slot;
1350
Ronald Crone907e552021-01-18 13:22:38 +01001351 /* Reject a zero-length output buffer now, since this can never be a
1352 * valid key representation. This way we know that data must be a valid
1353 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 if (data_size == 0) {
1355 return PSA_ERROR_BUFFER_TOO_SMALL;
1356 }
Ronald Crone907e552021-01-18 13:22:38 +01001357
Ronald Cron9486f9d2020-11-26 13:36:23 +01001358 /* Set the key to empty now, so that even when there are errors, we always
1359 * set data_length to a value between 0 and data_size. On error, setting
1360 * the key to empty is a good choice because an empty key representation is
1361 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001362 *data_length = 0;
1363
Ronald Cron9486f9d2020-11-26 13:36:23 +01001364 /* Export requires the EXPORT flag. There is an exception for public keys,
1365 * which don't require any flag, but
1366 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1367 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1369 PSA_KEY_USAGE_EXPORT, 0);
1370 if (status != PSA_SUCCESS) {
1371 return status;
1372 }
mohammad160306e79202018-03-28 13:17:44 +03001373
Ronald Crond18b5f82020-11-25 16:46:05 +01001374 psa_key_attributes_t attributes = {
1375 .core = slot->attr
1376 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 status = psa_driver_wrapper_export_key(&attributes,
1378 slot->key.data, slot->key.bytes,
1379 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001384}
1385
Ronald Cron7285cda2020-11-26 14:46:37 +01001386psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001387 const psa_key_attributes_t *attributes,
1388 const uint8_t *key_buffer,
1389 size_t key_buffer_size,
1390 uint8_t *data,
1391 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001393{
Ronald Crond18b5f82020-11-25 16:46:05 +01001394 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1397 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001398 /* Exporting public -> public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 return psa_export_key_buffer_internal(
1400 key_buffer, key_buffer_size,
1401 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001402 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001403
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001405#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1407 return mbedtls_psa_rsa_export_public_key(attributes,
1408 key_buffer,
1409 key_buffer_size,
1410 data,
1411 data_size,
1412 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001413#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001414 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001416#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1417 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001419#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1421 return mbedtls_psa_ecp_export_public_key(attributes,
1422 key_buffer,
1423 key_buffer_size,
1424 data,
1425 data_size,
1426 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001427#else
1428 /* We don't know how to convert a private ECC key to public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001430#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1431 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001432 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001434 /* This shouldn't happen in the reference implementation, but
1435 it is valid for a special-purpose implementation to omit
1436 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001438 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001439}
Ronald Crond18b5f82020-11-25 16:46:05 +01001440
Gilles Peskine449bd832023-01-11 14:50:10 +01001441psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1442 uint8_t *data,
1443 size_t data_size,
1444 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001445{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001446 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001447 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001448 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001449
Ronald Crone907e552021-01-18 13:22:38 +01001450 /* Reject a zero-length output buffer now, since this can never be a
1451 * valid key representation. This way we know that data must be a valid
1452 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 if (data_size == 0) {
1454 return PSA_ERROR_BUFFER_TOO_SMALL;
1455 }
Ronald Crone907e552021-01-18 13:22:38 +01001456
Darryl Greendd8fb772018-11-07 16:00:44 +00001457 /* Set the key to empty now, so that even when there are errors, we always
1458 * set data_length to a value between 0 and data_size. On error, setting
1459 * the key to empty is a good choice because an empty key representation is
1460 * unlikely to be accepted anywhere. */
1461 *data_length = 0;
1462
1463 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1465 if (status != PSA_SUCCESS) {
1466 return status;
1467 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001468
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1470 status = PSA_ERROR_INVALID_ARGUMENT;
1471 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001472 }
1473
Ronald Crond18b5f82020-11-25 16:46:05 +01001474 psa_key_attributes_t attributes = {
1475 .core = slot->attr
1476 };
Ronald Cron67227982020-11-26 15:16:05 +01001477 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001478 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001480
1481exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001485}
1486
Gilles Peskine91e8c332019-08-02 19:19:39 +02001487#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001488static_assert((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1489 "One or more key attribute flag is listed as both external-only and dual-use");
1490static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1491 "One or more key attribute flag is listed as both internal-only and dual-use");
1492static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1493 "One or more key attribute flag is listed as both internal-only and external-only");
Gilles Peskine91e8c332019-08-02 19:19:39 +02001494#endif
1495
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001496/** Validate that a key policy is internally well-formed.
1497 *
1498 * This function only rejects invalid policies. It does not validate the
1499 * consistency of the policy with respect to other attributes of the key
1500 * such as the key type.
1501 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001502static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001503{
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1505 PSA_KEY_USAGE_COPY |
1506 PSA_KEY_USAGE_ENCRYPT |
1507 PSA_KEY_USAGE_DECRYPT |
1508 PSA_KEY_USAGE_SIGN_MESSAGE |
1509 PSA_KEY_USAGE_VERIFY_MESSAGE |
1510 PSA_KEY_USAGE_SIGN_HASH |
1511 PSA_KEY_USAGE_VERIFY_HASH |
1512 PSA_KEY_USAGE_VERIFY_DERIVATION |
1513 PSA_KEY_USAGE_DERIVE)) != 0) {
1514 return PSA_ERROR_INVALID_ARGUMENT;
1515 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001518}
1519
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001520/** Validate the internal consistency of key attributes.
1521 *
1522 * This function only rejects invalid attribute values. If does not
1523 * validate the consistency of the attributes with any key data that may
1524 * be involved in the creation of the key.
1525 *
1526 * Call this function early in the key creation process.
1527 *
1528 * \param[in] attributes Key attributes for the new key.
1529 * \param[out] p_drv On any return, the driver for the key, if any.
1530 * NULL for a transparent key.
1531 *
1532 */
1533static psa_status_t psa_validate_key_attributes(
1534 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001536{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001537 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1539 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 status = psa_validate_key_location(lifetime, p_drv);
1542 if (status != PSA_SUCCESS) {
1543 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001544 }
1545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 status = psa_validate_key_persistence(lifetime);
1547 if (status != PSA_SUCCESS) {
1548 return status;
1549 }
1550
1551 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1552 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1553 return PSA_ERROR_INVALID_ARGUMENT;
1554 }
1555 } else {
1556 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1557 return PSA_ERROR_INVALID_ARGUMENT;
1558 }
1559 }
1560
1561 status = psa_validate_key_policy(&attributes->core.policy);
1562 if (status != PSA_SUCCESS) {
1563 return status;
1564 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001565
1566 /* Refuse to create overly large keys.
1567 * Note that this doesn't trigger on import if the attributes don't
1568 * explicitly specify a size (so psa_get_key_bits returns 0), so
1569 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1571 return PSA_ERROR_NOT_SUPPORTED;
1572 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001573
Gilles Peskine91e8c332019-08-02 19:19:39 +02001574 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1576 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1577 return PSA_ERROR_INVALID_ARGUMENT;
1578 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001579
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001581}
1582
Gilles Peskine4747d192019-04-17 15:05:45 +02001583/** Prepare a key slot to receive key material.
1584 *
1585 * This function allocates a key slot and sets its metadata.
1586 *
1587 * If this function fails, call psa_fail_key_creation().
1588 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001589 * This function is intended to be used as follows:
1590 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001591 * it with the specified attributes, and in case of a volatile key assign it
1592 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001593 * -# Populate the slot with the key material.
1594 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1595 * In case of failure at any step, stop the sequence and call
1596 * psa_fail_key_creation().
1597 *
Ronald Cron5c522922020-11-14 16:35:34 +01001598 * On success, the key slot is locked. It is the responsibility of the caller
1599 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001600 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001601 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001602 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001603 * \param[out] p_slot On success, a pointer to the prepared slot.
1604 * \param[out] p_drv On any return, the driver for the key, if any.
1605 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001606 *
1607 * \retval #PSA_SUCCESS
1608 * The key slot is ready to receive key material.
1609 * \return If this function fails, the key slot is an invalid state.
1610 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001611 */
1612static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001613 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001614 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001615 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001617{
1618 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001619 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001620 psa_key_slot_t *slot;
1621
Gilles Peskinedf179142019-07-15 22:02:14 +02001622 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001623 *p_drv = NULL;
1624
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 status = psa_validate_key_attributes(attributes, p_drv);
1626 if (status != PSA_SUCCESS) {
1627 return status;
1628 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001629
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1631 if (status != PSA_SUCCESS) {
1632 return status;
1633 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001634 slot = *p_slot;
1635
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001636 /* We're storing the declared bit-size of the key. It's up to each
1637 * creation mechanism to verify that this information is correct.
1638 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001639 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001640 * is optional (import, copy). In case of a volatile key, assign it the
1641 * volatile key identifier associated to the slot returned to contain its
1642 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001643
1644 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001646#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1647 slot->attr.id = volatile_key_id;
1648#else
1649 slot->attr.id.key_id = volatile_key_id;
1650#endif
1651 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001652
Gilles Peskine91e8c332019-08-02 19:19:39 +02001653 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001654 * external-only flags, query `attributes`. Thanks to the check
1655 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001656 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001657 * may have set. */
1658 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001659
Gilles Peskinecbaff462019-07-12 23:46:04 +02001660#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001661 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001662 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001663 * create the key file in internal storage, create the
1664 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001665 * persistent data. This is done by starting a transaction that will
1666 * encompass these three actions.
1667 * For registering a volatile key, we just need to find an appropriate
1668 * slot number inside the SE. Since the key is designated volatile, creating
1669 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001670 /* The first thing to do is to find a slot number for the new key.
1671 * We save the slot number in persistent storage as part of the
1672 * transaction data. It will be needed to recover if the power
1673 * fails during the key creation process, to clean up on the secure
1674 * element side after restarting. Obtaining a slot number from the
1675 * secure element driver updates its persistent state, but we do not yet
1676 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001677 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001679 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1681 &slot_number);
1682 if (status != PSA_SUCCESS) {
1683 return status;
1684 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001685
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1687 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001688 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001689 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001690 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 status = psa_crypto_save_transaction();
1692 if (status != PSA_SUCCESS) {
1693 (void) psa_crypto_stop_transaction();
1694 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001695 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001696 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001697
1698 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001700 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001701
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001703 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001705 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001706#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1707
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001709}
Gilles Peskine4747d192019-04-17 15:05:45 +02001710
1711/** Finalize the creation of a key once its key material has been set.
1712 *
1713 * This entails writing the key to persistent storage.
1714 *
1715 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001716 * See the documentation of psa_start_key_creation() for the intended use
1717 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001718 *
Ronald Cron5c522922020-11-14 16:35:34 +01001719 * If the finalization succeeds, the function unlocks the key slot (it was
1720 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1721 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001722 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001723 * \param[in,out] slot Pointer to the slot with key material.
1724 * \param[in] driver The secure element driver for the key,
1725 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001726 * \param[out] key On success, identifier of the key. Note that the
1727 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001728 *
1729 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001730 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001731 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1732 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1733 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1734 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1735 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1736 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001737 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001738 * \return If this function fails, the key slot is an invalid state.
1739 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001740 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001741static psa_status_t psa_finish_key_creation(
1742 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001743 psa_se_drv_table_entry_t *driver,
1744 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001745{
1746 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001747 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001748 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001749
1750#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001752#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001754 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001755 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001757
Gilles Peskineb46bef22019-07-30 21:32:04 +02001758#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 static_assert(sizeof(slot_number) ==
1760 sizeof(data.slot_number),
1761 "Slot number size does not match psa_se_key_data_storage_t");
Gilles Peskineb46bef22019-07-30 21:32:04 +02001762#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1764 status = psa_save_persistent_key(&slot->attr,
1765 (uint8_t *) &data,
1766 sizeof(data));
1767 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001768#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1769 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001770 /* Key material is saved in export representation in the slot, so
1771 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 status = psa_save_persistent_key(&slot->attr,
1773 slot->key.data,
1774 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001775 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001776 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001777#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1778
Gilles Peskinecbaff462019-07-12 23:46:04 +02001779#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001780 /* Finish the transaction for a key creation. This does not
1781 * happen when registering an existing key. Detect this case
1782 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001783 * creation of a persistent key in a secure element requires a transaction,
1784 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 if (driver != NULL &&
1786 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1787 status = psa_save_se_persistent_data(driver);
1788 if (status != PSA_SUCCESS) {
1789 psa_destroy_persistent_key(slot->attr.id);
1790 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001791 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001793 }
1794#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1795
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001797 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 status = psa_unlock_key_slot(slot);
1799 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001800 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001802 }
Ronald Cron50972942020-11-14 11:28:25 +01001803
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001805}
1806
1807/** Abort the creation of a key.
1808 *
1809 * You may call this function after calling psa_start_key_creation(),
1810 * or after psa_finish_key_creation() fails. In other circumstances, this
1811 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001812 * See the documentation of psa_start_key_creation() for the intended use
1813 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001814 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001815 * \param[in,out] slot Pointer to the slot with key material.
1816 * \param[in] driver The secure element driver for the key,
1817 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001818 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001819static void psa_fail_key_creation(psa_key_slot_t *slot,
1820 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001821{
Gilles Peskine011e4282019-06-26 18:34:38 +02001822 (void) driver;
1823
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001825 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001827
1828#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001829 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001830 * element, and the failure happened later (when saving metadata
1831 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001832 * element.
1833 * https://github.com/ARMmbed/mbed-crypto/issues/217
1834 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001835
Gilles Peskined7729582019-08-05 15:55:54 +02001836 /* Abort the ongoing transaction if any (there may not be one if
1837 * the creation process failed before starting one, or if the
1838 * key creation is a registration of a key in a secure element).
1839 * Earlier functions must already have done what it takes to undo any
1840 * partial creation. All that's left is to update the transaction data
1841 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001843#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1844
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001846}
1847
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001848/** Validate optional attributes during key creation.
1849 *
1850 * Some key attributes are optional during key creation. If they are
1851 * specified in the attributes structure, check that they are consistent
1852 * with the data in the slot.
1853 *
1854 * This function should be called near the end of key creation, after
1855 * the slot in memory is fully populated but before saving persistent data.
1856 */
1857static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001858 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001860{
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 if (attributes->core.type != 0) {
1862 if (attributes->core.type != slot->attr.type) {
1863 return PSA_ERROR_INVALID_ARGUMENT;
1864 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001865 }
1866
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001868#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1870 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001871 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001872 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001873 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001874
Ronald Cron90857082020-11-25 14:58:33 +01001875 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 slot->attr.type,
1877 slot->key.data,
1878 slot->key.bytes,
1879 &rsa);
1880 if (status != PSA_SUCCESS) {
1881 return status;
1882 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001883
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 mbedtls_mpi_init(&actual);
1885 mbedtls_mpi_init(&required);
1886 ret = mbedtls_rsa_export(rsa,
1887 NULL, NULL, NULL, NULL, &actual);
1888 mbedtls_rsa_free(rsa);
1889 mbedtls_free(rsa);
1890 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001891 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 }
1893 ret = mbedtls_mpi_read_binary(&required,
1894 attributes->domain_parameters,
1895 attributes->domain_parameters_size);
1896 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001897 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 }
1899 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001900 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 }
1902rsa_exit:
1903 mbedtls_mpi_free(&actual);
1904 mbedtls_mpi_free(&required);
1905 if (ret != 0) {
1906 return mbedtls_to_psa_error(ret);
1907 }
1908 } else
John Durkop9814fa22020-11-04 12:28:15 -08001909#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1910 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001911 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001913 }
1914 }
1915
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 if (attributes->core.bits != 0) {
1917 if (attributes->core.bits != slot->attr.bits) {
1918 return PSA_ERROR_INVALID_ARGUMENT;
1919 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001920 }
1921
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001923}
1924
Gilles Peskine449bd832023-01-11 14:50:10 +01001925psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1926 const uint8_t *data,
1927 size_t data_length,
1928 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001929{
1930 psa_status_t status;
1931 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001932 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001933 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301934 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001935
Ronald Cron81709fc2020-11-14 12:10:32 +01001936 *key = MBEDTLS_SVC_KEY_ID_INIT;
1937
Gilles Peskine0f84d622019-09-12 19:03:13 +02001938 /* Reject zero-length symmetric keys (including raw data key objects).
1939 * This also rejects any key which might be encoded as an empty string,
1940 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 if (data_length == 0) {
1942 return PSA_ERROR_INVALID_ARGUMENT;
1943 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001944
Archana449608b2021-09-08 15:36:05 +05301945 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 if (data_length > SIZE_MAX / 8) {
1947 return PSA_ERROR_NOT_SUPPORTED;
1948 }
Archana6ed4bda2021-08-04 10:47:15 +05301949
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1951 &slot, &driver);
1952 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001953 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001955
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001956 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301957 * storage ( thus not in the case of importing a key in a secure element
1958 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1959 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 if (slot->key.data == NULL) {
1961 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301962 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 attributes, data, data_length, &storage_size);
1964 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301965 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 }
Archanad8a83dc2021-06-14 10:04:16 +05301967 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 status = psa_allocate_buffer_to_slot(slot, storage_size);
1969 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001970 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001972 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001973
1974 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 status = psa_driver_wrapper_import_key(attributes,
1976 data, data_length,
1977 slot->key.data,
1978 slot->key.bytes,
1979 &slot->key.bytes, &bits);
1980 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001981 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001983
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001985 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001987 status = PSA_ERROR_INVALID_ARGUMENT;
1988 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001989 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001990
Archana6ed4bda2021-08-04 10:47:15 +05301991 /* Enforce a size limit, and in particular ensure that the bit
1992 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05301994 status = PSA_ERROR_NOT_SUPPORTED;
1995 goto exit;
1996 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 status = psa_validate_optional_attributes(slot, attributes);
1998 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02001999 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002001
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002003exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 if (status != PSA_SUCCESS) {
2005 psa_fail_key_creation(slot, driver);
2006 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002007
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002009}
2010
Gilles Peskined7729582019-08-05 15:55:54 +02002011#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2012psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002014{
2015 psa_status_t status;
2016 psa_key_slot_t *slot = NULL;
2017 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002018 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002019
2020 /* Leaving attributes unspecified is not currently supported.
2021 * It could make sense to query the key type and size from the
2022 * secure element, but not all secure elements support this
2023 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2025 return PSA_ERROR_NOT_SUPPORTED;
2026 }
2027 if (psa_get_key_bits(attributes) == 0) {
2028 return PSA_ERROR_NOT_SUPPORTED;
2029 }
Gilles Peskined7729582019-08-05 15:55:54 +02002030
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2032 &slot, &driver);
2033 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002034 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 }
Gilles Peskined7729582019-08-05 15:55:54 +02002036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002038
2039exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 if (status != PSA_SUCCESS) {
2041 psa_fail_key_creation(slot, driver);
2042 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002043
Gilles Peskined7729582019-08-05 15:55:54 +02002044 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 psa_close_key(key);
2046 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002047}
2048#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2049
Gilles Peskine449bd832023-01-11 14:50:10 +01002050psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2051 const psa_key_attributes_t *specified_attributes,
2052 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002053{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002054 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002055 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002056 psa_key_slot_t *source_slot = NULL;
2057 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002058 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002059 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302060 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002061
Ronald Cron81709fc2020-11-14 12:10:32 +01002062 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2063
Archana8a180362021-07-05 02:18:48 +05302064 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2066 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002067 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002069
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 status = psa_validate_optional_attributes(source_slot,
2071 specified_attributes);
2072 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002073 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002075
Archana9d17bf42021-09-10 06:22:44 +05302076 /* The target key type and number of bits have been validated by
2077 * psa_validate_optional_attributes() to be either equal to zero or
2078 * equal to the ones of the source key. So it is safe to inherit
2079 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302080 * */
Archana9d17bf42021-09-10 06:22:44 +05302081 actual_attributes.core.bits = source_slot->attr.bits;
2082 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302083
2084
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 status = psa_restrict_key_policy(source_slot->attr.type,
2086 &actual_attributes.core.policy,
2087 &source_slot->attr.policy);
2088 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002089 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002091
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2093 &target_slot, &driver);
2094 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002095 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 }
2097 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2098 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302099 /*
Archana9d17bf42021-09-10 06:22:44 +05302100 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302101 * the source key would need to be exported as plaintext and re-imported
2102 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302103 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302104 * appropriate API invocations from the application, if needed.
2105 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002106 status = PSA_ERROR_NOT_SUPPORTED;
2107 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002108 }
Archana8a180362021-07-05 02:18:48 +05302109 /*
2110 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302111 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302112 * - For opaque keys this translates to an invocation of the drivers'
2113 * copy_key entry point through the dispatch layer.
2114 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2116 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2117 &storage_size);
2118 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302119 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 }
Archana374fe5b2021-09-08 15:50:28 +05302121
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2123 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302124 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 }
Archana374fe5b2021-09-08 15:50:28 +05302126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 status = psa_driver_wrapper_copy_key(&actual_attributes,
2128 source_slot->key.data,
2129 source_slot->key.bytes,
2130 target_slot->key.data,
2131 target_slot->key.bytes,
2132 &target_slot->key.bytes);
2133 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302134 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 }
2136 } else {
2137 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302138 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 source_slot->key.bytes);
2140 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302141 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 }
Archana8a180362021-07-05 02:18:48 +05302143 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002145exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 if (status != PSA_SUCCESS) {
2147 psa_fail_key_creation(target_slot, driver);
2148 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002149
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002151
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002153}
2154
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002155
2156
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002157/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002158/* Message digests */
2159/****************************************************************/
2160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002162{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002163 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 if (operation->id == 0) {
2165 return PSA_SUCCESS;
2166 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002167
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002169 operation->id = 0;
2170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002172}
2173
Gilles Peskine449bd832023-01-11 14:50:10 +01002174psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2175 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002176{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002177 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002178
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002179 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002181 status = PSA_ERROR_BAD_STATE;
2182 goto exit;
2183 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002184
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002186 status = PSA_ERROR_INVALID_ARGUMENT;
2187 goto exit;
2188 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002189
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002190 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2191 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002193
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002195
2196exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 if (status != PSA_SUCCESS) {
2198 psa_hash_abort(operation);
2199 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002200
2201 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002202}
2203
Gilles Peskine449bd832023-01-11 14:50:10 +01002204psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2205 const uint8_t *input,
2206 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002207{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002208 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002211 status = PSA_ERROR_BAD_STATE;
2212 goto exit;
2213 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002214
Steven Cooremanc8288352021-03-04 14:02:19 +01002215 /* Don't require hash implementations to behave correctly on a
2216 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 if (input_length == 0) {
2218 return PSA_SUCCESS;
2219 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002222
2223exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 if (status != PSA_SUCCESS) {
2225 psa_hash_abort(operation);
2226 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002227
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002229}
2230
Gilles Peskine449bd832023-01-11 14:50:10 +01002231psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2232 uint8_t *hash,
2233 size_t hash_size,
2234 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002235{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002236 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 if (operation->id == 0) {
2238 return PSA_ERROR_BAD_STATE;
2239 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002240
Steven Cooreman1e582352021-02-18 17:24:37 +01002241 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002242 operation, hash, hash_size, hash_length);
2243 psa_hash_abort(operation);
2244 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002245}
2246
Gilles Peskine449bd832023-01-11 14:50:10 +01002247psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2248 const uint8_t *hash,
2249 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002250{
Ronald Cron69a63422021-10-18 09:47:58 +02002251 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002252 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002253 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 operation,
2255 actual_hash, sizeof(actual_hash),
2256 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002257
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002259 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002261
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002263 status = PSA_ERROR_INVALID_SIGNATURE;
2264 goto exit;
2265 }
2266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002268 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002270
2271exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2273 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002274 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002276
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002278}
2279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280psa_status_t psa_hash_compute(psa_algorithm_t alg,
2281 const uint8_t *input, size_t input_length,
2282 uint8_t *hash, size_t hash_size,
2283 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002284{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002285 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 if (!PSA_ALG_IS_HASH(alg)) {
2287 return PSA_ERROR_INVALID_ARGUMENT;
2288 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2291 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002292}
2293
Gilles Peskine449bd832023-01-11 14:50:10 +01002294psa_status_t psa_hash_compare(psa_algorithm_t alg,
2295 const uint8_t *input, size_t input_length,
2296 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002297{
Ronald Cron69a63422021-10-18 09:47:58 +02002298 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002299 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002300
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 if (!PSA_ALG_IS_HASH(alg)) {
2302 return PSA_ERROR_INVALID_ARGUMENT;
2303 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002304
Steven Cooreman1e582352021-02-18 17:24:37 +01002305 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 alg, input, input_length,
2307 actual_hash, sizeof(actual_hash),
2308 &actual_hash_length);
2309 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002310 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 }
2312 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002313 status = PSA_ERROR_INVALID_SIGNATURE;
2314 goto exit;
2315 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002317 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002319
2320exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2322 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002323}
2324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2326 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002327{
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 if (source_operation->id == 0 ||
2329 target_operation->id != 0) {
2330 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002331 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2334 target_operation);
2335 if (status != PSA_SUCCESS) {
2336 psa_hash_abort(target_operation);
2337 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002340}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002341
2342
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002343/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002344/* MAC */
2345/****************************************************************/
2346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002348{
Steven Cooremane6804192021-03-19 18:28:56 +01002349 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 if (operation->id == 0) {
2351 return PSA_SUCCESS;
2352 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002353
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002355 operation->mac_size = 0;
2356 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002357 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002358
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002360}
2361
Ronald Cron2dff3b22021-06-17 16:33:22 +02002362static psa_status_t psa_mac_finalize_alg_and_key_validation(
2363 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002364 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002366{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002367 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 psa_key_type_t key_type = psa_get_key_type(attributes);
2369 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002370
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 if (!PSA_ALG_IS_MAC(alg)) {
2372 return PSA_ERROR_INVALID_ARGUMENT;
2373 }
Steven Cooremane6804192021-03-19 18:28:56 +01002374
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002375 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 status = psa_mac_key_can_do(alg, key_type);
2377 if (status != PSA_SUCCESS) {
2378 return status;
2379 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002380
Steven Cooremand1a68f12021-05-10 11:13:41 +02002381 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002383
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002385 /* A very short MAC is too short for security since it can be
2386 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2387 * so we make this our minimum, even though 32 bits is still
2388 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002390 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002391
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2393 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002394 /* It's impossible to "truncate" to a larger length than the full length
2395 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002397 }
2398
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002400 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2401 * that is disabled in the compile-time configuration. The result can
2402 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2403 * configuration into account. In this case, force a return of
2404 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2405 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2406 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2407 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2408 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002410 }
2411
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002413}
2414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2416 mbedtls_svc_key_id_t key,
2417 psa_algorithm_t alg,
2418 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002419{
2420 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2421 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002422 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002423
2424 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002426 status = PSA_ERROR_BAD_STATE;
2427 goto exit;
2428 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002429
2430 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 key,
2432 &slot,
2433 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2434 alg);
2435 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002436 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002438
2439 psa_key_attributes_t attributes = {
2440 .core = slot->attr
2441 };
2442
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2444 &operation->mac_size);
2445 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002446 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002448
2449 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002450 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 if (is_sign) {
2452 status = psa_driver_wrapper_mac_sign_setup(operation,
2453 &attributes,
2454 slot->key.data,
2455 slot->key.bytes,
2456 alg);
2457 } else {
2458 status = psa_driver_wrapper_mac_verify_setup(operation,
2459 &attributes,
2460 slot->key.data,
2461 slot->key.bytes,
2462 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002463 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002464
Gilles Peskinefbfac682018-07-08 20:51:54 +02002465exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 if (status != PSA_SUCCESS) {
2467 psa_mac_abort(operation);
2468 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002469
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002471
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002473}
2474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2476 mbedtls_svc_key_id_t key,
2477 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002478{
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002480}
2481
Gilles Peskine449bd832023-01-11 14:50:10 +01002482psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2483 mbedtls_svc_key_id_t key,
2484 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002485{
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002487}
2488
Gilles Peskine449bd832023-01-11 14:50:10 +01002489psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2490 const uint8_t *input,
2491 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002492{
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 if (operation->id == 0) {
2494 return PSA_ERROR_BAD_STATE;
2495 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002496
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002497 /* Don't require hash implementations to behave correctly on a
2498 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 if (input_length == 0) {
2500 return PSA_SUCCESS;
2501 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2504 input, input_length);
2505 if (status != PSA_SUCCESS) {
2506 psa_mac_abort(operation);
2507 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002508
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002510}
2511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2513 uint8_t *mac,
2514 size_t mac_size,
2515 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002516{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002517 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2518 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002521 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002522 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002523 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002524
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002526 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002527 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002528 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002529
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002530 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2531 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002533 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002534 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002535 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002536
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002538 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002539 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002540 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002541
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 status = psa_driver_wrapper_mac_sign_finish(operation,
2543 mac, operation->mac_size,
2544 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002545
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002546exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002547 /* In case of success, set the potential excess room in the output buffer
2548 * to an invalid value, to avoid potentially leaking a longer MAC.
2549 * In case of error, set the output length and content to a safe default,
2550 * such that in case the caller misses an error check, the output would be
2551 * an unachievable MAC.
2552 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002554 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002555 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002556 }
mohammad16036df908f2018-04-02 08:34:15 -07002557
Paul Elliottdc42ca82023-02-24 18:11:59 +00002558 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002561
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002563}
2564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2566 const uint8_t *mac,
2567 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002568{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002569 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2570 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002573 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002574 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002575 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002576
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002578 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002579 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002580 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002583 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002584 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002585 }
2586
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 status = psa_driver_wrapper_mac_verify_finish(operation,
2588 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002589
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002590exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002592
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002594}
2595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2597 psa_algorithm_t alg,
2598 const uint8_t *input,
2599 size_t input_length,
2600 uint8_t *mac,
2601 size_t mac_size,
2602 size_t *mac_length,
2603 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002604{
2605 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002606 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2607 psa_key_slot_t *slot;
2608 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002609
Ronald Cron51131b52021-06-17 17:17:20 +02002610 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 key,
2612 &slot,
2613 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2614 alg);
2615 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002616 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002618
Ronald Cron51131b52021-06-17 17:17:20 +02002619 psa_key_attributes_t attributes = {
2620 .core = slot->attr
2621 };
2622
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2624 &operation_mac_size);
2625 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002626 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002630 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002631 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002632 }
2633
2634 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 &attributes,
2636 slot->key.data, slot->key.bytes,
2637 alg,
2638 input, input_length,
2639 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002640
2641exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002642 /* In case of success, set the potential excess room in the output buffer
2643 * to an invalid value, to avoid potentially leaking a longer MAC.
2644 * In case of error, set the output length and content to a safe default,
2645 * such that in case the caller misses an error check, the output would be
2646 * an unachievable MAC.
2647 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002649 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002650 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002651 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002652
2653 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002656
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002658}
2659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002661 psa_algorithm_t alg,
2662 const uint8_t *input,
2663 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 uint8_t *mac,
2665 size_t mac_size,
2666 size_t *mac_length)
2667{
2668 return psa_mac_compute_internal(key, alg,
2669 input, input_length,
2670 mac, mac_size, mac_length, 1);
2671}
2672
2673psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2674 psa_algorithm_t alg,
2675 const uint8_t *input,
2676 size_t input_length,
2677 const uint8_t *mac,
2678 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002679{
2680 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002681 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2682 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 status = psa_mac_compute_internal(key, alg,
2685 input, input_length,
2686 actual_mac, sizeof(actual_mac),
2687 &actual_mac_length, 0);
2688 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002689 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002691
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002693 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002694 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002695 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
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 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002700
2701exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002703
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002705}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002706
Gilles Peskinee59236f2018-01-27 23:32:46 +01002707/****************************************************************/
2708/* Asymmetric cryptography */
2709/****************************************************************/
2710
Gilles Peskine449bd832023-01-11 14:50:10 +01002711static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2712 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002713{
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 if (input_is_message) {
2715 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2716 return PSA_ERROR_INVALID_ARGUMENT;
2717 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002718
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2720 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2721 return PSA_ERROR_INVALID_ARGUMENT;
2722 }
2723 }
2724 } else {
2725 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2726 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002727 }
2728 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002729
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002731}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002732
Gilles Peskine449bd832023-01-11 14:50:10 +01002733static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2734 int input_is_message,
2735 psa_algorithm_t alg,
2736 const uint8_t *input,
2737 size_t input_length,
2738 uint8_t *signature,
2739 size_t signature_size,
2740 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002741{
2742 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2743 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2744 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002745
2746 *signature_length = 0;
2747
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 status = psa_sign_verify_check_alg(input_is_message, alg);
2749 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002750 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002752
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002753 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002754 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002755 * buffer can in principle be empty since it doesn't actually have
2756 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 if (signature_size == 0) {
2758 return PSA_ERROR_BUFFER_TOO_SMALL;
2759 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002760
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002761 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002762 key, &slot,
2763 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2764 PSA_KEY_USAGE_SIGN_HASH,
2765 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002766
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002768 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002770
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002772 status = PSA_ERROR_INVALID_ARGUMENT;
2773 goto exit;
2774 }
2775
2776 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002778 };
2779
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002781 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002782 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002783 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 signature, signature_size, signature_length);
2785 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002786
2787 status = psa_driver_wrapper_sign_hash(
2788 &attributes, slot->key.data, slot->key.bytes,
2789 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002791 }
2792
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002793
2794exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002795 psa_wipe_tag_output_buffer(signature, status, signature_size,
2796 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002797
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002799
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002801}
2802
Gilles Peskine449bd832023-01-11 14:50:10 +01002803static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2804 int input_is_message,
2805 psa_algorithm_t alg,
2806 const uint8_t *input,
2807 size_t input_length,
2808 const uint8_t *signature,
2809 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002810{
2811 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2812 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2813 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002814
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 status = psa_sign_verify_check_alg(input_is_message, alg);
2816 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002817 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002819
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002820 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 key, &slot,
2822 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2823 PSA_KEY_USAGE_VERIFY_HASH,
2824 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002825
Gilles Peskine449bd832023-01-11 14:50:10 +01002826 if (status != PSA_SUCCESS) {
2827 return status;
2828 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002829
2830 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002831 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002832 };
2833
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002835 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002836 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002837 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 signature, signature_length);
2839 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002840 status = psa_driver_wrapper_verify_hash(
2841 &attributes, slot->key.data, slot->key.bytes,
2842 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002844 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002845
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002847
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002849
2850}
2851
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002852psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002853 const psa_key_attributes_t *attributes,
2854 const uint8_t *key_buffer,
2855 size_t key_buffer_size,
2856 psa_algorithm_t alg,
2857 const uint8_t *input,
2858 size_t input_length,
2859 uint8_t *signature,
2860 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002862{
2863 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002864
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002866 size_t hash_length;
2867 uint8_t hash[PSA_HASH_MAX_SIZE];
2868
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002869 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 PSA_ALG_SIGN_GET_HASH(alg),
2871 input, input_length,
2872 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002873
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002875 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002877
2878 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 attributes, key_buffer, key_buffer_size,
2880 alg, hash, hash_length,
2881 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002882 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002883
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002885}
2886
Gilles Peskine449bd832023-01-11 14:50:10 +01002887psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2888 psa_algorithm_t alg,
2889 const uint8_t *input,
2890 size_t input_length,
2891 uint8_t *signature,
2892 size_t signature_size,
2893 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002894{
2895 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002896 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002898}
2899
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002900psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002901 const psa_key_attributes_t *attributes,
2902 const uint8_t *key_buffer,
2903 size_t key_buffer_size,
2904 psa_algorithm_t alg,
2905 const uint8_t *input,
2906 size_t input_length,
2907 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002908 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002909{
2910 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002911
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002913 size_t hash_length;
2914 uint8_t hash[PSA_HASH_MAX_SIZE];
2915
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002916 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 PSA_ALG_SIGN_GET_HASH(alg),
2918 input, input_length,
2919 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002920
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002922 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002924
2925 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 attributes, key_buffer, key_buffer_size,
2927 alg, hash, hash_length,
2928 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002929 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002930
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002932}
2933
Gilles Peskine449bd832023-01-11 14:50:10 +01002934psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2935 psa_algorithm_t alg,
2936 const uint8_t *input,
2937 size_t input_length,
2938 const uint8_t *signature,
2939 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002940{
2941 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002942 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002944}
2945
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002946psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002947 const psa_key_attributes_t *attributes,
2948 const uint8_t *key_buffer, size_t key_buffer_size,
2949 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002951{
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2953 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2954 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002955#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2957 return mbedtls_psa_rsa_sign_hash(
2958 attributes,
2959 key_buffer, key_buffer_size,
2960 alg, hash, hash_length,
2961 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002962#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2963 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 } else {
2965 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002966 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2968 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002969#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2971 return mbedtls_psa_ecdsa_sign_hash(
2972 attributes,
2973 key_buffer, key_buffer_size,
2974 alg, hash, hash_length,
2975 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002976#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2977 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 } else {
2979 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002980 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002981 }
Ronald Cron4501c982021-03-19 20:09:32 +01002982
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 (void) key_buffer;
2984 (void) key_buffer_size;
2985 (void) hash;
2986 (void) hash_length;
2987 (void) signature;
2988 (void) signature_size;
2989 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002990
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002992}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002993
Gilles Peskine449bd832023-01-11 14:50:10 +01002994psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2995 psa_algorithm_t alg,
2996 const uint8_t *hash,
2997 size_t hash_length,
2998 uint8_t *signature,
2999 size_t signature_size,
3000 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003001{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003002 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003003 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003005}
3006
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003007psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003008 const psa_key_attributes_t *attributes,
3009 const uint8_t *key_buffer, size_t key_buffer_size,
3010 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003012{
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3014 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3015 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003016#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3018 return mbedtls_psa_rsa_verify_hash(
3019 attributes,
3020 key_buffer, key_buffer_size,
3021 alg, hash, hash_length,
3022 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003023#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3024 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 } else {
3026 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003027 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3029 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003030#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3032 return mbedtls_psa_ecdsa_verify_hash(
3033 attributes,
3034 key_buffer, key_buffer_size,
3035 alg, hash, hash_length,
3036 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003037#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3038 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 } else {
3040 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003041 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003042 }
Ronald Cron4501c982021-03-19 20:09:32 +01003043
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 (void) key_buffer;
3045 (void) key_buffer_size;
3046 (void) hash;
3047 (void) hash_length;
3048 (void) signature;
3049 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003050
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003052}
3053
Gilles Peskine449bd832023-01-11 14:50:10 +01003054psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3055 psa_algorithm_t alg,
3056 const uint8_t *hash,
3057 size_t hash_length,
3058 const uint8_t *signature,
3059 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003060{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003061 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003062 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003064}
3065
Gilles Peskine449bd832023-01-11 14:50:10 +01003066psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3067 psa_algorithm_t alg,
3068 const uint8_t *input,
3069 size_t input_length,
3070 const uint8_t *salt,
3071 size_t salt_length,
3072 uint8_t *output,
3073 size_t output_size,
3074 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003075{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003076 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003077 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003078 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003079
Darryl Green5cc689a2018-07-24 15:34:10 +01003080 (void) input;
3081 (void) input_length;
3082 (void) salt;
3083 (void) output;
3084 (void) output_size;
3085
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003086 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003087
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3089 return PSA_ERROR_INVALID_ARGUMENT;
3090 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003091
Ronald Cron5c522922020-11-14 16:35:34 +01003092 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3094 if (status != PSA_SUCCESS) {
3095 return status;
3096 }
3097 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3098 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003099 status = PSA_ERROR_INVALID_ARGUMENT;
3100 goto exit;
3101 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003102
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003103 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003104 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003105 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003106
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003107 status = psa_driver_wrapper_asymmetric_encrypt(
3108 &attributes, slot->key.data, slot->key.bytes,
3109 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003111exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003113
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003115}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003116
Gilles Peskine449bd832023-01-11 14:50:10 +01003117psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3118 psa_algorithm_t alg,
3119 const uint8_t *input,
3120 size_t input_length,
3121 const uint8_t *salt,
3122 size_t salt_length,
3123 uint8_t *output,
3124 size_t output_size,
3125 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003126{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003127 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003128 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003129 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003130
Darryl Green5cc689a2018-07-24 15:34:10 +01003131 (void) input;
3132 (void) input_length;
3133 (void) salt;
3134 (void) output;
3135 (void) output_size;
3136
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003137 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003138
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3140 return PSA_ERROR_INVALID_ARGUMENT;
3141 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003142
Ronald Cron5c522922020-11-14 16:35:34 +01003143 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3145 if (status != PSA_SUCCESS) {
3146 return status;
3147 }
3148 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003149 status = PSA_ERROR_INVALID_ARGUMENT;
3150 goto exit;
3151 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003152
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003153 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003155 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003156
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003157 status = psa_driver_wrapper_asymmetric_decrypt(
3158 &attributes, slot->key.data, slot->key.bytes,
3159 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003161
3162exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003164
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003166}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003167
Paul Elliott9fe12f62022-11-30 19:16:02 +00003168/****************************************************************/
3169/* Asymmetric interruptible cryptography */
3170/****************************************************************/
3171
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003172static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3173
Paul Elliott9fe12f62022-11-30 19:16:02 +00003174void psa_interruptible_set_max_ops(uint32_t max_ops)
3175{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003176 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003177}
3178
3179uint32_t psa_interruptible_get_max_ops(void)
3180{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003181 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003182}
3183
Paul Elliott9fe12f62022-11-30 19:16:02 +00003184uint32_t psa_sign_hash_get_num_ops(
3185 const psa_sign_hash_interruptible_operation_t *operation)
3186{
Paul Elliott296ede92022-12-15 17:00:30 +00003187 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003188}
3189
3190uint32_t psa_verify_hash_get_num_ops(
3191 const psa_verify_hash_interruptible_operation_t *operation)
3192{
Paul Elliott296ede92022-12-15 17:00:30 +00003193 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003194}
3195
Paul Elliott59ad9452022-12-18 15:09:02 +00003196static psa_status_t psa_sign_hash_abort_internal(
3197 psa_sign_hash_interruptible_operation_t *operation)
3198{
3199 if (operation->id == 0) {
3200 /* The object has (apparently) been initialized but it is not (yet)
3201 * in use. It's ok to call abort on such an object, and there's
3202 * nothing to do. */
3203 return PSA_SUCCESS;
3204 }
3205
3206 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3207
3208 status = psa_driver_wrapper_sign_hash_abort(operation);
3209
3210 operation->id = 0;
3211
Paul Elliotte6145dc2023-02-07 12:51:21 +00003212 /* Do not clear either the error_occurred or num_ops elements here as they
3213 * only want to be cleared by the application calling abort, not by abort
3214 * being called at completion of an operation. */
3215
Paul Elliott59ad9452022-12-18 15:09:02 +00003216 return status;
3217}
3218
Paul Elliott9fe12f62022-11-30 19:16:02 +00003219psa_status_t psa_sign_hash_start(
3220 psa_sign_hash_interruptible_operation_t *operation,
3221 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3222 const uint8_t *hash, size_t hash_length)
3223{
3224 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3225 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3226 psa_key_slot_t *slot;
3227
Paul Elliottc9774412023-02-06 15:14:07 +00003228 /* Check that start has not been previously called, or operation has not
3229 * previously errored. */
3230 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003231 return PSA_ERROR_BAD_STATE;
3232 }
3233
Paul Elliott9fe12f62022-11-30 19:16:02 +00003234 status = psa_sign_verify_check_alg(0, alg);
3235 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003236 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003237 return status;
3238 }
3239
3240 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3241 PSA_KEY_USAGE_SIGN_HASH,
3242 alg);
3243
3244 if (status != PSA_SUCCESS) {
3245 goto exit;
3246 }
3247
3248 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3249 status = PSA_ERROR_INVALID_ARGUMENT;
3250 goto exit;
3251 }
3252
3253 psa_key_attributes_t attributes = {
3254 .core = slot->attr
3255 };
3256
Paul Elliott296ede92022-12-15 17:00:30 +00003257 /* Ensure ops count gets reset, in case of operation re-use. */
3258 operation->num_ops = 0;
3259
Paul Elliott9fe12f62022-11-30 19:16:02 +00003260 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3261 slot->key.data,
3262 slot->key.bytes, alg,
3263 hash, hash_length);
3264exit:
3265
3266 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003267 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003268 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003269 }
3270
3271 unlock_status = psa_unlock_key_slot(slot);
3272
Paul Elliottc9774412023-02-06 15:14:07 +00003273 if (unlock_status != PSA_SUCCESS) {
3274 operation->error_occurred = 1;
3275 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003276
Paul Elliottc9774412023-02-06 15:14:07 +00003277 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003278}
3279
3280
3281psa_status_t psa_sign_hash_complete(
3282 psa_sign_hash_interruptible_operation_t *operation,
3283 uint8_t *signature, size_t signature_size,
3284 size_t *signature_length)
3285{
3286 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3287
3288 *signature_length = 0;
3289
Paul Elliottc9774412023-02-06 15:14:07 +00003290 /* Check that start has been called first, and that operation has not
3291 * previously errored. */
3292 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003293 status = PSA_ERROR_BAD_STATE;
3294 goto exit;
3295 }
3296
Paul Elliott096abc42023-02-03 18:33:23 +00003297 /* Immediately reject a zero-length signature buffer. This guarantees that
3298 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003299 if (signature_size == 0) {
3300 status = PSA_ERROR_BUFFER_TOO_SMALL;
3301 goto exit;
3302 }
3303
3304 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3305 signature_size,
3306 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003307
Paul Elliott296ede92022-12-15 17:00:30 +00003308 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003309 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003310
Paul Elliottebe225c2023-02-10 14:32:53 +00003311exit:
3312
Paul Elliott59200a22023-02-21 15:07:40 +00003313 psa_wipe_tag_output_buffer(signature, status, signature_size,
3314 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003315
Paul Elliott53bb3122023-02-10 14:22:22 +00003316 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003317 if (status != PSA_SUCCESS) {
3318 operation->error_occurred = 1;
3319 }
3320
Paul Elliott59ad9452022-12-18 15:09:02 +00003321 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003322 }
3323
3324 return status;
3325}
3326
3327psa_status_t psa_sign_hash_abort(
3328 psa_sign_hash_interruptible_operation_t *operation)
3329{
Paul Elliott59ad9452022-12-18 15:09:02 +00003330 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3331
3332 status = psa_sign_hash_abort_internal(operation);
3333
3334 /* We clear the number of ops done here, so that it is not cleared when
3335 * the operation fails or succeeds, only on manual abort. */
3336 operation->num_ops = 0;
3337
Paul Elliottc9774412023-02-06 15:14:07 +00003338 /* Likewise, failure state. */
3339 operation->error_occurred = 0;
3340
Paul Elliott59ad9452022-12-18 15:09:02 +00003341 return status;
3342}
3343
3344static psa_status_t psa_verify_hash_abort_internal(
3345 psa_verify_hash_interruptible_operation_t *operation)
3346{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003347 if (operation->id == 0) {
3348 /* The object has (apparently) been initialized but it is not (yet)
3349 * in use. It's ok to call abort on such an object, and there's
3350 * nothing to do. */
3351 return PSA_SUCCESS;
3352 }
3353
Paul Elliott59ad9452022-12-18 15:09:02 +00003354 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3355
3356 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003357
3358 operation->id = 0;
3359
Paul Elliotte6145dc2023-02-07 12:51:21 +00003360 /* Do not clear either the error_occurred or num_ops elements here as they
3361 * only want to be cleared by the application calling abort, not by abort
3362 * being called at completion of an operation. */
3363
Paul Elliott59ad9452022-12-18 15:09:02 +00003364 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003365}
3366
3367psa_status_t psa_verify_hash_start(
3368 psa_verify_hash_interruptible_operation_t *operation,
3369 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3370 const uint8_t *hash, size_t hash_length,
3371 const uint8_t *signature, size_t signature_length)
3372{
3373 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3374 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3375 psa_key_slot_t *slot;
3376
Paul Elliottc9774412023-02-06 15:14:07 +00003377 /* Check that start has not been previously called, or operation has not
3378 * previously errored. */
3379 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003380 return PSA_ERROR_BAD_STATE;
3381 }
3382
3383 status = psa_sign_verify_check_alg(0, alg);
3384 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003385 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003386 return status;
3387 }
3388
3389 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3390 PSA_KEY_USAGE_VERIFY_HASH,
3391 alg);
3392
3393 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003394 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003395 return status;
3396 }
3397
3398 psa_key_attributes_t attributes = {
3399 .core = slot->attr
3400 };
3401
Paul Elliott296ede92022-12-15 17:00:30 +00003402 /* Ensure ops count gets reset, in case of operation re-use. */
3403 operation->num_ops = 0;
3404
Paul Elliott9fe12f62022-11-30 19:16:02 +00003405 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3406 slot->key.data,
3407 slot->key.bytes,
3408 alg, hash, hash_length,
3409 signature, signature_length);
3410
3411 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003412 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003413 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003414 }
3415
3416 unlock_status = psa_unlock_key_slot(slot);
3417
Paul Elliottc9774412023-02-06 15:14:07 +00003418 if (unlock_status != PSA_SUCCESS) {
3419 operation->error_occurred = 1;
3420 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003421
Paul Elliottc9774412023-02-06 15:14:07 +00003422 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003423}
3424
3425psa_status_t psa_verify_hash_complete(
3426 psa_verify_hash_interruptible_operation_t *operation)
3427{
3428 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3429
Paul Elliottc9774412023-02-06 15:14:07 +00003430 /* Check that start has been called first, and that operation has not
3431 * previously errored. */
3432 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003433 status = PSA_ERROR_BAD_STATE;
3434 goto exit;
3435 }
3436
3437 status = psa_driver_wrapper_verify_hash_complete(operation);
3438
Paul Elliott296ede92022-12-15 17:00:30 +00003439 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003440 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003441 operation);
3442
Paul Elliottebe225c2023-02-10 14:32:53 +00003443exit:
3444
Paul Elliott9fe12f62022-11-30 19:16:02 +00003445 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003446 if (status != PSA_SUCCESS) {
3447 operation->error_occurred = 1;
3448 }
3449
Paul Elliott59ad9452022-12-18 15:09:02 +00003450 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003451 }
3452
3453 return status;
3454}
3455
3456psa_status_t psa_verify_hash_abort(
3457 psa_verify_hash_interruptible_operation_t *operation)
3458{
Paul Elliott59ad9452022-12-18 15:09:02 +00003459 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003460
Paul Elliott59ad9452022-12-18 15:09:02 +00003461 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003462
Paul Elliott59ad9452022-12-18 15:09:02 +00003463 /* We clear the number of ops done here, so that it is not cleared when
3464 * the operation fails or succeeds, only on manual abort. */
3465 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003466
Paul Elliottc9774412023-02-06 15:14:07 +00003467 /* Likewise, failure state. */
3468 operation->error_occurred = 0;
3469
Paul Elliott59ad9452022-12-18 15:09:02 +00003470 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003471}
3472
Paul Elliott588f8ed2022-12-02 18:10:26 +00003473/****************************************************************/
3474/* Asymmetric interruptible cryptography internal */
3475/* implementations */
3476/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003477
Paul Elliott588f8ed2022-12-02 18:10:26 +00003478void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3479{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003480
Paul Elliott588f8ed2022-12-02 18:10:26 +00003481#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3482 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3483 defined(MBEDTLS_ECP_RESTARTABLE)
3484
3485 /* Internal implementation uses zero to indicate infinite number max ops,
3486 * therefore avoid this value, and set to minimum possible. */
3487 if (max_ops == 0) {
3488 max_ops = 1;
3489 }
3490
Paul Elliott588f8ed2022-12-02 18:10:26 +00003491 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003492#else
3493 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003494#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3495 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3496 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3497}
3498
Paul Elliott588f8ed2022-12-02 18:10:26 +00003499uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003500 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003501{
3502#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3503 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3504 defined(MBEDTLS_ECP_RESTARTABLE)
3505
Paul Elliott93d9ca82023-02-15 18:14:21 +00003506 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003507#else
3508 (void) operation;
3509 return 0;
3510#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3511 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3512 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3513}
3514
3515uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003516 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003517{
3518 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3519 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3520 defined(MBEDTLS_ECP_RESTARTABLE)
3521
Paul Elliott93d9ca82023-02-15 18:14:21 +00003522 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003523#else
3524 (void) operation;
3525 return 0;
3526#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3527 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3528 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3529}
3530
3531psa_status_t mbedtls_psa_sign_hash_start(
3532 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3533 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3534 size_t key_buffer_size, psa_algorithm_t alg,
3535 const uint8_t *hash, size_t hash_length)
3536{
3537 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003538 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003539
Paul Elliott068fe072023-01-16 13:59:15 +00003540 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3541 return PSA_ERROR_NOT_SUPPORTED;
3542 }
3543
3544 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003545 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003546 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003547
3548#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003549 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3550 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003551
Paul Elliotta1c94092023-02-15 16:38:04 +00003552 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3553
Paul Elliott93d9ca82023-02-15 18:14:21 +00003554 /* Ensure num_ops is zero'ed in case of context re-use. */
3555 operation->num_ops = 0;
3556
Paul Elliott068fe072023-01-16 13:59:15 +00003557 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3558 attributes->core.bits,
3559 key_buffer,
3560 key_buffer_size,
3561 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003562
Paul Elliott068fe072023-01-16 13:59:15 +00003563 if (status != PSA_SUCCESS) {
3564 return status;
3565 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003566
Paul Elliott1bc59df2023-02-05 13:41:57 +00003567 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003568 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003569
Paul Elliott068fe072023-01-16 13:59:15 +00003570 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3571 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3572 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003573
Paul Elliott0290a762023-02-09 14:30:24 +00003574 /* We only need to store the same length of hash as the private key size
3575 * here, it would be truncated by the internal implementation anyway. */
3576 required_hash_length = (hash_length < operation->coordinate_bytes ?
3577 hash_length : operation->coordinate_bytes);
3578
Paul Elliottba70ad42023-02-15 18:23:53 +00003579 if (required_hash_length > sizeof(operation->hash)) {
3580 /* Shouldn't happen, but better safe than sorry. */
3581 return PSA_ERROR_CORRUPTION_DETECTED;
3582 }
3583
Paul Elliott0290a762023-02-09 14:30:24 +00003584 memcpy(operation->hash, hash, required_hash_length);
3585 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003586
3587 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003588
3589#else
Paul Elliott068fe072023-01-16 13:59:15 +00003590 (void) operation;
3591 (void) key_buffer;
3592 (void) key_buffer_size;
3593 (void) alg;
3594 (void) hash;
3595 (void) hash_length;
3596 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003597 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003598
Paul Elliott068fe072023-01-16 13:59:15 +00003599 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003600#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3601 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3602 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003603}
3604
3605psa_status_t mbedtls_psa_sign_hash_complete(
3606 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3607 uint8_t *signature, size_t signature_size,
3608 size_t *signature_length)
3609{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003610#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3611 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3612 defined(MBEDTLS_ECP_RESTARTABLE)
3613
Paul Elliotta1c94092023-02-15 16:38:04 +00003614 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003615 mbedtls_mpi r;
3616 mbedtls_mpi s;
3617
Paul Elliott46845252023-02-03 14:59:11 +00003618 mbedtls_mpi_init(&r);
3619 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003620
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003621 /* Ensure max_ops is set to the current value (or default). */
3622 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3623
Paul Elliotta1c94092023-02-15 16:38:04 +00003624 if (signature_size < 2 * operation->coordinate_bytes) {
3625 status = PSA_ERROR_BUFFER_TOO_SMALL;
3626 goto exit;
3627 }
3628
Paul Elliott588f8ed2022-12-02 18:10:26 +00003629 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003630
Paul Elliott588f8ed2022-12-02 18:10:26 +00003631#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3632 status = mbedtls_to_psa_error(
3633 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003634 &r,
3635 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003636 &operation->ctx->d,
3637 operation->hash,
3638 operation->hash_length,
3639 operation->md_alg,
3640 mbedtls_psa_get_random,
3641 MBEDTLS_PSA_RANDOM_STATE,
3642 &operation->restart_ctx));
3643#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003644 status = PSA_ERROR_NOT_SUPPORTED;
3645 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003646#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3647 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003648 status = mbedtls_to_psa_error(
3649 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003650 &r,
3651 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003652 &operation->ctx->d,
3653 operation->hash,
3654 operation->hash_length,
3655 mbedtls_psa_get_random,
3656 MBEDTLS_PSA_RANDOM_STATE,
3657 mbedtls_psa_get_random,
3658 MBEDTLS_PSA_RANDOM_STATE,
3659 &operation->restart_ctx));
3660 }
3661
Paul Elliottf8e5b562023-02-19 18:43:45 +00003662 /* Hide the fact that the restart context only holds a delta of number of
3663 * ops done during the last operation, not an absolute value. */
3664 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3665
Paul Elliott724bd252023-02-08 12:35:08 +00003666 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003667 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003668 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003669 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003670 operation->coordinate_bytes)
3671 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003672
3673 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003674 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003675 }
3676
3677 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003678 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003679 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003680 operation->coordinate_bytes,
3681 operation->coordinate_bytes)
3682 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003683
3684 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003685 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003686 }
3687
Paul Elliott1bc59df2023-02-05 13:41:57 +00003688 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003689
Paul Elliott724bd252023-02-08 12:35:08 +00003690 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003691 }
Paul Elliott724bd252023-02-08 12:35:08 +00003692
3693exit:
3694
3695 mbedtls_mpi_free(&r);
3696 mbedtls_mpi_free(&s);
3697 return status;
3698
Paul Elliott588f8ed2022-12-02 18:10:26 +00003699 #else
3700
3701 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003702 (void) signature;
3703 (void) signature_size;
3704 (void) signature_length;
3705
3706 return PSA_ERROR_NOT_SUPPORTED;
3707
3708#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3709 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3710 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3711}
3712
3713psa_status_t mbedtls_psa_sign_hash_abort(
3714 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3715{
3716
3717#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3718 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3719 defined(MBEDTLS_ECP_RESTARTABLE)
3720
3721 if (operation->ctx) {
3722 mbedtls_ecdsa_free(operation->ctx);
3723 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003724 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003725 }
3726
3727 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3728
Paul Elliott93d9ca82023-02-15 18:14:21 +00003729 operation->num_ops = 0;
3730
Paul Elliott588f8ed2022-12-02 18:10:26 +00003731 return PSA_SUCCESS;
3732
3733#else
3734
3735 (void) operation;
3736
3737 return PSA_ERROR_NOT_SUPPORTED;
3738
3739#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3740 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3741 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3742}
3743
3744psa_status_t mbedtls_psa_verify_hash_start(
3745 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3746 const psa_key_attributes_t *attributes,
3747 const uint8_t *key_buffer, size_t key_buffer_size,
3748 psa_algorithm_t alg,
3749 const uint8_t *hash, size_t hash_length,
3750 const uint8_t *signature, size_t signature_length)
3751{
3752 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003753 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003754 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003755
Paul Elliott068fe072023-01-16 13:59:15 +00003756 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3757 return PSA_ERROR_NOT_SUPPORTED;
3758 }
3759
3760 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003761 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003762 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003763
3764#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003765 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3766 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003767
Paul Elliotta1c94092023-02-15 16:38:04 +00003768 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3769 mbedtls_mpi_init(&operation->r);
3770 mbedtls_mpi_init(&operation->s);
3771
Paul Elliott93d9ca82023-02-15 18:14:21 +00003772 /* Ensure num_ops is zero'ed in case of context re-use. */
3773 operation->num_ops = 0;
3774
Paul Elliott068fe072023-01-16 13:59:15 +00003775 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3776 attributes->core.bits,
3777 key_buffer,
3778 key_buffer_size,
3779 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003780
Paul Elliott068fe072023-01-16 13:59:15 +00003781 if (status != PSA_SUCCESS) {
3782 return status;
3783 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003784
Paul Elliottc569fc22023-02-10 13:02:54 +00003785 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003786
Paul Elliott1bc59df2023-02-05 13:41:57 +00003787 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003788 return PSA_ERROR_INVALID_SIGNATURE;
3789 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003790
Paul Elliott068fe072023-01-16 13:59:15 +00003791 status = mbedtls_to_psa_error(
3792 mbedtls_mpi_read_binary(&operation->r,
3793 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003794 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003795
Paul Elliott068fe072023-01-16 13:59:15 +00003796 if (status != PSA_SUCCESS) {
3797 return status;
3798 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003799
Paul Elliott068fe072023-01-16 13:59:15 +00003800 status = mbedtls_to_psa_error(
3801 mbedtls_mpi_read_binary(&operation->s,
3802 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003803 coordinate_bytes,
3804 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003805
Paul Elliott068fe072023-01-16 13:59:15 +00003806 if (status != PSA_SUCCESS) {
3807 return status;
3808 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003809
Paul Elliott2c9843f2023-02-15 17:32:42 +00003810 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003811
Paul Elliott2c9843f2023-02-15 17:32:42 +00003812 if (status != PSA_SUCCESS) {
3813 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003814 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003815
Paul Elliott0290a762023-02-09 14:30:24 +00003816 /* We only need to store the same length of hash as the private key size
3817 * here, it would be truncated by the internal implementation anyway. */
3818 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3819 coordinate_bytes);
3820
Paul Elliottba70ad42023-02-15 18:23:53 +00003821 if (required_hash_length > sizeof(operation->hash)) {
3822 /* Shouldn't happen, but better safe than sorry. */
3823 return PSA_ERROR_CORRUPTION_DETECTED;
3824 }
3825
Paul Elliott0290a762023-02-09 14:30:24 +00003826 memcpy(operation->hash, hash, required_hash_length);
3827 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003828
3829 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003830#else
Paul Elliott068fe072023-01-16 13:59:15 +00003831 (void) operation;
3832 (void) key_buffer;
3833 (void) key_buffer_size;
3834 (void) alg;
3835 (void) hash;
3836 (void) hash_length;
3837 (void) signature;
3838 (void) signature_length;
3839 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003840 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003841 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003842
Paul Elliott068fe072023-01-16 13:59:15 +00003843 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003844#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3845 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3846 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003847}
3848
3849psa_status_t mbedtls_psa_verify_hash_complete(
3850 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3851{
3852
3853#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3854 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3855 defined(MBEDTLS_ECP_RESTARTABLE)
3856
Paul Elliottf8e5b562023-02-19 18:43:45 +00003857 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3858
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003859 /* Ensure max_ops is set to the current value (or default). */
3860 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3861
Paul Elliottf8e5b562023-02-19 18:43:45 +00003862 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003863 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3864 operation->hash,
3865 operation->hash_length,
3866 &operation->ctx->Q,
3867 &operation->r,
3868 &operation->s,
3869 &operation->restart_ctx));
3870
Paul Elliottf8e5b562023-02-19 18:43:45 +00003871 /* Hide the fact that the restart context only holds a delta of number of
3872 * ops done during the last operation, not an absolute value. */
3873 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3874
3875 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003876#else
3877 (void) operation;
3878
3879 return PSA_ERROR_NOT_SUPPORTED;
3880
3881#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3882 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3883 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3884}
3885
3886psa_status_t mbedtls_psa_verify_hash_abort(
3887 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3888{
3889
3890#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3891 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3892 defined(MBEDTLS_ECP_RESTARTABLE)
3893
3894 if (operation->ctx) {
3895 mbedtls_ecdsa_free(operation->ctx);
3896 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003897 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003898 }
3899
3900 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3901
Paul Elliott93d9ca82023-02-15 18:14:21 +00003902 operation->num_ops = 0;
3903
Paul Elliott588f8ed2022-12-02 18:10:26 +00003904 mbedtls_mpi_free(&operation->r);
3905 mbedtls_mpi_free(&operation->s);
3906
3907 return PSA_SUCCESS;
3908
3909#else
3910 (void) operation;
3911
3912 return PSA_ERROR_NOT_SUPPORTED;
3913
3914#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3915 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3916 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3917}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003918
mohammad1603503973b2018-03-12 15:59:30 +02003919/****************************************************************/
3920/* Symmetric cryptography */
3921/****************************************************************/
3922
Gilles Peskine449bd832023-01-11 14:50:10 +01003923static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3924 mbedtls_svc_key_id_t key,
3925 psa_algorithm_t alg,
3926 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003927{
3928 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3929 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003930 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003931 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3932 PSA_KEY_USAGE_ENCRYPT :
3933 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003934
3935 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003936 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003937 status = PSA_ERROR_BAD_STATE;
3938 goto exit;
3939 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003940
Gilles Peskine449bd832023-01-11 14:50:10 +01003941 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003942 status = PSA_ERROR_INVALID_ARGUMENT;
3943 goto exit;
3944 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003945
Gilles Peskine449bd832023-01-11 14:50:10 +01003946 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3947 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003948 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003949 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003950
Ronald Cron49fafa92021-03-10 08:34:23 +01003951 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003952 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003953 * so we only set it (in the driver wrapper) after resources have been
3954 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003955 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003956 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003957 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003958 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003959 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003960 }
3961 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003962
Ronald Crona4af55f2020-12-14 14:36:06 +01003963 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003964 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003965 };
3966
Ronald Cronab99ac22020-10-01 16:38:28 +02003967 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003968 if (cipher_operation == MBEDTLS_ENCRYPT) {
3969 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3970 &attributes,
3971 slot->key.data,
3972 slot->key.bytes,
3973 alg);
3974 } else {
3975 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3976 &attributes,
3977 slot->key.data,
3978 slot->key.bytes,
3979 alg);
3980 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003981
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003982exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003983 if (status != PSA_SUCCESS) {
3984 psa_cipher_abort(operation);
3985 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003986
Gilles Peskine449bd832023-01-11 14:50:10 +01003987 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003988
Gilles Peskine449bd832023-01-11 14:50:10 +01003989 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003990}
3991
Gilles Peskine449bd832023-01-11 14:50:10 +01003992psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3993 mbedtls_svc_key_id_t key,
3994 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003995{
Gilles Peskine449bd832023-01-11 14:50:10 +01003996 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003997}
3998
Gilles Peskine449bd832023-01-11 14:50:10 +01003999psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4000 mbedtls_svc_key_id_t key,
4001 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004002{
Gilles Peskine449bd832023-01-11 14:50:10 +01004003 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004004}
4005
Gilles Peskine449bd832023-01-11 14:50:10 +01004006psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4007 uint8_t *iv,
4008 size_t iv_size,
4009 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004010{
Ronald Cron6d051732020-10-01 14:10:20 +02004011 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004012 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4013 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004014
Gilles Peskine449bd832023-01-11 14:50:10 +01004015 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004016 status = PSA_ERROR_BAD_STATE;
4017 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004018 }
4019
Gilles Peskine449bd832023-01-11 14:50:10 +01004020 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004021 status = PSA_ERROR_BAD_STATE;
4022 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004023 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004024
Ronald Cron2fb90522021-07-05 12:31:44 +02004025 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004026 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004027 status = PSA_ERROR_BUFFER_TOO_SMALL;
4028 goto exit;
4029 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004030
Gilles Peskine449bd832023-01-11 14:50:10 +01004031 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004032 status = PSA_ERROR_GENERIC_ERROR;
4033 goto exit;
4034 }
4035
Gilles Peskine449bd832023-01-11 14:50:10 +01004036 status = psa_generate_random(local_iv, default_iv_length);
4037 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004038 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004039 }
Ronald Cron5618a392021-03-26 09:52:26 +01004040
Gilles Peskine449bd832023-01-11 14:50:10 +01004041 status = psa_driver_wrapper_cipher_set_iv(operation,
4042 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004043
4044exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004045 if (status == PSA_SUCCESS) {
4046 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004047 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004048 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004049 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004050 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004051 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004052 }
Ronald Cron6d051732020-10-01 14:10:20 +02004053
Gilles Peskine449bd832023-01-11 14:50:10 +01004054 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004055}
4056
Gilles Peskine449bd832023-01-11 14:50:10 +01004057psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4058 const uint8_t *iv,
4059 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004060{
Ronald Cron6d051732020-10-01 14:10:20 +02004061 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004062
Gilles Peskine449bd832023-01-11 14:50:10 +01004063 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004064 status = PSA_ERROR_BAD_STATE;
4065 goto exit;
4066 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004067
Gilles Peskine449bd832023-01-11 14:50:10 +01004068 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004069 status = PSA_ERROR_BAD_STATE;
4070 goto exit;
4071 }
Ronald Crona0d68172021-03-26 10:15:08 +01004072
Gilles Peskine449bd832023-01-11 14:50:10 +01004073 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004074 status = PSA_ERROR_INVALID_ARGUMENT;
4075 goto exit;
4076 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004077
Gilles Peskine449bd832023-01-11 14:50:10 +01004078 status = psa_driver_wrapper_cipher_set_iv(operation,
4079 iv,
4080 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004081
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004082exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004083 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004084 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 } else {
4086 psa_cipher_abort(operation);
4087 }
4088 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004089}
4090
Gilles Peskine449bd832023-01-11 14:50:10 +01004091psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4092 const uint8_t *input,
4093 size_t input_length,
4094 uint8_t *output,
4095 size_t output_size,
4096 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004097{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004098 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004099
Gilles Peskine449bd832023-01-11 14:50:10 +01004100 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004101 status = PSA_ERROR_BAD_STATE;
4102 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004103 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004104
Gilles Peskine449bd832023-01-11 14:50:10 +01004105 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004106 status = PSA_ERROR_BAD_STATE;
4107 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004108 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004109
Gilles Peskine449bd832023-01-11 14:50:10 +01004110 status = psa_driver_wrapper_cipher_update(operation,
4111 input,
4112 input_length,
4113 output,
4114 output_size,
4115 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004116
4117exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 if (status != PSA_SUCCESS) {
4119 psa_cipher_abort(operation);
4120 }
Ronald Cron6d051732020-10-01 14:10:20 +02004121
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004123}
4124
Gilles Peskine449bd832023-01-11 14:50:10 +01004125psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4126 uint8_t *output,
4127 size_t output_size,
4128 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004129{
David Saadab4ecc272019-02-14 13:48:10 +02004130 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004131
Gilles Peskine449bd832023-01-11 14:50:10 +01004132 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004133 status = PSA_ERROR_BAD_STATE;
4134 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004135 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004136
Gilles Peskine449bd832023-01-11 14:50:10 +01004137 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004138 status = PSA_ERROR_BAD_STATE;
4139 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004140 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004141
Gilles Peskine449bd832023-01-11 14:50:10 +01004142 status = psa_driver_wrapper_cipher_finish(operation,
4143 output,
4144 output_size,
4145 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004146
4147exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004148 if (status == PSA_SUCCESS) {
4149 return psa_cipher_abort(operation);
4150 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004151 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004152 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004153
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004155 }
mohammad1603503973b2018-03-12 15:59:30 +02004156}
4157
Gilles Peskine449bd832023-01-11 14:50:10 +01004158psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004159{
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004161 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004162 * in use. It's ok to call abort on such an object, and there's
4163 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004164 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004165 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004166
Gilles Peskine449bd832023-01-11 14:50:10 +01004167 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004168
Ronald Cron49fafa92021-03-10 08:34:23 +01004169 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004170 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004171 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004172
Gilles Peskine449bd832023-01-11 14:50:10 +01004173 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004174}
4175
Gilles Peskine449bd832023-01-11 14:50:10 +01004176psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4177 psa_algorithm_t alg,
4178 const uint8_t *input,
4179 size_t input_length,
4180 uint8_t *output,
4181 size_t output_size,
4182 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004183{
4184 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004185 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004186 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004187 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4188 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004189
Gilles Peskine449bd832023-01-11 14:50:10 +01004190 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004191 status = PSA_ERROR_INVALID_ARGUMENT;
4192 goto exit;
4193 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004194
Gilles Peskine449bd832023-01-11 14:50:10 +01004195 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4196 PSA_KEY_USAGE_ENCRYPT,
4197 alg);
4198 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004199 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004201
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004202 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004204 };
4205
Gilles Peskine449bd832023-01-11 14:50:10 +01004206 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4207 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004208 status = PSA_ERROR_GENERIC_ERROR;
4209 goto exit;
4210 }
4211
Gilles Peskine449bd832023-01-11 14:50:10 +01004212 if (default_iv_length > 0) {
4213 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004214 status = PSA_ERROR_BUFFER_TOO_SMALL;
4215 goto exit;
4216 }
4217
Gilles Peskine449bd832023-01-11 14:50:10 +01004218 status = psa_generate_random(local_iv, default_iv_length);
4219 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004220 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004222 }
4223
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004224 status = psa_driver_wrapper_cipher_encrypt(
4225 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004226 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004227 mbedtls_buffer_offset(output, default_iv_length),
4228 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004229
4230exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 unlock_status = psa_unlock_key_slot(slot);
4232 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004233 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004234 }
Ronald Cron23919522021-07-08 19:00:07 +02004235
Gilles Peskine449bd832023-01-11 14:50:10 +01004236 if (status == PSA_SUCCESS) {
4237 if (default_iv_length > 0) {
4238 memcpy(output, local_iv, default_iv_length);
4239 }
4240 *output_length += default_iv_length;
4241 } else {
4242 *output_length = 0;
4243 }
4244
4245 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004246}
4247
Gilles Peskine449bd832023-01-11 14:50:10 +01004248psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4249 psa_algorithm_t alg,
4250 const uint8_t *input,
4251 size_t input_length,
4252 uint8_t *output,
4253 size_t output_size,
4254 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004255{
4256 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004257 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004258 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004259
Gilles Peskine449bd832023-01-11 14:50:10 +01004260 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004261 status = PSA_ERROR_INVALID_ARGUMENT;
4262 goto exit;
4263 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004264
Gilles Peskine449bd832023-01-11 14:50:10 +01004265 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4266 PSA_KEY_USAGE_DECRYPT,
4267 alg);
4268 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004269 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004270 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004271
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004272 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004273 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004274 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004275
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4277 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004278 status = PSA_ERROR_INVALID_ARGUMENT;
4279 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004280 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004281 status = PSA_ERROR_INVALID_ARGUMENT;
4282 goto exit;
4283 }
4284
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004285 status = psa_driver_wrapper_cipher_decrypt(
4286 &attributes, slot->key.data, slot->key.bytes,
4287 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004288 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004289
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004290exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004291 unlock_status = psa_unlock_key_slot(slot);
4292 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004293 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004294 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004295
Gilles Peskine449bd832023-01-11 14:50:10 +01004296 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004297 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 }
Ronald Cron23919522021-07-08 19:00:07 +02004299
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004301}
4302
4303
mohammad16035955c982018-04-26 00:53:03 +03004304/****************************************************************/
4305/* AEAD */
4306/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004307
Paul Elliottbaff51c2021-09-28 17:44:45 +01004308/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004309static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004310{
Gilles Peskine449bd832023-01-11 14:50:10 +01004311 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004312}
4313
4314/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004315static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4316 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004317{
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004319
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004321#if defined(PSA_WANT_ALG_GCM)
4322 case PSA_ALG_GCM:
4323 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004324 * arbitrarily large nonces. Please note that we do not generally
4325 * recommend the usage of nonces of greater length than
4326 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4327 * size, which can then lead to collisions if you encrypt a very
4328 * large number of messages.*/
4329 if (nonce_length != 0) {
4330 return PSA_SUCCESS;
4331 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004332 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004333#endif /* PSA_WANT_ALG_GCM */
4334#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004335 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004336 if (nonce_length >= 7 && nonce_length <= 13) {
4337 return PSA_SUCCESS;
4338 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004339 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004340#endif /* PSA_WANT_ALG_CCM */
4341#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004342 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004343 if (nonce_length == 12) {
4344 return PSA_SUCCESS;
4345 } else if (nonce_length == 8) {
4346 return PSA_ERROR_NOT_SUPPORTED;
4347 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004348 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004349#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004350 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004351 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004352 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004353 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004354
Gilles Peskine449bd832023-01-11 14:50:10 +01004355 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004356}
4357
Gilles Peskine449bd832023-01-11 14:50:10 +01004358static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004359{
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4361 return PSA_ERROR_INVALID_ARGUMENT;
4362 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004363
Gilles Peskine449bd832023-01-11 14:50:10 +01004364 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004365}
4366
Gilles Peskine449bd832023-01-11 14:50:10 +01004367psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4368 psa_algorithm_t alg,
4369 const uint8_t *nonce,
4370 size_t nonce_length,
4371 const uint8_t *additional_data,
4372 size_t additional_data_length,
4373 const uint8_t *plaintext,
4374 size_t plaintext_length,
4375 uint8_t *ciphertext,
4376 size_t ciphertext_size,
4377 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004378{
Ronald Cron215633c2021-03-16 17:15:37 +01004379 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4380 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004381
mohammad1603f08a5502018-06-03 15:05:47 +03004382 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004383
Gilles Peskine449bd832023-01-11 14:50:10 +01004384 status = psa_aead_check_algorithm(alg);
4385 if (status != PSA_SUCCESS) {
4386 return status;
4387 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004388
Ronald Cron9a986162021-03-26 12:40:07 +01004389 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4391 if (status != PSA_SUCCESS) {
4392 return status;
4393 }
mohammad16035955c982018-04-26 00:53:03 +03004394
Ronald Cron215633c2021-03-16 17:15:37 +01004395 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004396 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004397 };
mohammad16035955c982018-04-26 00:53:03 +03004398
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 status = psa_aead_check_nonce_length(alg, nonce_length);
4400 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004401 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004403
Ronald Cronde822812021-03-17 16:08:20 +01004404 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004405 &attributes, slot->key.data, slot->key.bytes,
4406 alg,
4407 nonce, nonce_length,
4408 additional_data, additional_data_length,
4409 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004411
Gilles Peskine449bd832023-01-11 14:50:10 +01004412 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4413 memset(ciphertext, 0, ciphertext_size);
4414 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004415
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004416exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004417 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004418
Gilles Peskine449bd832023-01-11 14:50:10 +01004419 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004420}
4421
Gilles Peskine449bd832023-01-11 14:50:10 +01004422psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4423 psa_algorithm_t alg,
4424 const uint8_t *nonce,
4425 size_t nonce_length,
4426 const uint8_t *additional_data,
4427 size_t additional_data_length,
4428 const uint8_t *ciphertext,
4429 size_t ciphertext_length,
4430 uint8_t *plaintext,
4431 size_t plaintext_size,
4432 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004433{
Ronald Cron215633c2021-03-16 17:15:37 +01004434 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4435 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004436
Gilles Peskineee652a32018-06-01 19:23:52 +02004437 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004438
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 status = psa_aead_check_algorithm(alg);
4440 if (status != PSA_SUCCESS) {
4441 return status;
4442 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004443
Ronald Cron9a986162021-03-26 12:40:07 +01004444 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4446 if (status != PSA_SUCCESS) {
4447 return status;
4448 }
mohammad16035955c982018-04-26 00:53:03 +03004449
Ronald Cron215633c2021-03-16 17:15:37 +01004450 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004452 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004453
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 status = psa_aead_check_nonce_length(alg, nonce_length);
4455 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004456 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004458
Ronald Cronde822812021-03-17 16:08:20 +01004459 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004460 &attributes, slot->key.data, slot->key.bytes,
4461 alg,
4462 nonce, nonce_length,
4463 additional_data, additional_data_length,
4464 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004466
Gilles Peskine449bd832023-01-11 14:50:10 +01004467 if (status != PSA_SUCCESS && plaintext_size != 0) {
4468 memset(plaintext, 0, plaintext_size);
4469 }
mohammad160360a64d02018-06-03 17:20:42 +03004470
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004471exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004472 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004473
Gilles Peskine449bd832023-01-11 14:50:10 +01004474 return status;
mohammad16035955c982018-04-26 00:53:03 +03004475}
4476
Gilles Peskine449bd832023-01-11 14:50:10 +01004477static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4478{
4479 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004480
Gilles Peskine449bd832023-01-11 14:50:10 +01004481 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004482#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004483 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004484 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4486 return PSA_ERROR_INVALID_ARGUMENT;
4487 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004488 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004489#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004490
Przemek Stekiel86679c72022-10-06 17:06:56 +02004491#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004492 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004493 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4495 return PSA_ERROR_INVALID_ARGUMENT;
4496 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004497 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004498#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004499
Przemek Stekiel86679c72022-10-06 17:06:56 +02004500#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004501 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004502 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 if (tag_len != 16) {
4504 return PSA_ERROR_INVALID_ARGUMENT;
4505 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004506 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004507#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004508
4509 default:
4510 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004512 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004514}
4515
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004516/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004517static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4518 int is_encrypt,
4519 mbedtls_svc_key_id_t key,
4520 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004521{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004522 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4523 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4524 psa_key_slot_t *slot = NULL;
4525 psa_key_usage_t key_usage = 0;
4526
Gilles Peskine449bd832023-01-11 14:50:10 +01004527 status = psa_aead_check_algorithm(alg);
4528 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004529 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004530 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004531
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004533 status = PSA_ERROR_BAD_STATE;
4534 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004535 }
4536
Gilles Peskine449bd832023-01-11 14:50:10 +01004537 if (operation->nonce_set || operation->lengths_set ||
4538 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004539 status = PSA_ERROR_BAD_STATE;
4540 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004541 }
4542
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004544 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004545 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004546 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004548
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4550 alg);
4551 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004552 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004554
4555 psa_key_attributes_t attributes = {
4556 .core = slot->attr
4557 };
4558
Gilles Peskine449bd832023-01-11 14:50:10 +01004559 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004560 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004561 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004562
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 if (is_encrypt) {
4564 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4565 &attributes,
4566 slot->key.data,
4567 slot->key.bytes,
4568 alg);
4569 } else {
4570 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4571 &attributes,
4572 slot->key.data,
4573 slot->key.bytes,
4574 alg);
4575 }
4576 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004577 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004578 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004579
Gilles Peskine449bd832023-01-11 14:50:10 +01004580 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004581
4582exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004584
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004586 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004588 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 } else {
4590 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004591 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004592
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004594}
4595
Paul Elliott302ff6b2021-04-20 18:10:30 +01004596/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004597psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4598 mbedtls_svc_key_id_t key,
4599 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004600{
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004602}
4603
4604/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004605psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4606 mbedtls_svc_key_id_t key,
4607 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004608{
Gilles Peskine449bd832023-01-11 14:50:10 +01004609 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004610}
4611
4612/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004613psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4614 uint8_t *nonce,
4615 size_t nonce_size,
4616 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004617{
4618 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004619 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004620 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004621
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004622 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004623
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004625 status = PSA_ERROR_BAD_STATE;
4626 goto exit;
4627 }
4628
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004630 status = PSA_ERROR_BAD_STATE;
4631 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004632 }
4633
Paul Elliotte193ea82021-10-01 13:00:16 +01004634 /* For CCM, this size may not be correct according to the PSA
4635 * specification. The PSA Crypto 1.0.1 specification states:
4636 *
4637 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4638 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4639 *
4640 * However this restriction that L has to be the smallest integer is not
4641 * applied in practice, and it is not implementable here since the
4642 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004643 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4644 operation->alg);
4645 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004646 status = PSA_ERROR_BUFFER_TOO_SMALL;
4647 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004648 }
4649
Gilles Peskine449bd832023-01-11 14:50:10 +01004650 status = psa_generate_random(local_nonce, required_nonce_size);
4651 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004652 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004654
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004656
Paul Elliott39dc6b82021-05-11 19:16:09 +01004657exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 if (status == PSA_SUCCESS) {
4659 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004660 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 } else {
4662 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004663 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004664
Gilles Peskine449bd832023-01-11 14:50:10 +01004665 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004666}
4667
4668/* Set the nonce for a multipart authenticated encryption or decryption
4669 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004670psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4671 const uint8_t *nonce,
4672 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004673{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004674 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4675
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004677 status = PSA_ERROR_BAD_STATE;
4678 goto exit;
4679 }
4680
Gilles Peskine449bd832023-01-11 14:50:10 +01004681 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004682 status = PSA_ERROR_BAD_STATE;
4683 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004684 }
4685
Gilles Peskine449bd832023-01-11 14:50:10 +01004686 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4687 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004688 status = PSA_ERROR_INVALID_ARGUMENT;
4689 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004690 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004691
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4693 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004694
Paul Elliott39dc6b82021-05-11 19:16:09 +01004695exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004697 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 } else {
4699 psa_aead_abort(operation);
4700 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004701
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004703}
4704
4705/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004706psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4707 size_t ad_length,
4708 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004709{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004710 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4711
Gilles Peskine449bd832023-01-11 14:50:10 +01004712 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004713 status = PSA_ERROR_BAD_STATE;
4714 goto exit;
4715 }
4716
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 if (operation->lengths_set || operation->ad_started ||
4718 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004719 status = PSA_ERROR_BAD_STATE;
4720 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004721 }
4722
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004724#if defined(PSA_WANT_ALG_GCM)
4725 case PSA_ALG_GCM:
4726 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 * bits. Without the guard this code will generate warnings on 32bit
4728 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004729#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 if (((uint64_t) ad_length) >> 61 != 0 ||
4731 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004732 status = PSA_ERROR_INVALID_ARGUMENT;
4733 goto exit;
4734 }
Paul Elliott325d3742021-09-27 17:56:28 +01004735#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004736 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004737#endif /* PSA_WANT_ALG_GCM */
4738#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004739 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004741 status = PSA_ERROR_INVALID_ARGUMENT;
4742 goto exit;
4743 }
4744 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004745#endif /* PSA_WANT_ALG_CCM */
4746#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004747 case PSA_ALG_CHACHA20_POLY1305:
4748 /* No length restrictions for ChaChaPoly. */
4749 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004750#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004751 default:
4752 break;
4753 }
Paul Elliott325d3742021-09-27 17:56:28 +01004754
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4756 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004757
Paul Elliott39dc6b82021-05-11 19:16:09 +01004758exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004760 operation->ad_remaining = ad_length;
4761 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004762 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 } else {
4764 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004765 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004766
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004768}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004769
4770/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004771psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4772 const uint8_t *input,
4773 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004774{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004775 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4776
Gilles Peskine449bd832023-01-11 14:50:10 +01004777 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004778 status = PSA_ERROR_BAD_STATE;
4779 goto exit;
4780 }
4781
Gilles Peskine449bd832023-01-11 14:50:10 +01004782 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004783 status = PSA_ERROR_BAD_STATE;
4784 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004785 }
4786
Gilles Peskine449bd832023-01-11 14:50:10 +01004787 if (operation->lengths_set) {
4788 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004789 status = PSA_ERROR_INVALID_ARGUMENT;
4790 goto exit;
4791 }
4792
4793 operation->ad_remaining -= input_length;
4794 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004795#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004796 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004797 status = PSA_ERROR_BAD_STATE;
4798 goto exit;
4799 }
4800#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004801
Gilles Peskine449bd832023-01-11 14:50:10 +01004802 status = psa_driver_wrapper_aead_update_ad(operation, input,
4803 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004804
Paul Elliott39dc6b82021-05-11 19:16:09 +01004805exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004806 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004807 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 } else {
4809 psa_aead_abort(operation);
4810 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004811
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004813}
4814
4815/* Encrypt or decrypt a message fragment in an active multipart AEAD
4816 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004817psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4818 const uint8_t *input,
4819 size_t input_length,
4820 uint8_t *output,
4821 size_t output_size,
4822 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004823{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004824 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004825
4826 *output_length = 0;
4827
Gilles Peskine449bd832023-01-11 14:50:10 +01004828 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004829 status = PSA_ERROR_BAD_STATE;
4830 goto exit;
4831 }
4832
Gilles Peskine449bd832023-01-11 14:50:10 +01004833 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004834 status = PSA_ERROR_BAD_STATE;
4835 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004836 }
4837
Gilles Peskine449bd832023-01-11 14:50:10 +01004838 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004839 /* Additional data length was supplied, but not all the additional
4840 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004841 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004842 status = PSA_ERROR_INVALID_ARGUMENT;
4843 goto exit;
4844 }
4845
4846 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004847 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004848 status = PSA_ERROR_INVALID_ARGUMENT;
4849 goto exit;
4850 }
4851
4852 operation->body_remaining -= input_length;
4853 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004854#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004855 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004856 status = PSA_ERROR_BAD_STATE;
4857 goto exit;
4858 }
4859#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004860
Gilles Peskine449bd832023-01-11 14:50:10 +01004861 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4862 output, output_size,
4863 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004864
Paul Elliott39dc6b82021-05-11 19:16:09 +01004865exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004866 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004867 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 } else {
4869 psa_aead_abort(operation);
4870 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004871
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004873}
4874
Gilles Peskine449bd832023-01-11 14:50:10 +01004875static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004876{
Gilles Peskine449bd832023-01-11 14:50:10 +01004877 if (operation->id == 0 || !operation->nonce_set) {
4878 return PSA_ERROR_BAD_STATE;
4879 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004880
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4882 operation->body_remaining != 0)) {
4883 return PSA_ERROR_INVALID_ARGUMENT;
4884 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004885
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004887}
4888
Paul Elliott302ff6b2021-04-20 18:10:30 +01004889/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004890psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4891 uint8_t *ciphertext,
4892 size_t ciphertext_size,
4893 size_t *ciphertext_length,
4894 uint8_t *tag,
4895 size_t tag_size,
4896 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004897{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004898 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4899
Paul Elliott302ff6b2021-04-20 18:10:30 +01004900 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004901 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004902
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 status = psa_aead_final_checks(operation);
4904 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004905 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004906 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004907
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004909 status = PSA_ERROR_BAD_STATE;
4910 goto exit;
4911 }
4912
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4914 ciphertext_size,
4915 ciphertext_length,
4916 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004917
Paul Elliott39dc6b82021-05-11 19:16:09 +01004918exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004919
4920
Paul Elliottf47b0952021-05-21 18:02:33 +01004921 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004922 * the zero tag size, make sure the tag is set to something implausible.
4923 * Even if the operation succeeds, make sure we clear the rest of the
4924 * buffer to prevent potential leakage of anything previously placed in
4925 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004926 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004927
Gilles Peskine449bd832023-01-11 14:50:10 +01004928 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004929
Gilles Peskine449bd832023-01-11 14:50:10 +01004930 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004931}
4932
4933/* Finish authenticating and decrypting a message in a multipart AEAD
4934 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004935psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4936 uint8_t *plaintext,
4937 size_t plaintext_size,
4938 size_t *plaintext_length,
4939 const uint8_t *tag,
4940 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004941{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004942 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4943
Paul Elliott302ff6b2021-04-20 18:10:30 +01004944 *plaintext_length = 0;
4945
Gilles Peskine449bd832023-01-11 14:50:10 +01004946 status = psa_aead_final_checks(operation);
4947 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004948 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004950
Gilles Peskine449bd832023-01-11 14:50:10 +01004951 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004952 status = PSA_ERROR_BAD_STATE;
4953 goto exit;
4954 }
4955
Gilles Peskine449bd832023-01-11 14:50:10 +01004956 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4957 plaintext_size,
4958 plaintext_length,
4959 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004960
4961exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004962 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004963
Gilles Peskine449bd832023-01-11 14:50:10 +01004964 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004965}
4966
4967/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004968psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004969{
4970 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4971
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004973 /* The object has (apparently) been initialized but it is not (yet)
4974 * in use. It's ok to call abort on such an object, and there's
4975 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004976 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004977 }
4978
Gilles Peskine449bd832023-01-11 14:50:10 +01004979 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004980
Gilles Peskine449bd832023-01-11 14:50:10 +01004981 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004982
Gilles Peskine449bd832023-01-11 14:50:10 +01004983 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004984}
4985
Gilles Peskinee59236f2018-01-27 23:32:46 +01004986/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004987/* Generators */
4988/****************************************************************/
4989
Przemek Stekiel69c46792022-06-10 12:59:51 +02004990#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004991 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004992 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
4993 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08004994#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004995#endif /* At least one builtin KDF */
4996
Przemek Stekiel69c46792022-06-10 12:59:51 +02004997#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02004998 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4999 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5000static psa_status_t psa_key_derivation_start_hmac(
5001 psa_mac_operation_t *operation,
5002 psa_algorithm_t hash_alg,
5003 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005005{
5006 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5007 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5009 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5010 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005011
Steven Cooreman72f736a2021-05-07 14:14:37 +02005012 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005013 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005014
Gilles Peskine449bd832023-01-11 14:50:10 +01005015 status = psa_driver_wrapper_mac_sign_setup(operation,
5016 &attributes,
5017 hmac_key, hmac_key_length,
5018 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005019
Gilles Peskine449bd832023-01-11 14:50:10 +01005020 psa_reset_key_attributes(&attributes);
5021 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005022}
5023#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005024
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005025#define HKDF_STATE_INIT 0 /* no input yet */
5026#define HKDF_STATE_STARTED 1 /* got salt */
5027#define HKDF_STATE_KEYED 2 /* got key */
5028#define HKDF_STATE_OUTPUT 3 /* output started */
5029
Gilles Peskinecbe66502019-05-16 16:59:18 +02005030static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005031 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005032{
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5034 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5035 } else {
5036 return operation->alg;
5037 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005038}
5039
Gilles Peskine449bd832023-01-11 14:50:10 +01005040psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005041{
5042 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5044 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005045 /* The object has (apparently) been initialized but it is not
5046 * in use. It's ok to call abort on such an object, and there's
5047 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005049#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005050 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5051 mbedtls_free(operation->ctx.hkdf.info);
5052 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5053 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005054#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005055#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5056 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005057 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5058 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5059 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5060 if (operation->ctx.tls12_prf.secret != NULL) {
5061 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5062 operation->ctx.tls12_prf.secret_length);
5063 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005064 }
5065
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 if (operation->ctx.tls12_prf.seed != NULL) {
5067 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5068 operation->ctx.tls12_prf.seed_length);
5069 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005070 }
5071
Gilles Peskine449bd832023-01-11 14:50:10 +01005072 if (operation->ctx.tls12_prf.label != NULL) {
5073 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5074 operation->ctx.tls12_prf.label_length);
5075 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005076 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005077#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 if (operation->ctx.tls12_prf.other_secret != NULL) {
5079 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5080 operation->ctx.tls12_prf.other_secret_length);
5081 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005082 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005083#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005084 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005085
5086 /* We leave the fields Ai and output_block to be erased safely by the
5087 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005089#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5090 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005091#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005092 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5093 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5094 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5095 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005096#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005097 {
5098 status = PSA_ERROR_BAD_STATE;
5099 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 mbedtls_platform_zeroize(operation, sizeof(*operation));
5101 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005102}
5103
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005104psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005106{
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005108 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005110 }
5111
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005112 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005113 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005114}
5115
Gilles Peskine449bd832023-01-11 14:50:10 +01005116psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5117 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005118{
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 if (operation->alg == 0) {
5120 return PSA_ERROR_BAD_STATE;
5121 }
5122 if (capacity > operation->capacity) {
5123 return PSA_ERROR_INVALID_ARGUMENT;
5124 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005125 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005126 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005127}
5128
Przemek Stekiel69c46792022-06-10 12:59:51 +02005129#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005130/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005131static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5132 psa_algorithm_t kdf_alg,
5133 uint8_t *output,
5134 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005135{
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5137 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005138 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005139 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005140#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005141 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005142#else
5143 const uint8_t last_block = 0xff;
5144#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005145
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 if (hkdf->state < HKDF_STATE_KEYED ||
5147 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005148#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005150#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 )) {
5152 return PSA_ERROR_BAD_STATE;
5153 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005154 hkdf->state = HKDF_STATE_OUTPUT;
5155
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005157 /* Copy what remains of the current block */
5158 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005160 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 }
5162 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005163 output += n;
5164 output_length -= n;
5165 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005167 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005168 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005169 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005170 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005171 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005172 * object was corrupted or if this function is called directly
5173 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 if (hkdf->block_number == last_block) {
5175 return PSA_ERROR_BAD_STATE;
5176 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005177
5178 /* We need a new block */
5179 ++hkdf->block_number;
5180 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005181
Gilles Peskine449bd832023-01-11 14:50:10 +01005182 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5183 hash_alg,
5184 hkdf->prk,
5185 hash_length);
5186 if (status != PSA_SUCCESS) {
5187 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005188 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005189
5190 if (hkdf->block_number != 1) {
5191 status = psa_mac_update(&hkdf->hmac,
5192 hkdf->output_block,
5193 hash_length);
5194 if (status != PSA_SUCCESS) {
5195 return status;
5196 }
5197 }
5198 status = psa_mac_update(&hkdf->hmac,
5199 hkdf->info,
5200 hkdf->info_length);
5201 if (status != PSA_SUCCESS) {
5202 return status;
5203 }
5204 status = psa_mac_update(&hkdf->hmac,
5205 &hkdf->block_number, 1);
5206 if (status != PSA_SUCCESS) {
5207 return status;
5208 }
5209 status = psa_mac_sign_finish(&hkdf->hmac,
5210 hkdf->output_block,
5211 sizeof(hkdf->output_block),
5212 &hmac_output_length);
5213 if (status != PSA_SUCCESS) {
5214 return status;
5215 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005216 }
5217
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005219}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005220#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005221
John Durkop07cc04a2020-11-16 22:08:34 -08005222#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5223 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005224static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5225 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005227{
Gilles Peskine449bd832023-01-11 14:50:10 +01005228 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5229 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005230 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5231 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005232 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005233
Janos Follath7742fee2019-06-17 12:58:10 +01005234 /* We can't be wanting more output after block 0xff, otherwise
5235 * the capacity check in psa_key_derivation_output_bytes() would have
5236 * prevented this call. It could happen only if the operation
5237 * object was corrupted or if this function is called directly
5238 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005239 if (tls12_prf->block_number == 0xff) {
5240 return PSA_ERROR_CORRUPTION_DETECTED;
5241 }
Janos Follath7742fee2019-06-17 12:58:10 +01005242
5243 /* We need a new block */
5244 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005245 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005246
5247 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5248 *
5249 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5250 *
5251 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5252 * HMAC_hash(secret, A(2) + seed) +
5253 * HMAC_hash(secret, A(3) + seed) + ...
5254 *
5255 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005256 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005257 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005258 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005259 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005260 * is currently extracted as `output_block` and where i is
5261 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005262 */
5263
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 status = psa_key_derivation_start_hmac(&hmac,
5265 hash_alg,
5266 tls12_prf->secret,
5267 tls12_prf->secret_length);
5268 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005269 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005271
5272 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005274 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5275 * the variable seed and in this instance means it in the context of the
5276 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 status = psa_mac_update(&hmac,
5278 tls12_prf->label,
5279 tls12_prf->label_length);
5280 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005281 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 }
5283 status = psa_mac_update(&hmac,
5284 tls12_prf->seed,
5285 tls12_prf->seed_length);
5286 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005287 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005288 }
5289 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005290 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5292 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005293 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005295 }
5296
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 status = psa_mac_sign_finish(&hmac,
5298 tls12_prf->Ai, hash_length,
5299 &hmac_output_length);
5300 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005301 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005302 }
5303 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005304 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005305 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005306
5307 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 status = psa_key_derivation_start_hmac(&hmac,
5309 hash_alg,
5310 tls12_prf->secret,
5311 tls12_prf->secret_length);
5312 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005313 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005314 }
5315 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_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->label, tls12_prf->label_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->seed, tls12_prf->seed_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_sign_finish(&hmac,
5328 tls12_prf->output_block, hash_length,
5329 &hmac_output_length);
5330 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005331 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005332 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005333
Janos Follath7742fee2019-06-17 12:58:10 +01005334
5335cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 cleanup_status = psa_mac_abort(&hmac);
5337 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005338 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005340
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005342}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005343
Janos Follath844eb0e2019-06-19 12:10:49 +01005344static psa_status_t psa_key_derivation_tls12_prf_read(
5345 psa_tls12_prf_key_derivation_t *tls12_prf,
5346 psa_algorithm_t alg,
5347 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005349{
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5351 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005352 psa_status_t status;
5353 uint8_t offset, length;
5354
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005356 case PSA_TLS12_PRF_STATE_LABEL_SET:
5357 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5358 break;
5359 case PSA_TLS12_PRF_STATE_OUTPUT:
5360 break;
5361 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005363 }
5364
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005366 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 if (tls12_prf->left_in_block == 0) {
5368 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5369 alg);
5370 if (status != PSA_SUCCESS) {
5371 return status;
5372 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005373
5374 continue;
5375 }
5376
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005378 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005380 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005382
5383 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005385 output += length;
5386 output_length -= length;
5387 tls12_prf->left_in_block -= length;
5388 }
5389
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005391}
John Durkop07cc04a2020-11-16 22:08:34 -08005392#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5393 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005394
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005395#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5396static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5397 psa_tls12_ecjpake_to_pms_t *ecjpake,
5398 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005399 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005400{
5401 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005402 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005403
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 if (output_length != 32) {
5405 return PSA_ERROR_INVALID_ARGUMENT;
5406 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005407
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5409 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5410 &output_size);
5411 if (status != PSA_SUCCESS) {
5412 return status;
5413 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005414
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 if (output_size != output_length) {
5416 return PSA_ERROR_GENERIC_ERROR;
5417 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005418
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005420}
5421#endif
5422
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005423psa_status_t psa_key_derivation_output_bytes(
5424 psa_key_derivation_operation_t *operation,
5425 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005426 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005427{
5428 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005430
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005432 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005433 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005434 }
5435
Gilles Peskine449bd832023-01-11 14:50:10 +01005436 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005437 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005438 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005439 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005440 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005441 goto exit;
5442 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005444 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005445 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005446 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5447 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005448 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005449 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005451 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005452 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005453
Przemek Stekiel69c46792022-06-10 12:59:51 +02005454#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5456 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5457 output, output_length);
5458 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005459#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005460#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5461 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5463 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5464 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5465 kdf_alg, output,
5466 output_length);
5467 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005468#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5469 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005470#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005472 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5474 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005475#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5476
Gilles Peskineeab56e42018-07-12 17:12:33 +02005477 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005478 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005480 }
5481
5482exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005484 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005485 * This allows us to differentiate between exhausted operations and
5486 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5487 * operations. */
5488 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005490 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005492 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005494}
5495
David Brown7807bf72021-02-09 16:28:23 -07005496#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005497static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005498{
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 if (data_size >= 8) {
5500 mbedtls_des_key_set_parity(data);
5501 }
5502 if (data_size >= 16) {
5503 mbedtls_des_key_set_parity(data + 8);
5504 }
5505 if (data_size >= 24) {
5506 mbedtls_des_key_set_parity(data + 16);
5507 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005508}
David Brown7807bf72021-02-09 16:28:23 -07005509#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005510
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005511/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 * ECC keys on a Weierstrass elliptic curve require the generation
5513 * of a private key which is an integer
5514 * in the range [1, N - 1], where N is the boundary of the private key domain:
5515 * N is the prime p for Diffie-Hellman, or the order of the
5516 * curve’s base point for ECC.
5517 *
5518 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5519 * This function generates the private key using the following process:
5520 *
5521 * 1. Draw a byte string of length ceiling(m/8) bytes.
5522 * 2. If m is not a multiple of 8, set the most significant
5523 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5524 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5525 * 4. If k > N - 2, discard the result and return to step 1.
5526 * 5. Output k + 1 as the private key.
5527 *
5528 * This method allows compliance to NIST standards, specifically the methods titled
5529 * Key-Pair Generation by Testing Candidates in the following publications:
5530 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5531 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5532 * Diffie-Hellman keys.
5533 *
5534 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5535 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5536 *
5537 * Note: Function allocates memory for *data buffer, so given *data should be
5538 * always NULL.
5539 */
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005540#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
5541 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
5542 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5543 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5544 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005545static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005546 psa_key_slot_t *slot,
5547 size_t bits,
5548 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005549 uint8_t **data
5550 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005551{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005552 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005553 mbedtls_mpi k;
5554 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005555 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005556 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005557
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 mbedtls_mpi_init(&k);
5559 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005560
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005561 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005563 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005565
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005567 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005568 goto cleanup;
5569 }
5570
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005571 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005572 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005573
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005575
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005576 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005577 /* Let m be the bit size of N. */
5578 size_t m = ecp_group.nbits;
5579
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005581
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005582 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005584
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005585 /* Note: This function is always called with *data == NULL and it
5586 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 *data = mbedtls_calloc(1, m_bytes);
5588 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005589 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005590 goto cleanup;
5591 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005592
Gilles Peskine449bd832023-01-11 14:50:10 +01005593 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005594 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005595 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005596 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005598
5599 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005600 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005601 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005602 * (8 * ceiling(m/8) - m) bits of the first byte in
5603 * the string to zero.
5604 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005605 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005606 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005607 }
5608
5609 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 * big-endian byte string.
5611 */
5612 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005613
5614 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 * Result of comparison is returned. When it indicates error
5616 * then this function is called again.
5617 */
5618 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005619 }
5620
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005621 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005622 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5623 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005624cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 if (ret != 0) {
5626 status = mbedtls_to_psa_error(ret);
5627 }
5628 if (status != PSA_SUCCESS) {
5629 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005630 *data = NULL;
5631 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 mbedtls_mpi_free(&k);
5633 mbedtls_mpi_free(&diff_N_2);
5634 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005635}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005636
5637/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5638 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5639 *
5640 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5641 * draw a 32-byte string and process it as specified in
5642 * Elliptic Curves for Security [RFC7748] §5.
5643 *
5644 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5645 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5646 *
5647 * Note: Function allocates memory for *data buffer, so given *data should be
5648 * always NULL.
5649 */
5650
5651static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5652 size_t bits,
5653 psa_key_derivation_operation_t *operation,
5654 uint8_t **data
5655 )
5656{
5657 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005658 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005659
Gilles Peskine449bd832023-01-11 14:50:10 +01005660 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005661 case 255:
5662 output_length = 32;
5663 break;
5664 case 448:
5665 output_length = 56;
5666 break;
5667 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005668 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005669 break;
5670 }
5671
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005673
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 if (*data == NULL) {
5675 return PSA_ERROR_INSUFFICIENT_MEMORY;
5676 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005677
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005679
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005681 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005682 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005683
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005685 case 255:
5686 (*data)[0] &= 248;
5687 (*data)[31] &= 127;
5688 (*data)[31] |= 64;
5689 break;
5690 case 448:
5691 (*data)[0] &= 252;
5692 (*data)[55] |= 128;
5693 break;
5694 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005695 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005696 break;
5697 }
5698
5699 return status;
5700}
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005701#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
5702 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
5703 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5704 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5705 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005706
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005707static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005708 psa_key_slot_t *slot,
5709 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005710 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005711{
5712 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305714 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005715 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005716
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5718 return PSA_ERROR_INVALID_ARGUMENT;
5719 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005720
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005721#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
5722 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
5723 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5724 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5725 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5727 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5728 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005729 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005730 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5731 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005732 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 }
5734 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005735 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5737 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005738 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005739 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005740 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005741 } else
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005742#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
5743 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
5744 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5745 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5746 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 if (key_type_is_raw_bytes(slot->attr.type)) {
5748 if (bits % 8 != 0) {
5749 return PSA_ERROR_INVALID_ARGUMENT;
5750 }
5751 data = mbedtls_calloc(1, bytes);
5752 if (data == NULL) {
5753 return PSA_ERROR_INSUFFICIENT_MEMORY;
5754 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005755
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 status = psa_key_derivation_output_bytes(operation, data, bytes);
5757 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005758 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005759 }
David Brown12ca5032021-02-11 11:02:00 -07005760#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5762 psa_des_set_key_parity(data, bytes);
5763 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005764#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 } else {
5766 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005767 }
Ronald Crondd04d422020-11-28 15:14:42 +01005768
Ronald Cronbf33c932020-11-28 18:06:53 +01005769 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005770 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005772 };
5773
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5775 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5776 &storage_size);
5777 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305778 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 }
Archanad8a83dc2021-06-14 10:04:16 +05305780 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 status = psa_allocate_buffer_to_slot(slot, storage_size);
5782 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305783 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005784 }
Archanad8a83dc2021-06-14 10:04:16 +05305785
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 status = psa_driver_wrapper_import_key(&attributes,
5787 data, bytes,
5788 slot->key.data,
5789 slot->key.bytes,
5790 &slot->key.bytes, &bits);
5791 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005792 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005793 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005794
5795exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 mbedtls_free(data);
5797 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005798}
5799
Gilles Peskine449bd832023-01-11 14:50:10 +01005800psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5801 psa_key_derivation_operation_t *operation,
5802 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005803{
5804 psa_status_t status;
5805 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005806 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005807
Ronald Cron81709fc2020-11-14 12:10:32 +01005808 *key = MBEDTLS_SVC_KEY_ID_INIT;
5809
Gilles Peskine0f84d622019-09-12 19:03:13 +02005810 /* Reject any attempt to create a zero-length key so that we don't
5811 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 if (psa_get_key_bits(attributes) == 0) {
5813 return PSA_ERROR_INVALID_ARGUMENT;
5814 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005815
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 if (operation->alg == PSA_ALG_NONE) {
5817 return PSA_ERROR_BAD_STATE;
5818 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005819
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 if (!operation->can_output_key) {
5821 return PSA_ERROR_NOT_PERMITTED;
5822 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005823
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5825 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005826#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005828 /* Deriving a key in a secure element is not implemented yet. */
5829 status = PSA_ERROR_NOT_SUPPORTED;
5830 }
5831#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 if (status == PSA_SUCCESS) {
5833 status = psa_generate_derived_key_internal(slot,
5834 attributes->core.bits,
5835 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005836 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 if (status == PSA_SUCCESS) {
5838 status = psa_finish_key_creation(slot, driver, key);
5839 }
5840 if (status != PSA_SUCCESS) {
5841 psa_fail_key_creation(slot, driver);
5842 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005843
Gilles Peskine449bd832023-01-11 14:50:10 +01005844 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005845}
5846
Gilles Peskineeab56e42018-07-12 17:12:33 +02005847
5848
5849/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005850/* Key derivation */
5851/****************************************************************/
5852
Steven Cooreman932ffb72021-02-15 12:14:32 +01005853#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005854static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005855{
5856#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005857 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5858 return 1;
5859 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005860#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005861#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5863 return 1;
5864 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005865#endif
5866#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5868 return 1;
5869 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005870#endif
5871#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5873 return 1;
5874 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005875#endif
5876#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5878 return 1;
5879 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005880#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005881#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5883 return 1;
5884 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005885#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005887}
5888
Gilles Peskine449bd832023-01-11 14:50:10 +01005889static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005890{
5891 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005892 psa_status_t status = psa_hash_setup(&operation, alg);
5893 psa_hash_abort(&operation);
5894 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005895}
5896
Gilles Peskine969c5d62019-01-16 15:53:06 +01005897static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005898 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005900{
Janos Follath5fe19732019-06-20 15:09:30 +01005901 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5902 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005903 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005904
Gilles Peskine969c5d62019-01-16 15:53:06 +01005905 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 if (!is_kdf_alg_supported(kdf_alg)) {
5907 return PSA_ERROR_NOT_SUPPORTED;
5908 }
John Durkop07cc04a2020-11-16 22:08:34 -08005909
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005910 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005911 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005912 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5913 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5914 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5915 if (hash_size == 0) {
5916 return PSA_ERROR_NOT_SUPPORTED;
5917 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005918
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005919 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5920 * we might fail later, which is somewhat unfriendly and potentially
5921 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 psa_status_t status = psa_hash_try_support(hash_alg);
5923 if (status != PSA_SUCCESS) {
5924 return status;
5925 }
5926 } else {
5927 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005928 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005929
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5931 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
5932 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
5933 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005934 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005935#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005936 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005937 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
5938 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005939 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005941#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5942 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 operation->capacity = 255 * hash_size;
5944 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005945}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005946
Gilles Peskine449bd832023-01-11 14:50:10 +01005947static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005948{
5949#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005950 if (alg == PSA_ALG_ECDH) {
5951 return PSA_SUCCESS;
5952 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005953#endif
5954 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005955 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005956}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005957
5958static int psa_key_derivation_allows_free_form_secret_input(
5959 psa_algorithm_t kdf_alg)
5960{
5961#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
5962 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5963 return 0;
5964 }
5965#endif
5966 (void) kdf_alg;
5967 return 1;
5968}
John Durkop07cc04a2020-11-16 22:08:34 -08005969#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005970
Gilles Peskine449bd832023-01-11 14:50:10 +01005971psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
5972 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005973{
5974 psa_status_t status;
5975
Gilles Peskine449bd832023-01-11 14:50:10 +01005976 if (operation->alg != 0) {
5977 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005978 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005979
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
5981 return PSA_ERROR_INVALID_ARGUMENT;
5982 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
5983#if defined(AT_LEAST_ONE_BUILTIN_KDF)
5984 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
5985 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
5986 status = psa_key_agreement_try_support(ka_alg);
5987 if (status != PSA_SUCCESS) {
5988 return status;
5989 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005990 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
5991 return PSA_ERROR_INVALID_ARGUMENT;
5992 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005993 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
5994#else
5995 return PSA_ERROR_NOT_SUPPORTED;
5996#endif /* AT_LEAST_ONE_BUILTIN_KDF */
5997 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
5998#if defined(AT_LEAST_ONE_BUILTIN_KDF)
5999 status = psa_key_derivation_setup_kdf(operation, alg);
6000#else
6001 return PSA_ERROR_NOT_SUPPORTED;
6002#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6003 } else {
6004 return PSA_ERROR_INVALID_ARGUMENT;
6005 }
6006
6007 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006008 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006009 }
6010 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006011}
6012
Przemek Stekiel69c46792022-06-10 12:59:51 +02006013#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006014static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6015 psa_algorithm_t kdf_alg,
6016 psa_key_derivation_step_t step,
6017 const uint8_t *data,
6018 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006019{
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006021 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006023 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006024#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6026 return PSA_ERROR_INVALID_ARGUMENT;
6027 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006028#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006029 if (hkdf->state != HKDF_STATE_INIT) {
6030 return PSA_ERROR_BAD_STATE;
6031 } else {
6032 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6033 hash_alg,
6034 data, data_length);
6035 if (status != PSA_SUCCESS) {
6036 return status;
6037 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006038 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006039 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006040 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006041 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006042#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006044 /* We shouldn't be in different state as HKDF_EXPAND only allows
6045 * two inputs: SECRET (this case) and INFO which does not modify
6046 * the state. It could happen only if the hkdf
6047 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 if (hkdf->state != HKDF_STATE_INIT) {
6049 return PSA_ERROR_BAD_STATE;
6050 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006051
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006052 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6054 return PSA_ERROR_INVALID_ARGUMENT;
6055 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006056
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 memcpy(hkdf->prk, data, data_length);
6058 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006059#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006060 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006061 /* HKDF: If no salt was provided, use an empty salt.
6062 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006064#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006065 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6066 return PSA_ERROR_BAD_STATE;
6067 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006068#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6070 hash_alg,
6071 NULL, 0);
6072 if (status != PSA_SUCCESS) {
6073 return status;
6074 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006075 hkdf->state = HKDF_STATE_STARTED;
6076 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 if (hkdf->state != HKDF_STATE_STARTED) {
6078 return PSA_ERROR_BAD_STATE;
6079 }
6080 status = psa_mac_update(&hkdf->hmac,
6081 data, data_length);
6082 if (status != PSA_SUCCESS) {
6083 return status;
6084 }
6085 status = psa_mac_sign_finish(&hkdf->hmac,
6086 hkdf->prk,
6087 sizeof(hkdf->prk),
6088 &data_length);
6089 if (status != PSA_SUCCESS) {
6090 return status;
6091 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006092 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006093
Gilles Peskine2b522db2019-04-12 15:11:49 +02006094 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006095 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006096#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006098 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006100 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006102#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006103 {
6104 /* Block 0 is empty, and the next block will be
6105 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006106 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006107 }
6108
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006110 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006111#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6113 return PSA_ERROR_INVALID_ARGUMENT;
6114 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006115#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006116#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6118 hkdf->state == HKDF_STATE_INIT) {
6119 return PSA_ERROR_BAD_STATE;
6120 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006121#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 if (hkdf->state == HKDF_STATE_OUTPUT) {
6123 return PSA_ERROR_BAD_STATE;
6124 }
6125 if (hkdf->info_set) {
6126 return PSA_ERROR_BAD_STATE;
6127 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006128 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 if (data_length != 0) {
6130 hkdf->info = mbedtls_calloc(1, data_length);
6131 if (hkdf->info == NULL) {
6132 return PSA_ERROR_INSUFFICIENT_MEMORY;
6133 }
6134 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006135 }
6136 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006137 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006138 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006140 }
6141}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006142#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006143
John Durkop07cc04a2020-11-16 22:08:34 -08006144#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6145 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006146static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6147 const uint8_t *data,
6148 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006149{
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6151 return PSA_ERROR_BAD_STATE;
6152 }
Janos Follathf08e2652019-06-13 09:05:41 +01006153
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 if (data_length != 0) {
6155 prf->seed = mbedtls_calloc(1, data_length);
6156 if (prf->seed == NULL) {
6157 return PSA_ERROR_INSUFFICIENT_MEMORY;
6158 }
Janos Follathf08e2652019-06-13 09:05:41 +01006159
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006161 prf->seed_length = data_length;
6162 }
Janos Follathf08e2652019-06-13 09:05:41 +01006163
Gilles Peskine43f958b2020-12-13 14:55:14 +01006164 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006165
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006167}
6168
Gilles Peskine449bd832023-01-11 14:50:10 +01006169static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6170 const uint8_t *data,
6171 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006172{
Gilles Peskine449bd832023-01-11 14:50:10 +01006173 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6174 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6175 return PSA_ERROR_BAD_STATE;
6176 }
Janos Follath81550542019-06-13 14:26:34 +01006177
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 if (data_length != 0) {
6179 prf->secret = mbedtls_calloc(1, data_length);
6180 if (prf->secret == NULL) {
6181 return PSA_ERROR_INSUFFICIENT_MEMORY;
6182 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006183
Gilles Peskine449bd832023-01-11 14:50:10 +01006184 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006185 prf->secret_length = data_length;
6186 }
Janos Follath81550542019-06-13 14:26:34 +01006187
Gilles Peskine43f958b2020-12-13 14:55:14 +01006188 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006189
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006191}
6192
Gilles Peskine449bd832023-01-11 14:50:10 +01006193static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6194 const uint8_t *data,
6195 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006196{
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6198 return PSA_ERROR_BAD_STATE;
6199 }
Janos Follath63028dd2019-06-13 09:15:47 +01006200
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 if (data_length != 0) {
6202 prf->label = mbedtls_calloc(1, data_length);
6203 if (prf->label == NULL) {
6204 return PSA_ERROR_INSUFFICIENT_MEMORY;
6205 }
Janos Follath63028dd2019-06-13 09:15:47 +01006206
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006208 prf->label_length = data_length;
6209 }
Janos Follath63028dd2019-06-13 09:15:47 +01006210
Gilles Peskine43f958b2020-12-13 14:55:14 +01006211 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006212
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006214}
6215
Gilles Peskine449bd832023-01-11 14:50:10 +01006216static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6217 psa_key_derivation_step_t step,
6218 const uint8_t *data,
6219 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006220{
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006222 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006223 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006224 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006226 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006228 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006230 }
6231}
John Durkop07cc04a2020-11-16 22:08:34 -08006232#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6233 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6234
6235#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6236static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6237 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006238 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006240{
6241 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006242 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6243 4 + data_length + prf->other_secret_length :
6244 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006245
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6247 return PSA_ERROR_INVALID_ARGUMENT;
6248 }
John Durkop07cc04a2020-11-16 22:08:34 -08006249
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 uint8_t *pms = mbedtls_calloc(1, pms_len);
6251 if (pms == NULL) {
6252 return PSA_ERROR_INSUFFICIENT_MEMORY;
6253 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006254 uint8_t *cur = pms;
6255
6256 /* pure-PSK:
6257 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006258 *
6259 * The premaster secret is formed as follows: if the PSK is N octets
6260 * long, concatenate a uint16 with the value N, N zero octets, a second
6261 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006262 *
6263 * mixed-PSK:
6264 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6265 * follows: concatenate a uint16 with the length of the other secret,
6266 * the other secret itself, uint16 with the length of PSK, and the
6267 * PSK itself.
6268 * For details please check:
6269 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6270 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6271 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006272 */
6273
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6275 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6276 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6277 if (prf->other_secret_length != 0) {
6278 memcpy(cur, prf->other_secret, prf->other_secret_length);
6279 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006280 cur += prf->other_secret_length;
6281 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 } else {
6283 *cur++ = MBEDTLS_BYTE_1(data_length);
6284 *cur++ = MBEDTLS_BYTE_0(data_length);
6285 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006286 cur += data_length;
6287 }
6288
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 *cur++ = MBEDTLS_BYTE_1(data_length);
6290 *cur++ = MBEDTLS_BYTE_0(data_length);
6291 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006292 cur += data_length;
6293
Gilles Peskine449bd832023-01-11 14:50:10 +01006294 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006295
Gilles Peskine449bd832023-01-11 14:50:10 +01006296 mbedtls_platform_zeroize(pms, pms_len);
6297 mbedtls_free(pms);
6298 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006299}
Janos Follath6660f0e2019-06-17 08:44:03 +01006300
Przemek Stekiele47201b2022-04-19 13:53:28 +02006301static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6302 psa_tls12_prf_key_derivation_t *prf,
6303 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006304 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006305{
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6307 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006308 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006309
6310 if (data_length != 0) {
6311 prf->other_secret = mbedtls_calloc(1, data_length);
6312 if (prf->other_secret == NULL) {
6313 return PSA_ERROR_INSUFFICIENT_MEMORY;
6314 }
6315
6316 memcpy(prf->other_secret, data, data_length);
6317 prf->other_secret_length = data_length;
6318 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006319 prf->other_secret_length = 0;
6320 }
6321
6322 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6323
Gilles Peskine449bd832023-01-11 14:50:10 +01006324 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006325}
6326
Janos Follath6660f0e2019-06-17 08:44:03 +01006327static psa_status_t psa_tls12_prf_psk_to_ms_input(
6328 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006329 psa_key_derivation_step_t step,
6330 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006332{
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006334 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 return psa_tls12_prf_psk_to_ms_set_key(prf,
6336 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006337 break;
6338 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006339 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6340 data,
6341 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006342 break;
6343 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006345 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006346
Przemek Stekiele47201b2022-04-19 13:53:28 +02006347 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006348}
John Durkop07cc04a2020-11-16 22:08:34 -08006349#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006350
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006351#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6352static psa_status_t psa_tls12_ecjpake_to_pms_input(
6353 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006354 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006355 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006357{
Gilles Peskine449bd832023-01-11 14:50:10 +01006358 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6359 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6360 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006361 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006362
6363 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 if (data[0] != 0x04) {
6365 return PSA_ERROR_INVALID_ARGUMENT;
6366 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006367
6368 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006370
Gilles Peskine449bd832023-01-11 14:50:10 +01006371 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006372}
6373#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006374/** Check whether the given key type is acceptable for the given
6375 * input step of a key derivation.
6376 *
6377 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6378 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6379 * Both secret and non-secret inputs can alternatively have the type
6380 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6381 * that the input was passed as a buffer rather than via a key object.
6382 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006383static int psa_key_derivation_check_input_type(
6384 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006386{
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006388 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 if (key_type == PSA_KEY_TYPE_DERIVE) {
6390 return PSA_SUCCESS;
6391 }
6392 if (key_type == PSA_KEY_TYPE_NONE) {
6393 return PSA_SUCCESS;
6394 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006395 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006396 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 if (key_type == PSA_KEY_TYPE_DERIVE) {
6398 return PSA_SUCCESS;
6399 }
6400 if (key_type == PSA_KEY_TYPE_NONE) {
6401 return PSA_SUCCESS;
6402 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006403 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006404 case PSA_KEY_DERIVATION_INPUT_LABEL:
6405 case PSA_KEY_DERIVATION_INPUT_SALT:
6406 case PSA_KEY_DERIVATION_INPUT_INFO:
6407 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6409 return PSA_SUCCESS;
6410 }
6411 if (key_type == PSA_KEY_TYPE_NONE) {
6412 return PSA_SUCCESS;
6413 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006414 break;
6415 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006417}
6418
Janos Follathb80a94e2019-06-12 15:54:46 +01006419static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006420 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006421 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006422 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006423 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006424 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006425{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006426 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006428
Gilles Peskine449bd832023-01-11 14:50:10 +01006429 status = psa_key_derivation_check_input_type(step, key_type);
6430 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006431 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006432 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006433
Przemek Stekiel69c46792022-06-10 12:59:51 +02006434#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006435 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6436 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6437 step, data, data_length);
6438 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006439#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006440#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6442 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6443 step, data, data_length);
6444 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006445#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6446#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006447 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6448 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6449 step, data, data_length);
6450 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006451#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006452#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006453 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006454 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006455 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6456 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006457#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006458 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006459 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006460 (void) data;
6461 (void) data_length;
6462 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006464 }
6465
Gilles Peskine224b0d62019-09-23 18:13:17 +02006466exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006467 if (status != PSA_SUCCESS) {
6468 psa_key_derivation_abort(operation);
6469 }
6470 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006471}
6472
Janos Follath51f4a0f2019-06-14 11:35:55 +01006473psa_status_t psa_key_derivation_input_bytes(
6474 psa_key_derivation_operation_t *operation,
6475 psa_key_derivation_step_t step,
6476 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006478{
Gilles Peskine449bd832023-01-11 14:50:10 +01006479 return psa_key_derivation_input_internal(operation, step,
6480 PSA_KEY_TYPE_NONE,
6481 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006482}
6483
Janos Follath51f4a0f2019-06-14 11:35:55 +01006484psa_status_t psa_key_derivation_input_key(
6485 psa_key_derivation_operation_t *operation,
6486 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006487 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006488{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006489 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006490 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006491 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006492
Ronald Cron5c522922020-11-14 16:35:34 +01006493 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6495 if (status != PSA_SUCCESS) {
6496 psa_key_derivation_abort(operation);
6497 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006498 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006499
6500 /* Passing a key object as a SECRET input unlocks the permission
6501 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006502 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006503 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006505
Gilles Peskine449bd832023-01-11 14:50:10 +01006506 status = psa_key_derivation_input_internal(operation,
6507 step, slot->attr.type,
6508 slot->key.data,
6509 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006510
Gilles Peskine449bd832023-01-11 14:50:10 +01006511 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006512
Gilles Peskine449bd832023-01-11 14:50:10 +01006513 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006514}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006515
6516
6517
6518/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006519/* Key agreement */
6520/****************************************************************/
6521
Gilles Peskine449bd832023-01-11 14:50:10 +01006522psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6523 const uint8_t *key_buffer,
6524 size_t key_buffer_size,
6525 psa_algorithm_t alg,
6526 const uint8_t *peer_key,
6527 size_t peer_key_length,
6528 uint8_t *shared_secret,
6529 size_t shared_secret_size,
6530 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006531{
Gilles Peskine449bd832023-01-11 14:50:10 +01006532 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006533#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006534 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006535 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6536 key_buffer_size, alg,
6537 peer_key, peer_key_length,
6538 shared_secret,
6539 shared_secret_size,
6540 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006541#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006542 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006543 (void) attributes;
6544 (void) key_buffer;
6545 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006546 (void) peer_key;
6547 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006548 (void) shared_secret;
6549 (void) shared_secret_size;
6550 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006551 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006552 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006553}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006554
Aditya Deshpande17845b82022-10-13 17:21:01 +01006555/** Internal function for raw key agreement
6556 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006557 * to the driver's implementation if a driver is present.
6558 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006559 * (psa_key_agreement_raw_builtin).
6560 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006561static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6562 psa_key_slot_t *private_key,
6563 const uint8_t *peer_key,
6564 size_t peer_key_length,
6565 uint8_t *shared_secret,
6566 size_t shared_secret_size,
6567 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006568{
Gilles Peskine449bd832023-01-11 14:50:10 +01006569 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6570 return PSA_ERROR_NOT_SUPPORTED;
6571 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006572
6573 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006574 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006575 };
6576
Gilles Peskine449bd832023-01-11 14:50:10 +01006577 return psa_driver_wrapper_key_agreement(&attributes,
6578 private_key->key.data,
6579 private_key->key.bytes, alg,
6580 peer_key, peer_key_length,
6581 shared_secret,
6582 shared_secret_size,
6583 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006584}
6585
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006586/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006587 * to potentially free embedded data structures and wipe confidential data.
6588 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006589static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6590 psa_key_derivation_step_t step,
6591 psa_key_slot_t *private_key,
6592 const uint8_t *peer_key,
6593 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006594{
6595 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006596 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006597 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006598 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006599
6600 /* Step 1: run the secret agreement algorithm to generate the shared
6601 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006602 status = psa_key_agreement_raw_internal(ka_alg,
6603 private_key,
6604 peer_key, peer_key_length,
6605 shared_secret,
6606 sizeof(shared_secret),
6607 &shared_secret_length);
6608 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006609 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006610 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006611
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006612 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006613 * the shared secret. A shared secret is permitted wherever a key
6614 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006615 status = psa_key_derivation_input_internal(operation, step,
6616 PSA_KEY_TYPE_DERIVE,
6617 shared_secret,
6618 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006619exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006620 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6621 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006622}
6623
Gilles Peskine449bd832023-01-11 14:50:10 +01006624psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6625 psa_key_derivation_step_t step,
6626 mbedtls_svc_key_id_t private_key,
6627 const uint8_t *peer_key,
6628 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006629{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006630 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006631 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006632 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006633
Gilles Peskine449bd832023-01-11 14:50:10 +01006634 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6635 return PSA_ERROR_INVALID_ARGUMENT;
6636 }
Ronald Cron5c522922020-11-14 16:35:34 +01006637 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006638 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6639 if (status != PSA_SUCCESS) {
6640 return status;
6641 }
6642 status = psa_key_agreement_internal(operation, step,
6643 slot,
6644 peer_key, peer_key_length);
6645 if (status != PSA_SUCCESS) {
6646 psa_key_derivation_abort(operation);
6647 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006648 /* If a private key has been added as SECRET, we allow the derived
6649 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006650 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006651 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006652 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006653 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006654
Gilles Peskine449bd832023-01-11 14:50:10 +01006655 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006656
Gilles Peskine449bd832023-01-11 14:50:10 +01006657 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006658}
6659
Gilles Peskine449bd832023-01-11 14:50:10 +01006660psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6661 mbedtls_svc_key_id_t private_key,
6662 const uint8_t *peer_key,
6663 size_t peer_key_length,
6664 uint8_t *output,
6665 size_t output_size,
6666 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006667{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006668 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006669 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006670 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006671
Gilles Peskine449bd832023-01-11 14:50:10 +01006672 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006673 status = PSA_ERROR_INVALID_ARGUMENT;
6674 goto exit;
6675 }
Ronald Cron5c522922020-11-14 16:35:34 +01006676 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006677 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6678 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006679 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006680 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006681
Gilles Peskine3e561302022-04-14 00:17:15 +02006682 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6683 * for the output size. The PSA specification only guarantees that this
6684 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6685 * but it might be nice to allow smaller buffers if the output fits.
6686 * At the time of writing this comment, with only ECDH implemented,
6687 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6688 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6689 * be exact for it as well. */
6690 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6692 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006693 status = PSA_ERROR_BUFFER_TOO_SMALL;
6694 goto exit;
6695 }
6696
Gilles Peskine449bd832023-01-11 14:50:10 +01006697 status = psa_key_agreement_raw_internal(alg, slot,
6698 peer_key, peer_key_length,
6699 output, output_size,
6700 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006701
6702exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006703 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006704 /* If an error happens and is not handled properly, the output
6705 * may be used as a key to protect sensitive data. Arrange for such
6706 * a key to be random, which is likely to result in decryption or
6707 * verification errors. This is better than filling the buffer with
6708 * some constant data such as zeros, which would result in the data
6709 * being protected with a reproducible, easily knowable key.
6710 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006711 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006712 *output_length = output_size;
6713 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006714
Gilles Peskine449bd832023-01-11 14:50:10 +01006715 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006716
Gilles Peskine449bd832023-01-11 14:50:10 +01006717 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006718}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006719
6720
Gilles Peskine30524eb2020-11-13 17:02:26 +01006721
Gilles Peskine86a440b2018-11-12 18:39:40 +01006722/****************************************************************/
6723/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006724/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006725
Gilles Peskine30524eb2020-11-13 17:02:26 +01006726/** Initialize the PSA random generator.
6727 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006728static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006729{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006730#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006731 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006732#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6733
Gilles Peskine30524eb2020-11-13 17:02:26 +01006734 /* Set default configuration if
6735 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006736 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006737 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006738 }
6739 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006740 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006741 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006742
Gilles Peskine449bd832023-01-11 14:50:10 +01006743 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006744#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6745 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6746 /* The PSA entropy injection feature depends on using NV seed as an entropy
6747 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006748 mbedtls_entropy_add_source(&rng->entropy,
6749 mbedtls_nv_seed_poll, NULL,
6750 MBEDTLS_ENTROPY_BLOCK_SIZE,
6751 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006752#endif
6753
Gilles Peskine449bd832023-01-11 14:50:10 +01006754 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006755#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006756}
6757
6758/** Deinitialize the PSA random generator.
6759 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006760static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006761{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006762#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006763 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006764#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01006765 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
6766 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006767#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006768}
6769
6770/** Seed the PSA random generator.
6771 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006772static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006773{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006774#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6775 /* Do nothing: the external RNG seeds itself. */
6776 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006777 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006778#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006779 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01006780 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
6781 drbg_seed, sizeof(drbg_seed) - 1);
6782 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006783#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006784}
6785
Gilles Peskine449bd832023-01-11 14:50:10 +01006786psa_status_t psa_generate_random(uint8_t *output,
6787 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006788{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006789 GUARD_MODULE_INITIALIZED;
6790
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006791#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6792
6793 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006794 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6795 output, output_size,
6796 &output_length);
6797 if (status != PSA_SUCCESS) {
6798 return status;
6799 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006800 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006801 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006802 if (output_length != output_size) {
6803 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6804 }
6805 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006806
6807#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6808
Gilles Peskine449bd832023-01-11 14:50:10 +01006809 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006810 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006811 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6812 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6813 output_size);
6814 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6815 output, request_size);
6816 if (ret != 0) {
6817 return mbedtls_to_psa_error(ret);
6818 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006819 output_size -= request_size;
6820 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006821 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006822 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006823#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006824}
6825
Gilles Peskine8814fc42020-12-14 15:33:44 +01006826/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006827 *
6828 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6829 * `psa_generate_random(...)`. The state parameter is ignored since the
6830 * PSA API doesn't support passing an explicit state.
6831 *
6832 * In the non-external case, psa_generate_random() calls an
6833 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6834 * and semantics as mbedtls_psa_get_random(). As an optimization,
6835 * instead of doing this back-and-forth between the PSA API and the
6836 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6837 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006838 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006839#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6840int mbedtls_psa_get_random(void *p_rng,
6841 unsigned char *output,
6842 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01006843{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006844 /* This function takes a pointer to the RNG state because that's what
6845 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6846 * its own state internally and doesn't let the caller access that state.
6847 * So we just ignore the state parameter, and in practice we'll pass
6848 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006849 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006850 psa_status_t status = psa_generate_random(output, output_size);
6851 if (status == PSA_SUCCESS) {
6852 return 0;
6853 } else {
6854 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
6855 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01006856}
6857#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6858
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006859#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006860#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006861
Gilles Peskine449bd832023-01-11 14:50:10 +01006862psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
6863 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006864{
Gilles Peskine449bd832023-01-11 14:50:10 +01006865 if (global_data.initialized) {
6866 return PSA_ERROR_NOT_PERMITTED;
6867 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006868
Gilles Peskine449bd832023-01-11 14:50:10 +01006869 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
6870 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
6871 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
6872 return PSA_ERROR_INVALID_ARGUMENT;
6873 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006874
Gilles Peskine449bd832023-01-11 14:50:10 +01006875 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006876}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006877#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006878
Ronald Cron3772afe2021-02-08 16:10:05 +01006879/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006880 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006881 * \param type The key type
6882 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006883 *
6884 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006885 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006886 * \retval #PSA_ERROR_INVALID_ARGUMENT
6887 * The size in bits of the key is not valid.
6888 * \retval #PSA_ERROR_NOT_SUPPORTED
6889 * The type and/or the size in bits of the key or the combination of
6890 * the two is not supported.
6891 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006892static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006893 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006894{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006895 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006896
Gilles Peskine449bd832023-01-11 14:50:10 +01006897 if (key_type_is_raw_bytes(type)) {
6898 status = psa_validate_unstructured_key_bit_size(type, bits);
6899 if (status != PSA_SUCCESS) {
6900 return status;
6901 }
6902 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006903#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006904 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6905 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
6906 return PSA_ERROR_NOT_SUPPORTED;
6907 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006908
Ronald Cron01b2aba2020-10-05 09:42:02 +02006909 /* Accept only byte-aligned keys, for the same reasons as
6910 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006911 if (bits % 8 != 0) {
6912 return PSA_ERROR_NOT_SUPPORTED;
6913 }
6914 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006915#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006916
Ronald Cron5c4d3862020-12-07 11:07:24 +01006917#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006918 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01006919 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006920 return PSA_SUCCESS;
6921 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006922#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006923 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006924 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006925 }
6926
Gilles Peskine449bd832023-01-11 14:50:10 +01006927 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006928}
6929
Ronald Cron55ed0592020-10-05 10:30:40 +02006930psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006931 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01006932 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006933{
6934 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006935 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006936
Gilles Peskine449bd832023-01-11 14:50:10 +01006937 if ((attributes->domain_parameters == NULL) &&
6938 (attributes->domain_parameters_size != 0)) {
6939 return PSA_ERROR_INVALID_ARGUMENT;
6940 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02006941
Gilles Peskine449bd832023-01-11 14:50:10 +01006942 if (key_type_is_raw_bytes(type)) {
6943 status = psa_generate_random(key_buffer, key_buffer_size);
6944 if (status != PSA_SUCCESS) {
6945 return status;
6946 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006947
David Brown12ca5032021-02-11 11:02:00 -07006948#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006949 if (type == PSA_KEY_TYPE_DES) {
6950 psa_des_set_key_parity(key_buffer, key_buffer_size);
6951 }
David Brown8107e312021-02-12 08:08:46 -07006952#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01006953 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01006954
Jaeden Amero424fa932021-05-14 08:34:32 +01006955#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
6956 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01006957 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
6958 return mbedtls_psa_rsa_generate_key(attributes,
6959 key_buffer,
6960 key_buffer_size,
6961 key_buffer_length);
6962 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01006963#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
6964 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006965
John Durkop9814fa22020-11-04 12:28:15 -08006966#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006967 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6968 return mbedtls_psa_ecp_generate_key(attributes,
6969 key_buffer,
6970 key_buffer_size,
6971 key_buffer_length);
6972 } else
John Durkop9814fa22020-11-04 12:28:15 -08006973#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006974 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006975 (void) key_buffer_length;
6976 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02006977 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006978
Gilles Peskine449bd832023-01-11 14:50:10 +01006979 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006980}
Darryl Green0c6575a2018-11-07 16:05:30 +00006981
Gilles Peskine449bd832023-01-11 14:50:10 +01006982psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
6983 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006984{
6985 psa_status_t status;
6986 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006987 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006988 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006989
Ronald Cron81709fc2020-11-14 12:10:32 +01006990 *key = MBEDTLS_SVC_KEY_ID_INIT;
6991
Gilles Peskine0f84d622019-09-12 19:03:13 +02006992 /* Reject any attempt to create a zero-length key so that we don't
6993 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006994 if (psa_get_key_bits(attributes) == 0) {
6995 return PSA_ERROR_INVALID_ARGUMENT;
6996 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006997
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02006998 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006999 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7000 return PSA_ERROR_INVALID_ARGUMENT;
7001 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007002
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7004 &slot, &driver);
7005 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007006 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007007 }
Gilles Peskine11792082019-08-06 18:36:36 +02007008
Ronald Cron977c2472020-10-13 08:32:21 +02007009 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307010 * storage ( thus not in the case of generating a key in a secure element
7011 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7012 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007013 if (slot->key.data == NULL) {
7014 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7015 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007016 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007017 attributes->core.type, attributes->core.bits);
7018 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007019 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007020 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007021
7022 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007023 attributes->core.type,
7024 attributes->core.bits);
7025 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007026 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007027 attributes, &key_buffer_size);
7028 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007029 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007030 }
Ronald Cron977c2472020-10-13 08:32:21 +02007031 }
Ronald Cron977c2472020-10-13 08:32:21 +02007032
Gilles Peskine449bd832023-01-11 14:50:10 +01007033 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7034 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007035 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007036 }
Ronald Cron977c2472020-10-13 08:32:21 +02007037 }
7038
Gilles Peskine449bd832023-01-11 14:50:10 +01007039 status = psa_driver_wrapper_generate_key(attributes,
7040 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007041
Gilles Peskine449bd832023-01-11 14:50:10 +01007042 if (status != PSA_SUCCESS) {
7043 psa_remove_key_data_from_memory(slot);
7044 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007045
Gilles Peskine11792082019-08-06 18:36:36 +02007046exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007047 if (status == PSA_SUCCESS) {
7048 status = psa_finish_key_creation(slot, driver, key);
7049 }
7050 if (status != PSA_SUCCESS) {
7051 psa_fail_key_creation(slot, driver);
7052 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007053
Gilles Peskine449bd832023-01-11 14:50:10 +01007054 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007055}
7056
Gilles Peskinee59236f2018-01-27 23:32:46 +01007057/****************************************************************/
7058/* Module setup */
7059/****************************************************************/
7060
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007061#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007062psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007063 void (* entropy_init)(mbedtls_entropy_context *ctx),
7064 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007065{
Gilles Peskine449bd832023-01-11 14:50:10 +01007066 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7067 return PSA_ERROR_BAD_STATE;
7068 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007069 global_data.rng.entropy_init = entropy_init;
7070 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007071 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007072}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007073#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007074
Gilles Peskine449bd832023-01-11 14:50:10 +01007075void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007076{
Gilles Peskine449bd832023-01-11 14:50:10 +01007077 psa_wipe_all_key_slots();
7078 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7079 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007080 }
7081 /* Wipe all remaining data, including configuration.
7082 * In particular, this sets all state indicator to the value
7083 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007084 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007085
7086 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007087 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007088}
7089
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007090#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7091/** Recover a transaction that was interrupted by a power failure.
7092 *
7093 * This function is called during initialization, before psa_crypto_init()
7094 * returns. If this function returns a failure status, the initialization
7095 * fails.
7096 */
7097static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007099{
Gilles Peskine449bd832023-01-11 14:50:10 +01007100 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007101 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7102 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007103 /* TODO - fall through to the failure case until this
7104 * is implemented.
7105 * https://github.com/ARMmbed/mbed-crypto/issues/218
7106 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007107 default:
7108 /* We found an unsupported transaction in the storage.
7109 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007110 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007111 }
7112}
7113#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7114
Gilles Peskine449bd832023-01-11 14:50:10 +01007115psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007116{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007117 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007118
Gilles Peskinec6b69072018-11-20 21:42:52 +01007119 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007120 if (global_data.initialized != 0) {
7121 return PSA_SUCCESS;
7122 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007123
Gilles Peskine30524eb2020-11-13 17:02:26 +01007124 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007125 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007126 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007127 status = mbedtls_psa_random_seed(&global_data.rng);
7128 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007129 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007130 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007131 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007132
Gilles Peskine449bd832023-01-11 14:50:10 +01007133 status = psa_initialize_key_slots();
7134 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007135 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007136 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007137
Ronald Cron9ba76912021-04-10 16:57:30 +02007138 /* Init drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007139 status = psa_driver_wrapper_init();
7140 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02007141 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007142 }
Gilles Peskined9348f22019-10-01 15:22:29 +02007143
Gilles Peskine4b734222019-07-24 15:56:31 +02007144#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007145 status = psa_crypto_load_transaction();
7146 if (status == PSA_SUCCESS) {
7147 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7148 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007149 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007150 }
7151 status = psa_crypto_stop_transaction();
7152 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007153 /* There's no transaction to complete. It's all good. */
7154 status = PSA_SUCCESS;
7155 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007156#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007157
Gilles Peskinec6b69072018-11-20 21:42:52 +01007158 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007159 global_data.initialized = 1;
7160
7161exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007162 if (status != PSA_SUCCESS) {
7163 mbedtls_psa_crypto_free();
7164 }
7165 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007166}
7167
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007168psa_status_t psa_crypto_driver_pake_get_password_len(
7169 const psa_crypto_driver_pake_inputs_t *inputs,
7170 size_t *password_len)
7171{
7172 if (inputs->password_len == 0) {
7173 return PSA_ERROR_BAD_STATE;
7174 }
7175
7176 *password_len = inputs->password_len;
7177
7178 return PSA_SUCCESS;
7179}
7180
7181psa_status_t psa_crypto_driver_pake_get_password(
7182 const psa_crypto_driver_pake_inputs_t *inputs,
7183 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7184{
7185 if (inputs->password_len == 0) {
7186 return PSA_ERROR_BAD_STATE;
7187 }
7188
7189 if (buffer_size < inputs->password_len) {
7190 return PSA_ERROR_BUFFER_TOO_SMALL;
7191 }
7192
7193 memcpy(buffer, inputs->password, inputs->password_len);
7194 *buffer_length = inputs->password_len;
7195
7196 return PSA_SUCCESS;
7197}
7198
7199psa_status_t psa_crypto_driver_pake_get_role(
7200 const psa_crypto_driver_pake_inputs_t *inputs,
7201 psa_pake_role_t *role)
7202{
7203 if (inputs->role == PSA_PAKE_ROLE_NONE) {
7204 return PSA_ERROR_BAD_STATE;
7205 }
7206
7207 *role = inputs->role;
7208
7209 return PSA_SUCCESS;
7210}
7211
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007212psa_status_t psa_crypto_driver_pake_get_user_len(
7213 const psa_crypto_driver_pake_inputs_t *inputs,
7214 size_t *user_len)
7215{
7216 if (inputs->user_len == 0) {
7217 return PSA_ERROR_BAD_STATE;
7218 }
7219
7220 *user_len = inputs->user_len;
7221
7222 return PSA_SUCCESS;
7223}
7224
7225psa_status_t psa_crypto_driver_pake_get_user(
7226 const psa_crypto_driver_pake_inputs_t *inputs,
7227 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7228{
7229 if (inputs->user_len == 0) {
7230 return PSA_ERROR_BAD_STATE;
7231 }
7232
7233 if (buffer_size < inputs->user_len) {
7234 return PSA_ERROR_BUFFER_TOO_SMALL;
7235 }
7236
7237 memcpy(buffer, inputs->user, inputs->user_len);
7238 *buffer_length = inputs->user_len;
7239
7240 return PSA_SUCCESS;
7241}
7242
7243psa_status_t psa_crypto_driver_pake_get_peer_len(
7244 const psa_crypto_driver_pake_inputs_t *inputs,
7245 size_t *peer_len)
7246{
7247 if (inputs->peer_len == 0) {
7248 return PSA_ERROR_BAD_STATE;
7249 }
7250
7251 *peer_len = inputs->peer_len;
7252
7253 return PSA_SUCCESS;
7254}
7255
7256psa_status_t psa_crypto_driver_pake_get_peer(
7257 const psa_crypto_driver_pake_inputs_t *inputs,
7258 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7259{
7260 if (inputs->peer_len == 0) {
7261 return PSA_ERROR_BAD_STATE;
7262 }
7263
7264 if (buffer_size < inputs->peer_len) {
7265 return PSA_ERROR_BUFFER_TOO_SMALL;
7266 }
7267
7268 memcpy(buffer, inputs->peer, inputs->peer_len);
7269 *buffer_length = inputs->peer_len;
7270
7271 return PSA_SUCCESS;
7272}
7273
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007274psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7275 const psa_crypto_driver_pake_inputs_t *inputs,
7276 psa_pake_cipher_suite_t *cipher_suite)
7277{
7278 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7279 return PSA_ERROR_BAD_STATE;
7280 }
7281
7282 *cipher_suite = inputs->cipher_suite;
7283
7284 return PSA_SUCCESS;
7285}
7286
Neil Armstronga7d08c32022-06-01 18:21:20 +02007287psa_status_t psa_pake_setup(
7288 psa_pake_operation_t *operation,
7289 const psa_pake_cipher_suite_t *cipher_suite)
7290{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007291 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007292
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007293 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007294 status = PSA_ERROR_BAD_STATE;
7295 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007296 }
7297
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007298 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007299 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007300 status = PSA_ERROR_INVALID_ARGUMENT;
7301 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007302 }
7303
Przemek Stekiel51eac532022-12-07 11:04:51 +01007304 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7305
Przemek Stekiele12ed362022-12-21 12:54:46 +01007306 operation->alg = cipher_suite->algorithm;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007307 operation->data.inputs.cipher_suite = *cipher_suite;
7308
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007309#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007310 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007311 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007312 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007313
Przemek Stekiele12ed362022-12-21 12:54:46 +01007314 computation_stage->state = PSA_PAKE_STATE_SETUP;
7315 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7316 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7317 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007318 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007319#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007320 {
7321 status = PSA_ERROR_NOT_SUPPORTED;
7322 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007323 }
7324
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007325 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7326
Przemek Stekiel51eac532022-12-07 11:04:51 +01007327 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007328exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007329 psa_pake_abort(operation);
7330 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007331}
7332
7333psa_status_t psa_pake_set_password_key(
7334 psa_pake_operation_t *operation,
7335 mbedtls_svc_key_id_t password)
7336{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007337 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007338 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7339 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007340
Przemek Stekiel51eac532022-12-07 11:04:51 +01007341 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007342 status = PSA_ERROR_BAD_STATE;
7343 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007344 }
7345
Przemek Stekiel6c764412022-11-22 14:05:12 +01007346 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7347 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007348 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007349 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007350 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007351 }
7352
Przemek Stekiel6c764412022-11-22 14:05:12 +01007353 psa_key_attributes_t attributes = {
7354 .core = slot->attr
7355 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007356
Przemek Stekiel51eac532022-12-07 11:04:51 +01007357 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007358
Przemek Stekiel51eac532022-12-07 11:04:51 +01007359 if (type != PSA_KEY_TYPE_PASSWORD &&
7360 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7361 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007362 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007363 }
7364
Przemek Stekiel51eac532022-12-07 11:04:51 +01007365 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7366 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007367 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007368 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007369 }
7370
7371 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7372 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007373 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007374exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007375 if (status != PSA_SUCCESS) {
7376 psa_pake_abort(operation);
7377 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007378 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007379 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007380}
7381
7382psa_status_t psa_pake_set_user(
7383 psa_pake_operation_t *operation,
7384 const uint8_t *user_id,
7385 size_t user_id_len)
7386{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007387 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007388
7389 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007390 status = PSA_ERROR_BAD_STATE;
7391 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007392 }
7393
Przemek Stekiel51eac532022-12-07 11:04:51 +01007394 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007395 status = PSA_ERROR_INVALID_ARGUMENT;
7396 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007397 }
7398
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007399 if (operation->data.inputs.peer_len != 0) {
7400 status = PSA_ERROR_BAD_STATE;
7401 goto exit;
7402 }
7403
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007404 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007405 if (memcmp(peer_id, PSA_JPAKE_SERVER_ID, peer_id_len) != 0 &&
7406 memcmp(peer_id, PSA_JPAKE_CLIENT_ID, peer_id_len) != 0) {
7407 status = PSA_ERROR_NOT_SUPPORTED;
7408 goto exit;
7409 }
7410
7411 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7412 if (operation->data.inputs.peer == NULL) {
7413 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7414 goto exit;
7415 }
7416
7417 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7418 operation->data.inputs.peer_len = peer_id_len;
7419
7420 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007421exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007422 psa_pake_abort(operation);
7423 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007424}
7425
7426psa_status_t psa_pake_set_peer(
7427 psa_pake_operation_t *operation,
7428 const uint8_t *peer_id,
7429 size_t peer_id_len)
7430{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007431 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007432
7433 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007434 status = PSA_ERROR_BAD_STATE;
7435 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007436 }
7437
Przemek Stekiel51eac532022-12-07 11:04:51 +01007438 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007439 status = PSA_ERROR_INVALID_ARGUMENT;
7440 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007441 }
7442
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007443 if (operation->data.inputs.user_len != 0) {
7444 status = PSA_ERROR_BAD_STATE;
7445 goto exit;
7446 }
7447
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007448 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007449 if (memcmp(user_id, PSA_JPAKE_SERVER_ID, user_id_len) != 0 &&
7450 memcmp(user_id, PSA_JPAKE_CLIENT_ID, user_id_len) != 0) {
7451 status = PSA_ERROR_NOT_SUPPORTED;
7452 goto exit;
7453 }
7454
7455 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7456 if (operation->data.inputs.user == NULL) {
7457 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7458 goto exit;
7459 }
7460
7461 memcpy(operation->data.inputs.user, user_id, user_id_len);
7462 operation->data.inputs.user_len = user_id_len;
7463
7464 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007465exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007466 psa_pake_abort(operation);
7467 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007468}
7469
7470psa_status_t psa_pake_set_role(
7471 psa_pake_operation_t *operation,
7472 psa_pake_role_t role)
7473{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007474 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007475
Przemek Stekiel51eac532022-12-07 11:04:51 +01007476 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007477 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007478 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007479 }
7480
Przemek Stekiel09104b82023-03-06 14:11:51 +01007481 switch (operation->alg) {
7482#if defined(PSA_WANT_ALG_JPAKE)
7483 case PSA_ALG_JPAKE:
7484 if (role == PSA_PAKE_ROLE_NONE) {
7485 return PSA_SUCCESS;
7486 }
7487 status = PSA_ERROR_INVALID_ARGUMENT;
7488 break;
7489#endif
7490 default:
7491 (void) role;
7492 status = PSA_ERROR_NOT_SUPPORTED;
7493 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007494 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007495exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007496 psa_pake_abort(operation);
7497 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007498}
7499
Przemek Stekielb09c4872023-01-17 12:05:38 +01007500/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007501#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007502static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007503 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007504{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007505 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007506 case PSA_PAKE_OUTPUT_X1_X2:
7507 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007508 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007509 case PSA_PAKE_X1_STEP_KEY_SHARE:
7510 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007511 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7512 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007513 case PSA_PAKE_X1_STEP_ZK_PROOF:
7514 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007515 case PSA_PAKE_X2_STEP_KEY_SHARE:
7516 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007517 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7518 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007519 case PSA_PAKE_X2_STEP_ZK_PROOF:
7520 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007521 default:
7522 return PSA_JPAKE_STEP_INVALID;
7523 }
7524 break;
7525 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007526 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007527 case PSA_PAKE_X1_STEP_KEY_SHARE:
7528 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007529 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7530 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007531 case PSA_PAKE_X1_STEP_ZK_PROOF:
7532 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007533 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007534 return PSA_JPAKE_STEP_INVALID;
7535 }
7536 break;
7537 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007538 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007539 case PSA_PAKE_X1_STEP_KEY_SHARE:
7540 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007541 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7542 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007543 case PSA_PAKE_X1_STEP_ZK_PROOF:
7544 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007545 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007546 return PSA_JPAKE_STEP_INVALID;
7547 }
7548 break;
7549 default:
7550 return PSA_JPAKE_STEP_INVALID;
7551 }
7552 return PSA_JPAKE_STEP_INVALID;
7553}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007554#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007555
Przemek Stekiel2797d372022-12-22 11:19:22 +01007556static psa_status_t psa_pake_complete_inputs(
7557 psa_pake_operation_t *operation)
7558{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007559 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007560 /* Create copy of the inputs on stack as inputs share memory
7561 with the driver context which will be setup by the driver. */
7562 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007563
Przemek Stekiele1d51bf2023-02-14 14:28:33 +01007564 if (inputs.password_len == 0 ||
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007565 inputs.user_len == 0 ||
7566 inputs.peer_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007567 return PSA_ERROR_BAD_STATE;
7568 }
7569
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007570 if (operation->alg == PSA_ALG_JPAKE) {
7571 if (memcmp(inputs.user, PSA_JPAKE_CLIENT_ID, inputs.user_len) == 0 &&
7572 memcmp(inputs.peer, PSA_JPAKE_SERVER_ID, inputs.peer_len) == 0) {
7573 inputs.role = PSA_PAKE_ROLE_CLIENT;
7574 } else
7575 if (memcmp(inputs.user, PSA_JPAKE_SERVER_ID, inputs.user_len) == 0 &&
7576 memcmp(inputs.peer, PSA_JPAKE_CLIENT_ID, inputs.peer_len) == 0) {
7577 inputs.role = PSA_PAKE_ROLE_SERVER;
7578 }
7579
7580 if (inputs.role != PSA_PAKE_ROLE_CLIENT &&
7581 inputs.role != PSA_PAKE_ROLE_SERVER) {
7582 return PSA_ERROR_NOT_SUPPORTED;
7583 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007584 }
7585
Przemek Stekiel18620a32023-01-17 16:34:52 +01007586 /* Clear driver context */
7587 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7588
7589 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007590
7591 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007592 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7593 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007594
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007595 /* User and peer are translated to role. */
7596 mbedtls_free(inputs.user);
7597 mbedtls_free(inputs.peer);
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007598 inputs.user = NULL; inputs.user_len = 0;
7599 inputs.peer = NULL; inputs.peer_len = 0;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007600
Przemek Stekiel2797d372022-12-22 11:19:22 +01007601 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007602#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007603 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007604 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007605 psa_jpake_computation_stage_t *computation_stage =
7606 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007607 computation_stage->state = PSA_PAKE_STATE_READY;
7608 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7609 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7610 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007611 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007612#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007613 {
7614 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007615 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007616 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007617 return status;
7618}
7619
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007620#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007621static psa_status_t psa_jpake_output_prologue(
7622 psa_pake_operation_t *operation,
7623 psa_pake_step_t step)
7624{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007625 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7626 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7627 step != PSA_PAKE_STEP_ZK_PROOF) {
7628 return PSA_ERROR_INVALID_ARGUMENT;
7629 }
7630
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007631 psa_jpake_computation_stage_t *computation_stage =
7632 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007633
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007634 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7635 return PSA_ERROR_BAD_STATE;
7636 }
7637
7638 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7639 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
7640 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
7641 return PSA_ERROR_BAD_STATE;
7642 }
7643
7644 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7645 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007646 return PSA_ERROR_BAD_STATE;
7647 }
7648
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007649 switch (computation_stage->output_step) {
7650 case PSA_PAKE_STEP_X1_X2:
7651 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007652 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007653 case PSA_PAKE_STEP_X2S:
7654 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007655 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007656 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007657 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007658 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007659
7660 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7661 }
7662
7663 /* Check if step matches current sequence */
7664 switch (computation_stage->sequence) {
7665 case PSA_PAKE_X1_STEP_KEY_SHARE:
7666 case PSA_PAKE_X2_STEP_KEY_SHARE:
7667 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7668 return PSA_ERROR_BAD_STATE;
7669 }
7670 break;
7671
7672 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7673 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7674 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7675 return PSA_ERROR_BAD_STATE;
7676 }
7677 break;
7678
7679 case PSA_PAKE_X1_STEP_ZK_PROOF:
7680 case PSA_PAKE_X2_STEP_ZK_PROOF:
7681 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7682 return PSA_ERROR_BAD_STATE;
7683 }
7684 break;
7685
7686 default:
7687 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007688 }
7689
7690 return PSA_SUCCESS;
7691}
7692
7693static psa_status_t psa_jpake_output_epilogue(
7694 psa_pake_operation_t *operation)
7695{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007696 psa_jpake_computation_stage_t *computation_stage =
7697 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007698
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007699 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007700 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007701 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007702 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007703 computation_stage->state = PSA_PAKE_STATE_READY;
7704 computation_stage->output_step++;
7705 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7706 } else {
7707 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007708 }
7709
7710 return PSA_SUCCESS;
7711}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007712#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007713
Neil Armstronga7d08c32022-06-01 18:21:20 +02007714psa_status_t psa_pake_output(
7715 psa_pake_operation_t *operation,
7716 psa_pake_step_t step,
7717 uint8_t *output,
7718 size_t output_size,
7719 size_t *output_length)
7720{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007721 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007722 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007723 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007724
7725 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007726 status = psa_pake_complete_inputs(operation);
7727 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007728 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007729 }
7730 }
7731
7732 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007733 status = PSA_ERROR_BAD_STATE;
7734 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007735 }
7736
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007737 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007738 status = PSA_ERROR_INVALID_ARGUMENT;
7739 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007740 }
7741
Przemek Stekiele12ed362022-12-21 12:54:46 +01007742 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007743#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007744 case PSA_ALG_JPAKE:
7745 status = psa_jpake_output_prologue(operation, step);
7746 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007747 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007748 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007749 driver_step = convert_jpake_computation_stage_to_driver_step(
7750 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007751 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007752#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007753 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007754 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007755 status = PSA_ERROR_NOT_SUPPORTED;
7756 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007757 }
7758
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007759 status = psa_driver_wrapper_pake_output(operation, driver_step,
7760 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007761
7762 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007763 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007764 }
7765
7766 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007767#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007768 case PSA_ALG_JPAKE:
7769 status = psa_jpake_output_epilogue(operation);
7770 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007771 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007772 }
7773 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007774#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007775 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007776 status = PSA_ERROR_NOT_SUPPORTED;
7777 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007778 }
7779
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007780 return PSA_SUCCESS;
7781exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007782 psa_pake_abort(operation);
7783 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007784}
7785
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007786#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007787static psa_status_t psa_jpake_input_prologue(
7788 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007789 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007790{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007791 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7792 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7793 step != PSA_PAKE_STEP_ZK_PROOF) {
7794 return PSA_ERROR_INVALID_ARGUMENT;
7795 }
7796
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007797 psa_jpake_computation_stage_t *computation_stage =
7798 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007799
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007800 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7801 return PSA_ERROR_BAD_STATE;
7802 }
7803
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007804 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7805 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
7806 computation_stage->state != PSA_PAKE_INPUT_X4S) {
7807 return PSA_ERROR_BAD_STATE;
7808 }
7809
7810 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7811 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007812 return PSA_ERROR_BAD_STATE;
7813 }
7814
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007815 switch (computation_stage->input_step) {
7816 case PSA_PAKE_STEP_X1_X2:
7817 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007818 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007819 case PSA_PAKE_STEP_X2S:
7820 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007821 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007822 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007823 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007824 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007825
7826 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7827 }
7828
7829 /* Check if step matches current sequence */
7830 switch (computation_stage->sequence) {
7831 case PSA_PAKE_X1_STEP_KEY_SHARE:
7832 case PSA_PAKE_X2_STEP_KEY_SHARE:
7833 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7834 return PSA_ERROR_BAD_STATE;
7835 }
7836 break;
7837
7838 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7839 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7840 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7841 return PSA_ERROR_BAD_STATE;
7842 }
7843 break;
7844
7845 case PSA_PAKE_X1_STEP_ZK_PROOF:
7846 case PSA_PAKE_X2_STEP_ZK_PROOF:
7847 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7848 return PSA_ERROR_BAD_STATE;
7849 }
7850 break;
7851
7852 default:
7853 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007854 }
7855
7856 return PSA_SUCCESS;
7857}
7858
Przemek Stekiele12ed362022-12-21 12:54:46 +01007859static psa_status_t psa_jpake_input_epilogue(
7860 psa_pake_operation_t *operation)
7861{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007862 psa_jpake_computation_stage_t *computation_stage =
7863 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007864
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007865 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007866 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007867 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007868 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007869 computation_stage->state = PSA_PAKE_STATE_READY;
7870 computation_stage->input_step++;
7871 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7872 } else {
7873 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007874 }
7875
7876 return PSA_SUCCESS;
7877}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007878#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007879
Neil Armstronga7d08c32022-06-01 18:21:20 +02007880psa_status_t psa_pake_input(
7881 psa_pake_operation_t *operation,
7882 psa_pake_step_t step,
7883 const uint8_t *input,
7884 size_t input_length)
7885{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007886 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007887 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007888
7889 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007890 status = psa_pake_complete_inputs(operation);
7891 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007892 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007893 }
7894 }
7895
7896 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007897 status = PSA_ERROR_BAD_STATE;
7898 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007899 }
7900
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007901 if (input_length == 0 || input_length > PSA_PAKE_INPUT_MAX_SIZE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007902 status = PSA_ERROR_INVALID_ARGUMENT;
7903 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007904 }
7905
Przemek Stekiele12ed362022-12-21 12:54:46 +01007906 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007907#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007908 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007909 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007910 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007911 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007912 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007913 driver_step = convert_jpake_computation_stage_to_driver_step(
7914 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007915 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007916#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007917 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007918 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007919 status = PSA_ERROR_NOT_SUPPORTED;
7920 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007921 }
7922
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007923 status = psa_driver_wrapper_pake_input(operation, driver_step,
7924 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007925
7926 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007927 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007928 }
7929
7930 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007931#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007932 case PSA_ALG_JPAKE:
7933 status = psa_jpake_input_epilogue(operation);
7934 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007935 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007936 }
7937 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007938#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007939 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007940 status = PSA_ERROR_NOT_SUPPORTED;
7941 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007942 }
7943
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007944 return PSA_SUCCESS;
7945exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007946 psa_pake_abort(operation);
7947 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007948}
7949
7950psa_status_t psa_pake_get_implicit_key(
7951 psa_pake_operation_t *operation,
7952 psa_key_derivation_operation_t *output)
7953{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007954 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007955 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007956 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01007957 size_t shared_key_len = 0;
7958
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007959 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007960 status = PSA_ERROR_BAD_STATE;
7961 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007962 }
7963
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007964#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007965 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007966 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01007967 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007968 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
7969 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007970 status = PSA_ERROR_BAD_STATE;
7971 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007972 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01007973 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007974#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007975 {
7976 status = PSA_ERROR_NOT_SUPPORTED;
7977 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007978 }
7979
Przemek Stekiel0c781802022-11-29 14:53:13 +01007980 status = psa_driver_wrapper_pake_get_implicit_key(operation,
7981 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01007982 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01007983 &shared_key_len);
7984
7985 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007986 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01007987 }
7988
7989 status = psa_key_derivation_input_bytes(output,
7990 PSA_KEY_DERIVATION_INPUT_SECRET,
7991 shared_key,
7992 shared_key_len);
7993
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007994 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007995exit:
7996 abort_status = psa_pake_abort(operation);
7997 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007998}
7999
8000psa_status_t psa_pake_abort(
8001 psa_pake_operation_t *operation)
8002{
Przemek Stekield69dca92023-01-31 19:59:20 +01008003 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008004
Przemek Stekield69dca92023-01-31 19:59:20 +01008005 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008006 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008007 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008008
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008009 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8010 if (operation->data.inputs.password != NULL) {
8011 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008012 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008013 mbedtls_free(operation->data.inputs.password);
8014 }
8015 if (operation->data.inputs.user != NULL) {
8016 mbedtls_free(operation->data.inputs.user);
8017 }
8018 if (operation->data.inputs.peer != NULL) {
8019 mbedtls_free(operation->data.inputs.peer);
8020 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008021 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008022 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008023
Przemek Stekield69dca92023-01-31 19:59:20 +01008024 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008025}
Neil Armstronga7d08c32022-06-01 18:21:20 +02008026
Gilles Peskinee59236f2018-01-27 23:32:46 +01008027#endif /* MBEDTLS_PSA_CRYPTO_C */