blob: a21f6d9639fd85d957f14e3dcfb315a96eca784a [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"
Gilles Peskinea5905292018-02-07 20:59:33 +010084
Gilles Peskine449bd832023-01-11 14:50:10 +010085#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020086
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020087#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
88 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
89 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020090#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020091#endif
92
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010093/****************************************************************/
94/* Global data, support functions and library management */
95/****************************************************************/
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020098{
Gilles Peskine449bd832023-01-11 14:50:10 +010099 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200100}
101
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100102/* Values for psa_global_data_t::rng_state */
103#define RNG_NOT_INITIALIZED 0
104#define RNG_INITIALIZED 1
105#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100108 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100109 unsigned rng_state : 2;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100110 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100111} psa_global_data_t;
112
113static psa_global_data_t global_data;
114
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100115#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
116mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
117 &global_data.rng.drbg;
118#endif
119
itayzafrir0adf0fc2018-09-06 16:24:41 +0300120#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 if (global_data.initialized == 0) \
122 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100125{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100126 /* Mbed TLS error codes can combine a high-level error code and a
127 * low-level error code. The low-level error usually reflects the
128 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 int low_level_ret = -(-ret & 0x007f);
130 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100133
134 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
135 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200137 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
138 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
139 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
140 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
141 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200143 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200145 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200147
Jaeden Amero93e21112019-02-20 13:57:28 +0000148#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000149 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000150#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100151 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100153
154 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100156 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100158
Gilles Peskine26869f22019-05-06 15:25:00 +0200159 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200161
162 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200164 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200166
Gilles Peskinea5905292018-02-07 20:59:33 +0100167 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100169 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100171 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100177 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100179 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
183 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100184 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
185 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100186 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100188 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
189 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100191 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100193#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100194
195 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100197
Gilles Peskinee59236f2018-01-27 23:32:46 +0100198 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
199 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
200 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100202
203 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200205 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100209
Gilles Peskine82e57d12020-11-13 21:31:17 +0100210#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100212 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
213 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100214 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100216 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
217 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100219 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100221#endif
222
Gilles Peskinea5905292018-02-07 20:59:33 +0100223 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100227 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100229 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100231
Gilles Peskinef76aa772018-10-29 19:24:33 +0100232 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100234 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100236 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100238 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100240 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100242 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100244 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100246 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100248
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100249 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100251 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
252 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100254 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100256 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
257 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100259 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100261 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
262 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100264 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100266 case MBEDTLS_ERR_PK_INVALID_ALG:
267 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
268 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100270 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200272 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100274
Gilles Peskineff2d2002019-05-06 15:26:23 +0200275 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200277 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200279
Gilles Peskinea5905292018-02-07 20:59:33 +0100280 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100282 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100284 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100286 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100288 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
289 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100291 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100293 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100295 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200297
itayzafrir5c753392018-05-08 11:18:38 +0300298 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300299 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300301 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300303 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300305 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
306 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300308 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100310 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100312
Janos Follatha13b9052019-11-22 12:48:59 +0000313 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300315
Gilles Peskinee59236f2018-01-27 23:32:46 +0100316 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100318 }
319}
320
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200321
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200322
323
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100324/****************************************************************/
325/* Key management */
326/****************************************************************/
327
John Durkop9814fa22020-11-04 12:28:15 -0800328#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800329 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cronf467d632021-11-19 18:02:34 +0100330 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
331 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
332 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100333mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
334 size_t bits,
335 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200336{
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100338 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100340#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100341 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100343#endif
344#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100345 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100347#endif
348#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100349 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100351#endif
352#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100353 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100355#endif
356#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100357 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100359 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if (bits_is_sloppy) {
361 return MBEDTLS_ECP_DP_SECP521R1;
362 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100363 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100364#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100365 }
366 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100367
Paul Elliott8ff510a2020-06-02 17:19:28 +0100368 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100370#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100371 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100373#endif
374#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100375 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100377#endif
378#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100379 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100381#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100382 }
383 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100384
Paul Elliott8ff510a2020-06-02 17:19:28 +0100385 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100387#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100388 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100390 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if (bits_is_sloppy) {
392 return MBEDTLS_ECP_DP_CURVE25519;
393 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100394 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100395#endif
396#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100397 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100399#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100400 }
401 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100402
Paul Elliott8ff510a2020-06-02 17:19:28 +0100403 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100405#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100406 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100408#endif
409#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100410 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100412#endif
413#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100414 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100416#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100417 }
418 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200419 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100420
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100421 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200423}
John Durkop9814fa22020-11-04 12:28:15 -0800424#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cronf467d632021-11-19 18:02:34 +0100425 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
426 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
427 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
428 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
431 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200432{
433 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200435 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200436 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200437 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200438 case PSA_KEY_TYPE_PASSWORD:
439 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200440 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100441#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200442 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if (bits != 128 && bits != 192 && bits != 256) {
444 return PSA_ERROR_INVALID_ARGUMENT;
445 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446 break;
447#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200448#if defined(PSA_WANT_KEY_TYPE_ARIA)
449 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 if (bits != 128 && bits != 192 && bits != 256) {
451 return PSA_ERROR_INVALID_ARGUMENT;
452 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200453 break;
454#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100455#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200456 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if (bits != 128 && bits != 192 && bits != 256) {
458 return PSA_ERROR_INVALID_ARGUMENT;
459 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200460 break;
461#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100462#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200463 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if (bits != 64 && bits != 128 && bits != 192) {
465 return PSA_ERROR_INVALID_ARGUMENT;
466 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200467 break;
468#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100469#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200470 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 if (bits != 256) {
472 return PSA_ERROR_INVALID_ARGUMENT;
473 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200474 break;
475#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200476 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200478 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 if (bits % 8 != 0) {
480 return PSA_ERROR_INVALID_ARGUMENT;
481 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200484}
485
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100486/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100487 *
Steven Cooremancd640932021-03-08 12:00:27 +0100488 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
489 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100490 *
491 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100492 * \param[in] key_type The key type of the key to be used with the
493 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100494 *
495 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100496 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100497 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100498 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100499 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100500MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100501 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100503{
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (PSA_ALG_IS_HMAC(algorithm)) {
505 if (key_type == PSA_KEY_TYPE_HMAC) {
506 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100507 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100508 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
511 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
512 * key. */
513 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
514 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
515 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
516 * the block length (larger than 1) for block ciphers. */
517 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
518 return PSA_SUCCESS;
519 }
520 }
521 }
522
523 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100524}
525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
527 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200528{
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 if (slot->key.data != NULL) {
530 return PSA_ERROR_ALREADY_EXISTS;
531 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 slot->key.data = mbedtls_calloc(1, buffer_length);
534 if (slot->key.data == NULL) {
535 return PSA_ERROR_INSUFFICIENT_MEMORY;
536 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200537
Ronald Cronea0f8a62020-11-25 17:52:23 +0100538 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200540}
541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
543 const uint8_t *data,
544 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200545{
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 psa_status_t status = psa_allocate_buffer_to_slot(slot,
547 data_length);
548 if (status != PSA_SUCCESS) {
549 return status;
550 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 memcpy(slot->key.data, data, data_length);
553 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200554}
555
Ronald Crona0fe59f2020-11-29 11:14:53 +0100556psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100557 const psa_key_attributes_t *attributes,
558 const uint8_t *data, size_t data_length,
559 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100561{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100562 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
563 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100564
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200565 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 if (data_length == 0) {
567 return PSA_ERROR_NOT_SUPPORTED;
568 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if (key_type_is_raw_bytes(type)) {
571 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
574 *bits);
575 if (status != PSA_SUCCESS) {
576 return status;
577 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200578
Ronald Crondd04d422020-11-28 15:14:42 +0100579 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100581 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 return PSA_SUCCESS;
585 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800586#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
588 if (PSA_KEY_TYPE_IS_ECC(type)) {
589 return mbedtls_psa_ecp_import_key(attributes,
590 data, data_length,
591 key_buffer, key_buffer_size,
592 key_buffer_length,
593 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100594 }
John Durkop9814fa22020-11-04 12:28:15 -0800595#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
596 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700597#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
599 if (PSA_KEY_TYPE_IS_RSA(type)) {
600 return mbedtls_psa_rsa_import_key(attributes,
601 data, data_length,
602 key_buffer, key_buffer_size,
603 key_buffer_length,
604 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200605 }
John Durkop9814fa22020-11-04 12:28:15 -0800606#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
607 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100608 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100611}
612
Gilles Peskinef603c712019-01-19 13:40:11 +0100613/** Calculate the intersection of two algorithm usage policies.
614 *
615 * Return 0 (which allows no operation) on incompatibility.
616 */
617static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100618 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100619 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100621{
Gilles Peskine549ea862019-05-22 11:45:59 +0200622 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 if (alg1 == alg2) {
624 return alg1;
625 }
Steven Cooreman03488022021-02-18 12:07:20 +0100626 /* If the policies are from the same hash-and-sign family, check
627 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
629 PSA_ALG_IS_SIGN_HASH(alg2) &&
630 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
631 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
632 return alg2;
633 }
634 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
635 return alg1;
636 }
Steven Cooreman03488022021-02-18 12:07:20 +0100637 }
638 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100639 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100640 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
642 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
643 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
644 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
645 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100646 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100647
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100648 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
650 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
651 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
652 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100653 }
654 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
656 (alg1_len <= alg2_len)) {
657 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100658 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
660 (alg2_len <= alg1_len)) {
661 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100662 }
663 }
664 /* If the policies are from the same MAC family, check whether one
665 * of them is a minimum-MAC-length policy. Calculate the most
666 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
668 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
669 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100670 /* Validate the combination of key type and algorithm. Since the base
671 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
673 return 0;
674 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100675
Steven Cooreman31a876d2021-03-03 20:47:40 +0100676 /* Get the (exact or at-least) output lengths for both sides of the
677 * requested intersection. None of the currently supported algorithms
678 * have an output length dependent on the actual key size, so setting it
679 * to a bogus value of 0 is currently OK.
680 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100681 * Note that for at-least-this-length wildcard algorithms, the output
682 * length is set to the shortest allowed length, which allows us to
683 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
685 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100686 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100687
Steven Cooremana1d83222021-02-25 10:20:29 +0100688 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
690 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
691 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100692 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100693
694 /* If only one is an at-least-this-length policy, the intersection would
695 * be the other (fixed-length) policy as long as said fixed length is
696 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
698 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100699 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
701 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100702 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100703
Steven Cooremancd640932021-03-08 12:00:27 +0100704 /* If none of them are wildcards, check whether they define the same tag
705 * length. This is still possible here when one is default-length and
706 * the other specific-length. Ensure to always return the
707 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (alg1_len == alg2_len) {
709 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
710 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100711 }
712 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100714}
715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716static int psa_key_algorithm_permits(psa_key_type_t key_type,
717 psa_algorithm_t policy_alg,
718 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200719{
Gilles Peskine549ea862019-05-22 11:45:59 +0200720 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if (requested_alg == policy_alg) {
722 return 1;
723 }
Steven Cooreman03488022021-02-18 12:07:20 +0100724 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
725 * and requested_alg is the same hash-and-sign family with any hash,
726 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
728 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
729 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
730 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100731 }
732 /* If policy_alg is a wildcard AEAD algorithm of the same base as
733 * the requested algorithm, check the requested tag length to be
734 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 if (PSA_ALG_IS_AEAD(policy_alg) &&
736 PSA_ALG_IS_AEAD(requested_alg) &&
737 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
738 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
739 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
740 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
741 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100742 }
Steven Cooremancd640932021-03-08 12:00:27 +0100743 /* If policy_alg is a MAC algorithm of the same base as the requested
744 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if (PSA_ALG_IS_MAC(policy_alg) &&
746 PSA_ALG_IS_MAC(requested_alg) &&
747 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
748 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100749 /* Validate the combination of key type and algorithm. Since the policy
750 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
752 return 0;
753 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100754
Steven Cooreman31a876d2021-03-03 20:47:40 +0100755 /* Get both the requested output length for the algorithm which is to be
756 * verified, and the default output length for the base algorithm.
757 * Note that none of the currently supported algorithms have an output
758 * length dependent on actual key size, so setting it to a bogus value
759 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100760 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100762 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 key_type, 0,
764 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100765
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100766 /* If the policy is default-length, only allow an algorithm with
767 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
769 return requested_output_length == default_output_length;
770 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100771
772 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100773 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
775 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
776 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100777 }
778
Steven Cooremancd640932021-03-08 12:00:27 +0100779 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
780 * check for the requested MAC length to be equal to or longer than the
781 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
783 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
784 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100785 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200786 }
Steven Cooremance48e852020-10-05 16:02:45 +0200787 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200788 * a key derivation with that key agreement should also be allowed. This
789 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
791 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
792 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
793 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200794 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100795 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200797}
798
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100799/** Test whether a policy permits an algorithm.
800 *
801 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100802 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100803 * \note This function requires providing the key type for which the policy is
804 * being validated, since some algorithm policy definitions (e.g. MAC)
805 * have different properties depending on what kind of cipher it is
806 * combined with.
807 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100808 * \retval PSA_SUCCESS When \p alg is a specific algorithm
809 * allowed by the \p policy.
810 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
811 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
812 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100813 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100814static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
815 psa_key_type_t key_type,
816 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100817{
Steven Cooremand788fab2021-02-25 11:29:17 +0100818 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (alg == 0) {
820 return PSA_ERROR_INVALID_ARGUMENT;
821 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100822
Steven Cooreman7e39f052021-02-23 14:18:32 +0100823 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 if (PSA_ALG_IS_WILDCARD(alg)) {
825 return PSA_ERROR_INVALID_ARGUMENT;
826 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
829 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
830 return PSA_SUCCESS;
831 } else {
832 return PSA_ERROR_NOT_PERMITTED;
833 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100834}
835
Gilles Peskinef603c712019-01-19 13:40:11 +0100836/** Restrict a key policy based on a constraint.
837 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100838 * \note This function requires providing the key type for which the policy is
839 * being restricted, since some algorithm policy definitions (e.g. MAC)
840 * have different properties depending on what kind of cipher it is
841 * combined with.
842 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100843 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100844 * \param[in,out] policy The policy to restrict.
845 * \param[in] constraint The policy constraint to apply.
846 *
847 * \retval #PSA_SUCCESS
848 * \c *policy contains the intersection of the original value of
849 * \c *policy and \c *constraint.
850 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100851 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100852 * \c *policy is unchanged.
853 */
854static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100855 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100856 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100858{
859 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 psa_key_policy_algorithm_intersection(key_type, policy->alg,
861 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200862 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
864 constraint->alg2);
865 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
866 return PSA_ERROR_INVALID_ARGUMENT;
867 }
868 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
869 return PSA_ERROR_INVALID_ARGUMENT;
870 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100871 policy->usage &= constraint->usage;
872 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200873 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100875}
876
Przemek Stekiel348410f2022-11-15 22:22:07 +0100877psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100878 mbedtls_svc_key_id_t key,
879 psa_key_slot_t **p_slot,
880 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100882{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200883 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
884 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 status = psa_get_and_lock_key_slot(key, p_slot);
887 if (status != PSA_SUCCESS) {
888 return status;
889 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200890 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100891
892 /* Enforce that usage policy for the key slot contains all the flags
893 * required by the usage parameter. There is one exception: public
894 * keys can always be exported, so we treat public key objects as
895 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100897 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100901 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200902 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100903 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100904
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800905 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 if (alg != 0) {
907 status = psa_key_policy_permits(&slot->attr.policy,
908 slot->attr.type,
909 alg);
910 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100911 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100913 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200916
917error:
918 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100922}
Darryl Green940d72c2018-07-13 13:18:51 +0100923
Ronald Cron5c522922020-11-14 16:35:34 +0100924/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200925 *
926 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200927 * available, as opposed to a key in a secure element and/or to be used
928 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200929 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200930 * This is a temporary function that may be used instead of
931 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
932 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200933 *
Ronald Cron5c522922020-11-14 16:35:34 +0100934 * On success, the returned key slot is locked. It is the responsibility of the
935 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200936 */
Ronald Cron5c522922020-11-14 16:35:34 +0100937static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
938 mbedtls_svc_key_id_t key,
939 psa_key_slot_t **p_slot,
940 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +0200942{
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
944 usage, alg);
945 if (status != PSA_SUCCESS) {
946 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +0200947 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
950 psa_unlock_key_slot(*p_slot);
951 *p_slot = NULL;
952 return PSA_ERROR_NOT_SUPPORTED;
953 }
954
955 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +0200956}
Gilles Peskine28f8f302019-07-24 13:30:31 +0200957
Gilles Peskine449bd832023-01-11 14:50:10 +0100958psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +0000959{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100960 /* Data pointer will always be either a valid pointer or NULL in an
961 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 if (slot->key.data != NULL) {
963 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
964 }
Ronald Cronea0f8a62020-11-25 17:52:23 +0100965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +0100967 slot->key.data = NULL;
968 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +0000969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +0000971}
972
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100973/** Completely wipe a slot in memory, including its policy.
974 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100975psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100976{
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +0100978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 /*
980 * As the return error code may not be handled in case of multiple errors,
981 * do our best to report an unexpected lock counter. Assert with
982 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
983 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
984 * function is called as part of the execution of a test suite, the
985 * execution of the test suite is stopped in error if the assertion fails.
986 */
987 if (slot->lock_count != 1) {
988 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +0100989 status = PSA_ERROR_CORRUPTION_DETECTED;
990 }
991
Gilles Peskine3f7cd622019-08-13 15:01:08 +0200992 /* Multipart operations may still be using the key. This is safe
993 * because all multipart operation objects are independent from
994 * the key slot: if they need to access the key after the setup
995 * phase, they have a copy of the key. Note that this means that
996 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100997 /* At this point, key material and other type-specific content has
998 * been wiped. Clear remaining metadata. We can call memset and not
999 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 memset(slot, 0, sizeof(*slot));
1001 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001002}
1003
Gilles Peskine449bd832023-01-11 14:50:10 +01001004psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001005{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001006 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001007 psa_status_t status; /* status of the last operation */
1008 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001009#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1010 psa_se_drv_table_entry_t *driver;
1011#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001012
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 if (mbedtls_svc_key_id_is_null(key)) {
1014 return PSA_SUCCESS;
1015 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001016
Ronald Cronf2911112020-10-29 17:51:10 +01001017 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001018 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001019 * key, this will load the key description from persistent memory if not
1020 * done yet. We cannot avoid this loading as without it we don't know if
1021 * the key is operated by an SE or not and this information is needed by
1022 * the current implementation.
1023 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 status = psa_get_and_lock_key_slot(key, &slot);
1025 if (status != PSA_SUCCESS) {
1026 return status;
1027 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001028
Ronald Cronf2911112020-10-29 17:51:10 +01001029 /*
1030 * If the key slot containing the key description is under access by the
1031 * library (apart from the present access), the key cannot be destroyed
1032 * yet. For the time being, just return in error. Eventually (to be
1033 * implemented), the key should be destroyed when all accesses have
1034 * stopped.
1035 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 if (slot->lock_count > 1) {
1037 psa_unlock_key_slot(slot);
1038 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001039 }
1040
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001042 /* Refuse the destruction of a read-only key (which may or may not work
1043 * if we attempt it, depending on whether the key is merely read-only
1044 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001045 * Just do the best we can, which is to wipe the copy in memory
1046 * (done in this function's cleanup code). */
1047 overall_status = PSA_ERROR_NOT_PERMITTED;
1048 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001049 }
1050
Gilles Peskine354f7672019-07-12 23:46:38 +02001051#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1053 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001054 /* For a key in a secure element, we need to do three things:
1055 * remove the key file in internal storage, destroy the
1056 * key inside the secure element, and update the driver's
1057 * persistent data. Start a transaction that will encompass these
1058 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001060 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001062 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 status = psa_crypto_save_transaction();
1064 if (status != PSA_SUCCESS) {
1065 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001066 /* We should still try to destroy the key in the secure
1067 * element and the key metadata in storage. This is especially
1068 * important if the error is that the storage is full.
1069 * But how to do it exactly without risking an inconsistent
1070 * state after a reset?
1071 * https://github.com/ARMmbed/mbed-crypto/issues/215
1072 */
1073 overall_status = status;
1074 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001075 }
1076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 status = psa_destroy_se_key(driver,
1078 psa_key_slot_get_slot_number(slot));
1079 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001080 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001082 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001083#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1084
Darryl Greend49a4992018-06-18 17:27:26 +01001085#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1087 status = psa_destroy_persistent_key(slot->attr.id);
1088 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001089 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001091
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001092 /* TODO: other slots may have a copy of the same key. We should
1093 * invalidate them.
1094 * https://github.com/ARMmbed/mbed-crypto/issues/214
1095 */
Darryl Greend49a4992018-06-18 17:27:26 +01001096 }
1097#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001098
Gilles Peskinefc762652019-07-22 19:30:34 +02001099#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 if (driver != NULL) {
1101 status = psa_save_se_persistent_data(driver);
1102 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001103 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 }
1105 status = psa_crypto_stop_transaction();
1106 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001107 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001109 }
1110#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1111
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001112exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001114 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001116 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 }
1118 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001119}
1120
John Durkop07cc04a2020-11-16 22:08:34 -08001121#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001122 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001123static psa_status_t psa_get_rsa_public_exponent(
1124 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001126{
1127 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001128 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001129 uint8_t *buffer = NULL;
1130 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1134 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001135 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 }
1137 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001138 /* It's the default value, which is reported as an empty string,
1139 * so there's nothing to do. */
1140 goto exit;
1141 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 buflen = mbedtls_mpi_size(&mpi);
1144 buffer = mbedtls_calloc(1, buflen);
1145 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001146 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1147 goto exit;
1148 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1150 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001151 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001153 attributes->domain_parameters = buffer;
1154 attributes->domain_parameters_size = buflen;
1155
1156exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 mbedtls_mpi_free(&mpi);
1158 if (ret != 0) {
1159 mbedtls_free(buffer);
1160 }
1161 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001162}
John Durkop07cc04a2020-11-16 22:08:34 -08001163#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001164 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001165
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001166/** Retrieve all the publicly-accessible attributes of a key.
1167 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001168psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1169 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001170{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001171 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001172 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001173 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1178 if (status != PSA_SUCCESS) {
1179 return status;
1180 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001181
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001182 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1184 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001185
1186#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1188 psa_set_key_slot_number(attributes,
1189 psa_key_slot_get_slot_number(slot));
1190 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001191#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001194#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001195 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001196 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001197 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001198 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001199 * is not yet implemented.
1200 * https://github.com/ARMmbed/mbed-crypto/issues/216
1201 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001203 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001204
Ronald Cron90857082020-11-25 14:58:33 +01001205 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 slot->attr.type,
1207 slot->key.data,
1208 slot->key.bytes,
1209 &rsa);
1210 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001211 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001213
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 status = psa_get_rsa_public_exponent(rsa,
1215 attributes);
1216 mbedtls_rsa_free(rsa);
1217 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001218 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001219 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001220#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001221 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001222 default:
1223 /* Nothing else to do. */
1224 break;
1225 }
1226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 if (status != PSA_SUCCESS) {
1228 psa_reset_key_attributes(attributes);
1229 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001232
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001234}
1235
Gilles Peskinec8000c02019-08-02 20:15:51 +02001236#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1237psa_status_t psa_get_key_slot_number(
1238 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001240{
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001242 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 return PSA_SUCCESS;
1244 } else {
1245 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001246 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001247}
1248#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1249
Gilles Peskine449bd832023-01-11 14:50:10 +01001250static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1251 size_t key_buffer_size,
1252 uint8_t *data,
1253 size_t data_size,
1254 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001255{
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 if (key_buffer_size > data_size) {
1257 return PSA_ERROR_BUFFER_TOO_SMALL;
1258 }
1259 memcpy(data, key_buffer, key_buffer_size);
1260 memset(data + key_buffer_size, 0,
1261 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001262 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001264}
1265
Ronald Cron7285cda2020-11-26 14:46:37 +01001266psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001267 const psa_key_attributes_t *attributes,
1268 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001270{
Ronald Crond18b5f82020-11-25 16:46:05 +01001271 psa_key_type_t type = attributes->core.type;
1272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 if (key_type_is_raw_bytes(type) ||
1274 PSA_KEY_TYPE_IS_RSA(type) ||
1275 PSA_KEY_TYPE_IS_ECC(type)) {
1276 return psa_export_key_buffer_internal(
1277 key_buffer, key_buffer_size,
1278 data, data_size, data_length);
1279 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001280 /* This shouldn't happen in the reference implementation, but
1281 it is valid for a special-purpose implementation to omit
1282 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001284 }
1285}
1286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1288 uint8_t *data,
1289 size_t data_size,
1290 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001291{
1292 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1293 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1294 psa_key_slot_t *slot;
1295
Ronald Crone907e552021-01-18 13:22:38 +01001296 /* Reject a zero-length output buffer now, since this can never be a
1297 * valid key representation. This way we know that data must be a valid
1298 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 if (data_size == 0) {
1300 return PSA_ERROR_BUFFER_TOO_SMALL;
1301 }
Ronald Crone907e552021-01-18 13:22:38 +01001302
Ronald Cron9486f9d2020-11-26 13:36:23 +01001303 /* Set the key to empty now, so that even when there are errors, we always
1304 * set data_length to a value between 0 and data_size. On error, setting
1305 * the key to empty is a good choice because an empty key representation is
1306 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001307 *data_length = 0;
1308
Ronald Cron9486f9d2020-11-26 13:36:23 +01001309 /* Export requires the EXPORT flag. There is an exception for public keys,
1310 * which don't require any flag, but
1311 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1312 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1314 PSA_KEY_USAGE_EXPORT, 0);
1315 if (status != PSA_SUCCESS) {
1316 return status;
1317 }
mohammad160306e79202018-03-28 13:17:44 +03001318
Ronald Crond18b5f82020-11-25 16:46:05 +01001319 psa_key_attributes_t attributes = {
1320 .core = slot->attr
1321 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 status = psa_driver_wrapper_export_key(&attributes,
1323 slot->key.data, slot->key.bytes,
1324 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001325
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001329}
1330
Ronald Cron7285cda2020-11-26 14:46:37 +01001331psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001332 const psa_key_attributes_t *attributes,
1333 const uint8_t *key_buffer,
1334 size_t key_buffer_size,
1335 uint8_t *data,
1336 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001338{
Ronald Crond18b5f82020-11-25 16:46:05 +01001339 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001340
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1342 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001343 /* Exporting public -> public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 return psa_export_key_buffer_internal(
1345 key_buffer, key_buffer_size,
1346 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001347 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001350#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1352 return mbedtls_psa_rsa_export_public_key(attributes,
1353 key_buffer,
1354 key_buffer_size,
1355 data,
1356 data_size,
1357 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001358#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001359 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001361#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1362 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001364#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1366 return mbedtls_psa_ecp_export_public_key(attributes,
1367 key_buffer,
1368 key_buffer_size,
1369 data,
1370 data_size,
1371 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001372#else
1373 /* We don't know how to convert a private ECC key to public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001375#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1376 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001377 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001379 /* This shouldn't happen in the reference implementation, but
1380 it is valid for a special-purpose implementation to omit
1381 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001383 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001384}
Ronald Crond18b5f82020-11-25 16:46:05 +01001385
Gilles Peskine449bd832023-01-11 14:50:10 +01001386psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1387 uint8_t *data,
1388 size_t data_size,
1389 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001390{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001391 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001392 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001393 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001394
Ronald Crone907e552021-01-18 13:22:38 +01001395 /* Reject a zero-length output buffer now, since this can never be a
1396 * valid key representation. This way we know that data must be a valid
1397 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 if (data_size == 0) {
1399 return PSA_ERROR_BUFFER_TOO_SMALL;
1400 }
Ronald Crone907e552021-01-18 13:22:38 +01001401
Darryl Greendd8fb772018-11-07 16:00:44 +00001402 /* Set the key to empty now, so that even when there are errors, we always
1403 * set data_length to a value between 0 and data_size. On error, setting
1404 * the key to empty is a good choice because an empty key representation is
1405 * unlikely to be accepted anywhere. */
1406 *data_length = 0;
1407
1408 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1410 if (status != PSA_SUCCESS) {
1411 return status;
1412 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001413
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1415 status = PSA_ERROR_INVALID_ARGUMENT;
1416 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001417 }
1418
Ronald Crond18b5f82020-11-25 16:46:05 +01001419 psa_key_attributes_t attributes = {
1420 .core = slot->attr
1421 };
Ronald Cron67227982020-11-26 15:16:05 +01001422 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001423 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001425
1426exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001430}
1431
Gilles Peskine91e8c332019-08-02 19:19:39 +02001432#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001433static_assert((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1434 "One or more key attribute flag is listed as both external-only and dual-use");
1435static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1436 "One or more key attribute flag is listed as both internal-only and dual-use");
1437static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1438 "One or more key attribute flag is listed as both internal-only and external-only");
Gilles Peskine91e8c332019-08-02 19:19:39 +02001439#endif
1440
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001441/** Validate that a key policy is internally well-formed.
1442 *
1443 * This function only rejects invalid policies. It does not validate the
1444 * consistency of the policy with respect to other attributes of the key
1445 * such as the key type.
1446 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001447static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001448{
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1450 PSA_KEY_USAGE_COPY |
1451 PSA_KEY_USAGE_ENCRYPT |
1452 PSA_KEY_USAGE_DECRYPT |
1453 PSA_KEY_USAGE_SIGN_MESSAGE |
1454 PSA_KEY_USAGE_VERIFY_MESSAGE |
1455 PSA_KEY_USAGE_SIGN_HASH |
1456 PSA_KEY_USAGE_VERIFY_HASH |
1457 PSA_KEY_USAGE_VERIFY_DERIVATION |
1458 PSA_KEY_USAGE_DERIVE)) != 0) {
1459 return PSA_ERROR_INVALID_ARGUMENT;
1460 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001463}
1464
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001465/** Validate the internal consistency of key attributes.
1466 *
1467 * This function only rejects invalid attribute values. If does not
1468 * validate the consistency of the attributes with any key data that may
1469 * be involved in the creation of the key.
1470 *
1471 * Call this function early in the key creation process.
1472 *
1473 * \param[in] attributes Key attributes for the new key.
1474 * \param[out] p_drv On any return, the driver for the key, if any.
1475 * NULL for a transparent key.
1476 *
1477 */
1478static psa_status_t psa_validate_key_attributes(
1479 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001481{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001482 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1484 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 status = psa_validate_key_location(lifetime, p_drv);
1487 if (status != PSA_SUCCESS) {
1488 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001489 }
1490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 status = psa_validate_key_persistence(lifetime);
1492 if (status != PSA_SUCCESS) {
1493 return status;
1494 }
1495
1496 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1497 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1498 return PSA_ERROR_INVALID_ARGUMENT;
1499 }
1500 } else {
1501 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1502 return PSA_ERROR_INVALID_ARGUMENT;
1503 }
1504 }
1505
1506 status = psa_validate_key_policy(&attributes->core.policy);
1507 if (status != PSA_SUCCESS) {
1508 return status;
1509 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001510
1511 /* Refuse to create overly large keys.
1512 * Note that this doesn't trigger on import if the attributes don't
1513 * explicitly specify a size (so psa_get_key_bits returns 0), so
1514 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1516 return PSA_ERROR_NOT_SUPPORTED;
1517 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001518
Gilles Peskine91e8c332019-08-02 19:19:39 +02001519 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1521 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1522 return PSA_ERROR_INVALID_ARGUMENT;
1523 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001526}
1527
Gilles Peskine4747d192019-04-17 15:05:45 +02001528/** Prepare a key slot to receive key material.
1529 *
1530 * This function allocates a key slot and sets its metadata.
1531 *
1532 * If this function fails, call psa_fail_key_creation().
1533 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001534 * This function is intended to be used as follows:
1535 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001536 * it with the specified attributes, and in case of a volatile key assign it
1537 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001538 * -# Populate the slot with the key material.
1539 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1540 * In case of failure at any step, stop the sequence and call
1541 * psa_fail_key_creation().
1542 *
Ronald Cron5c522922020-11-14 16:35:34 +01001543 * On success, the key slot is locked. It is the responsibility of the caller
1544 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001545 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001546 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001547 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001548 * \param[out] p_slot On success, a pointer to the prepared slot.
1549 * \param[out] p_drv On any return, the driver for the key, if any.
1550 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001551 *
1552 * \retval #PSA_SUCCESS
1553 * The key slot is ready to receive key material.
1554 * \return If this function fails, the key slot is an invalid state.
1555 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001556 */
1557static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001558 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001559 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001560 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001562{
1563 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001564 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001565 psa_key_slot_t *slot;
1566
Gilles Peskinedf179142019-07-15 22:02:14 +02001567 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001568 *p_drv = NULL;
1569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 status = psa_validate_key_attributes(attributes, p_drv);
1571 if (status != PSA_SUCCESS) {
1572 return status;
1573 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001574
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1576 if (status != PSA_SUCCESS) {
1577 return status;
1578 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001579 slot = *p_slot;
1580
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001581 /* We're storing the declared bit-size of the key. It's up to each
1582 * creation mechanism to verify that this information is correct.
1583 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001584 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001585 * is optional (import, copy). In case of a volatile key, assign it the
1586 * volatile key identifier associated to the slot returned to contain its
1587 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001588
1589 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001591#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1592 slot->attr.id = volatile_key_id;
1593#else
1594 slot->attr.id.key_id = volatile_key_id;
1595#endif
1596 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001597
Gilles Peskine91e8c332019-08-02 19:19:39 +02001598 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001599 * external-only flags, query `attributes`. Thanks to the check
1600 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001601 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001602 * may have set. */
1603 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001604
Gilles Peskinecbaff462019-07-12 23:46:04 +02001605#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001606 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001607 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001608 * create the key file in internal storage, create the
1609 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001610 * persistent data. This is done by starting a transaction that will
1611 * encompass these three actions.
1612 * For registering a volatile key, we just need to find an appropriate
1613 * slot number inside the SE. Since the key is designated volatile, creating
1614 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001615 /* The first thing to do is to find a slot number for the new key.
1616 * We save the slot number in persistent storage as part of the
1617 * transaction data. It will be needed to recover if the power
1618 * fails during the key creation process, to clean up on the secure
1619 * element side after restarting. Obtaining a slot number from the
1620 * secure element driver updates its persistent state, but we do not yet
1621 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001622 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001624 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1626 &slot_number);
1627 if (status != PSA_SUCCESS) {
1628 return status;
1629 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001630
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1632 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001633 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001634 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001635 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 status = psa_crypto_save_transaction();
1637 if (status != PSA_SUCCESS) {
1638 (void) psa_crypto_stop_transaction();
1639 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001640 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001641 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001642
1643 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001645 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001646
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001648 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001650 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001651#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001654}
Gilles Peskine4747d192019-04-17 15:05:45 +02001655
1656/** Finalize the creation of a key once its key material has been set.
1657 *
1658 * This entails writing the key to persistent storage.
1659 *
1660 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001661 * See the documentation of psa_start_key_creation() for the intended use
1662 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001663 *
Ronald Cron5c522922020-11-14 16:35:34 +01001664 * If the finalization succeeds, the function unlocks the key slot (it was
1665 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1666 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001667 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001668 * \param[in,out] slot Pointer to the slot with key material.
1669 * \param[in] driver The secure element driver for the key,
1670 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001671 * \param[out] key On success, identifier of the key. Note that the
1672 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001673 *
1674 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001675 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001676 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1677 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1678 * \retval #PSA_ERROR_ALREADY_EXISTS
1679 * \retval #PSA_ERROR_DATA_INVALID
1680 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001681 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001682 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001683 * \return If this function fails, the key slot is an invalid state.
1684 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001685 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001686static psa_status_t psa_finish_key_creation(
1687 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001688 psa_se_drv_table_entry_t *driver,
1689 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001690{
1691 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001692 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001693 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001694
1695#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001697#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001699 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001700 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001702
Gilles Peskineb46bef22019-07-30 21:32:04 +02001703#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 static_assert(sizeof(slot_number) ==
1705 sizeof(data.slot_number),
1706 "Slot number size does not match psa_se_key_data_storage_t");
Gilles Peskineb46bef22019-07-30 21:32:04 +02001707#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1709 status = psa_save_persistent_key(&slot->attr,
1710 (uint8_t *) &data,
1711 sizeof(data));
1712 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001713#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1714 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001715 /* Key material is saved in export representation in the slot, so
1716 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 status = psa_save_persistent_key(&slot->attr,
1718 slot->key.data,
1719 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001720 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001721 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001722#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1723
Gilles Peskinecbaff462019-07-12 23:46:04 +02001724#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001725 /* Finish the transaction for a key creation. This does not
1726 * happen when registering an existing key. Detect this case
1727 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001728 * creation of a persistent key in a secure element requires a transaction,
1729 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 if (driver != NULL &&
1731 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1732 status = psa_save_se_persistent_data(driver);
1733 if (status != PSA_SUCCESS) {
1734 psa_destroy_persistent_key(slot->attr.id);
1735 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001736 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001738 }
1739#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1740
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001742 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 status = psa_unlock_key_slot(slot);
1744 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001745 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001747 }
Ronald Cron50972942020-11-14 11:28:25 +01001748
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001750}
1751
1752/** Abort the creation of a key.
1753 *
1754 * You may call this function after calling psa_start_key_creation(),
1755 * or after psa_finish_key_creation() fails. In other circumstances, this
1756 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001757 * See the documentation of psa_start_key_creation() for the intended use
1758 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001759 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001760 * \param[in,out] slot Pointer to the slot with key material.
1761 * \param[in] driver The secure element driver for the key,
1762 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001763 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001764static void psa_fail_key_creation(psa_key_slot_t *slot,
1765 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001766{
Gilles Peskine011e4282019-06-26 18:34:38 +02001767 (void) driver;
1768
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001770 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001772
1773#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001774 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001775 * element, and the failure happened later (when saving metadata
1776 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001777 * element.
1778 * https://github.com/ARMmbed/mbed-crypto/issues/217
1779 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001780
Gilles Peskined7729582019-08-05 15:55:54 +02001781 /* Abort the ongoing transaction if any (there may not be one if
1782 * the creation process failed before starting one, or if the
1783 * key creation is a registration of a key in a secure element).
1784 * Earlier functions must already have done what it takes to undo any
1785 * partial creation. All that's left is to update the transaction data
1786 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001788#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001791}
1792
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001793/** Validate optional attributes during key creation.
1794 *
1795 * Some key attributes are optional during key creation. If they are
1796 * specified in the attributes structure, check that they are consistent
1797 * with the data in the slot.
1798 *
1799 * This function should be called near the end of key creation, after
1800 * the slot in memory is fully populated but before saving persistent data.
1801 */
1802static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001803 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001805{
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 if (attributes->core.type != 0) {
1807 if (attributes->core.type != slot->attr.type) {
1808 return PSA_ERROR_INVALID_ARGUMENT;
1809 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001810 }
1811
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001813#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1815 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001816 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001817 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001818 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001819
Ronald Cron90857082020-11-25 14:58:33 +01001820 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 slot->attr.type,
1822 slot->key.data,
1823 slot->key.bytes,
1824 &rsa);
1825 if (status != PSA_SUCCESS) {
1826 return status;
1827 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001828
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 mbedtls_mpi_init(&actual);
1830 mbedtls_mpi_init(&required);
1831 ret = mbedtls_rsa_export(rsa,
1832 NULL, NULL, NULL, NULL, &actual);
1833 mbedtls_rsa_free(rsa);
1834 mbedtls_free(rsa);
1835 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001836 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 }
1838 ret = mbedtls_mpi_read_binary(&required,
1839 attributes->domain_parameters,
1840 attributes->domain_parameters_size);
1841 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001842 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 }
1844 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001845 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 }
1847rsa_exit:
1848 mbedtls_mpi_free(&actual);
1849 mbedtls_mpi_free(&required);
1850 if (ret != 0) {
1851 return mbedtls_to_psa_error(ret);
1852 }
1853 } else
John Durkop9814fa22020-11-04 12:28:15 -08001854#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1855 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001856 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001858 }
1859 }
1860
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 if (attributes->core.bits != 0) {
1862 if (attributes->core.bits != slot->attr.bits) {
1863 return PSA_ERROR_INVALID_ARGUMENT;
1864 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001865 }
1866
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001868}
1869
Gilles Peskine449bd832023-01-11 14:50:10 +01001870psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1871 const uint8_t *data,
1872 size_t data_length,
1873 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001874{
1875 psa_status_t status;
1876 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001877 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001878 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301879 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001880
Ronald Cron81709fc2020-11-14 12:10:32 +01001881 *key = MBEDTLS_SVC_KEY_ID_INIT;
1882
Gilles Peskine0f84d622019-09-12 19:03:13 +02001883 /* Reject zero-length symmetric keys (including raw data key objects).
1884 * This also rejects any key which might be encoded as an empty string,
1885 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 if (data_length == 0) {
1887 return PSA_ERROR_INVALID_ARGUMENT;
1888 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001889
Archana449608b2021-09-08 15:36:05 +05301890 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 if (data_length > SIZE_MAX / 8) {
1892 return PSA_ERROR_NOT_SUPPORTED;
1893 }
Archana6ed4bda2021-08-04 10:47:15 +05301894
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1896 &slot, &driver);
1897 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001898 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001900
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001901 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301902 * storage ( thus not in the case of importing a key in a secure element
1903 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1904 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 if (slot->key.data == NULL) {
1906 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301907 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 attributes, data, data_length, &storage_size);
1909 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301910 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 }
Archanad8a83dc2021-06-14 10:04:16 +05301912 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 status = psa_allocate_buffer_to_slot(slot, storage_size);
1914 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001915 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001917 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001918
1919 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 status = psa_driver_wrapper_import_key(attributes,
1921 data, data_length,
1922 slot->key.data,
1923 slot->key.bytes,
1924 &slot->key.bytes, &bits);
1925 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001926 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001928
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001930 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001932 status = PSA_ERROR_INVALID_ARGUMENT;
1933 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001934 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001935
Archana6ed4bda2021-08-04 10:47:15 +05301936 /* Enforce a size limit, and in particular ensure that the bit
1937 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05301939 status = PSA_ERROR_NOT_SUPPORTED;
1940 goto exit;
1941 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 status = psa_validate_optional_attributes(slot, attributes);
1943 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02001944 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001946
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001948exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 if (status != PSA_SUCCESS) {
1950 psa_fail_key_creation(slot, driver);
1951 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001952
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001954}
1955
Gilles Peskined7729582019-08-05 15:55:54 +02001956#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1957psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02001959{
1960 psa_status_t status;
1961 psa_key_slot_t *slot = NULL;
1962 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001963 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02001964
1965 /* Leaving attributes unspecified is not currently supported.
1966 * It could make sense to query the key type and size from the
1967 * secure element, but not all secure elements support this
1968 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
1970 return PSA_ERROR_NOT_SUPPORTED;
1971 }
1972 if (psa_get_key_bits(attributes) == 0) {
1973 return PSA_ERROR_NOT_SUPPORTED;
1974 }
Gilles Peskined7729582019-08-05 15:55:54 +02001975
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
1977 &slot, &driver);
1978 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02001979 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 }
Gilles Peskined7729582019-08-05 15:55:54 +02001981
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02001983
1984exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 if (status != PSA_SUCCESS) {
1986 psa_fail_key_creation(slot, driver);
1987 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001988
Gilles Peskined7729582019-08-05 15:55:54 +02001989 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 psa_close_key(key);
1991 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02001992}
1993#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1994
Gilles Peskine449bd832023-01-11 14:50:10 +01001995psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
1996 const psa_key_attributes_t *specified_attributes,
1997 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01001998{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001999 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002000 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002001 psa_key_slot_t *source_slot = NULL;
2002 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002003 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002004 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302005 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002006
Ronald Cron81709fc2020-11-14 12:10:32 +01002007 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2008
Archana8a180362021-07-05 02:18:48 +05302009 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2011 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002012 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002014
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 status = psa_validate_optional_attributes(source_slot,
2016 specified_attributes);
2017 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002018 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002020
Archana9d17bf42021-09-10 06:22:44 +05302021 /* The target key type and number of bits have been validated by
2022 * psa_validate_optional_attributes() to be either equal to zero or
2023 * equal to the ones of the source key. So it is safe to inherit
2024 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302025 * */
Archana9d17bf42021-09-10 06:22:44 +05302026 actual_attributes.core.bits = source_slot->attr.bits;
2027 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302028
2029
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 status = psa_restrict_key_policy(source_slot->attr.type,
2031 &actual_attributes.core.policy,
2032 &source_slot->attr.policy);
2033 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002034 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2038 &target_slot, &driver);
2039 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002040 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 }
2042 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2043 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302044 /*
Archana9d17bf42021-09-10 06:22:44 +05302045 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302046 * the source key would need to be exported as plaintext and re-imported
2047 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302048 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302049 * appropriate API invocations from the application, if needed.
2050 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002051 status = PSA_ERROR_NOT_SUPPORTED;
2052 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002053 }
Archana8a180362021-07-05 02:18:48 +05302054 /*
2055 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302056 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302057 * - For opaque keys this translates to an invocation of the drivers'
2058 * copy_key entry point through the dispatch layer.
2059 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2061 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2062 &storage_size);
2063 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302064 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 }
Archana374fe5b2021-09-08 15:50:28 +05302066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2068 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302069 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 }
Archana374fe5b2021-09-08 15:50:28 +05302071
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 status = psa_driver_wrapper_copy_key(&actual_attributes,
2073 source_slot->key.data,
2074 source_slot->key.bytes,
2075 target_slot->key.data,
2076 target_slot->key.bytes,
2077 &target_slot->key.bytes);
2078 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302079 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 }
2081 } else {
2082 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302083 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 source_slot->key.bytes);
2085 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302086 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 }
Archana8a180362021-07-05 02:18:48 +05302088 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002090exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 if (status != PSA_SUCCESS) {
2092 psa_fail_key_creation(target_slot, driver);
2093 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002096
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002098}
2099
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002100
2101
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002102/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002103/* Message digests */
2104/****************************************************************/
2105
Gilles Peskine449bd832023-01-11 14:50:10 +01002106psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002107{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002108 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 if (operation->id == 0) {
2110 return PSA_SUCCESS;
2111 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002114 operation->id = 0;
2115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002117}
2118
Gilles Peskine449bd832023-01-11 14:50:10 +01002119psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2120 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002121{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002122 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002123
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002124 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002126 status = PSA_ERROR_BAD_STATE;
2127 goto exit;
2128 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002129
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002131 status = PSA_ERROR_INVALID_ARGUMENT;
2132 goto exit;
2133 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002134
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002135 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2136 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002140
2141exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 if (status != PSA_SUCCESS) {
2143 psa_hash_abort(operation);
2144 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002145
2146 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002147}
2148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2150 const uint8_t *input,
2151 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002152{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002153 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002156 status = PSA_ERROR_BAD_STATE;
2157 goto exit;
2158 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002159
Steven Cooremanc8288352021-03-04 14:02:19 +01002160 /* Don't require hash implementations to behave correctly on a
2161 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 if (input_length == 0) {
2163 return PSA_SUCCESS;
2164 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002167
2168exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 if (status != PSA_SUCCESS) {
2170 psa_hash_abort(operation);
2171 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002174}
2175
Gilles Peskine449bd832023-01-11 14:50:10 +01002176psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2177 uint8_t *hash,
2178 size_t hash_size,
2179 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002180{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002181 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 if (operation->id == 0) {
2183 return PSA_ERROR_BAD_STATE;
2184 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002185
Steven Cooreman1e582352021-02-18 17:24:37 +01002186 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 operation, hash, hash_size, hash_length);
2188 psa_hash_abort(operation);
2189 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002190}
2191
Gilles Peskine449bd832023-01-11 14:50:10 +01002192psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2193 const uint8_t *hash,
2194 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002195{
Ronald Cron69a63422021-10-18 09:47:58 +02002196 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002197 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002198 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 operation,
2200 actual_hash, sizeof(actual_hash),
2201 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002204 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002208 status = PSA_ERROR_INVALID_SIGNATURE;
2209 goto exit;
2210 }
2211
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002213 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002215
2216exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2218 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002219 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002221
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002223}
2224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225psa_status_t psa_hash_compute(psa_algorithm_t alg,
2226 const uint8_t *input, size_t input_length,
2227 uint8_t *hash, size_t hash_size,
2228 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002229{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002230 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 if (!PSA_ALG_IS_HASH(alg)) {
2232 return PSA_ERROR_INVALID_ARGUMENT;
2233 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002234
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2236 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002237}
2238
Gilles Peskine449bd832023-01-11 14:50:10 +01002239psa_status_t psa_hash_compare(psa_algorithm_t alg,
2240 const uint8_t *input, size_t input_length,
2241 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002242{
Ronald Cron69a63422021-10-18 09:47:58 +02002243 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002244 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 if (!PSA_ALG_IS_HASH(alg)) {
2247 return PSA_ERROR_INVALID_ARGUMENT;
2248 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002249
Steven Cooreman1e582352021-02-18 17:24:37 +01002250 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 alg, input, input_length,
2252 actual_hash, sizeof(actual_hash),
2253 &actual_hash_length);
2254 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002255 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 }
2257 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002258 status = PSA_ERROR_INVALID_SIGNATURE;
2259 goto exit;
2260 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002262 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002264
2265exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2267 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002268}
2269
Gilles Peskine449bd832023-01-11 14:50:10 +01002270psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2271 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002272{
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 if (source_operation->id == 0 ||
2274 target_operation->id != 0) {
2275 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002276 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002277
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2279 target_operation);
2280 if (status != PSA_SUCCESS) {
2281 psa_hash_abort(target_operation);
2282 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002283
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002285}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002286
2287
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002288/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002289/* MAC */
2290/****************************************************************/
2291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002293{
Steven Cooremane6804192021-03-19 18:28:56 +01002294 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 if (operation->id == 0) {
2296 return PSA_SUCCESS;
2297 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002300 operation->mac_size = 0;
2301 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002302 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002305}
2306
Ronald Cron2dff3b22021-06-17 16:33:22 +02002307static psa_status_t psa_mac_finalize_alg_and_key_validation(
2308 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002309 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002311{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002312 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 psa_key_type_t key_type = psa_get_key_type(attributes);
2314 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 if (!PSA_ALG_IS_MAC(alg)) {
2317 return PSA_ERROR_INVALID_ARGUMENT;
2318 }
Steven Cooremane6804192021-03-19 18:28:56 +01002319
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002320 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 status = psa_mac_key_can_do(alg, key_type);
2322 if (status != PSA_SUCCESS) {
2323 return status;
2324 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002325
Steven Cooremand1a68f12021-05-10 11:13:41 +02002326 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002330 /* A very short MAC is too short for security since it can be
2331 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2332 * so we make this our minimum, even though 32 bits is still
2333 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002335 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002336
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2338 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002339 /* It's impossible to "truncate" to a larger length than the full length
2340 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002342 }
2343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002345 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2346 * that is disabled in the compile-time configuration. The result can
2347 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2348 * configuration into account. In this case, force a return of
2349 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2350 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2351 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2352 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2353 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002355 }
2356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002358}
2359
Gilles Peskine449bd832023-01-11 14:50:10 +01002360static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2361 mbedtls_svc_key_id_t key,
2362 psa_algorithm_t alg,
2363 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002364{
2365 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2366 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002367 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002368
2369 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002371 status = PSA_ERROR_BAD_STATE;
2372 goto exit;
2373 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002374
2375 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 key,
2377 &slot,
2378 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2379 alg);
2380 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002381 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002383
2384 psa_key_attributes_t attributes = {
2385 .core = slot->attr
2386 };
2387
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2389 &operation->mac_size);
2390 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002391 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002393
2394 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002395 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 if (is_sign) {
2397 status = psa_driver_wrapper_mac_sign_setup(operation,
2398 &attributes,
2399 slot->key.data,
2400 slot->key.bytes,
2401 alg);
2402 } else {
2403 status = psa_driver_wrapper_mac_verify_setup(operation,
2404 &attributes,
2405 slot->key.data,
2406 slot->key.bytes,
2407 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002408 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002409
Gilles Peskinefbfac682018-07-08 20:51:54 +02002410exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 if (status != PSA_SUCCESS) {
2412 psa_mac_abort(operation);
2413 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002418}
2419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2421 mbedtls_svc_key_id_t key,
2422 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002423{
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002425}
2426
Gilles Peskine449bd832023-01-11 14:50:10 +01002427psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2428 mbedtls_svc_key_id_t key,
2429 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002430{
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002432}
2433
Gilles Peskine449bd832023-01-11 14:50:10 +01002434psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2435 const uint8_t *input,
2436 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002437{
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 if (operation->id == 0) {
2439 return PSA_ERROR_BAD_STATE;
2440 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002441
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002442 /* Don't require hash implementations to behave correctly on a
2443 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 if (input_length == 0) {
2445 return PSA_SUCCESS;
2446 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002447
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2449 input, input_length);
2450 if (status != PSA_SUCCESS) {
2451 psa_mac_abort(operation);
2452 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002453
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002455}
2456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2458 uint8_t *mac,
2459 size_t mac_size,
2460 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002461{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002462 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2463 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002464
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002466 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002467 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002468 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002469
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002471 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002472 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002473 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002474
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002475 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2476 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002478 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002479 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002480 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002481
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002483 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002484 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002485 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002486
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 status = psa_driver_wrapper_mac_sign_finish(operation,
2488 mac, operation->mac_size,
2489 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002490
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002491exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002492 /* In case of success, set the potential excess room in the output buffer
2493 * to an invalid value, to avoid potentially leaking a longer MAC.
2494 * In case of error, set the output length and content to a safe default,
2495 * such that in case the caller misses an error check, the output would be
2496 * an unachievable MAC.
2497 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002499 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002500 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002501 }
mohammad16036df908f2018-04-02 08:34:15 -07002502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 if (mac_size > operation->mac_size) {
2504 memset(&mac[operation->mac_size], '!',
2505 mac_size - operation->mac_size);
2506 }
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002511}
2512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2514 const uint8_t *mac,
2515 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002516{
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
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002531 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002532 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002533 }
2534
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 status = psa_driver_wrapper_mac_verify_finish(operation,
2536 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002537
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002538exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002540
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002542}
2543
Gilles Peskine449bd832023-01-11 14:50:10 +01002544static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2545 psa_algorithm_t alg,
2546 const uint8_t *input,
2547 size_t input_length,
2548 uint8_t *mac,
2549 size_t mac_size,
2550 size_t *mac_length,
2551 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002552{
2553 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002554 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2555 psa_key_slot_t *slot;
2556 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002557
Ronald Cron51131b52021-06-17 17:17:20 +02002558 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 key,
2560 &slot,
2561 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2562 alg);
2563 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002564 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002566
Ronald Cron51131b52021-06-17 17:17:20 +02002567 psa_key_attributes_t attributes = {
2568 .core = slot->attr
2569 };
2570
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2572 &operation_mac_size);
2573 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002574 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002576
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002578 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002579 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002580 }
2581
2582 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 &attributes,
2584 slot->key.data, slot->key.bytes,
2585 alg,
2586 input, input_length,
2587 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002588
2589exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002590 /* In case of success, set the potential excess room in the output buffer
2591 * to an invalid value, to avoid potentially leaking a longer MAC.
2592 * In case of error, set the output length and content to a safe default,
2593 * such that in case the caller misses an error check, the output would be
2594 * an unachievable MAC.
2595 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002597 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002598 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002599 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 if (mac_size > operation_mac_size) {
2601 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2602 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002603
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002607}
2608
Gilles Peskine449bd832023-01-11 14:50:10 +01002609psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002610 psa_algorithm_t alg,
2611 const uint8_t *input,
2612 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 uint8_t *mac,
2614 size_t mac_size,
2615 size_t *mac_length)
2616{
2617 return psa_mac_compute_internal(key, alg,
2618 input, input_length,
2619 mac, mac_size, mac_length, 1);
2620}
2621
2622psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2623 psa_algorithm_t alg,
2624 const uint8_t *input,
2625 size_t input_length,
2626 const uint8_t *mac,
2627 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002628{
2629 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002630 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2631 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002632
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 status = psa_mac_compute_internal(key, alg,
2634 input, input_length,
2635 actual_mac, sizeof(actual_mac),
2636 &actual_mac_length, 0);
2637 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002638 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002640
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002642 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002643 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002644 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002646 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002647 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002648 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002649
2650exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002652
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002654}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002655
Gilles Peskinee59236f2018-01-27 23:32:46 +01002656/****************************************************************/
2657/* Asymmetric cryptography */
2658/****************************************************************/
2659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2661 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002662{
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 if (input_is_message) {
2664 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2665 return PSA_ERROR_INVALID_ARGUMENT;
2666 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002667
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2669 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2670 return PSA_ERROR_INVALID_ARGUMENT;
2671 }
2672 }
2673 } else {
2674 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2675 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002676 }
2677 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002678
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002680}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002681
Gilles Peskine449bd832023-01-11 14:50:10 +01002682static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2683 int input_is_message,
2684 psa_algorithm_t alg,
2685 const uint8_t *input,
2686 size_t input_length,
2687 uint8_t *signature,
2688 size_t signature_size,
2689 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002690{
2691 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2692 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2693 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002694
2695 *signature_length = 0;
2696
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 status = psa_sign_verify_check_alg(input_is_message, alg);
2698 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002699 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002701
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002702 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002703 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002704 * buffer can in principle be empty since it doesn't actually have
2705 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 if (signature_size == 0) {
2707 return PSA_ERROR_BUFFER_TOO_SMALL;
2708 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002709
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002710 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 key, &slot,
2712 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2713 PSA_KEY_USAGE_SIGN_HASH,
2714 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002715
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002717 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002721 status = PSA_ERROR_INVALID_ARGUMENT;
2722 goto exit;
2723 }
2724
2725 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002727 };
2728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002730 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002731 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002732 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 signature, signature_size, signature_length);
2734 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002735
2736 status = psa_driver_wrapper_sign_hash(
2737 &attributes, slot->key.data, slot->key.bytes,
2738 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002740 }
2741
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002742
2743exit:
2744 /* Fill the unused part of the output buffer (the whole buffer on error,
2745 * the trailing part on success) with something that isn't a valid signature
2746 * (barring an attack on the signature and deliberately-crafted input),
2747 * in case the caller doesn't check the return status properly. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 if (status == PSA_SUCCESS) {
2749 memset(signature + *signature_length, '!',
2750 signature_size - *signature_length);
2751 } else {
2752 memset(signature, '!', signature_size);
2753 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002754 /* If signature_size is 0 then we have nothing to do. We must not call
2755 * memset because signature may be NULL in this case. */
2756
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002758
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002760}
2761
Gilles Peskine449bd832023-01-11 14:50:10 +01002762static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2763 int input_is_message,
2764 psa_algorithm_t alg,
2765 const uint8_t *input,
2766 size_t input_length,
2767 const uint8_t *signature,
2768 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002769{
2770 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2771 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2772 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002773
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 status = psa_sign_verify_check_alg(input_is_message, alg);
2775 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002776 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002778
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002779 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 key, &slot,
2781 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2782 PSA_KEY_USAGE_VERIFY_HASH,
2783 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002784
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 if (status != PSA_SUCCESS) {
2786 return status;
2787 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002788
2789 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002791 };
2792
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002794 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002795 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002796 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002797 signature, signature_length);
2798 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002799 status = psa_driver_wrapper_verify_hash(
2800 &attributes, slot->key.data, slot->key.bytes,
2801 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002803 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002804
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002806
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002808
2809}
2810
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002811psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002812 const psa_key_attributes_t *attributes,
2813 const uint8_t *key_buffer,
2814 size_t key_buffer_size,
2815 psa_algorithm_t alg,
2816 const uint8_t *input,
2817 size_t input_length,
2818 uint8_t *signature,
2819 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002821{
2822 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002823
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002825 size_t hash_length;
2826 uint8_t hash[PSA_HASH_MAX_SIZE];
2827
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002828 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 PSA_ALG_SIGN_GET_HASH(alg),
2830 input, input_length,
2831 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002832
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002834 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002836
2837 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 attributes, key_buffer, key_buffer_size,
2839 alg, hash, hash_length,
2840 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002841 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002842
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002844}
2845
Gilles Peskine449bd832023-01-11 14:50:10 +01002846psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2847 psa_algorithm_t alg,
2848 const uint8_t *input,
2849 size_t input_length,
2850 uint8_t *signature,
2851 size_t signature_size,
2852 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002853{
2854 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002855 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002857}
2858
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002859psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002860 const psa_key_attributes_t *attributes,
2861 const uint8_t *key_buffer,
2862 size_t key_buffer_size,
2863 psa_algorithm_t alg,
2864 const uint8_t *input,
2865 size_t input_length,
2866 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002868{
2869 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002870
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002872 size_t hash_length;
2873 uint8_t hash[PSA_HASH_MAX_SIZE];
2874
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002875 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 PSA_ALG_SIGN_GET_HASH(alg),
2877 input, input_length,
2878 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002879
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002881 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002883
2884 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 attributes, key_buffer, key_buffer_size,
2886 alg, hash, hash_length,
2887 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002888 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002889
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002891}
2892
Gilles Peskine449bd832023-01-11 14:50:10 +01002893psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2894 psa_algorithm_t alg,
2895 const uint8_t *input,
2896 size_t input_length,
2897 const uint8_t *signature,
2898 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002899{
2900 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002901 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002903}
2904
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002905psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002906 const psa_key_attributes_t *attributes,
2907 const uint8_t *key_buffer, size_t key_buffer_size,
2908 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002910{
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2912 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2913 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002914#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2916 return mbedtls_psa_rsa_sign_hash(
2917 attributes,
2918 key_buffer, key_buffer_size,
2919 alg, hash, hash_length,
2920 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002921#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2922 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 } else {
2924 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002925 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2927 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002928#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2930 return mbedtls_psa_ecdsa_sign_hash(
2931 attributes,
2932 key_buffer, key_buffer_size,
2933 alg, hash, hash_length,
2934 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002935#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2936 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 } else {
2938 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002939 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002940 }
Ronald Cron4501c982021-03-19 20:09:32 +01002941
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 (void) key_buffer;
2943 (void) key_buffer_size;
2944 (void) hash;
2945 (void) hash_length;
2946 (void) signature;
2947 (void) signature_size;
2948 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002949
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002951}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002952
Gilles Peskine449bd832023-01-11 14:50:10 +01002953psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2954 psa_algorithm_t alg,
2955 const uint8_t *hash,
2956 size_t hash_length,
2957 uint8_t *signature,
2958 size_t signature_size,
2959 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002960{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002961 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002962 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03002964}
2965
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002966psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002967 const psa_key_attributes_t *attributes,
2968 const uint8_t *key_buffer, size_t key_buffer_size,
2969 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03002971{
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
2973 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2974 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002975#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2977 return mbedtls_psa_rsa_verify_hash(
2978 attributes,
2979 key_buffer, key_buffer_size,
2980 alg, hash, hash_length,
2981 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002982#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2983 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002984 } else {
2985 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002986 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2988 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002989#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2991 return mbedtls_psa_ecdsa_verify_hash(
2992 attributes,
2993 key_buffer, key_buffer_size,
2994 alg, hash, hash_length,
2995 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002996#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2997 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 } else {
2999 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003000 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003001 }
Ronald Cron4501c982021-03-19 20:09:32 +01003002
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 (void) key_buffer;
3004 (void) key_buffer_size;
3005 (void) hash;
3006 (void) hash_length;
3007 (void) signature;
3008 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003009
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003011}
3012
Gilles Peskine449bd832023-01-11 14:50:10 +01003013psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3014 psa_algorithm_t alg,
3015 const uint8_t *hash,
3016 size_t hash_length,
3017 const uint8_t *signature,
3018 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003019{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003020 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003021 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003023}
3024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3026 psa_algorithm_t alg,
3027 const uint8_t *input,
3028 size_t input_length,
3029 const uint8_t *salt,
3030 size_t salt_length,
3031 uint8_t *output,
3032 size_t output_size,
3033 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003034{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003035 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003036 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003037 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003038
Darryl Green5cc689a2018-07-24 15:34:10 +01003039 (void) input;
3040 (void) input_length;
3041 (void) salt;
3042 (void) output;
3043 (void) output_size;
3044
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003045 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003046
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3048 return PSA_ERROR_INVALID_ARGUMENT;
3049 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003050
Ronald Cron5c522922020-11-14 16:35:34 +01003051 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3053 if (status != PSA_SUCCESS) {
3054 return status;
3055 }
3056 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3057 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003058 status = PSA_ERROR_INVALID_ARGUMENT;
3059 goto exit;
3060 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003061
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003062 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003064 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003065
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003066 status = psa_driver_wrapper_asymmetric_encrypt(
3067 &attributes, slot->key.data, slot->key.bytes,
3068 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003070exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003072
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003074}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003075
Gilles Peskine449bd832023-01-11 14:50:10 +01003076psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3077 psa_algorithm_t alg,
3078 const uint8_t *input,
3079 size_t input_length,
3080 const uint8_t *salt,
3081 size_t salt_length,
3082 uint8_t *output,
3083 size_t output_size,
3084 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003085{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003086 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003087 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003088 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003089
Darryl Green5cc689a2018-07-24 15:34:10 +01003090 (void) input;
3091 (void) input_length;
3092 (void) salt;
3093 (void) output;
3094 (void) output_size;
3095
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003096 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003097
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3099 return PSA_ERROR_INVALID_ARGUMENT;
3100 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003101
Ronald Cron5c522922020-11-14 16:35:34 +01003102 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3104 if (status != PSA_SUCCESS) {
3105 return status;
3106 }
3107 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003108 status = PSA_ERROR_INVALID_ARGUMENT;
3109 goto exit;
3110 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003111
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003112 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003114 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003115
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003116 status = psa_driver_wrapper_asymmetric_decrypt(
3117 &attributes, slot->key.data, slot->key.bytes,
3118 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003120
3121exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003123
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003125}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003126
Paul Elliott9fe12f62022-11-30 19:16:02 +00003127/****************************************************************/
3128/* Asymmetric interruptible cryptography */
3129/****************************************************************/
3130
3131void psa_interruptible_set_max_ops(uint32_t max_ops)
3132{
3133 psa_driver_wrapper_interruptible_set_max_ops(max_ops);
3134}
3135
3136uint32_t psa_interruptible_get_max_ops(void)
3137{
3138 return psa_driver_wrapper_interruptible_get_max_ops();
3139}
3140
3141
3142uint32_t psa_sign_hash_get_num_ops(
3143 const psa_sign_hash_interruptible_operation_t *operation)
3144{
3145 return psa_driver_wrapper_sign_hash_get_num_ops(operation);
3146}
3147
3148uint32_t psa_verify_hash_get_num_ops(
3149 const psa_verify_hash_interruptible_operation_t *operation)
3150{
3151 return psa_driver_wrapper_verify_hash_get_num_ops(operation);
3152}
3153
3154psa_status_t psa_sign_hash_start(
3155 psa_sign_hash_interruptible_operation_t *operation,
3156 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3157 const uint8_t *hash, size_t hash_length)
3158{
3159 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3160 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3161 psa_key_slot_t *slot;
3162
3163 /* Check that start has not been previously called. */
3164 if (operation->id != 0) {
3165 return PSA_ERROR_BAD_STATE;
3166 }
3167
3168
3169 status = psa_sign_verify_check_alg(0, alg);
3170 if (status != PSA_SUCCESS) {
3171 return status;
3172 }
3173
3174 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3175 PSA_KEY_USAGE_SIGN_HASH,
3176 alg);
3177
3178 if (status != PSA_SUCCESS) {
3179 goto exit;
3180 }
3181
3182 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3183 status = PSA_ERROR_INVALID_ARGUMENT;
3184 goto exit;
3185 }
3186
3187 psa_key_attributes_t attributes = {
3188 .core = slot->attr
3189 };
3190
3191 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3192 slot->key.data,
3193 slot->key.bytes, alg,
3194 hash, hash_length);
3195exit:
3196
3197 if (status != PSA_SUCCESS) {
3198 psa_sign_hash_abort(operation);
3199 }
3200
3201 unlock_status = psa_unlock_key_slot(slot);
3202
3203 return (status == PSA_SUCCESS) ? unlock_status : status;
3204
3205}
3206
3207
3208psa_status_t psa_sign_hash_complete(
3209 psa_sign_hash_interruptible_operation_t *operation,
3210 uint8_t *signature, size_t signature_size,
3211 size_t *signature_length)
3212{
3213 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3214
3215 *signature_length = 0;
3216
3217 /* Check that start has been called first. */
3218 if (operation->id == 0) {
3219 status = PSA_ERROR_BAD_STATE;
3220 goto exit;
3221 }
3222
3223 /* Immediately reject a zero-length signature buffer. This guarantees
3224 * that signature must be a valid pointer. (On the other hand, the input
3225 * buffer can in principle be empty since it doesn't actually have
3226 * to be a hash.) */
3227 if (signature_size == 0) {
3228 status = PSA_ERROR_BUFFER_TOO_SMALL;
3229 goto exit;
3230 }
3231
3232 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3233 signature_size,
3234 signature_length);
3235exit:
3236
3237 if (status != PSA_OPERATION_INCOMPLETE) {
3238 /* Fill the unused part of the output buffer (the whole buffer on error,
3239 * the trailing part on success) with something that isn't a valid
3240 * signature (barring an attack on the signature and
3241 * deliberately-crafted input), in case the caller doesn't check the
3242 * return status properly.*/
3243 if (status == PSA_SUCCESS) {
3244 memset(signature + *signature_length, '!',
3245 signature_size - *signature_length);
3246 } else if (signature_size > 0) {
3247 memset(signature, '!', signature_size);
3248 }
3249 /* If signature_size is 0 then we have nothing to do. We must not
3250 * call memset because signature may be NULL in this case.*/
3251
3252 psa_sign_hash_abort(operation);
3253 }
3254
3255 return status;
3256}
3257
3258psa_status_t psa_sign_hash_abort(
3259 psa_sign_hash_interruptible_operation_t *operation)
3260{
3261 if (operation->id == 0) {
3262 /* The object has (apparently) been initialized but it is not (yet)
3263 * in use. It's ok to call abort on such an object, and there's
3264 * nothing to do. */
3265 return PSA_SUCCESS;
3266 }
3267
3268 psa_driver_wrapper_sign_hash_abort(operation);
3269
3270 operation->id = 0;
3271
3272 return PSA_SUCCESS;
3273}
3274
3275psa_status_t psa_verify_hash_start(
3276 psa_verify_hash_interruptible_operation_t *operation,
3277 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3278 const uint8_t *hash, size_t hash_length,
3279 const uint8_t *signature, size_t signature_length)
3280{
3281 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3282 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3283 psa_key_slot_t *slot;
3284
3285 /* Check that start has not been previously called. */
3286 if (operation->id != 0) {
3287 return PSA_ERROR_BAD_STATE;
3288 }
3289
3290 status = psa_sign_verify_check_alg(0, alg);
3291 if (status != PSA_SUCCESS) {
3292 return status;
3293 }
3294
3295 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3296 PSA_KEY_USAGE_VERIFY_HASH,
3297 alg);
3298
3299 if (status != PSA_SUCCESS) {
3300 return status;
3301 }
3302
3303 psa_key_attributes_t attributes = {
3304 .core = slot->attr
3305 };
3306
3307 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3308 slot->key.data,
3309 slot->key.bytes,
3310 alg, hash, hash_length,
3311 signature, signature_length);
3312
3313 if (status != PSA_SUCCESS) {
3314 psa_verify_hash_abort(operation);
3315 }
3316
3317 unlock_status = psa_unlock_key_slot(slot);
3318
3319 return (status == PSA_SUCCESS) ? unlock_status : status;
3320
3321 return status;
3322}
3323
3324psa_status_t psa_verify_hash_complete(
3325 psa_verify_hash_interruptible_operation_t *operation)
3326{
3327 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3328
3329 /* Check that start has been called first. */
3330 if (operation->id == 0) {
3331 status = PSA_ERROR_BAD_STATE;
3332 goto exit;
3333 }
3334
3335 status = psa_driver_wrapper_verify_hash_complete(operation);
3336
3337exit:
3338
3339 if (status != PSA_OPERATION_INCOMPLETE) {
3340 psa_verify_hash_abort(operation);
3341 }
3342
3343 return status;
3344}
3345
3346psa_status_t psa_verify_hash_abort(
3347 psa_verify_hash_interruptible_operation_t *operation)
3348{
3349 if (operation->id == 0) {
3350 /* The object has (apparently) been initialized but it is not (yet)
3351 * in use. It's ok to call abort on such an object, and there's
3352 * nothing to do. */
3353 return PSA_SUCCESS;
3354 }
3355
3356 psa_driver_wrapper_verify_hash_abort(operation);
3357
3358 operation->id = 0;
3359
3360 return PSA_SUCCESS;
3361}
3362
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003363
3364
mohammad1603503973b2018-03-12 15:59:30 +02003365/****************************************************************/
3366/* Symmetric cryptography */
3367/****************************************************************/
3368
Gilles Peskine449bd832023-01-11 14:50:10 +01003369static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3370 mbedtls_svc_key_id_t key,
3371 psa_algorithm_t alg,
3372 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003373{
3374 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3375 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003376 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003377 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3378 PSA_KEY_USAGE_ENCRYPT :
3379 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003380
3381 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003383 status = PSA_ERROR_BAD_STATE;
3384 goto exit;
3385 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003386
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003388 status = PSA_ERROR_INVALID_ARGUMENT;
3389 goto exit;
3390 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003391
Gilles Peskine449bd832023-01-11 14:50:10 +01003392 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3393 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003394 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003396
Ronald Cron49fafa92021-03-10 08:34:23 +01003397 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003398 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003399 * so we only set it (in the driver wrapper) after resources have been
3400 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003401 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003403 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003404 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003405 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003406 }
3407 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003408
Ronald Crona4af55f2020-12-14 14:36:06 +01003409 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003410 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003411 };
3412
Ronald Cronab99ac22020-10-01 16:38:28 +02003413 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003414 if (cipher_operation == MBEDTLS_ENCRYPT) {
3415 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3416 &attributes,
3417 slot->key.data,
3418 slot->key.bytes,
3419 alg);
3420 } else {
3421 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3422 &attributes,
3423 slot->key.data,
3424 slot->key.bytes,
3425 alg);
3426 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003427
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003428exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003429 if (status != PSA_SUCCESS) {
3430 psa_cipher_abort(operation);
3431 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003432
Gilles Peskine449bd832023-01-11 14:50:10 +01003433 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003434
Gilles Peskine449bd832023-01-11 14:50:10 +01003435 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003436}
3437
Gilles Peskine449bd832023-01-11 14:50:10 +01003438psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3439 mbedtls_svc_key_id_t key,
3440 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003441{
Gilles Peskine449bd832023-01-11 14:50:10 +01003442 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003443}
3444
Gilles Peskine449bd832023-01-11 14:50:10 +01003445psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3446 mbedtls_svc_key_id_t key,
3447 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003448{
Gilles Peskine449bd832023-01-11 14:50:10 +01003449 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003450}
3451
Gilles Peskine449bd832023-01-11 14:50:10 +01003452psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3453 uint8_t *iv,
3454 size_t iv_size,
3455 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003456{
Ronald Cron6d051732020-10-01 14:10:20 +02003457 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02003458 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3459 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01003460
Gilles Peskine449bd832023-01-11 14:50:10 +01003461 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003462 status = PSA_ERROR_BAD_STATE;
3463 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003464 }
3465
Gilles Peskine449bd832023-01-11 14:50:10 +01003466 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003467 status = PSA_ERROR_BAD_STATE;
3468 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003469 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003470
Ronald Cron2fb90522021-07-05 12:31:44 +02003471 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003472 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003473 status = PSA_ERROR_BUFFER_TOO_SMALL;
3474 goto exit;
3475 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003476
Gilles Peskine449bd832023-01-11 14:50:10 +01003477 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02003478 status = PSA_ERROR_GENERIC_ERROR;
3479 goto exit;
3480 }
3481
Gilles Peskine449bd832023-01-11 14:50:10 +01003482 status = psa_generate_random(local_iv, default_iv_length);
3483 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003484 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003485 }
Ronald Cron5618a392021-03-26 09:52:26 +01003486
Gilles Peskine449bd832023-01-11 14:50:10 +01003487 status = psa_driver_wrapper_cipher_set_iv(operation,
3488 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003489
3490exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003491 if (status == PSA_SUCCESS) {
3492 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02003493 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003494 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003495 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02003496 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003497 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02003498 }
Ronald Cron6d051732020-10-01 14:10:20 +02003499
Gilles Peskine449bd832023-01-11 14:50:10 +01003500 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003501}
3502
Gilles Peskine449bd832023-01-11 14:50:10 +01003503psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
3504 const uint8_t *iv,
3505 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003506{
Ronald Cron6d051732020-10-01 14:10:20 +02003507 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003508
Gilles Peskine449bd832023-01-11 14:50:10 +01003509 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003510 status = PSA_ERROR_BAD_STATE;
3511 goto exit;
3512 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003513
Gilles Peskine449bd832023-01-11 14:50:10 +01003514 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003515 status = PSA_ERROR_BAD_STATE;
3516 goto exit;
3517 }
Ronald Crona0d68172021-03-26 10:15:08 +01003518
Gilles Peskine449bd832023-01-11 14:50:10 +01003519 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003520 status = PSA_ERROR_INVALID_ARGUMENT;
3521 goto exit;
3522 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003523
Gilles Peskine449bd832023-01-11 14:50:10 +01003524 status = psa_driver_wrapper_cipher_set_iv(operation,
3525 iv,
3526 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003527
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003528exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003529 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003530 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003531 } else {
3532 psa_cipher_abort(operation);
3533 }
3534 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003535}
3536
Gilles Peskine449bd832023-01-11 14:50:10 +01003537psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
3538 const uint8_t *input,
3539 size_t input_length,
3540 uint8_t *output,
3541 size_t output_size,
3542 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003543{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003544 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003545
Gilles Peskine449bd832023-01-11 14:50:10 +01003546 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003547 status = PSA_ERROR_BAD_STATE;
3548 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003549 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003550
Gilles Peskine449bd832023-01-11 14:50:10 +01003551 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003552 status = PSA_ERROR_BAD_STATE;
3553 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003554 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003555
Gilles Peskine449bd832023-01-11 14:50:10 +01003556 status = psa_driver_wrapper_cipher_update(operation,
3557 input,
3558 input_length,
3559 output,
3560 output_size,
3561 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003562
3563exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003564 if (status != PSA_SUCCESS) {
3565 psa_cipher_abort(operation);
3566 }
Ronald Cron6d051732020-10-01 14:10:20 +02003567
Gilles Peskine449bd832023-01-11 14:50:10 +01003568 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003569}
3570
Gilles Peskine449bd832023-01-11 14:50:10 +01003571psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
3572 uint8_t *output,
3573 size_t output_size,
3574 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003575{
David Saadab4ecc272019-02-14 13:48:10 +02003576 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003577
Gilles Peskine449bd832023-01-11 14:50:10 +01003578 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003579 status = PSA_ERROR_BAD_STATE;
3580 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003581 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003582
Gilles Peskine449bd832023-01-11 14:50:10 +01003583 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003584 status = PSA_ERROR_BAD_STATE;
3585 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003586 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003587
Gilles Peskine449bd832023-01-11 14:50:10 +01003588 status = psa_driver_wrapper_cipher_finish(operation,
3589 output,
3590 output_size,
3591 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003592
3593exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003594 if (status == PSA_SUCCESS) {
3595 return psa_cipher_abort(operation);
3596 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003597 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003598 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01003599
Gilles Peskine449bd832023-01-11 14:50:10 +01003600 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003601 }
mohammad1603503973b2018-03-12 15:59:30 +02003602}
3603
Gilles Peskine449bd832023-01-11 14:50:10 +01003604psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003605{
Gilles Peskine449bd832023-01-11 14:50:10 +01003606 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003607 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003608 * in use. It's ok to call abort on such an object, and there's
3609 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003610 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003611 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003612
Gilles Peskine449bd832023-01-11 14:50:10 +01003613 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003614
Ronald Cron49fafa92021-03-10 08:34:23 +01003615 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003616 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003617 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003618
Gilles Peskine449bd832023-01-11 14:50:10 +01003619 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003620}
3621
Gilles Peskine449bd832023-01-11 14:50:10 +01003622psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3623 psa_algorithm_t alg,
3624 const uint8_t *input,
3625 size_t input_length,
3626 uint8_t *output,
3627 size_t output_size,
3628 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003629{
3630 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003631 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02003632 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02003633 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3634 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02003635
Gilles Peskine449bd832023-01-11 14:50:10 +01003636 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02003637 status = PSA_ERROR_INVALID_ARGUMENT;
3638 goto exit;
3639 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003640
Gilles Peskine449bd832023-01-11 14:50:10 +01003641 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3642 PSA_KEY_USAGE_ENCRYPT,
3643 alg);
3644 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02003645 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003646 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003647
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003648 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003649 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003650 };
3651
Gilles Peskine449bd832023-01-11 14:50:10 +01003652 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
3653 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02003654 status = PSA_ERROR_GENERIC_ERROR;
3655 goto exit;
3656 }
3657
Gilles Peskine449bd832023-01-11 14:50:10 +01003658 if (default_iv_length > 0) {
3659 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003660 status = PSA_ERROR_BUFFER_TOO_SMALL;
3661 goto exit;
3662 }
3663
Gilles Peskine449bd832023-01-11 14:50:10 +01003664 status = psa_generate_random(local_iv, default_iv_length);
3665 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003666 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003667 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003668 }
3669
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003670 status = psa_driver_wrapper_cipher_encrypt(
3671 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02003672 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003673 mbedtls_buffer_offset(output, default_iv_length),
3674 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003675
3676exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003677 unlock_status = psa_unlock_key_slot(slot);
3678 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02003679 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02003680 }
Ronald Cron23919522021-07-08 19:00:07 +02003681
Gilles Peskine449bd832023-01-11 14:50:10 +01003682 if (status == PSA_SUCCESS) {
3683 if (default_iv_length > 0) {
3684 memcpy(output, local_iv, default_iv_length);
3685 }
3686 *output_length += default_iv_length;
3687 } else {
3688 *output_length = 0;
3689 }
3690
3691 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003692}
3693
Gilles Peskine449bd832023-01-11 14:50:10 +01003694psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
3695 psa_algorithm_t alg,
3696 const uint8_t *input,
3697 size_t input_length,
3698 uint8_t *output,
3699 size_t output_size,
3700 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003701{
3702 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003703 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02003704 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02003705
Gilles Peskine449bd832023-01-11 14:50:10 +01003706 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02003707 status = PSA_ERROR_INVALID_ARGUMENT;
3708 goto exit;
3709 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003710
Gilles Peskine449bd832023-01-11 14:50:10 +01003711 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3712 PSA_KEY_USAGE_DECRYPT,
3713 alg);
3714 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02003715 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003716 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003717
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003718 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003719 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003720 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003721
Gilles Peskine449bd832023-01-11 14:50:10 +01003722 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
3723 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02003724 status = PSA_ERROR_INVALID_ARGUMENT;
3725 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003726 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02003727 status = PSA_ERROR_INVALID_ARGUMENT;
3728 goto exit;
3729 }
3730
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003731 status = psa_driver_wrapper_cipher_decrypt(
3732 &attributes, slot->key.data, slot->key.bytes,
3733 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003734 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003735
gabor-mezei-arm258ae072021-06-25 15:25:38 +02003736exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003737 unlock_status = psa_unlock_key_slot(slot);
3738 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02003739 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003740 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003741
Gilles Peskine449bd832023-01-11 14:50:10 +01003742 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02003743 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003744 }
Ronald Cron23919522021-07-08 19:00:07 +02003745
Gilles Peskine449bd832023-01-11 14:50:10 +01003746 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003747}
3748
3749
mohammad16035955c982018-04-26 00:53:03 +03003750/****************************************************************/
3751/* AEAD */
3752/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003753
Paul Elliottbaff51c2021-09-28 17:44:45 +01003754/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003755static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01003756{
Gilles Peskine449bd832023-01-11 14:50:10 +01003757 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01003758}
3759
3760/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003761static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
3762 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003763{
Gilles Peskine449bd832023-01-11 14:50:10 +01003764 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01003765
Gilles Peskine449bd832023-01-11 14:50:10 +01003766 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003767#if defined(PSA_WANT_ALG_GCM)
3768 case PSA_ALG_GCM:
3769 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01003770 * arbitrarily large nonces. Please note that we do not generally
3771 * recommend the usage of nonces of greater length than
3772 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
3773 * size, which can then lead to collisions if you encrypt a very
3774 * large number of messages.*/
3775 if (nonce_length != 0) {
3776 return PSA_SUCCESS;
3777 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003778 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003779#endif /* PSA_WANT_ALG_GCM */
3780#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003781 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01003782 if (nonce_length >= 7 && nonce_length <= 13) {
3783 return PSA_SUCCESS;
3784 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003785 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003786#endif /* PSA_WANT_ALG_CCM */
3787#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003788 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01003789 if (nonce_length == 12) {
3790 return PSA_SUCCESS;
3791 } else if (nonce_length == 8) {
3792 return PSA_ERROR_NOT_SUPPORTED;
3793 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01003794 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003795#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003796 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02003797 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003798 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003799 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003800
Gilles Peskine449bd832023-01-11 14:50:10 +01003801 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003802}
3803
Gilles Peskine449bd832023-01-11 14:50:10 +01003804static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01003805{
Gilles Peskine449bd832023-01-11 14:50:10 +01003806 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3807 return PSA_ERROR_INVALID_ARGUMENT;
3808 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01003809
Gilles Peskine449bd832023-01-11 14:50:10 +01003810 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01003811}
3812
Gilles Peskine449bd832023-01-11 14:50:10 +01003813psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
3814 psa_algorithm_t alg,
3815 const uint8_t *nonce,
3816 size_t nonce_length,
3817 const uint8_t *additional_data,
3818 size_t additional_data_length,
3819 const uint8_t *plaintext,
3820 size_t plaintext_length,
3821 uint8_t *ciphertext,
3822 size_t ciphertext_size,
3823 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03003824{
Ronald Cron215633c2021-03-16 17:15:37 +01003825 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3826 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003827
mohammad1603f08a5502018-06-03 15:05:47 +03003828 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003829
Gilles Peskine449bd832023-01-11 14:50:10 +01003830 status = psa_aead_check_algorithm(alg);
3831 if (status != PSA_SUCCESS) {
3832 return status;
3833 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003834
Ronald Cron9a986162021-03-26 12:40:07 +01003835 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003836 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3837 if (status != PSA_SUCCESS) {
3838 return status;
3839 }
mohammad16035955c982018-04-26 00:53:03 +03003840
Ronald Cron215633c2021-03-16 17:15:37 +01003841 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003842 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003843 };
mohammad16035955c982018-04-26 00:53:03 +03003844
Gilles Peskine449bd832023-01-11 14:50:10 +01003845 status = psa_aead_check_nonce_length(alg, nonce_length);
3846 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003847 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003848 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003849
Ronald Cronde822812021-03-17 16:08:20 +01003850 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003851 &attributes, slot->key.data, slot->key.bytes,
3852 alg,
3853 nonce, nonce_length,
3854 additional_data, additional_data_length,
3855 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003856 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02003857
Gilles Peskine449bd832023-01-11 14:50:10 +01003858 if (status != PSA_SUCCESS && ciphertext_size != 0) {
3859 memset(ciphertext, 0, ciphertext_size);
3860 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003861
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003862exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003863 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03003864
Gilles Peskine449bd832023-01-11 14:50:10 +01003865 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02003866}
3867
Gilles Peskine449bd832023-01-11 14:50:10 +01003868psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
3869 psa_algorithm_t alg,
3870 const uint8_t *nonce,
3871 size_t nonce_length,
3872 const uint8_t *additional_data,
3873 size_t additional_data_length,
3874 const uint8_t *ciphertext,
3875 size_t ciphertext_length,
3876 uint8_t *plaintext,
3877 size_t plaintext_size,
3878 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03003879{
Ronald Cron215633c2021-03-16 17:15:37 +01003880 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3881 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003882
Gilles Peskineee652a32018-06-01 19:23:52 +02003883 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003884
Gilles Peskine449bd832023-01-11 14:50:10 +01003885 status = psa_aead_check_algorithm(alg);
3886 if (status != PSA_SUCCESS) {
3887 return status;
3888 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003889
Ronald Cron9a986162021-03-26 12:40:07 +01003890 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003891 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3892 if (status != PSA_SUCCESS) {
3893 return status;
3894 }
mohammad16035955c982018-04-26 00:53:03 +03003895
Ronald Cron215633c2021-03-16 17:15:37 +01003896 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003897 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003898 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003899
Gilles Peskine449bd832023-01-11 14:50:10 +01003900 status = psa_aead_check_nonce_length(alg, nonce_length);
3901 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003902 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003903 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003904
Ronald Cronde822812021-03-17 16:08:20 +01003905 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003906 &attributes, slot->key.data, slot->key.bytes,
3907 alg,
3908 nonce, nonce_length,
3909 additional_data, additional_data_length,
3910 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003911 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02003912
Gilles Peskine449bd832023-01-11 14:50:10 +01003913 if (status != PSA_SUCCESS && plaintext_size != 0) {
3914 memset(plaintext, 0, plaintext_size);
3915 }
mohammad160360a64d02018-06-03 17:20:42 +03003916
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003917exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003918 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01003919
Gilles Peskine449bd832023-01-11 14:50:10 +01003920 return status;
mohammad16035955c982018-04-26 00:53:03 +03003921}
3922
Gilles Peskine449bd832023-01-11 14:50:10 +01003923static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
3924{
3925 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01003926
Gilles Peskine449bd832023-01-11 14:50:10 +01003927 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02003928#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01003929 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01003930 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01003931 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
3932 return PSA_ERROR_INVALID_ARGUMENT;
3933 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01003934 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02003935#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01003936
Przemek Stekiel86679c72022-10-06 17:06:56 +02003937#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01003938 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01003939 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003940 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
3941 return PSA_ERROR_INVALID_ARGUMENT;
3942 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01003943 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02003944#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01003945
Przemek Stekiel86679c72022-10-06 17:06:56 +02003946#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01003947 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01003948 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003949 if (tag_len != 16) {
3950 return PSA_ERROR_INVALID_ARGUMENT;
3951 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01003952 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02003953#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01003954
3955 default:
3956 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003957 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01003958 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003959 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01003960}
3961
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003962/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003963static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
3964 int is_encrypt,
3965 mbedtls_svc_key_id_t key,
3966 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01003967{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003968 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3969 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3970 psa_key_slot_t *slot = NULL;
3971 psa_key_usage_t key_usage = 0;
3972
Gilles Peskine449bd832023-01-11 14:50:10 +01003973 status = psa_aead_check_algorithm(alg);
3974 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003975 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003976 }
Paul Elliottc2b71442021-06-24 18:17:52 +01003977
Gilles Peskine449bd832023-01-11 14:50:10 +01003978 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003979 status = PSA_ERROR_BAD_STATE;
3980 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01003981 }
3982
Gilles Peskine449bd832023-01-11 14:50:10 +01003983 if (operation->nonce_set || operation->lengths_set ||
3984 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003985 status = PSA_ERROR_BAD_STATE;
3986 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01003987 }
3988
Gilles Peskine449bd832023-01-11 14:50:10 +01003989 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003990 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01003991 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003992 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01003993 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003994
Gilles Peskine449bd832023-01-11 14:50:10 +01003995 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
3996 alg);
3997 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003998 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003999 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004000
4001 psa_key_attributes_t attributes = {
4002 .core = slot->attr
4003 };
4004
Gilles Peskine449bd832023-01-11 14:50:10 +01004005 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004006 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004007 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004008
Gilles Peskine449bd832023-01-11 14:50:10 +01004009 if (is_encrypt) {
4010 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4011 &attributes,
4012 slot->key.data,
4013 slot->key.bytes,
4014 alg);
4015 } else {
4016 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4017 &attributes,
4018 slot->key.data,
4019 slot->key.bytes,
4020 alg);
4021 }
4022 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004023 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004024 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004025
Gilles Peskine449bd832023-01-11 14:50:10 +01004026 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004027
4028exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004029 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004030
Gilles Peskine449bd832023-01-11 14:50:10 +01004031 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004032 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004033 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004034 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 } else {
4036 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004037 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004038
Gilles Peskine449bd832023-01-11 14:50:10 +01004039 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004040}
4041
Paul Elliott302ff6b2021-04-20 18:10:30 +01004042/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004043psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4044 mbedtls_svc_key_id_t key,
4045 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004046{
Gilles Peskine449bd832023-01-11 14:50:10 +01004047 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004048}
4049
4050/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004051psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4052 mbedtls_svc_key_id_t key,
4053 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004054{
Gilles Peskine449bd832023-01-11 14:50:10 +01004055 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004056}
4057
4058/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004059psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4060 uint8_t *nonce,
4061 size_t nonce_size,
4062 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004063{
4064 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004065 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004066 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004067
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004068 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004069
Gilles Peskine449bd832023-01-11 14:50:10 +01004070 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004071 status = PSA_ERROR_BAD_STATE;
4072 goto exit;
4073 }
4074
Gilles Peskine449bd832023-01-11 14:50:10 +01004075 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004076 status = PSA_ERROR_BAD_STATE;
4077 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004078 }
4079
Paul Elliotte193ea82021-10-01 13:00:16 +01004080 /* For CCM, this size may not be correct according to the PSA
4081 * specification. The PSA Crypto 1.0.1 specification states:
4082 *
4083 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4084 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4085 *
4086 * However this restriction that L has to be the smallest integer is not
4087 * applied in practice, and it is not implementable here since the
4088 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004089 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4090 operation->alg);
4091 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004092 status = PSA_ERROR_BUFFER_TOO_SMALL;
4093 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004094 }
4095
Gilles Peskine449bd832023-01-11 14:50:10 +01004096 status = psa_generate_random(local_nonce, required_nonce_size);
4097 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004098 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004099 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004100
Gilles Peskine449bd832023-01-11 14:50:10 +01004101 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004102
Paul Elliott39dc6b82021-05-11 19:16:09 +01004103exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 if (status == PSA_SUCCESS) {
4105 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004106 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004107 } else {
4108 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004109 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004110
Gilles Peskine449bd832023-01-11 14:50:10 +01004111 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004112}
4113
4114/* Set the nonce for a multipart authenticated encryption or decryption
4115 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004116psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4117 const uint8_t *nonce,
4118 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004119{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004120 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4121
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004123 status = PSA_ERROR_BAD_STATE;
4124 goto exit;
4125 }
4126
Gilles Peskine449bd832023-01-11 14:50:10 +01004127 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004128 status = PSA_ERROR_BAD_STATE;
4129 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004130 }
4131
Gilles Peskine449bd832023-01-11 14:50:10 +01004132 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4133 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004134 status = PSA_ERROR_INVALID_ARGUMENT;
4135 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004136 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004137
Gilles Peskine449bd832023-01-11 14:50:10 +01004138 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4139 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004140
Paul Elliott39dc6b82021-05-11 19:16:09 +01004141exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004142 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004143 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004144 } else {
4145 psa_aead_abort(operation);
4146 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004147
Gilles Peskine449bd832023-01-11 14:50:10 +01004148 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004149}
4150
4151/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004152psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4153 size_t ad_length,
4154 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004155{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004156 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4157
Gilles Peskine449bd832023-01-11 14:50:10 +01004158 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004159 status = PSA_ERROR_BAD_STATE;
4160 goto exit;
4161 }
4162
Gilles Peskine449bd832023-01-11 14:50:10 +01004163 if (operation->lengths_set || operation->ad_started ||
4164 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004165 status = PSA_ERROR_BAD_STATE;
4166 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004167 }
4168
Gilles Peskine449bd832023-01-11 14:50:10 +01004169 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004170#if defined(PSA_WANT_ALG_GCM)
4171 case PSA_ALG_GCM:
4172 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004173 * bits. Without the guard this code will generate warnings on 32bit
4174 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004175#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004176 if (((uint64_t) ad_length) >> 61 != 0 ||
4177 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004178 status = PSA_ERROR_INVALID_ARGUMENT;
4179 goto exit;
4180 }
Paul Elliott325d3742021-09-27 17:56:28 +01004181#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004182 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004183#endif /* PSA_WANT_ALG_GCM */
4184#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004185 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004186 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004187 status = PSA_ERROR_INVALID_ARGUMENT;
4188 goto exit;
4189 }
4190 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004191#endif /* PSA_WANT_ALG_CCM */
4192#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004193 case PSA_ALG_CHACHA20_POLY1305:
4194 /* No length restrictions for ChaChaPoly. */
4195 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004196#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004197 default:
4198 break;
4199 }
Paul Elliott325d3742021-09-27 17:56:28 +01004200
Gilles Peskine449bd832023-01-11 14:50:10 +01004201 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4202 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004203
Paul Elliott39dc6b82021-05-11 19:16:09 +01004204exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004205 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004206 operation->ad_remaining = ad_length;
4207 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004208 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 } else {
4210 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004211 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004212
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004214}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004215
4216/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004217psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4218 const uint8_t *input,
4219 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004220{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004221 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4222
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004224 status = PSA_ERROR_BAD_STATE;
4225 goto exit;
4226 }
4227
Gilles Peskine449bd832023-01-11 14:50:10 +01004228 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004229 status = PSA_ERROR_BAD_STATE;
4230 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004231 }
4232
Gilles Peskine449bd832023-01-11 14:50:10 +01004233 if (operation->lengths_set) {
4234 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004235 status = PSA_ERROR_INVALID_ARGUMENT;
4236 goto exit;
4237 }
4238
4239 operation->ad_remaining -= input_length;
4240 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004241#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004242 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004243 status = PSA_ERROR_BAD_STATE;
4244 goto exit;
4245 }
4246#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004247
Gilles Peskine449bd832023-01-11 14:50:10 +01004248 status = psa_driver_wrapper_aead_update_ad(operation, input,
4249 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004250
Paul Elliott39dc6b82021-05-11 19:16:09 +01004251exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004252 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004253 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004254 } else {
4255 psa_aead_abort(operation);
4256 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004257
Gilles Peskine449bd832023-01-11 14:50:10 +01004258 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004259}
4260
4261/* Encrypt or decrypt a message fragment in an active multipart AEAD
4262 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004263psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4264 const uint8_t *input,
4265 size_t input_length,
4266 uint8_t *output,
4267 size_t output_size,
4268 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004269{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004270 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004271
4272 *output_length = 0;
4273
Gilles Peskine449bd832023-01-11 14:50:10 +01004274 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004275 status = PSA_ERROR_BAD_STATE;
4276 goto exit;
4277 }
4278
Gilles Peskine449bd832023-01-11 14:50:10 +01004279 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004280 status = PSA_ERROR_BAD_STATE;
4281 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004282 }
4283
Gilles Peskine449bd832023-01-11 14:50:10 +01004284 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004285 /* Additional data length was supplied, but not all the additional
4286 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004287 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004288 status = PSA_ERROR_INVALID_ARGUMENT;
4289 goto exit;
4290 }
4291
4292 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004293 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004294 status = PSA_ERROR_INVALID_ARGUMENT;
4295 goto exit;
4296 }
4297
4298 operation->body_remaining -= input_length;
4299 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004300#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004301 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004302 status = PSA_ERROR_BAD_STATE;
4303 goto exit;
4304 }
4305#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004306
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4308 output, output_size,
4309 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004310
Paul Elliott39dc6b82021-05-11 19:16:09 +01004311exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004313 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004314 } else {
4315 psa_aead_abort(operation);
4316 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004317
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004319}
4320
Gilles Peskine449bd832023-01-11 14:50:10 +01004321static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004322{
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 if (operation->id == 0 || !operation->nonce_set) {
4324 return PSA_ERROR_BAD_STATE;
4325 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004326
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4328 operation->body_remaining != 0)) {
4329 return PSA_ERROR_INVALID_ARGUMENT;
4330 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004331
Gilles Peskine449bd832023-01-11 14:50:10 +01004332 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004333}
4334
Paul Elliott302ff6b2021-04-20 18:10:30 +01004335/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004336psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4337 uint8_t *ciphertext,
4338 size_t ciphertext_size,
4339 size_t *ciphertext_length,
4340 uint8_t *tag,
4341 size_t tag_size,
4342 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004343{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004344 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4345
Paul Elliott302ff6b2021-04-20 18:10:30 +01004346 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004347 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004348
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 status = psa_aead_final_checks(operation);
4350 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004351 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004352 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004353
Gilles Peskine449bd832023-01-11 14:50:10 +01004354 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004355 status = PSA_ERROR_BAD_STATE;
4356 goto exit;
4357 }
4358
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4360 ciphertext_size,
4361 ciphertext_length,
4362 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004363
Paul Elliott39dc6b82021-05-11 19:16:09 +01004364exit:
Paul Elliottf47b0952021-05-21 18:02:33 +01004365 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004366 * the zero tag size, make sure the tag is set to something implausible.
4367 * Even if the operation succeeds, make sure we clear the rest of the
4368 * buffer to prevent potential leakage of anything previously placed in
4369 * the same buffer.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004370 if (tag != NULL) {
4371 if (status != PSA_SUCCESS) {
4372 memset(tag, '!', tag_size);
4373 } else if (*tag_length < tag_size) {
4374 memset(tag + *tag_length, '!', (tag_size - *tag_length));
4375 }
Paul Elliottec95cc92021-09-19 22:33:09 +01004376 }
Paul Elliottf47b0952021-05-21 18:02:33 +01004377
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004379
Gilles Peskine449bd832023-01-11 14:50:10 +01004380 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004381}
4382
4383/* Finish authenticating and decrypting a message in a multipart AEAD
4384 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004385psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4386 uint8_t *plaintext,
4387 size_t plaintext_size,
4388 size_t *plaintext_length,
4389 const uint8_t *tag,
4390 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004391{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004392 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4393
Paul Elliott302ff6b2021-04-20 18:10:30 +01004394 *plaintext_length = 0;
4395
Gilles Peskine449bd832023-01-11 14:50:10 +01004396 status = psa_aead_final_checks(operation);
4397 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004398 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004400
Gilles Peskine449bd832023-01-11 14:50:10 +01004401 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004402 status = PSA_ERROR_BAD_STATE;
4403 goto exit;
4404 }
4405
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4407 plaintext_size,
4408 plaintext_length,
4409 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004410
4411exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004412 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004413
Gilles Peskine449bd832023-01-11 14:50:10 +01004414 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004415}
4416
4417/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004418psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004419{
4420 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4421
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004423 /* The object has (apparently) been initialized but it is not (yet)
4424 * in use. It's ok to call abort on such an object, and there's
4425 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004426 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004427 }
4428
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004430
Gilles Peskine449bd832023-01-11 14:50:10 +01004431 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004432
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004434}
4435
Gilles Peskinee59236f2018-01-27 23:32:46 +01004436/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004437/* Generators */
4438/****************************************************************/
4439
Przemek Stekiel69c46792022-06-10 12:59:51 +02004440#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004441 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004442 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
4443 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08004444#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004445#endif /* At least one builtin KDF */
4446
Przemek Stekiel69c46792022-06-10 12:59:51 +02004447#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02004448 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4449 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4450static psa_status_t psa_key_derivation_start_hmac(
4451 psa_mac_operation_t *operation,
4452 psa_algorithm_t hash_alg,
4453 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02004455{
4456 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4457 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004458 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
4459 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
4460 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02004461
Steven Cooreman72f736a2021-05-07 14:14:37 +02004462 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004463 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02004464
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 status = psa_driver_wrapper_mac_sign_setup(operation,
4466 &attributes,
4467 hmac_key, hmac_key_length,
4468 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02004469
Gilles Peskine449bd832023-01-11 14:50:10 +01004470 psa_reset_key_attributes(&attributes);
4471 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02004472}
4473#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004474
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004475#define HKDF_STATE_INIT 0 /* no input yet */
4476#define HKDF_STATE_STARTED 1 /* got salt */
4477#define HKDF_STATE_KEYED 2 /* got key */
4478#define HKDF_STATE_OUTPUT 3 /* output started */
4479
Gilles Peskinecbe66502019-05-16 16:59:18 +02004480static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01004481 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004482{
Gilles Peskine449bd832023-01-11 14:50:10 +01004483 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4484 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
4485 } else {
4486 return operation->alg;
4487 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004488}
4489
Gilles Peskine449bd832023-01-11 14:50:10 +01004490psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004491{
4492 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01004493 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
4494 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004495 /* The object has (apparently) been initialized but it is not
4496 * in use. It's ok to call abort on such an object, and there's
4497 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004498 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004499#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
4501 mbedtls_free(operation->ctx.hkdf.info);
4502 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
4503 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004504#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08004505#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4506 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4508 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
4509 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4510 if (operation->ctx.tls12_prf.secret != NULL) {
4511 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
4512 operation->ctx.tls12_prf.secret_length);
4513 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02004514 }
4515
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 if (operation->ctx.tls12_prf.seed != NULL) {
4517 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
4518 operation->ctx.tls12_prf.seed_length);
4519 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01004520 }
4521
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 if (operation->ctx.tls12_prf.label != NULL) {
4523 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
4524 operation->ctx.tls12_prf.label_length);
4525 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01004526 }
4527
Gilles Peskine449bd832023-01-11 14:50:10 +01004528 if (operation->ctx.tls12_prf.other_secret != NULL) {
4529 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
4530 operation->ctx.tls12_prf.other_secret_length);
4531 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02004532 }
4533
Steven Cooremana6df6042021-04-29 19:32:25 +02004534 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01004535
4536 /* We leave the fields Ai and output_block to be erased safely by the
4537 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004538 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004539#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4540 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004541#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
4543 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
4544 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
4545 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004546#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004547 {
4548 status = PSA_ERROR_BAD_STATE;
4549 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 mbedtls_platform_zeroize(operation, sizeof(*operation));
4551 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004552}
4553
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004554psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004556{
Gilles Peskine449bd832023-01-11 14:50:10 +01004557 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004558 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004559 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004560 }
4561
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004562 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004564}
4565
Gilles Peskine449bd832023-01-11 14:50:10 +01004566psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
4567 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004568{
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 if (operation->alg == 0) {
4570 return PSA_ERROR_BAD_STATE;
4571 }
4572 if (capacity > operation->capacity) {
4573 return PSA_ERROR_INVALID_ARGUMENT;
4574 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004575 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004577}
4578
Przemek Stekiel69c46792022-06-10 12:59:51 +02004579#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02004580/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004581static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
4582 psa_algorithm_t kdf_alg,
4583 uint8_t *output,
4584 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004585{
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4587 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004588 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004589 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02004590#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02004592#else
4593 const uint8_t last_block = 0xff;
4594#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004595
Gilles Peskine449bd832023-01-11 14:50:10 +01004596 if (hkdf->state < HKDF_STATE_KEYED ||
4597 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02004598#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02004600#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 )) {
4602 return PSA_ERROR_BAD_STATE;
4603 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004604 hkdf->state = HKDF_STATE_OUTPUT;
4605
Gilles Peskine449bd832023-01-11 14:50:10 +01004606 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004607 /* Copy what remains of the current block */
4608 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01004609 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004610 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 }
4612 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02004613 output += n;
4614 output_length -= n;
4615 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004617 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02004619 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004620 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004621 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004622 * object was corrupted or if this function is called directly
4623 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 if (hkdf->block_number == last_block) {
4625 return PSA_ERROR_BAD_STATE;
4626 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004627
4628 /* We need a new block */
4629 ++hkdf->block_number;
4630 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004631
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4633 hash_alg,
4634 hkdf->prk,
4635 hash_length);
4636 if (status != PSA_SUCCESS) {
4637 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004638 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004639
4640 if (hkdf->block_number != 1) {
4641 status = psa_mac_update(&hkdf->hmac,
4642 hkdf->output_block,
4643 hash_length);
4644 if (status != PSA_SUCCESS) {
4645 return status;
4646 }
4647 }
4648 status = psa_mac_update(&hkdf->hmac,
4649 hkdf->info,
4650 hkdf->info_length);
4651 if (status != PSA_SUCCESS) {
4652 return status;
4653 }
4654 status = psa_mac_update(&hkdf->hmac,
4655 &hkdf->block_number, 1);
4656 if (status != PSA_SUCCESS) {
4657 return status;
4658 }
4659 status = psa_mac_sign_finish(&hkdf->hmac,
4660 hkdf->output_block,
4661 sizeof(hkdf->output_block),
4662 &hmac_output_length);
4663 if (status != PSA_SUCCESS) {
4664 return status;
4665 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004666 }
4667
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004669}
Przemek Stekiel69c46792022-06-10 12:59:51 +02004670#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004671
John Durkop07cc04a2020-11-16 22:08:34 -08004672#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4673 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004674static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4675 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01004677{
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
4679 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02004680 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4681 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004682 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004683
Janos Follath7742fee2019-06-17 12:58:10 +01004684 /* We can't be wanting more output after block 0xff, otherwise
4685 * the capacity check in psa_key_derivation_output_bytes() would have
4686 * prevented this call. It could happen only if the operation
4687 * object was corrupted or if this function is called directly
4688 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 if (tls12_prf->block_number == 0xff) {
4690 return PSA_ERROR_CORRUPTION_DETECTED;
4691 }
Janos Follath7742fee2019-06-17 12:58:10 +01004692
4693 /* We need a new block */
4694 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004695 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004696
4697 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4698 *
4699 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4700 *
4701 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4702 * HMAC_hash(secret, A(2) + seed) +
4703 * HMAC_hash(secret, A(3) + seed) + ...
4704 *
4705 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004706 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004707 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004708 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004709 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004710 * is currently extracted as `output_block` and where i is
4711 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004712 */
4713
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 status = psa_key_derivation_start_hmac(&hmac,
4715 hash_alg,
4716 tls12_prf->secret,
4717 tls12_prf->secret_length);
4718 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004719 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004721
4722 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004724 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4725 * the variable seed and in this instance means it in the context of the
4726 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 status = psa_mac_update(&hmac,
4728 tls12_prf->label,
4729 tls12_prf->label_length);
4730 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004731 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 }
4733 status = psa_mac_update(&hmac,
4734 tls12_prf->seed,
4735 tls12_prf->seed_length);
4736 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004737 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 }
4739 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004740 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4742 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004743 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004745 }
4746
Gilles Peskine449bd832023-01-11 14:50:10 +01004747 status = psa_mac_sign_finish(&hmac,
4748 tls12_prf->Ai, hash_length,
4749 &hmac_output_length);
4750 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02004751 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 }
4753 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004754 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004756
4757 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 status = psa_key_derivation_start_hmac(&hmac,
4759 hash_alg,
4760 tls12_prf->secret,
4761 tls12_prf->secret_length);
4762 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004763 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004764 }
4765 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4766 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004767 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 }
4769 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
4770 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004771 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004772 }
4773 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
4774 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004775 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004776 }
4777 status = psa_mac_sign_finish(&hmac,
4778 tls12_prf->output_block, hash_length,
4779 &hmac_output_length);
4780 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004781 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004782 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004783
Janos Follath7742fee2019-06-17 12:58:10 +01004784
4785cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01004786 cleanup_status = psa_mac_abort(&hmac);
4787 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004788 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004789 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004790
Gilles Peskine449bd832023-01-11 14:50:10 +01004791 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004792}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004793
Janos Follath844eb0e2019-06-19 12:10:49 +01004794static psa_status_t psa_key_derivation_tls12_prf_read(
4795 psa_tls12_prf_key_derivation_t *tls12_prf,
4796 psa_algorithm_t alg,
4797 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004799{
Gilles Peskine449bd832023-01-11 14:50:10 +01004800 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4801 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004802 psa_status_t status;
4803 uint8_t offset, length;
4804
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02004806 case PSA_TLS12_PRF_STATE_LABEL_SET:
4807 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4808 break;
4809 case PSA_TLS12_PRF_STATE_OUTPUT:
4810 break;
4811 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02004813 }
4814
Gilles Peskine449bd832023-01-11 14:50:10 +01004815 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004816 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 if (tls12_prf->left_in_block == 0) {
4818 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4819 alg);
4820 if (status != PSA_SUCCESS) {
4821 return status;
4822 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004823
4824 continue;
4825 }
4826
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004828 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004830 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004832
4833 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004835 output += length;
4836 output_length -= length;
4837 tls12_prf->left_in_block -= length;
4838 }
4839
Gilles Peskine449bd832023-01-11 14:50:10 +01004840 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004841}
John Durkop07cc04a2020-11-16 22:08:34 -08004842#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4843 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004844
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004845#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
4846static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
4847 psa_tls12_ecjpake_to_pms_t *ecjpake,
4848 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004850{
4851 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04004852 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004853
Gilles Peskine449bd832023-01-11 14:50:10 +01004854 if (output_length != 32) {
4855 return PSA_ERROR_INVALID_ARGUMENT;
4856 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004857
Gilles Peskine449bd832023-01-11 14:50:10 +01004858 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
4859 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
4860 &output_size);
4861 if (status != PSA_SUCCESS) {
4862 return status;
4863 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004864
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 if (output_size != output_length) {
4866 return PSA_ERROR_GENERIC_ERROR;
4867 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004868
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004870}
4871#endif
4872
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004873psa_status_t psa_key_derivation_output_bytes(
4874 psa_key_derivation_operation_t *operation,
4875 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004877{
4878 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004879 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004880
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004882 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004884 }
4885
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004887 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004888 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004889 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004890 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004891 goto exit;
4892 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004893 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004894 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004895 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004896 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4897 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004898 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004899 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004900 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004901 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004902 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004903
Przemek Stekiel69c46792022-06-10 12:59:51 +02004904#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
4906 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
4907 output, output_length);
4908 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004909#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08004910#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4911 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4913 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4914 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4915 kdf_alg, output,
4916 output_length);
4917 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004918#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4919 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004920#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004921 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004922 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01004923 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
4924 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004925#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
4926
Gilles Peskineeab56e42018-07-12 17:12:33 +02004927 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004928 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004929 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004930 }
4931
4932exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004933 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004934 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004935 * This allows us to differentiate between exhausted operations and
4936 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4937 * operations. */
4938 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004939 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004940 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004941 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004942 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004944}
4945
David Brown7807bf72021-02-09 16:28:23 -07004946#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01004947static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004948{
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 if (data_size >= 8) {
4950 mbedtls_des_key_set_parity(data);
4951 }
4952 if (data_size >= 16) {
4953 mbedtls_des_key_set_parity(data + 8);
4954 }
4955 if (data_size >= 24) {
4956 mbedtls_des_key_set_parity(data + 16);
4957 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004958}
David Brown7807bf72021-02-09 16:28:23 -07004959#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004960
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004961/*
Gilles Peskine449bd832023-01-11 14:50:10 +01004962 * ECC keys on a Weierstrass elliptic curve require the generation
4963 * of a private key which is an integer
4964 * in the range [1, N - 1], where N is the boundary of the private key domain:
4965 * N is the prime p for Diffie-Hellman, or the order of the
4966 * curve’s base point for ECC.
4967 *
4968 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
4969 * This function generates the private key using the following process:
4970 *
4971 * 1. Draw a byte string of length ceiling(m/8) bytes.
4972 * 2. If m is not a multiple of 8, set the most significant
4973 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
4974 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
4975 * 4. If k > N - 2, discard the result and return to step 1.
4976 * 5. Output k + 1 as the private key.
4977 *
4978 * This method allows compliance to NIST standards, specifically the methods titled
4979 * Key-Pair Generation by Testing Candidates in the following publications:
4980 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
4981 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
4982 * Diffie-Hellman keys.
4983 *
4984 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
4985 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
4986 *
4987 * Note: Function allocates memory for *data buffer, so given *data should be
4988 * always NULL.
4989 */
Przemek Stekiel7fc07512022-03-04 14:41:11 +01004990#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
4991 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
4992 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4993 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
4994 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004995static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004996 psa_key_slot_t *slot,
4997 size_t bits,
4998 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004999 uint8_t **data
5000 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005001{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005002 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005003 mbedtls_mpi k;
5004 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005005 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005006 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005007
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 mbedtls_mpi_init(&k);
5009 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005010
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005011 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005012 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005013 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005014 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005015
Gilles Peskine449bd832023-01-11 14:50:10 +01005016 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005017 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005018 goto cleanup;
5019 }
5020
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005021 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005022 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005023
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005025
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005026 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005027 /* Let m be the bit size of N. */
5028 size_t m = ecp_group.nbits;
5029
Gilles Peskine449bd832023-01-11 14:50:10 +01005030 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005031
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005032 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005034
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005035 /* Note: This function is always called with *data == NULL and it
5036 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 *data = mbedtls_calloc(1, m_bytes);
5038 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005039 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005040 goto cleanup;
5041 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005042
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005044 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005046 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005048
5049 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005050 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005051 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005052 * (8 * ceiling(m/8) - m) bits of the first byte in
5053 * the string to zero.
5054 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005056 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005057 }
5058
5059 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 * big-endian byte string.
5061 */
5062 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005063
5064 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005065 * Result of comparison is returned. When it indicates error
5066 * then this function is called again.
5067 */
5068 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005069 }
5070
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005071 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005072 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5073 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005074cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 if (ret != 0) {
5076 status = mbedtls_to_psa_error(ret);
5077 }
5078 if (status != PSA_SUCCESS) {
5079 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005080 *data = NULL;
5081 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005082 mbedtls_mpi_free(&k);
5083 mbedtls_mpi_free(&diff_N_2);
5084 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005085}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005086
5087/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5088 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5089 *
5090 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5091 * draw a 32-byte string and process it as specified in
5092 * Elliptic Curves for Security [RFC7748] §5.
5093 *
5094 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5095 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5096 *
5097 * Note: Function allocates memory for *data buffer, so given *data should be
5098 * always NULL.
5099 */
5100
5101static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5102 size_t bits,
5103 psa_key_derivation_operation_t *operation,
5104 uint8_t **data
5105 )
5106{
5107 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005108 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005109
Gilles Peskine449bd832023-01-11 14:50:10 +01005110 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005111 case 255:
5112 output_length = 32;
5113 break;
5114 case 448:
5115 output_length = 56;
5116 break;
5117 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005118 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005119 break;
5120 }
5121
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005123
Gilles Peskine449bd832023-01-11 14:50:10 +01005124 if (*data == NULL) {
5125 return PSA_ERROR_INSUFFICIENT_MEMORY;
5126 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005127
Gilles Peskine449bd832023-01-11 14:50:10 +01005128 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005129
Gilles Peskine449bd832023-01-11 14:50:10 +01005130 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005131 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005132 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005133
Gilles Peskine449bd832023-01-11 14:50:10 +01005134 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005135 case 255:
5136 (*data)[0] &= 248;
5137 (*data)[31] &= 127;
5138 (*data)[31] |= 64;
5139 break;
5140 case 448:
5141 (*data)[0] &= 252;
5142 (*data)[55] |= 128;
5143 break;
5144 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005145 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005146 break;
5147 }
5148
5149 return status;
5150}
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005151#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
5152 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
5153 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5154 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5155 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005156
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005157static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005158 psa_key_slot_t *slot,
5159 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005161{
5162 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305164 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005165 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005166
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5168 return PSA_ERROR_INVALID_ARGUMENT;
5169 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005170
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005171#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
5172 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
5173 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5174 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5175 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005176 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5177 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5178 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005179 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5181 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005182 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 }
5184 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005185 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005186 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5187 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005188 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005190 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005191 } else
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005192#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
5193 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
5194 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5195 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5196 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005197 if (key_type_is_raw_bytes(slot->attr.type)) {
5198 if (bits % 8 != 0) {
5199 return PSA_ERROR_INVALID_ARGUMENT;
5200 }
5201 data = mbedtls_calloc(1, bytes);
5202 if (data == NULL) {
5203 return PSA_ERROR_INSUFFICIENT_MEMORY;
5204 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005205
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 status = psa_key_derivation_output_bytes(operation, data, bytes);
5207 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005208 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005209 }
David Brown12ca5032021-02-11 11:02:00 -07005210#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005211 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5212 psa_des_set_key_parity(data, bytes);
5213 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005214#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005215 } else {
5216 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005217 }
Ronald Crondd04d422020-11-28 15:14:42 +01005218
Ronald Cronbf33c932020-11-28 18:06:53 +01005219 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005220 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005222 };
5223
Gilles Peskine449bd832023-01-11 14:50:10 +01005224 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5225 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5226 &storage_size);
5227 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305228 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 }
Archanad8a83dc2021-06-14 10:04:16 +05305230 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 status = psa_allocate_buffer_to_slot(slot, storage_size);
5232 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305233 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 }
Archanad8a83dc2021-06-14 10:04:16 +05305235
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 status = psa_driver_wrapper_import_key(&attributes,
5237 data, bytes,
5238 slot->key.data,
5239 slot->key.bytes,
5240 &slot->key.bytes, &bits);
5241 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005242 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005244
5245exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 mbedtls_free(data);
5247 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005248}
5249
Gilles Peskine449bd832023-01-11 14:50:10 +01005250psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5251 psa_key_derivation_operation_t *operation,
5252 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005253{
5254 psa_status_t status;
5255 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005256 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005257
Ronald Cron81709fc2020-11-14 12:10:32 +01005258 *key = MBEDTLS_SVC_KEY_ID_INIT;
5259
Gilles Peskine0f84d622019-09-12 19:03:13 +02005260 /* Reject any attempt to create a zero-length key so that we don't
5261 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 if (psa_get_key_bits(attributes) == 0) {
5263 return PSA_ERROR_INVALID_ARGUMENT;
5264 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005265
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 if (operation->alg == PSA_ALG_NONE) {
5267 return PSA_ERROR_BAD_STATE;
5268 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005269
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 if (!operation->can_output_key) {
5271 return PSA_ERROR_NOT_PERMITTED;
5272 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005273
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5275 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005276#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005278 /* Deriving a key in a secure element is not implemented yet. */
5279 status = PSA_ERROR_NOT_SUPPORTED;
5280 }
5281#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 if (status == PSA_SUCCESS) {
5283 status = psa_generate_derived_key_internal(slot,
5284 attributes->core.bits,
5285 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005286 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 if (status == PSA_SUCCESS) {
5288 status = psa_finish_key_creation(slot, driver, key);
5289 }
5290 if (status != PSA_SUCCESS) {
5291 psa_fail_key_creation(slot, driver);
5292 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005293
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005295}
5296
Gilles Peskineeab56e42018-07-12 17:12:33 +02005297
5298
5299/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005300/* Key derivation */
5301/****************************************************************/
5302
Steven Cooreman932ffb72021-02-15 12:14:32 +01005303#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005304static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005305{
5306#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005307 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5308 return 1;
5309 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005310#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005311#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005312 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5313 return 1;
5314 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005315#endif
5316#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5318 return 1;
5319 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005320#endif
5321#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5323 return 1;
5324 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005325#endif
5326#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5328 return 1;
5329 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005330#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005331#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005332 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5333 return 1;
5334 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005335#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005337}
5338
Gilles Peskine449bd832023-01-11 14:50:10 +01005339static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005340{
5341 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 psa_status_t status = psa_hash_setup(&operation, alg);
5343 psa_hash_abort(&operation);
5344 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005345}
5346
Gilles Peskine969c5d62019-01-16 15:53:06 +01005347static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005348 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005349 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005350{
Janos Follath5fe19732019-06-20 15:09:30 +01005351 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5352 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005354
Gilles Peskine969c5d62019-01-16 15:53:06 +01005355 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 if (!is_kdf_alg_supported(kdf_alg)) {
5357 return PSA_ERROR_NOT_SUPPORTED;
5358 }
John Durkop07cc04a2020-11-16 22:08:34 -08005359
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005360 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005361 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5363 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5364 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5365 if (hash_size == 0) {
5366 return PSA_ERROR_NOT_SUPPORTED;
5367 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005368
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005369 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5370 * we might fail later, which is somewhat unfriendly and potentially
5371 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 psa_status_t status = psa_hash_try_support(hash_alg);
5373 if (status != PSA_SUCCESS) {
5374 return status;
5375 }
5376 } else {
5377 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005378 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005379
Gilles Peskine449bd832023-01-11 14:50:10 +01005380 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5381 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
5382 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
5383 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005384 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005385#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005386 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
5388 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005389 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005391#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5392 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 operation->capacity = 255 * hash_size;
5394 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005395}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005396
Gilles Peskine449bd832023-01-11 14:50:10 +01005397static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005398{
5399#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 if (alg == PSA_ALG_ECDH) {
5401 return PSA_SUCCESS;
5402 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005403#endif
5404 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005406}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005407
5408static int psa_key_derivation_allows_free_form_secret_input(
5409 psa_algorithm_t kdf_alg)
5410{
5411#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
5412 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5413 return 0;
5414 }
5415#endif
5416 (void) kdf_alg;
5417 return 1;
5418}
John Durkop07cc04a2020-11-16 22:08:34 -08005419#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005420
Gilles Peskine449bd832023-01-11 14:50:10 +01005421psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
5422 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005423{
5424 psa_status_t status;
5425
Gilles Peskine449bd832023-01-11 14:50:10 +01005426 if (operation->alg != 0) {
5427 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005428 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005429
Gilles Peskine449bd832023-01-11 14:50:10 +01005430 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
5431 return PSA_ERROR_INVALID_ARGUMENT;
5432 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
5433#if defined(AT_LEAST_ONE_BUILTIN_KDF)
5434 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
5435 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
5436 status = psa_key_agreement_try_support(ka_alg);
5437 if (status != PSA_SUCCESS) {
5438 return status;
5439 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005440 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
5441 return PSA_ERROR_INVALID_ARGUMENT;
5442 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
5444#else
5445 return PSA_ERROR_NOT_SUPPORTED;
5446#endif /* AT_LEAST_ONE_BUILTIN_KDF */
5447 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
5448#if defined(AT_LEAST_ONE_BUILTIN_KDF)
5449 status = psa_key_derivation_setup_kdf(operation, alg);
5450#else
5451 return PSA_ERROR_NOT_SUPPORTED;
5452#endif /* AT_LEAST_ONE_BUILTIN_KDF */
5453 } else {
5454 return PSA_ERROR_INVALID_ARGUMENT;
5455 }
5456
5457 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005458 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 }
5460 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005461}
5462
Przemek Stekiel69c46792022-06-10 12:59:51 +02005463#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005464static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
5465 psa_algorithm_t kdf_alg,
5466 psa_key_derivation_step_t step,
5467 const uint8_t *data,
5468 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005469{
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005471 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02005473 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005474#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5476 return PSA_ERROR_INVALID_ARGUMENT;
5477 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005478#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 if (hkdf->state != HKDF_STATE_INIT) {
5480 return PSA_ERROR_BAD_STATE;
5481 } else {
5482 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5483 hash_alg,
5484 data, data_length);
5485 if (status != PSA_SUCCESS) {
5486 return status;
5487 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005488 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005490 }
Gilles Peskine03410b52019-05-16 16:05:19 +02005491 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005492#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02005494 /* We shouldn't be in different state as HKDF_EXPAND only allows
5495 * two inputs: SECRET (this case) and INFO which does not modify
5496 * the state. It could happen only if the hkdf
5497 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 if (hkdf->state != HKDF_STATE_INIT) {
5499 return PSA_ERROR_BAD_STATE;
5500 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005501
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02005502 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
5504 return PSA_ERROR_INVALID_ARGUMENT;
5505 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005506
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 memcpy(hkdf->prk, data, data_length);
5508 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005509#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005510 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02005511 /* HKDF: If no salt was provided, use an empty salt.
5512 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02005514#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5516 return PSA_ERROR_BAD_STATE;
5517 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02005518#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5520 hash_alg,
5521 NULL, 0);
5522 if (status != PSA_SUCCESS) {
5523 return status;
5524 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005525 hkdf->state = HKDF_STATE_STARTED;
5526 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 if (hkdf->state != HKDF_STATE_STARTED) {
5528 return PSA_ERROR_BAD_STATE;
5529 }
5530 status = psa_mac_update(&hkdf->hmac,
5531 data, data_length);
5532 if (status != PSA_SUCCESS) {
5533 return status;
5534 }
5535 status = psa_mac_sign_finish(&hkdf->hmac,
5536 hkdf->prk,
5537 sizeof(hkdf->prk),
5538 &data_length);
5539 if (status != PSA_SUCCESS) {
5540 return status;
5541 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005542 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005543
Gilles Peskine2b522db2019-04-12 15:11:49 +02005544 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005545 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005546#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005548 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005550 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005551 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005552#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005553 {
5554 /* Block 0 is empty, and the next block will be
5555 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005556 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005557 }
5558
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02005560 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005561#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5563 return PSA_ERROR_INVALID_ARGUMENT;
5564 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005565#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02005566#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
5568 hkdf->state == HKDF_STATE_INIT) {
5569 return PSA_ERROR_BAD_STATE;
5570 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02005571#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005572 if (hkdf->state == HKDF_STATE_OUTPUT) {
5573 return PSA_ERROR_BAD_STATE;
5574 }
5575 if (hkdf->info_set) {
5576 return PSA_ERROR_BAD_STATE;
5577 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005578 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 if (data_length != 0) {
5580 hkdf->info = mbedtls_calloc(1, data_length);
5581 if (hkdf->info == NULL) {
5582 return PSA_ERROR_INSUFFICIENT_MEMORY;
5583 }
5584 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005585 }
5586 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005588 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005590 }
5591}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005592#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005593
John Durkop07cc04a2020-11-16 22:08:34 -08005594#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5595 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005596static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
5597 const uint8_t *data,
5598 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01005599{
Gilles Peskine449bd832023-01-11 14:50:10 +01005600 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
5601 return PSA_ERROR_BAD_STATE;
5602 }
Janos Follathf08e2652019-06-13 09:05:41 +01005603
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 if (data_length != 0) {
5605 prf->seed = mbedtls_calloc(1, data_length);
5606 if (prf->seed == NULL) {
5607 return PSA_ERROR_INSUFFICIENT_MEMORY;
5608 }
Janos Follathf08e2652019-06-13 09:05:41 +01005609
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01005611 prf->seed_length = data_length;
5612 }
Janos Follathf08e2652019-06-13 09:05:41 +01005613
Gilles Peskine43f958b2020-12-13 14:55:14 +01005614 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005615
Gilles Peskine449bd832023-01-11 14:50:10 +01005616 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01005617}
5618
Gilles Peskine449bd832023-01-11 14:50:10 +01005619static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
5620 const uint8_t *data,
5621 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01005622{
Gilles Peskine449bd832023-01-11 14:50:10 +01005623 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
5624 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
5625 return PSA_ERROR_BAD_STATE;
5626 }
Janos Follath81550542019-06-13 14:26:34 +01005627
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 if (data_length != 0) {
5629 prf->secret = mbedtls_calloc(1, data_length);
5630 if (prf->secret == NULL) {
5631 return PSA_ERROR_INSUFFICIENT_MEMORY;
5632 }
Steven Cooremana6df6042021-04-29 19:32:25 +02005633
Gilles Peskine449bd832023-01-11 14:50:10 +01005634 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005635 prf->secret_length = data_length;
5636 }
Janos Follath81550542019-06-13 14:26:34 +01005637
Gilles Peskine43f958b2020-12-13 14:55:14 +01005638 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005639
Gilles Peskine449bd832023-01-11 14:50:10 +01005640 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01005641}
5642
Gilles Peskine449bd832023-01-11 14:50:10 +01005643static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
5644 const uint8_t *data,
5645 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01005646{
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
5648 return PSA_ERROR_BAD_STATE;
5649 }
Janos Follath63028dd2019-06-13 09:15:47 +01005650
Gilles Peskine449bd832023-01-11 14:50:10 +01005651 if (data_length != 0) {
5652 prf->label = mbedtls_calloc(1, data_length);
5653 if (prf->label == NULL) {
5654 return PSA_ERROR_INSUFFICIENT_MEMORY;
5655 }
Janos Follath63028dd2019-06-13 09:15:47 +01005656
Gilles Peskine449bd832023-01-11 14:50:10 +01005657 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01005658 prf->label_length = data_length;
5659 }
Janos Follath63028dd2019-06-13 09:15:47 +01005660
Gilles Peskine43f958b2020-12-13 14:55:14 +01005661 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005662
Gilles Peskine449bd832023-01-11 14:50:10 +01005663 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01005664}
5665
Gilles Peskine449bd832023-01-11 14:50:10 +01005666static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
5667 psa_key_derivation_step_t step,
5668 const uint8_t *data,
5669 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01005670{
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01005672 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01005673 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01005674 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01005675 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01005676 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01005677 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01005678 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01005680 }
5681}
John Durkop07cc04a2020-11-16 22:08:34 -08005682#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5683 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5684
5685#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5686static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5687 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08005688 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005689 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08005690{
5691 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
5693 4 + data_length + prf->other_secret_length :
5694 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08005695
Gilles Peskine449bd832023-01-11 14:50:10 +01005696 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
5697 return PSA_ERROR_INVALID_ARGUMENT;
5698 }
John Durkop07cc04a2020-11-16 22:08:34 -08005699
Gilles Peskine449bd832023-01-11 14:50:10 +01005700 uint8_t *pms = mbedtls_calloc(1, pms_len);
5701 if (pms == NULL) {
5702 return PSA_ERROR_INSUFFICIENT_MEMORY;
5703 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005704 uint8_t *cur = pms;
5705
5706 /* pure-PSK:
5707 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08005708 *
5709 * The premaster secret is formed as follows: if the PSK is N octets
5710 * long, concatenate a uint16 with the value N, N zero octets, a second
5711 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005712 *
5713 * mixed-PSK:
5714 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
5715 * follows: concatenate a uint16 with the length of the other secret,
5716 * the other secret itself, uint16 with the length of PSK, and the
5717 * PSK itself.
5718 * For details please check:
5719 * - RFC 4279, Section 4 for the definition of RSA-PSK,
5720 * - RFC 4279, Section 3 for the definition of DHE-PSK,
5721 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08005722 */
5723
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
5725 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
5726 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
5727 if (prf->other_secret_length != 0) {
5728 memcpy(cur, prf->other_secret, prf->other_secret_length);
5729 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02005730 cur += prf->other_secret_length;
5731 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 } else {
5733 *cur++ = MBEDTLS_BYTE_1(data_length);
5734 *cur++ = MBEDTLS_BYTE_0(data_length);
5735 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005736 cur += data_length;
5737 }
5738
Gilles Peskine449bd832023-01-11 14:50:10 +01005739 *cur++ = MBEDTLS_BYTE_1(data_length);
5740 *cur++ = MBEDTLS_BYTE_0(data_length);
5741 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08005742 cur += data_length;
5743
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08005745
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 mbedtls_platform_zeroize(pms, pms_len);
5747 mbedtls_free(pms);
5748 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08005749}
Janos Follath6660f0e2019-06-17 08:44:03 +01005750
Przemek Stekiele47201b2022-04-19 13:53:28 +02005751static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
5752 psa_tls12_prf_key_derivation_t *prf,
5753 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02005755{
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
5757 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02005758 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005759
5760 if (data_length != 0) {
5761 prf->other_secret = mbedtls_calloc(1, data_length);
5762 if (prf->other_secret == NULL) {
5763 return PSA_ERROR_INSUFFICIENT_MEMORY;
5764 }
5765
5766 memcpy(prf->other_secret, data, data_length);
5767 prf->other_secret_length = data_length;
5768 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02005769 prf->other_secret_length = 0;
5770 }
5771
5772 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
5773
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02005775}
5776
Janos Follath6660f0e2019-06-17 08:44:03 +01005777static psa_status_t psa_tls12_prf_psk_to_ms_input(
5778 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01005779 psa_key_derivation_step_t step,
5780 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01005782{
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02005784 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 return psa_tls12_prf_psk_to_ms_set_key(prf,
5786 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02005787 break;
5788 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
5790 data,
5791 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02005792 break;
5793 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005794 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02005795 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01005796
Przemek Stekiele47201b2022-04-19 13:53:28 +02005797 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005798}
John Durkop07cc04a2020-11-16 22:08:34 -08005799#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005800
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005801#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5802static psa_status_t psa_tls12_ecjpake_to_pms_input(
5803 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04005804 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005805 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005806 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005807{
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
5809 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
5810 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005811 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005812
5813 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01005814 if (data[0] != 0x04) {
5815 return PSA_ERROR_INVALID_ARGUMENT;
5816 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005817
5818 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005819 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005820
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005822}
5823#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02005824/** Check whether the given key type is acceptable for the given
5825 * input step of a key derivation.
5826 *
5827 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5828 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5829 * Both secret and non-secret inputs can alternatively have the type
5830 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5831 * that the input was passed as a buffer rather than via a key object.
5832 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005833static int psa_key_derivation_check_input_type(
5834 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01005835 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02005836{
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02005838 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01005839 if (key_type == PSA_KEY_TYPE_DERIVE) {
5840 return PSA_SUCCESS;
5841 }
5842 if (key_type == PSA_KEY_TYPE_NONE) {
5843 return PSA_SUCCESS;
5844 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005845 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02005846 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01005847 if (key_type == PSA_KEY_TYPE_DERIVE) {
5848 return PSA_SUCCESS;
5849 }
5850 if (key_type == PSA_KEY_TYPE_NONE) {
5851 return PSA_SUCCESS;
5852 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02005853 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02005854 case PSA_KEY_DERIVATION_INPUT_LABEL:
5855 case PSA_KEY_DERIVATION_INPUT_SALT:
5856 case PSA_KEY_DERIVATION_INPUT_INFO:
5857 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
5859 return PSA_SUCCESS;
5860 }
5861 if (key_type == PSA_KEY_TYPE_NONE) {
5862 return PSA_SUCCESS;
5863 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005864 break;
5865 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02005867}
5868
Janos Follathb80a94e2019-06-12 15:54:46 +01005869static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005870 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005871 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005872 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005873 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005874 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005875{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005876 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005878
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 status = psa_key_derivation_check_input_type(step, key_type);
5880 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02005881 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005883
Przemek Stekiel69c46792022-06-10 12:59:51 +02005884#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5886 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
5887 step, data, data_length);
5888 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005889#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005890#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005891 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5892 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
5893 step, data, data_length);
5894 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005895#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5896#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005897 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5898 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
5899 step, data, data_length);
5900 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005901#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005902#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005903 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005904 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
5906 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005907#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005908 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005909 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005910 (void) data;
5911 (void) data_length;
5912 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005914 }
5915
Gilles Peskine224b0d62019-09-23 18:13:17 +02005916exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005917 if (status != PSA_SUCCESS) {
5918 psa_key_derivation_abort(operation);
5919 }
5920 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005921}
5922
Janos Follath51f4a0f2019-06-14 11:35:55 +01005923psa_status_t psa_key_derivation_input_bytes(
5924 psa_key_derivation_operation_t *operation,
5925 psa_key_derivation_step_t step,
5926 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005927 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005928{
Gilles Peskine449bd832023-01-11 14:50:10 +01005929 return psa_key_derivation_input_internal(operation, step,
5930 PSA_KEY_TYPE_NONE,
5931 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005932}
5933
Janos Follath51f4a0f2019-06-14 11:35:55 +01005934psa_status_t psa_key_derivation_input_key(
5935 psa_key_derivation_operation_t *operation,
5936 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01005937 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005938{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005939 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005940 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005941 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005942
Ronald Cron5c522922020-11-14 16:35:34 +01005943 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01005944 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5945 if (status != PSA_SUCCESS) {
5946 psa_key_derivation_abort(operation);
5947 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02005948 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005949
5950 /* Passing a key object as a SECRET input unlocks the permission
5951 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005953 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005955
Gilles Peskine449bd832023-01-11 14:50:10 +01005956 status = psa_key_derivation_input_internal(operation,
5957 step, slot->attr.type,
5958 slot->key.data,
5959 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005960
Gilles Peskine449bd832023-01-11 14:50:10 +01005961 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005962
Gilles Peskine449bd832023-01-11 14:50:10 +01005963 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005964}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005965
5966
5967
5968/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005969/* Key agreement */
5970/****************************************************************/
5971
Gilles Peskine449bd832023-01-11 14:50:10 +01005972psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
5973 const uint8_t *key_buffer,
5974 size_t key_buffer_size,
5975 psa_algorithm_t alg,
5976 const uint8_t *peer_key,
5977 size_t peer_key_length,
5978 uint8_t *shared_secret,
5979 size_t shared_secret_size,
5980 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005981{
Gilles Peskine449bd832023-01-11 14:50:10 +01005982 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08005983#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005984 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
5986 key_buffer_size, alg,
5987 peer_key, peer_key_length,
5988 shared_secret,
5989 shared_secret_size,
5990 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02005991#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005992 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01005993 (void) attributes;
5994 (void) key_buffer;
5995 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01005996 (void) peer_key;
5997 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005998 (void) shared_secret;
5999 (void) shared_secret_size;
6000 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006002 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006003}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006004
Aditya Deshpande17845b82022-10-13 17:21:01 +01006005/** Internal function for raw key agreement
6006 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006007 * to the driver's implementation if a driver is present.
6008 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006009 * (psa_key_agreement_raw_builtin).
6010 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006011static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6012 psa_key_slot_t *private_key,
6013 const uint8_t *peer_key,
6014 size_t peer_key_length,
6015 uint8_t *shared_secret,
6016 size_t shared_secret_size,
6017 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006018{
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6020 return PSA_ERROR_NOT_SUPPORTED;
6021 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006022
6023 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006025 };
6026
Gilles Peskine449bd832023-01-11 14:50:10 +01006027 return psa_driver_wrapper_key_agreement(&attributes,
6028 private_key->key.data,
6029 private_key->key.bytes, alg,
6030 peer_key, peer_key_length,
6031 shared_secret,
6032 shared_secret_size,
6033 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006034}
6035
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006036/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006037 * to potentially free embedded data structures and wipe confidential data.
6038 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006039static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6040 psa_key_derivation_step_t step,
6041 psa_key_slot_t *private_key,
6042 const uint8_t *peer_key,
6043 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006044{
6045 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006046 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006047 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006049
6050 /* Step 1: run the secret agreement algorithm to generate the shared
6051 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 status = psa_key_agreement_raw_internal(ka_alg,
6053 private_key,
6054 peer_key, peer_key_length,
6055 shared_secret,
6056 sizeof(shared_secret),
6057 &shared_secret_length);
6058 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006059 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006061
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006062 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006063 * the shared secret. A shared secret is permitted wherever a key
6064 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006065 status = psa_key_derivation_input_internal(operation, step,
6066 PSA_KEY_TYPE_DERIVE,
6067 shared_secret,
6068 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006069exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006070 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6071 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006072}
6073
Gilles Peskine449bd832023-01-11 14:50:10 +01006074psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6075 psa_key_derivation_step_t step,
6076 mbedtls_svc_key_id_t private_key,
6077 const uint8_t *peer_key,
6078 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006079{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006080 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006081 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006082 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006083
Gilles Peskine449bd832023-01-11 14:50:10 +01006084 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6085 return PSA_ERROR_INVALID_ARGUMENT;
6086 }
Ronald Cron5c522922020-11-14 16:35:34 +01006087 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6089 if (status != PSA_SUCCESS) {
6090 return status;
6091 }
6092 status = psa_key_agreement_internal(operation, step,
6093 slot,
6094 peer_key, peer_key_length);
6095 if (status != PSA_SUCCESS) {
6096 psa_key_derivation_abort(operation);
6097 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006098 /* If a private key has been added as SECRET, we allow the derived
6099 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006100 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006101 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006103 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006104
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006106
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006108}
6109
Gilles Peskine449bd832023-01-11 14:50:10 +01006110psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6111 mbedtls_svc_key_id_t private_key,
6112 const uint8_t *peer_key,
6113 size_t peer_key_length,
6114 uint8_t *output,
6115 size_t output_size,
6116 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006117{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006118 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006119 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006120 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006121
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006123 status = PSA_ERROR_INVALID_ARGUMENT;
6124 goto exit;
6125 }
Ronald Cron5c522922020-11-14 16:35:34 +01006126 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006127 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6128 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006129 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006131
Gilles Peskine3e561302022-04-14 00:17:15 +02006132 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6133 * for the output size. The PSA specification only guarantees that this
6134 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6135 * but it might be nice to allow smaller buffers if the output fits.
6136 * At the time of writing this comment, with only ECDH implemented,
6137 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6138 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6139 * be exact for it as well. */
6140 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6142 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006143 status = PSA_ERROR_BUFFER_TOO_SMALL;
6144 goto exit;
6145 }
6146
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 status = psa_key_agreement_raw_internal(alg, slot,
6148 peer_key, peer_key_length,
6149 output, output_size,
6150 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006151
6152exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006154 /* If an error happens and is not handled properly, the output
6155 * may be used as a key to protect sensitive data. Arrange for such
6156 * a key to be random, which is likely to result in decryption or
6157 * verification errors. This is better than filling the buffer with
6158 * some constant data such as zeros, which would result in the data
6159 * being protected with a reproducible, easily knowable key.
6160 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006162 *output_length = output_size;
6163 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006164
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006166
Gilles Peskine449bd832023-01-11 14:50:10 +01006167 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006168}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006169
6170
Gilles Peskine30524eb2020-11-13 17:02:26 +01006171
Gilles Peskine86a440b2018-11-12 18:39:40 +01006172/****************************************************************/
6173/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006174/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006175
Gilles Peskine30524eb2020-11-13 17:02:26 +01006176/** Initialize the PSA random generator.
6177 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006178static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006179{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006180#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006182#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6183
Gilles Peskine30524eb2020-11-13 17:02:26 +01006184 /* Set default configuration if
6185 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006187 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 }
6189 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006190 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006192
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006194#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6195 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6196 /* The PSA entropy injection feature depends on using NV seed as an entropy
6197 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 mbedtls_entropy_add_source(&rng->entropy,
6199 mbedtls_nv_seed_poll, NULL,
6200 MBEDTLS_ENTROPY_BLOCK_SIZE,
6201 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006202#endif
6203
Gilles Peskine449bd832023-01-11 14:50:10 +01006204 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006205#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006206}
6207
6208/** Deinitialize the PSA random generator.
6209 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006210static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006211{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006212#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006214#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
6216 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006217#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006218}
6219
6220/** Seed the PSA random generator.
6221 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006222static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006223{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006224#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6225 /* Do nothing: the external RNG seeds itself. */
6226 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006228#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006229 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01006230 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
6231 drbg_seed, sizeof(drbg_seed) - 1);
6232 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006233#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006234}
6235
Gilles Peskine449bd832023-01-11 14:50:10 +01006236psa_status_t psa_generate_random(uint8_t *output,
6237 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006238{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006239 GUARD_MODULE_INITIALIZED;
6240
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006241#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6242
6243 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6245 output, output_size,
6246 &output_length);
6247 if (status != PSA_SUCCESS) {
6248 return status;
6249 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006250 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006251 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006252 if (output_length != output_size) {
6253 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6254 }
6255 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006256
6257#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6258
Gilles Peskine449bd832023-01-11 14:50:10 +01006259 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006260 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006261 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6262 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6263 output_size);
6264 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6265 output, request_size);
6266 if (ret != 0) {
6267 return mbedtls_to_psa_error(ret);
6268 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006269 output_size -= request_size;
6270 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006271 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006273#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006274}
6275
Gilles Peskine8814fc42020-12-14 15:33:44 +01006276/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006277 *
6278 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6279 * `psa_generate_random(...)`. The state parameter is ignored since the
6280 * PSA API doesn't support passing an explicit state.
6281 *
6282 * In the non-external case, psa_generate_random() calls an
6283 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6284 * and semantics as mbedtls_psa_get_random(). As an optimization,
6285 * instead of doing this back-and-forth between the PSA API and the
6286 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6287 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006288 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006289#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6290int mbedtls_psa_get_random(void *p_rng,
6291 unsigned char *output,
6292 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01006293{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006294 /* This function takes a pointer to the RNG state because that's what
6295 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6296 * its own state internally and doesn't let the caller access that state.
6297 * So we just ignore the state parameter, and in practice we'll pass
6298 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006299 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 psa_status_t status = psa_generate_random(output, output_size);
6301 if (status == PSA_SUCCESS) {
6302 return 0;
6303 } else {
6304 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
6305 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01006306}
6307#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6308
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006309#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006310#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006311
Gilles Peskine449bd832023-01-11 14:50:10 +01006312psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
6313 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006314{
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 if (global_data.initialized) {
6316 return PSA_ERROR_NOT_PERMITTED;
6317 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006318
Gilles Peskine449bd832023-01-11 14:50:10 +01006319 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
6320 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
6321 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
6322 return PSA_ERROR_INVALID_ARGUMENT;
6323 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006324
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006326}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006327#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006328
Ronald Cron3772afe2021-02-08 16:10:05 +01006329/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006330 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006331 * \param type The key type
6332 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006333 *
6334 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006335 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006336 * \retval #PSA_ERROR_INVALID_ARGUMENT
6337 * The size in bits of the key is not valid.
6338 * \retval #PSA_ERROR_NOT_SUPPORTED
6339 * The type and/or the size in bits of the key or the combination of
6340 * the two is not supported.
6341 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006342static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006344{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006345 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006346
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 if (key_type_is_raw_bytes(type)) {
6348 status = psa_validate_unstructured_key_bit_size(type, bits);
6349 if (status != PSA_SUCCESS) {
6350 return status;
6351 }
6352 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006353#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6355 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
6356 return PSA_ERROR_NOT_SUPPORTED;
6357 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006358
Ronald Cron01b2aba2020-10-05 09:42:02 +02006359 /* Accept only byte-aligned keys, for the same reasons as
6360 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 if (bits % 8 != 0) {
6362 return PSA_ERROR_NOT_SUPPORTED;
6363 }
6364 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006365#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006366
Ronald Cron5c4d3862020-12-07 11:07:24 +01006367#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01006369 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 return PSA_SUCCESS;
6371 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006372#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006373 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006374 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006375 }
6376
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006378}
6379
Ronald Cron55ed0592020-10-05 10:30:40 +02006380psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006381 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006383{
6384 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006385 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006386
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 if ((attributes->domain_parameters == NULL) &&
6388 (attributes->domain_parameters_size != 0)) {
6389 return PSA_ERROR_INVALID_ARGUMENT;
6390 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02006391
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 if (key_type_is_raw_bytes(type)) {
6393 status = psa_generate_random(key_buffer, key_buffer_size);
6394 if (status != PSA_SUCCESS) {
6395 return status;
6396 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006397
David Brown12ca5032021-02-11 11:02:00 -07006398#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 if (type == PSA_KEY_TYPE_DES) {
6400 psa_des_set_key_parity(key_buffer, key_buffer_size);
6401 }
David Brown8107e312021-02-12 08:08:46 -07006402#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01006404
Jaeden Amero424fa932021-05-14 08:34:32 +01006405#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
6406 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
6408 return mbedtls_psa_rsa_generate_key(attributes,
6409 key_buffer,
6410 key_buffer_size,
6411 key_buffer_length);
6412 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01006413#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
6414 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006415
John Durkop9814fa22020-11-04 12:28:15 -08006416#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6418 return mbedtls_psa_ecp_generate_key(attributes,
6419 key_buffer,
6420 key_buffer_size,
6421 key_buffer_length);
6422 } else
John Durkop9814fa22020-11-04 12:28:15 -08006423#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006424 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 (void) key_buffer_length;
6426 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02006427 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006428
Gilles Peskine449bd832023-01-11 14:50:10 +01006429 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006430}
Darryl Green0c6575a2018-11-07 16:05:30 +00006431
Gilles Peskine449bd832023-01-11 14:50:10 +01006432psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
6433 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006434{
6435 psa_status_t status;
6436 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006437 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006438 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006439
Ronald Cron81709fc2020-11-14 12:10:32 +01006440 *key = MBEDTLS_SVC_KEY_ID_INIT;
6441
Gilles Peskine0f84d622019-09-12 19:03:13 +02006442 /* Reject any attempt to create a zero-length key so that we don't
6443 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 if (psa_get_key_bits(attributes) == 0) {
6445 return PSA_ERROR_INVALID_ARGUMENT;
6446 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006447
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02006448 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
6450 return PSA_ERROR_INVALID_ARGUMENT;
6451 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02006452
Gilles Peskine449bd832023-01-11 14:50:10 +01006453 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
6454 &slot, &driver);
6455 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02006456 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 }
Gilles Peskine11792082019-08-06 18:36:36 +02006458
Ronald Cron977c2472020-10-13 08:32:21 +02006459 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05306460 * storage ( thus not in the case of generating a key in a secure element
6461 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
6462 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 if (slot->key.data == NULL) {
6464 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
6465 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01006466 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006467 attributes->core.type, attributes->core.bits);
6468 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01006469 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006470 }
Ronald Cron3772afe2021-02-08 16:10:05 +01006471
6472 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 attributes->core.type,
6474 attributes->core.bits);
6475 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02006476 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 attributes, &key_buffer_size);
6478 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01006479 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 }
Ronald Cron977c2472020-10-13 08:32:21 +02006481 }
Ronald Cron977c2472020-10-13 08:32:21 +02006482
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
6484 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02006485 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 }
Ronald Cron977c2472020-10-13 08:32:21 +02006487 }
6488
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 status = psa_driver_wrapper_generate_key(attributes,
6490 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02006491
Gilles Peskine449bd832023-01-11 14:50:10 +01006492 if (status != PSA_SUCCESS) {
6493 psa_remove_key_data_from_memory(slot);
6494 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02006495
Gilles Peskine11792082019-08-06 18:36:36 +02006496exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 if (status == PSA_SUCCESS) {
6498 status = psa_finish_key_creation(slot, driver, key);
6499 }
6500 if (status != PSA_SUCCESS) {
6501 psa_fail_key_creation(slot, driver);
6502 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006503
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006505}
6506
Gilles Peskinee59236f2018-01-27 23:32:46 +01006507/****************************************************************/
6508/* Module setup */
6509/****************************************************************/
6510
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006511#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006512psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01006513 void (* entropy_init)(mbedtls_entropy_context *ctx),
6514 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01006515{
Gilles Peskine449bd832023-01-11 14:50:10 +01006516 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
6517 return PSA_ERROR_BAD_STATE;
6518 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006519 global_data.rng.entropy_init = entropy_init;
6520 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01006522}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006523#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006524
Gilles Peskine449bd832023-01-11 14:50:10 +01006525void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006526{
Gilles Peskine449bd832023-01-11 14:50:10 +01006527 psa_wipe_all_key_slots();
6528 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
6529 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01006530 }
6531 /* Wipe all remaining data, including configuration.
6532 * In particular, this sets all state indicator to the value
6533 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01006534 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02006535
6536 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006537 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01006538}
6539
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006540#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6541/** Recover a transaction that was interrupted by a power failure.
6542 *
6543 * This function is called during initialization, before psa_crypto_init()
6544 * returns. If this function returns a failure status, the initialization
6545 * fails.
6546 */
6547static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01006548 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006549{
Gilles Peskine449bd832023-01-11 14:50:10 +01006550 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006551 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6552 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01006553 /* TODO - fall through to the failure case until this
6554 * is implemented.
6555 * https://github.com/ARMmbed/mbed-crypto/issues/218
6556 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006557 default:
6558 /* We found an unsupported transaction in the storage.
6559 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006560 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006561 }
6562}
6563#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6564
Gilles Peskine449bd832023-01-11 14:50:10 +01006565psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006566{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006567 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006568
Gilles Peskinec6b69072018-11-20 21:42:52 +01006569 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006570 if (global_data.initialized != 0) {
6571 return PSA_SUCCESS;
6572 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006573
Gilles Peskine30524eb2020-11-13 17:02:26 +01006574 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006575 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01006576 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006577 status = mbedtls_psa_random_seed(&global_data.rng);
6578 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006579 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006580 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01006581 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006582
Gilles Peskine449bd832023-01-11 14:50:10 +01006583 status = psa_initialize_key_slots();
6584 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01006585 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01006587
Ronald Cron9ba76912021-04-10 16:57:30 +02006588 /* Init drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006589 status = psa_driver_wrapper_init();
6590 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02006591 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006592 }
Gilles Peskined9348f22019-10-01 15:22:29 +02006593
Gilles Peskine4b734222019-07-24 15:56:31 +02006594#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006595 status = psa_crypto_load_transaction();
6596 if (status == PSA_SUCCESS) {
6597 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
6598 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006599 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006600 }
6601 status = psa_crypto_stop_transaction();
6602 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02006603 /* There's no transaction to complete. It's all good. */
6604 status = PSA_SUCCESS;
6605 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006606#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006607
Gilles Peskinec6b69072018-11-20 21:42:52 +01006608 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006609 global_data.initialized = 1;
6610
6611exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006612 if (status != PSA_SUCCESS) {
6613 mbedtls_psa_crypto_free();
6614 }
6615 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006616}
6617
6618#endif /* MBEDTLS_PSA_CRYPTO_C */