blob: a89b5ff5317541bb25eb6665d79cc26f2e5c9ca2 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Ronald Cronafbc7ed2023-01-24 16:02:04 +010022#include "psa_crypto_core_common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010023
24#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030025
John Durkop07cc04a2020-11-16 22:08:34 -080026#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
27#include "check_crypto_config.h"
28#endif
29
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010031#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010032
Ronald Crond6d28882020-12-14 14:56:02 +010033#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010034#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010035#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020036#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010037#include "psa_crypto_ecp.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010038#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010039#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010040#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010041#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020042#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020043#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020044#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010045#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010046/* Include internal declarations that are useful for implementing persistently
47 * stored keys. */
48#include "psa_crypto_storage.h"
49
Gilles Peskineb2b64d32020-12-14 16:43:58 +010050#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010051
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010052#include <stdlib.h>
53#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010054#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010055
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010056#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020057#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000058#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020059#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020061#include "mbedtls/chacha20.h"
62#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/cipher.h"
64#include "mbedtls/ccm.h"
65#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020067#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010068#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010069#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/error.h"
71#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010072#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000074#include "md_wrap.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000076#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000078#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010080#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/sha1.h"
82#include "mbedtls/sha256.h"
83#include "mbedtls/sha512.h"
Paul Elliott588f8ed2022-12-02 18:10:26 +000084#include "hash_info.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010085
Gilles Peskine449bd832023-01-11 14:50:10 +010086#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020087
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020088#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
89 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
90 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020091#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020092#endif
93
Przemek Stekielfde11282023-03-13 16:06:09 +010094/* The only two JPAKE user/peer identifiers supported for the time being. */
95static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
96static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
Przemek Stekiel18cd6c92023-03-06 15:40:35 +010097
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010098/****************************************************************/
99/* Global data, support functions and library management */
100/****************************************************************/
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200103{
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105}
106
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100107/* Values for psa_global_data_t::rng_state */
108#define RNG_NOT_INITIALIZED 0
109#define RNG_INITIALIZED 1
110#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100113 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100114 unsigned rng_state : 2;
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100115 unsigned drivers_initialized : 1;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100116 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100117} psa_global_data_t;
118
119static psa_global_data_t global_data;
120
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100121#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
122mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
123 &global_data.rng.drbg;
124#endif
125
itayzafrir0adf0fc2018-09-06 16:24:41 +0300126#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 if (global_data.initialized == 0) \
128 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300129
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100130int psa_can_do_hash(psa_algorithm_t hash_alg)
131{
132 (void) hash_alg;
133 return global_data.drivers_initialized;
134}
135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100137{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100138 /* Mbed TLS error codes can combine a high-level error code and a
139 * low-level error code. The low-level error usually reflects the
140 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 int low_level_ret = -(-ret & 0x007f);
142 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100143 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100145
146 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
147 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200149 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
150 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
151 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
152 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
153 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200155 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200157 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200159
Jaeden Amero93e21112019-02-20 13:57:28 +0000160#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000161 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000162#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100163 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100165
166 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100168 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100170
Gilles Peskine26869f22019-05-06 15:25:00 +0200171 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200173
174 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200176 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200178
Gilles Peskinea5905292018-02-07 20:59:33 +0100179 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100181 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100183 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100185 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100187 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100189 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100191 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
195 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100196 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
197 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100198 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100200 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
201 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100203 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100205#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100206
207 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100209
Gilles Peskinee59236f2018-01-27 23:32:46 +0100210 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
211 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
212 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100214
215 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200217 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100219 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100221
Gilles Peskine82e57d12020-11-13 21:31:17 +0100222#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100224 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
225 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100226 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100228 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
229 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100231 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100233#endif
234
Gilles Peskinea5905292018-02-07 20:59:33 +0100235 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100237 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100239 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100241 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100243
Gilles Peskinef76aa772018-10-29 19:24:33 +0100244 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100246 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100248 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100250 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100252 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100254 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100256 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100258 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100260
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100261 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100263 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
264 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
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_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100268 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
269 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
274 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100276 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_INVALID_ALG:
279 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
280 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100282 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200284 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100286
Gilles Peskineff2d2002019-05-06 15:26:23 +0200287 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200289 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200291
Gilles Peskinea5905292018-02-07 20:59:33 +0100292 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100298 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100300 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
301 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100303 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100307 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200309
itayzafrir5c753392018-05-08 11:18:38 +0300310 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300311 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300313 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300315 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300317 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
318 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300320 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100322 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100324
Paul Elliott588f8ed2022-12-02 18:10:26 +0000325 case MBEDTLS_ERR_ECP_IN_PROGRESS:
326 return PSA_OPERATION_INCOMPLETE;
327
Janos Follatha13b9052019-11-22 12:48:59 +0000328 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300330
Gilles Peskinee59236f2018-01-27 23:32:46 +0100331 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100333 }
334}
335
Paul Elliottdc42ca82023-02-24 18:11:59 +0000336/**
337 * \brief For output buffers which contain "tags"
338 * (outputs that may be checked for validity like
339 * hashes, MACs and signatures), fill the unused
340 * part of the output buffer (the whole buffer on
341 * error, the trailing part on success) with
342 * something that isn't a valid tag (barring an
343 * attack on the tag and deliberately-crafted
344 * input), in case the caller doesn't check the
345 * return status properly.
346 *
347 * \param output_buffer Pointer to buffer to wipe. May not be NULL
348 * unless \p output_buffer_size is zero.
349 * \param status Status of function called to generate
350 * output_buffer originally
351 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
352 * could be NULL.
353 * \param output_buffer_length Length of data written to output_buffer, must be
354 * less than \p output_buffer_size
355 */
356static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000357 size_t output_buffer_size, size_t output_buffer_length)
358{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000359 size_t offset = 0;
360
361 if (output_buffer_size == 0) {
362 /* If output_buffer_size is 0 then we have nothing to do. We must not
363 call memset because output_buffer may be NULL in this case */
364 return;
365 }
366
367 if (status == PSA_SUCCESS) {
368 offset = output_buffer_length;
369 }
370
371 memset(output_buffer + offset, '!', output_buffer_size - offset);
372}
373
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200374
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200375
376
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100377/****************************************************************/
378/* Key management */
379/****************************************************************/
380
Valerio Settid4a5d462023-04-05 18:19:01 +0200381#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100382mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
383 size_t bits,
384 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200385{
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100387 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100389#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100390 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100392#endif
393#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100394 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100396#endif
397#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100398 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100400#endif
401#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100402 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100404#endif
405#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100406 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100408 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 if (bits_is_sloppy) {
410 return MBEDTLS_ECP_DP_SECP521R1;
411 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100412 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100413#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100414 }
415 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100416
Paul Elliott8ff510a2020-06-02 17:19:28 +0100417 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100419#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100420 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100422#endif
423#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100424 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100426#endif
427#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100428 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100430#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100431 }
432 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100433
Paul Elliott8ff510a2020-06-02 17:19:28 +0100434 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100436#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100437 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100439 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if (bits_is_sloppy) {
441 return MBEDTLS_ECP_DP_CURVE25519;
442 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100443 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100444#endif
445#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100446 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100448#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100449 }
450 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100451
Paul Elliott8ff510a2020-06-02 17:19:28 +0100452 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100454#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100455 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100457#endif
458#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100459 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100461#endif
462#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100463 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100465#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100466 }
467 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200468 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100469
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100470 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200472}
Valerio Settid4a5d462023-04-05 18:19:01 +0200473#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
476 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200477{
478 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200480 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200481 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200482 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200483 case PSA_KEY_TYPE_PASSWORD:
484 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200485 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100486#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200487 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 if (bits != 128 && bits != 192 && bits != 256) {
489 return PSA_ERROR_INVALID_ARGUMENT;
490 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200491 break;
492#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200493#if defined(PSA_WANT_KEY_TYPE_ARIA)
494 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if (bits != 128 && bits != 192 && bits != 256) {
496 return PSA_ERROR_INVALID_ARGUMENT;
497 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200498 break;
499#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100500#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200501 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 if (bits != 128 && bits != 192 && bits != 256) {
503 return PSA_ERROR_INVALID_ARGUMENT;
504 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200505 break;
506#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100507#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200508 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if (bits != 64 && bits != 128 && bits != 192) {
510 return PSA_ERROR_INVALID_ARGUMENT;
511 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200512 break;
513#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100514#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200515 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 if (bits != 256) {
517 return PSA_ERROR_INVALID_ARGUMENT;
518 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200519 break;
520#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200521 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200523 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (bits % 8 != 0) {
525 return PSA_ERROR_INVALID_ARGUMENT;
526 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200529}
530
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100531/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100532 *
Steven Cooremancd640932021-03-08 12:00:27 +0100533 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
534 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100535 *
536 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100537 * \param[in] key_type The key type of the key to be used with the
538 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100539 *
540 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100541 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100542 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100543 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100544 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100545MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100546 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100548{
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 if (PSA_ALG_IS_HMAC(algorithm)) {
550 if (key_type == PSA_KEY_TYPE_HMAC) {
551 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100552 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100553 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
556 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
557 * key. */
558 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
559 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
560 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
561 * the block length (larger than 1) for block ciphers. */
562 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
563 return PSA_SUCCESS;
564 }
565 }
566 }
567
568 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100569}
570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
572 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200573{
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if (slot->key.data != NULL) {
575 return PSA_ERROR_ALREADY_EXISTS;
576 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 slot->key.data = mbedtls_calloc(1, buffer_length);
579 if (slot->key.data == NULL) {
580 return PSA_ERROR_INSUFFICIENT_MEMORY;
581 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200582
Ronald Cronea0f8a62020-11-25 17:52:23 +0100583 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200585}
586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
588 const uint8_t *data,
589 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200590{
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 psa_status_t status = psa_allocate_buffer_to_slot(slot,
592 data_length);
593 if (status != PSA_SUCCESS) {
594 return status;
595 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 memcpy(slot->key.data, data, data_length);
598 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200599}
600
Ronald Crona0fe59f2020-11-29 11:14:53 +0100601psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100602 const psa_key_attributes_t *attributes,
603 const uint8_t *data, size_t data_length,
604 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100606{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100607 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
608 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100609
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200610 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 if (data_length == 0) {
612 return PSA_ERROR_NOT_SUPPORTED;
613 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 if (key_type_is_raw_bytes(type)) {
616 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
619 *bits);
620 if (status != PSA_SUCCESS) {
621 return status;
622 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200623
Ronald Crondd04d422020-11-28 15:14:42 +0100624 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100626 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 return PSA_SUCCESS;
630 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800631#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
633 if (PSA_KEY_TYPE_IS_ECC(type)) {
634 return mbedtls_psa_ecp_import_key(attributes,
635 data, data_length,
636 key_buffer, key_buffer_size,
637 key_buffer_length,
638 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100639 }
John Durkop9814fa22020-11-04 12:28:15 -0800640#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
641 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700642#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
644 if (PSA_KEY_TYPE_IS_RSA(type)) {
645 return mbedtls_psa_rsa_import_key(attributes,
646 data, data_length,
647 key_buffer, key_buffer_size,
648 key_buffer_length,
649 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200650 }
John Durkop9814fa22020-11-04 12:28:15 -0800651#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
652 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100653 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100656}
657
Gilles Peskinef603c712019-01-19 13:40:11 +0100658/** Calculate the intersection of two algorithm usage policies.
659 *
660 * Return 0 (which allows no operation) on incompatibility.
661 */
662static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100663 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100664 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100666{
Gilles Peskine549ea862019-05-22 11:45:59 +0200667 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if (alg1 == alg2) {
669 return alg1;
670 }
Steven Cooreman03488022021-02-18 12:07:20 +0100671 /* If the policies are from the same hash-and-sign family, check
672 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
674 PSA_ALG_IS_SIGN_HASH(alg2) &&
675 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
676 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
677 return alg2;
678 }
679 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
680 return alg1;
681 }
Steven Cooreman03488022021-02-18 12:07:20 +0100682 }
683 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100684 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100685 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
687 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
688 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
689 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
690 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100691 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100692
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100693 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
695 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
696 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
697 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100698 }
699 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
701 (alg1_len <= alg2_len)) {
702 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100703 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
705 (alg2_len <= alg1_len)) {
706 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100707 }
708 }
709 /* If the policies are from the same MAC family, check whether one
710 * of them is a minimum-MAC-length policy. Calculate the most
711 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
713 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
714 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100715 /* Validate the combination of key type and algorithm. Since the base
716 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
718 return 0;
719 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100720
Steven Cooreman31a876d2021-03-03 20:47:40 +0100721 /* Get the (exact or at-least) output lengths for both sides of the
722 * requested intersection. None of the currently supported algorithms
723 * have an output length dependent on the actual key size, so setting it
724 * to a bogus value of 0 is currently OK.
725 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100726 * Note that for at-least-this-length wildcard algorithms, the output
727 * length is set to the shortest allowed length, which allows us to
728 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
730 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100731 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100732
Steven Cooremana1d83222021-02-25 10:20:29 +0100733 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
735 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
736 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100737 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100738
739 /* If only one is an at-least-this-length policy, the intersection would
740 * be the other (fixed-length) policy as long as said fixed length is
741 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
743 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100744 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
746 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100747 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100748
Steven Cooremancd640932021-03-08 12:00:27 +0100749 /* If none of them are wildcards, check whether they define the same tag
750 * length. This is still possible here when one is default-length and
751 * the other specific-length. Ensure to always return the
752 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 if (alg1_len == alg2_len) {
754 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
755 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100756 }
757 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100759}
760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761static int psa_key_algorithm_permits(psa_key_type_t key_type,
762 psa_algorithm_t policy_alg,
763 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200764{
Gilles Peskine549ea862019-05-22 11:45:59 +0200765 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 if (requested_alg == policy_alg) {
767 return 1;
768 }
Steven Cooreman03488022021-02-18 12:07:20 +0100769 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
770 * and requested_alg is the same hash-and-sign family with any hash,
771 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
773 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
774 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
775 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100776 }
777 /* If policy_alg is a wildcard AEAD algorithm of the same base as
778 * the requested algorithm, check the requested tag length to be
779 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if (PSA_ALG_IS_AEAD(policy_alg) &&
781 PSA_ALG_IS_AEAD(requested_alg) &&
782 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
783 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
784 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
785 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
786 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100787 }
Steven Cooremancd640932021-03-08 12:00:27 +0100788 /* If policy_alg is a MAC algorithm of the same base as the requested
789 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if (PSA_ALG_IS_MAC(policy_alg) &&
791 PSA_ALG_IS_MAC(requested_alg) &&
792 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
793 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100794 /* Validate the combination of key type and algorithm. Since the policy
795 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
797 return 0;
798 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100799
Steven Cooreman31a876d2021-03-03 20:47:40 +0100800 /* Get both the requested output length for the algorithm which is to be
801 * verified, and the default output length for the base algorithm.
802 * Note that none of the currently supported algorithms have an output
803 * length dependent on actual key size, so setting it to a bogus value
804 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100805 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100807 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 key_type, 0,
809 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100810
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100811 /* If the policy is default-length, only allow an algorithm with
812 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
814 return requested_output_length == default_output_length;
815 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100816
817 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100818 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
820 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
821 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100822 }
823
Steven Cooremancd640932021-03-08 12:00:27 +0100824 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
825 * check for the requested MAC length to be equal to or longer than the
826 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
828 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
829 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100830 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200831 }
Steven Cooremance48e852020-10-05 16:02:45 +0200832 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200833 * a key derivation with that key agreement should also be allowed. This
834 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
836 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
837 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
838 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200839 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100840 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200842}
843
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100844/** Test whether a policy permits an algorithm.
845 *
846 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100847 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100848 * \note This function requires providing the key type for which the policy is
849 * being validated, since some algorithm policy definitions (e.g. MAC)
850 * have different properties depending on what kind of cipher it is
851 * combined with.
852 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100853 * \retval PSA_SUCCESS When \p alg is a specific algorithm
854 * allowed by the \p policy.
855 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
856 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
857 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100858 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100859static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
860 psa_key_type_t key_type,
861 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100862{
Steven Cooremand788fab2021-02-25 11:29:17 +0100863 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 if (alg == 0) {
865 return PSA_ERROR_INVALID_ARGUMENT;
866 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100867
Steven Cooreman7e39f052021-02-23 14:18:32 +0100868 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 if (PSA_ALG_IS_WILDCARD(alg)) {
870 return PSA_ERROR_INVALID_ARGUMENT;
871 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
874 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
875 return PSA_SUCCESS;
876 } else {
877 return PSA_ERROR_NOT_PERMITTED;
878 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100879}
880
Gilles Peskinef603c712019-01-19 13:40:11 +0100881/** Restrict a key policy based on a constraint.
882 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100883 * \note This function requires providing the key type for which the policy is
884 * being restricted, since some algorithm policy definitions (e.g. MAC)
885 * have different properties depending on what kind of cipher it is
886 * combined with.
887 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100888 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100889 * \param[in,out] policy The policy to restrict.
890 * \param[in] constraint The policy constraint to apply.
891 *
892 * \retval #PSA_SUCCESS
893 * \c *policy contains the intersection of the original value of
894 * \c *policy and \c *constraint.
895 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100896 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100897 * \c *policy is unchanged.
898 */
899static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100900 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100901 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100903{
904 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 psa_key_policy_algorithm_intersection(key_type, policy->alg,
906 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200907 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
909 constraint->alg2);
910 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
911 return PSA_ERROR_INVALID_ARGUMENT;
912 }
913 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
914 return PSA_ERROR_INVALID_ARGUMENT;
915 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100916 policy->usage &= constraint->usage;
917 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200918 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100920}
921
Przemek Stekiel061f6942022-11-30 10:51:35 +0100922/** Get the description of a key given its identifier and policy constraints
923 * and lock it.
924 *
925 * The key must have allow all the usage flags set in \p usage. If \p alg is
926 * nonzero, the key must allow operations with this algorithm. If \p alg is
927 * zero, the algorithm is not checked.
928 *
929 * In case of a persistent key, the function loads the description of the key
930 * into a key slot if not already done.
931 *
932 * On success, the returned key slot is locked. It is the responsibility of
933 * the caller to unlock the key slot when it does not access it anymore.
934 */
935static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100936 mbedtls_svc_key_id_t key,
937 psa_key_slot_t **p_slot,
938 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100940{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200941 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100942 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 status = psa_get_and_lock_key_slot(key, p_slot);
945 if (status != PSA_SUCCESS) {
946 return status;
947 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200948 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100949
950 /* Enforce that usage policy for the key slot contains all the flags
951 * required by the usage parameter. There is one exception: public
952 * keys can always be exported, so we treat public key objects as
953 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100955 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200957
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100959 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200960 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100961 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100962
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800963 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 if (alg != 0) {
965 status = psa_key_policy_permits(&slot->attr.policy,
966 slot->attr.type,
967 alg);
968 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100969 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100971 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200974
975error:
976 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100980}
Darryl Green940d72c2018-07-13 13:18:51 +0100981
Ronald Cron5c522922020-11-14 16:35:34 +0100982/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200983 *
984 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200985 * available, as opposed to a key in a secure element and/or to be used
986 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200987 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200988 * This is a temporary function that may be used instead of
989 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
990 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200991 *
Ronald Cron5c522922020-11-14 16:35:34 +0100992 * On success, the returned key slot is locked. It is the responsibility of the
993 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200994 */
Ronald Cron5c522922020-11-14 16:35:34 +0100995static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
996 mbedtls_svc_key_id_t key,
997 psa_key_slot_t **p_slot,
998 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001000{
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1002 usage, alg);
1003 if (status != PSA_SUCCESS) {
1004 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001005 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1008 psa_unlock_key_slot(*p_slot);
1009 *p_slot = NULL;
1010 return PSA_ERROR_NOT_SUPPORTED;
1011 }
1012
1013 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001014}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001015
Gilles Peskine449bd832023-01-11 14:50:10 +01001016psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001017{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001018 /* Data pointer will always be either a valid pointer or NULL in an
1019 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 if (slot->key.data != NULL) {
1021 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1022 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001023
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001025 slot->key.data = NULL;
1026 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001029}
1030
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001031/** Completely wipe a slot in memory, including its policy.
1032 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001033psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001034{
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001036
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 /*
1038 * As the return error code may not be handled in case of multiple errors,
1039 * do our best to report an unexpected lock counter. Assert with
1040 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1041 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1042 * function is called as part of the execution of a test suite, the
1043 * execution of the test suite is stopped in error if the assertion fails.
1044 */
1045 if (slot->lock_count != 1) {
1046 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001047 status = PSA_ERROR_CORRUPTION_DETECTED;
1048 }
1049
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001050 /* Multipart operations may still be using the key. This is safe
1051 * because all multipart operation objects are independent from
1052 * the key slot: if they need to access the key after the setup
1053 * phase, they have a copy of the key. Note that this means that
1054 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001055 /* At this point, key material and other type-specific content has
1056 * been wiped. Clear remaining metadata. We can call memset and not
1057 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 memset(slot, 0, sizeof(*slot));
1059 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001060}
1061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001063{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001064 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001065 psa_status_t status; /* status of the last operation */
1066 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001067#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1068 psa_se_drv_table_entry_t *driver;
1069#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 if (mbedtls_svc_key_id_is_null(key)) {
1072 return PSA_SUCCESS;
1073 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001074
Ronald Cronf2911112020-10-29 17:51:10 +01001075 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001076 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001077 * key, this will load the key description from persistent memory if not
1078 * done yet. We cannot avoid this loading as without it we don't know if
1079 * the key is operated by an SE or not and this information is needed by
1080 * the current implementation.
1081 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 status = psa_get_and_lock_key_slot(key, &slot);
1083 if (status != PSA_SUCCESS) {
1084 return status;
1085 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001086
Ronald Cronf2911112020-10-29 17:51:10 +01001087 /*
1088 * If the key slot containing the key description is under access by the
1089 * library (apart from the present access), the key cannot be destroyed
1090 * yet. For the time being, just return in error. Eventually (to be
1091 * implemented), the key should be destroyed when all accesses have
1092 * stopped.
1093 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 if (slot->lock_count > 1) {
1095 psa_unlock_key_slot(slot);
1096 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001097 }
1098
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001100 /* Refuse the destruction of a read-only key (which may or may not work
1101 * if we attempt it, depending on whether the key is merely read-only
1102 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001103 * Just do the best we can, which is to wipe the copy in memory
1104 * (done in this function's cleanup code). */
1105 overall_status = PSA_ERROR_NOT_PERMITTED;
1106 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001107 }
1108
Gilles Peskine354f7672019-07-12 23:46:38 +02001109#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1111 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001112 /* For a key in a secure element, we need to do three things:
1113 * remove the key file in internal storage, destroy the
1114 * key inside the secure element, and update the driver's
1115 * persistent data. Start a transaction that will encompass these
1116 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001118 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001120 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 status = psa_crypto_save_transaction();
1122 if (status != PSA_SUCCESS) {
1123 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001124 /* We should still try to destroy the key in the secure
1125 * element and the key metadata in storage. This is especially
1126 * important if the error is that the storage is full.
1127 * But how to do it exactly without risking an inconsistent
1128 * state after a reset?
1129 * https://github.com/ARMmbed/mbed-crypto/issues/215
1130 */
1131 overall_status = status;
1132 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001133 }
1134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 status = psa_destroy_se_key(driver,
1136 psa_key_slot_get_slot_number(slot));
1137 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001138 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001140 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001141#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1142
Darryl Greend49a4992018-06-18 17:27:26 +01001143#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1145 status = psa_destroy_persistent_key(slot->attr.id);
1146 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001147 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001149
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001150 /* TODO: other slots may have a copy of the same key. We should
1151 * invalidate them.
1152 * https://github.com/ARMmbed/mbed-crypto/issues/214
1153 */
Darryl Greend49a4992018-06-18 17:27:26 +01001154 }
1155#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001156
Gilles Peskinefc762652019-07-22 19:30:34 +02001157#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 if (driver != NULL) {
1159 status = psa_save_se_persistent_data(driver);
1160 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001161 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 }
1163 status = psa_crypto_stop_transaction();
1164 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001165 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001167 }
1168#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1169
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001170exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001172 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001174 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 }
1176 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001177}
1178
John Durkop07cc04a2020-11-16 22:08:34 -08001179#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001180 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001181static psa_status_t psa_get_rsa_public_exponent(
1182 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001184{
1185 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001186 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001187 uint8_t *buffer = NULL;
1188 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001190
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1192 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001193 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 }
1195 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001196 /* It's the default value, which is reported as an empty string,
1197 * so there's nothing to do. */
1198 goto exit;
1199 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001200
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 buflen = mbedtls_mpi_size(&mpi);
1202 buffer = mbedtls_calloc(1, buflen);
1203 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001204 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1205 goto exit;
1206 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1208 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001209 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001211 attributes->domain_parameters = buffer;
1212 attributes->domain_parameters_size = buflen;
1213
1214exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 mbedtls_mpi_free(&mpi);
1216 if (ret != 0) {
1217 mbedtls_free(buffer);
1218 }
1219 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001220}
John Durkop07cc04a2020-11-16 22:08:34 -08001221#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001222 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001223
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001224/** Retrieve all the publicly-accessible attributes of a key.
1225 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001226psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1227 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001228{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001229 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001230 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001231 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001232
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1236 if (status != PSA_SUCCESS) {
1237 return status;
1238 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001239
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001240 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1242 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001243
1244#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1246 psa_set_key_slot_number(attributes,
1247 psa_key_slot_get_slot_number(slot));
1248 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001249#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001252#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001253 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001254 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001255 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001256 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001257 * is not yet implemented.
1258 * https://github.com/ARMmbed/mbed-crypto/issues/216
1259 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001261 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001262
Ronald Cron90857082020-11-25 14:58:33 +01001263 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 slot->attr.type,
1265 slot->key.data,
1266 slot->key.bytes,
1267 &rsa);
1268 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001269 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 status = psa_get_rsa_public_exponent(rsa,
1273 attributes);
1274 mbedtls_rsa_free(rsa);
1275 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001276 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001277 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001278#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001279 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001280 default:
1281 /* Nothing else to do. */
1282 break;
1283 }
1284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 if (status != PSA_SUCCESS) {
1286 psa_reset_key_attributes(attributes);
1287 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001288
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001292}
1293
Gilles Peskinec8000c02019-08-02 20:15:51 +02001294#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1295psa_status_t psa_get_key_slot_number(
1296 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001298{
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001300 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 return PSA_SUCCESS;
1302 } else {
1303 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001304 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001305}
1306#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1307
Gilles Peskine449bd832023-01-11 14:50:10 +01001308static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1309 size_t key_buffer_size,
1310 uint8_t *data,
1311 size_t data_size,
1312 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001313{
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 if (key_buffer_size > data_size) {
1315 return PSA_ERROR_BUFFER_TOO_SMALL;
1316 }
1317 memcpy(data, key_buffer, key_buffer_size);
1318 memset(data + key_buffer_size, 0,
1319 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001320 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001322}
1323
Ronald Cron7285cda2020-11-26 14:46:37 +01001324psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001325 const psa_key_attributes_t *attributes,
1326 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001328{
Ronald Crond18b5f82020-11-25 16:46:05 +01001329 psa_key_type_t type = attributes->core.type;
1330
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 if (key_type_is_raw_bytes(type) ||
1332 PSA_KEY_TYPE_IS_RSA(type) ||
1333 PSA_KEY_TYPE_IS_ECC(type)) {
1334 return psa_export_key_buffer_internal(
1335 key_buffer, key_buffer_size,
1336 data, data_size, data_length);
1337 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001338 /* This shouldn't happen in the reference implementation, but
1339 it is valid for a special-purpose implementation to omit
1340 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001342 }
1343}
1344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1346 uint8_t *data,
1347 size_t data_size,
1348 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001349{
1350 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1351 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1352 psa_key_slot_t *slot;
1353
Ronald Crone907e552021-01-18 13:22:38 +01001354 /* Reject a zero-length output buffer now, since this can never be a
1355 * valid key representation. This way we know that data must be a valid
1356 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 if (data_size == 0) {
1358 return PSA_ERROR_BUFFER_TOO_SMALL;
1359 }
Ronald Crone907e552021-01-18 13:22:38 +01001360
Ronald Cron9486f9d2020-11-26 13:36:23 +01001361 /* Set the key to empty now, so that even when there are errors, we always
1362 * set data_length to a value between 0 and data_size. On error, setting
1363 * the key to empty is a good choice because an empty key representation is
1364 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001365 *data_length = 0;
1366
Ronald Cron9486f9d2020-11-26 13:36:23 +01001367 /* Export requires the EXPORT flag. There is an exception for public keys,
1368 * which don't require any flag, but
1369 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1370 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1372 PSA_KEY_USAGE_EXPORT, 0);
1373 if (status != PSA_SUCCESS) {
1374 return status;
1375 }
mohammad160306e79202018-03-28 13:17:44 +03001376
Ronald Crond18b5f82020-11-25 16:46:05 +01001377 psa_key_attributes_t attributes = {
1378 .core = slot->attr
1379 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 status = psa_driver_wrapper_export_key(&attributes,
1381 slot->key.data, slot->key.bytes,
1382 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001385
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001387}
1388
Ronald Cron7285cda2020-11-26 14:46:37 +01001389psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001390 const psa_key_attributes_t *attributes,
1391 const uint8_t *key_buffer,
1392 size_t key_buffer_size,
1393 uint8_t *data,
1394 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001396{
Ronald Crond18b5f82020-11-25 16:46:05 +01001397 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001398
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1400 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001401 /* Exporting public -> public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 return psa_export_key_buffer_internal(
1403 key_buffer, key_buffer_size,
1404 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001405 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001408#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1410 return mbedtls_psa_rsa_export_public_key(attributes,
1411 key_buffer,
1412 key_buffer_size,
1413 data,
1414 data_size,
1415 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001416#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001417 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001419#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1420 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001422#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1424 return mbedtls_psa_ecp_export_public_key(attributes,
1425 key_buffer,
1426 key_buffer_size,
1427 data,
1428 data_size,
1429 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001430#else
1431 /* We don't know how to convert a private ECC key to public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001433#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1434 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001435 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001437 /* This shouldn't happen in the reference implementation, but
1438 it is valid for a special-purpose implementation to omit
1439 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001441 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001442}
Ronald Crond18b5f82020-11-25 16:46:05 +01001443
Gilles Peskine449bd832023-01-11 14:50:10 +01001444psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1445 uint8_t *data,
1446 size_t data_size,
1447 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001448{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001449 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001450 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001451 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001452
Ronald Crone907e552021-01-18 13:22:38 +01001453 /* Reject a zero-length output buffer now, since this can never be a
1454 * valid key representation. This way we know that data must be a valid
1455 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 if (data_size == 0) {
1457 return PSA_ERROR_BUFFER_TOO_SMALL;
1458 }
Ronald Crone907e552021-01-18 13:22:38 +01001459
Darryl Greendd8fb772018-11-07 16:00:44 +00001460 /* Set the key to empty now, so that even when there are errors, we always
1461 * set data_length to a value between 0 and data_size. On error, setting
1462 * the key to empty is a good choice because an empty key representation is
1463 * unlikely to be accepted anywhere. */
1464 *data_length = 0;
1465
1466 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1468 if (status != PSA_SUCCESS) {
1469 return status;
1470 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1473 status = PSA_ERROR_INVALID_ARGUMENT;
1474 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001475 }
1476
Ronald Crond18b5f82020-11-25 16:46:05 +01001477 psa_key_attributes_t attributes = {
1478 .core = slot->attr
1479 };
Ronald Cron67227982020-11-26 15:16:05 +01001480 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001481 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001483
1484exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001488}
1489
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001490MBEDTLS_STATIC_ASSERT(
1491 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1492 "One or more key attribute flag is listed as both external-only and dual-use")
1493MBEDTLS_STATIC_ASSERT(
1494 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1495 "One or more key attribute flag is listed as both internal-only and dual-use")
1496MBEDTLS_STATIC_ASSERT(
1497 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1498 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001499
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001500/** Validate that a key policy is internally well-formed.
1501 *
1502 * This function only rejects invalid policies. It does not validate the
1503 * consistency of the policy with respect to other attributes of the key
1504 * such as the key type.
1505 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001506static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001507{
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1509 PSA_KEY_USAGE_COPY |
1510 PSA_KEY_USAGE_ENCRYPT |
1511 PSA_KEY_USAGE_DECRYPT |
1512 PSA_KEY_USAGE_SIGN_MESSAGE |
1513 PSA_KEY_USAGE_VERIFY_MESSAGE |
1514 PSA_KEY_USAGE_SIGN_HASH |
1515 PSA_KEY_USAGE_VERIFY_HASH |
1516 PSA_KEY_USAGE_VERIFY_DERIVATION |
1517 PSA_KEY_USAGE_DERIVE)) != 0) {
1518 return PSA_ERROR_INVALID_ARGUMENT;
1519 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001522}
1523
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001524/** Validate the internal consistency of key attributes.
1525 *
1526 * This function only rejects invalid attribute values. If does not
1527 * validate the consistency of the attributes with any key data that may
1528 * be involved in the creation of the key.
1529 *
1530 * Call this function early in the key creation process.
1531 *
1532 * \param[in] attributes Key attributes for the new key.
1533 * \param[out] p_drv On any return, the driver for the key, if any.
1534 * NULL for a transparent key.
1535 *
1536 */
1537static psa_status_t psa_validate_key_attributes(
1538 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001540{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001541 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1543 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 status = psa_validate_key_location(lifetime, p_drv);
1546 if (status != PSA_SUCCESS) {
1547 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001548 }
1549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 status = psa_validate_key_persistence(lifetime);
1551 if (status != PSA_SUCCESS) {
1552 return status;
1553 }
1554
1555 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1556 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1557 return PSA_ERROR_INVALID_ARGUMENT;
1558 }
1559 } else {
1560 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1561 return PSA_ERROR_INVALID_ARGUMENT;
1562 }
1563 }
1564
1565 status = psa_validate_key_policy(&attributes->core.policy);
1566 if (status != PSA_SUCCESS) {
1567 return status;
1568 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001569
1570 /* Refuse to create overly large keys.
1571 * Note that this doesn't trigger on import if the attributes don't
1572 * explicitly specify a size (so psa_get_key_bits returns 0), so
1573 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1575 return PSA_ERROR_NOT_SUPPORTED;
1576 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001577
Gilles Peskine91e8c332019-08-02 19:19:39 +02001578 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1580 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1581 return PSA_ERROR_INVALID_ARGUMENT;
1582 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001583
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001585}
1586
Gilles Peskine4747d192019-04-17 15:05:45 +02001587/** Prepare a key slot to receive key material.
1588 *
1589 * This function allocates a key slot and sets its metadata.
1590 *
1591 * If this function fails, call psa_fail_key_creation().
1592 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001593 * This function is intended to be used as follows:
1594 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001595 * it with the specified attributes, and in case of a volatile key assign it
1596 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001597 * -# Populate the slot with the key material.
1598 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1599 * In case of failure at any step, stop the sequence and call
1600 * psa_fail_key_creation().
1601 *
Ronald Cron5c522922020-11-14 16:35:34 +01001602 * On success, the key slot is locked. It is the responsibility of the caller
1603 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001604 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001605 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001606 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001607 * \param[out] p_slot On success, a pointer to the prepared slot.
1608 * \param[out] p_drv On any return, the driver for the key, if any.
1609 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001610 *
1611 * \retval #PSA_SUCCESS
1612 * The key slot is ready to receive key material.
1613 * \return If this function fails, the key slot is an invalid state.
1614 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001615 */
1616static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001617 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001618 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001619 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001621{
1622 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001623 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001624 psa_key_slot_t *slot;
1625
Gilles Peskinedf179142019-07-15 22:02:14 +02001626 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001627 *p_drv = NULL;
1628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 status = psa_validate_key_attributes(attributes, p_drv);
1630 if (status != PSA_SUCCESS) {
1631 return status;
1632 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1635 if (status != PSA_SUCCESS) {
1636 return status;
1637 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001638 slot = *p_slot;
1639
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001640 /* We're storing the declared bit-size of the key. It's up to each
1641 * creation mechanism to verify that this information is correct.
1642 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001643 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001644 * is optional (import, copy). In case of a volatile key, assign it the
1645 * volatile key identifier associated to the slot returned to contain its
1646 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001647
1648 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001650#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1651 slot->attr.id = volatile_key_id;
1652#else
1653 slot->attr.id.key_id = volatile_key_id;
1654#endif
1655 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001656
Gilles Peskine91e8c332019-08-02 19:19:39 +02001657 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001658 * external-only flags, query `attributes`. Thanks to the check
1659 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001660 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001661 * may have set. */
1662 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001663
Gilles Peskinecbaff462019-07-12 23:46:04 +02001664#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001665 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001666 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001667 * create the key file in internal storage, create the
1668 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001669 * persistent data. This is done by starting a transaction that will
1670 * encompass these three actions.
1671 * For registering a volatile key, we just need to find an appropriate
1672 * slot number inside the SE. Since the key is designated volatile, creating
1673 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001674 /* The first thing to do is to find a slot number for the new key.
1675 * We save the slot number in persistent storage as part of the
1676 * transaction data. It will be needed to recover if the power
1677 * fails during the key creation process, to clean up on the secure
1678 * element side after restarting. Obtaining a slot number from the
1679 * secure element driver updates its persistent state, but we do not yet
1680 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001681 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001683 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1685 &slot_number);
1686 if (status != PSA_SUCCESS) {
1687 return status;
1688 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1691 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001692 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001693 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001694 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 status = psa_crypto_save_transaction();
1696 if (status != PSA_SUCCESS) {
1697 (void) psa_crypto_stop_transaction();
1698 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001699 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001700 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001701
1702 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001704 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001705
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001707 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001709 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001710#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1711
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001713}
Gilles Peskine4747d192019-04-17 15:05:45 +02001714
1715/** Finalize the creation of a key once its key material has been set.
1716 *
1717 * This entails writing the key to persistent storage.
1718 *
1719 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001720 * See the documentation of psa_start_key_creation() for the intended use
1721 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001722 *
Ronald Cron5c522922020-11-14 16:35:34 +01001723 * If the finalization succeeds, the function unlocks the key slot (it was
1724 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1725 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001726 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001727 * \param[in,out] slot Pointer to the slot with key material.
1728 * \param[in] driver The secure element driver for the key,
1729 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001730 * \param[out] key On success, identifier of the key. Note that the
1731 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001732 *
1733 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001734 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001735 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1736 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1737 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1738 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1739 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1740 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001741 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001742 * \return If this function fails, the key slot is an invalid state.
1743 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001744 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001745static psa_status_t psa_finish_key_creation(
1746 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001747 psa_se_drv_table_entry_t *driver,
1748 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001749{
1750 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001751 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001752 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001753
1754#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001756#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001758 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001759 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001761
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001762 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1763 sizeof(data.slot_number),
1764 "Slot number size does not match psa_se_key_data_storage_t");
1765
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1767 status = psa_save_persistent_key(&slot->attr,
1768 (uint8_t *) &data,
1769 sizeof(data));
1770 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001771#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1772 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001773 /* Key material is saved in export representation in the slot, so
1774 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 status = psa_save_persistent_key(&slot->attr,
1776 slot->key.data,
1777 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001778 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001779 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001780#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1781
Gilles Peskinecbaff462019-07-12 23:46:04 +02001782#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001783 /* Finish the transaction for a key creation. This does not
1784 * happen when registering an existing key. Detect this case
1785 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001786 * creation of a persistent key in a secure element requires a transaction,
1787 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 if (driver != NULL &&
1789 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1790 status = psa_save_se_persistent_data(driver);
1791 if (status != PSA_SUCCESS) {
1792 psa_destroy_persistent_key(slot->attr.id);
1793 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001794 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001796 }
1797#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1798
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001800 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 status = psa_unlock_key_slot(slot);
1802 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001803 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001805 }
Ronald Cron50972942020-11-14 11:28:25 +01001806
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001808}
1809
1810/** Abort the creation of a key.
1811 *
1812 * You may call this function after calling psa_start_key_creation(),
1813 * or after psa_finish_key_creation() fails. In other circumstances, this
1814 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001815 * See the documentation of psa_start_key_creation() for the intended use
1816 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001817 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001818 * \param[in,out] slot Pointer to the slot with key material.
1819 * \param[in] driver The secure element driver for the key,
1820 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001821 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001822static void psa_fail_key_creation(psa_key_slot_t *slot,
1823 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001824{
Gilles Peskine011e4282019-06-26 18:34:38 +02001825 (void) driver;
1826
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001828 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001830
1831#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001832 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001833 * element, and the failure happened later (when saving metadata
1834 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001835 * element.
1836 * https://github.com/ARMmbed/mbed-crypto/issues/217
1837 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001838
Gilles Peskined7729582019-08-05 15:55:54 +02001839 /* Abort the ongoing transaction if any (there may not be one if
1840 * the creation process failed before starting one, or if the
1841 * key creation is a registration of a key in a secure element).
1842 * Earlier functions must already have done what it takes to undo any
1843 * partial creation. All that's left is to update the transaction data
1844 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001846#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1847
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001849}
1850
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001851/** Validate optional attributes during key creation.
1852 *
1853 * Some key attributes are optional during key creation. If they are
1854 * specified in the attributes structure, check that they are consistent
1855 * with the data in the slot.
1856 *
1857 * This function should be called near the end of key creation, after
1858 * the slot in memory is fully populated but before saving persistent data.
1859 */
1860static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001861 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001863{
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 if (attributes->core.type != 0) {
1865 if (attributes->core.type != slot->attr.type) {
1866 return PSA_ERROR_INVALID_ARGUMENT;
1867 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001868 }
1869
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001871#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1873 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001874 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001875 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001876 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001877
Ronald Cron90857082020-11-25 14:58:33 +01001878 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 slot->attr.type,
1880 slot->key.data,
1881 slot->key.bytes,
1882 &rsa);
1883 if (status != PSA_SUCCESS) {
1884 return status;
1885 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 mbedtls_mpi_init(&actual);
1888 mbedtls_mpi_init(&required);
1889 ret = mbedtls_rsa_export(rsa,
1890 NULL, NULL, NULL, NULL, &actual);
1891 mbedtls_rsa_free(rsa);
1892 mbedtls_free(rsa);
1893 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001894 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 }
1896 ret = mbedtls_mpi_read_binary(&required,
1897 attributes->domain_parameters,
1898 attributes->domain_parameters_size);
1899 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001900 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 }
1902 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001903 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 }
1905rsa_exit:
1906 mbedtls_mpi_free(&actual);
1907 mbedtls_mpi_free(&required);
1908 if (ret != 0) {
1909 return mbedtls_to_psa_error(ret);
1910 }
1911 } else
John Durkop9814fa22020-11-04 12:28:15 -08001912#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1913 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001914 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001916 }
1917 }
1918
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 if (attributes->core.bits != 0) {
1920 if (attributes->core.bits != slot->attr.bits) {
1921 return PSA_ERROR_INVALID_ARGUMENT;
1922 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001923 }
1924
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001926}
1927
Gilles Peskine449bd832023-01-11 14:50:10 +01001928psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1929 const uint8_t *data,
1930 size_t data_length,
1931 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001932{
1933 psa_status_t status;
1934 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001935 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001936 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301937 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001938
Ronald Cron81709fc2020-11-14 12:10:32 +01001939 *key = MBEDTLS_SVC_KEY_ID_INIT;
1940
Gilles Peskine0f84d622019-09-12 19:03:13 +02001941 /* Reject zero-length symmetric keys (including raw data key objects).
1942 * This also rejects any key which might be encoded as an empty string,
1943 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 if (data_length == 0) {
1945 return PSA_ERROR_INVALID_ARGUMENT;
1946 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001947
Archana449608b2021-09-08 15:36:05 +05301948 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 if (data_length > SIZE_MAX / 8) {
1950 return PSA_ERROR_NOT_SUPPORTED;
1951 }
Archana6ed4bda2021-08-04 10:47:15 +05301952
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1954 &slot, &driver);
1955 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001956 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001958
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001959 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301960 * storage ( thus not in the case of importing a key in a secure element
1961 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1962 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 if (slot->key.data == NULL) {
1964 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301965 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 attributes, data, data_length, &storage_size);
1967 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301968 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 }
Archanad8a83dc2021-06-14 10:04:16 +05301970 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 status = psa_allocate_buffer_to_slot(slot, storage_size);
1972 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001973 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001975 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001976
1977 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 status = psa_driver_wrapper_import_key(attributes,
1979 data, data_length,
1980 slot->key.data,
1981 slot->key.bytes,
1982 &slot->key.bytes, &bits);
1983 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001984 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001986
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001988 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001990 status = PSA_ERROR_INVALID_ARGUMENT;
1991 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001992 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001993
Archana6ed4bda2021-08-04 10:47:15 +05301994 /* Enforce a size limit, and in particular ensure that the bit
1995 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05301997 status = PSA_ERROR_NOT_SUPPORTED;
1998 goto exit;
1999 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 status = psa_validate_optional_attributes(slot, attributes);
2001 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002002 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002004
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002006exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 if (status != PSA_SUCCESS) {
2008 psa_fail_key_creation(slot, driver);
2009 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002010
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002012}
2013
Gilles Peskined7729582019-08-05 15:55:54 +02002014#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2015psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002017{
2018 psa_status_t status;
2019 psa_key_slot_t *slot = NULL;
2020 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002021 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002022
2023 /* Leaving attributes unspecified is not currently supported.
2024 * It could make sense to query the key type and size from the
2025 * secure element, but not all secure elements support this
2026 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2028 return PSA_ERROR_NOT_SUPPORTED;
2029 }
2030 if (psa_get_key_bits(attributes) == 0) {
2031 return PSA_ERROR_NOT_SUPPORTED;
2032 }
Gilles Peskined7729582019-08-05 15:55:54 +02002033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2035 &slot, &driver);
2036 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002037 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 }
Gilles Peskined7729582019-08-05 15:55:54 +02002039
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002041
2042exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 if (status != PSA_SUCCESS) {
2044 psa_fail_key_creation(slot, driver);
2045 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002046
Gilles Peskined7729582019-08-05 15:55:54 +02002047 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 psa_close_key(key);
2049 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002050}
2051#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2052
Gilles Peskine449bd832023-01-11 14:50:10 +01002053psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2054 const psa_key_attributes_t *specified_attributes,
2055 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002056{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002057 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002058 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002059 psa_key_slot_t *source_slot = NULL;
2060 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002061 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002062 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302063 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002064
Ronald Cron81709fc2020-11-14 12:10:32 +01002065 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2066
Archana8a180362021-07-05 02:18:48 +05302067 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2069 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002070 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 status = psa_validate_optional_attributes(source_slot,
2074 specified_attributes);
2075 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002076 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002078
Archana9d17bf42021-09-10 06:22:44 +05302079 /* The target key type and number of bits have been validated by
2080 * psa_validate_optional_attributes() to be either equal to zero or
2081 * equal to the ones of the source key. So it is safe to inherit
2082 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302083 * */
Archana9d17bf42021-09-10 06:22:44 +05302084 actual_attributes.core.bits = source_slot->attr.bits;
2085 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302086
2087
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 status = psa_restrict_key_policy(source_slot->attr.type,
2089 &actual_attributes.core.policy,
2090 &source_slot->attr.policy);
2091 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002092 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2096 &target_slot, &driver);
2097 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002098 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 }
2100 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2101 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302102 /*
Archana9d17bf42021-09-10 06:22:44 +05302103 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302104 * the source key would need to be exported as plaintext and re-imported
2105 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302106 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302107 * appropriate API invocations from the application, if needed.
2108 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002109 status = PSA_ERROR_NOT_SUPPORTED;
2110 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002111 }
Archana8a180362021-07-05 02:18:48 +05302112 /*
2113 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302114 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302115 * - For opaque keys this translates to an invocation of the drivers'
2116 * copy_key entry point through the dispatch layer.
2117 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2119 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2120 &storage_size);
2121 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302122 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 }
Archana374fe5b2021-09-08 15:50:28 +05302124
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2126 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302127 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 }
Archana374fe5b2021-09-08 15:50:28 +05302129
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 status = psa_driver_wrapper_copy_key(&actual_attributes,
2131 source_slot->key.data,
2132 source_slot->key.bytes,
2133 target_slot->key.data,
2134 target_slot->key.bytes,
2135 &target_slot->key.bytes);
2136 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302137 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 }
2139 } else {
2140 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302141 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 source_slot->key.bytes);
2143 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302144 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 }
Archana8a180362021-07-05 02:18:48 +05302146 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002148exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 if (status != PSA_SUCCESS) {
2150 psa_fail_key_creation(target_slot, driver);
2151 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002152
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002156}
2157
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002158
2159
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002160/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002161/* Message digests */
2162/****************************************************************/
2163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002165{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002166 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 if (operation->id == 0) {
2168 return PSA_SUCCESS;
2169 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002172 operation->id = 0;
2173
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002175}
2176
Gilles Peskine449bd832023-01-11 14:50:10 +01002177psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2178 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002179{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002180 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002181
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002182 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002184 status = PSA_ERROR_BAD_STATE;
2185 goto exit;
2186 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002187
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002189 status = PSA_ERROR_INVALID_ARGUMENT;
2190 goto exit;
2191 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002192
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002193 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2194 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002196
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002198
2199exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 if (status != PSA_SUCCESS) {
2201 psa_hash_abort(operation);
2202 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002203
2204 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002205}
2206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2208 const uint8_t *input,
2209 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002210{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002211 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002214 status = PSA_ERROR_BAD_STATE;
2215 goto exit;
2216 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002217
Steven Cooremanc8288352021-03-04 14:02:19 +01002218 /* Don't require hash implementations to behave correctly on a
2219 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 if (input_length == 0) {
2221 return PSA_SUCCESS;
2222 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002223
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002225
2226exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 if (status != PSA_SUCCESS) {
2228 psa_hash_abort(operation);
2229 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002230
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002232}
2233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2235 uint8_t *hash,
2236 size_t hash_size,
2237 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002238{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002239 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002240 if (operation->id == 0) {
2241 return PSA_ERROR_BAD_STATE;
2242 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002243
Steven Cooreman1e582352021-02-18 17:24:37 +01002244 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 operation, hash, hash_size, hash_length);
2246 psa_hash_abort(operation);
2247 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002248}
2249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2251 const uint8_t *hash,
2252 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002253{
Ronald Cron69a63422021-10-18 09:47:58 +02002254 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002255 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002256 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 operation,
2258 actual_hash, sizeof(actual_hash),
2259 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002260
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002262 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002264
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002266 status = PSA_ERROR_INVALID_SIGNATURE;
2267 goto exit;
2268 }
2269
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002271 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002273
2274exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2276 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002277 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002281}
2282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283psa_status_t psa_hash_compute(psa_algorithm_t alg,
2284 const uint8_t *input, size_t input_length,
2285 uint8_t *hash, size_t hash_size,
2286 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002287{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002288 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 if (!PSA_ALG_IS_HASH(alg)) {
2290 return PSA_ERROR_INVALID_ARGUMENT;
2291 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002292
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2294 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002295}
2296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297psa_status_t psa_hash_compare(psa_algorithm_t alg,
2298 const uint8_t *input, size_t input_length,
2299 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002300{
Ronald Cron69a63422021-10-18 09:47:58 +02002301 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002302 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 if (!PSA_ALG_IS_HASH(alg)) {
2305 return PSA_ERROR_INVALID_ARGUMENT;
2306 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002307
Steven Cooreman1e582352021-02-18 17:24:37 +01002308 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 alg, input, input_length,
2310 actual_hash, sizeof(actual_hash),
2311 &actual_hash_length);
2312 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002313 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 }
2315 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002316 status = PSA_ERROR_INVALID_SIGNATURE;
2317 goto exit;
2318 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002320 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002322
2323exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2325 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002326}
2327
Gilles Peskine449bd832023-01-11 14:50:10 +01002328psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2329 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002330{
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 if (source_operation->id == 0 ||
2332 target_operation->id != 0) {
2333 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002334 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002335
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2337 target_operation);
2338 if (status != PSA_SUCCESS) {
2339 psa_hash_abort(target_operation);
2340 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002341
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002343}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002344
2345
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002346/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002347/* MAC */
2348/****************************************************************/
2349
Gilles Peskine449bd832023-01-11 14:50:10 +01002350psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002351{
Steven Cooremane6804192021-03-19 18:28:56 +01002352 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 if (operation->id == 0) {
2354 return PSA_SUCCESS;
2355 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002358 operation->mac_size = 0;
2359 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002360 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002361
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002363}
2364
Ronald Cron2dff3b22021-06-17 16:33:22 +02002365static psa_status_t psa_mac_finalize_alg_and_key_validation(
2366 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002367 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002369{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002370 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 psa_key_type_t key_type = psa_get_key_type(attributes);
2372 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002373
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 if (!PSA_ALG_IS_MAC(alg)) {
2375 return PSA_ERROR_INVALID_ARGUMENT;
2376 }
Steven Cooremane6804192021-03-19 18:28:56 +01002377
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002378 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 status = psa_mac_key_can_do(alg, key_type);
2380 if (status != PSA_SUCCESS) {
2381 return status;
2382 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002383
Steven Cooremand1a68f12021-05-10 11:13:41 +02002384 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002388 /* A very short MAC is too short for security since it can be
2389 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2390 * so we make this our minimum, even though 32 bits is still
2391 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002393 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002394
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2396 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002397 /* It's impossible to "truncate" to a larger length than the full length
2398 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002400 }
2401
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002403 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2404 * that is disabled in the compile-time configuration. The result can
2405 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2406 * configuration into account. In this case, force a return of
2407 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2408 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2409 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2410 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2411 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002413 }
2414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002416}
2417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2419 mbedtls_svc_key_id_t key,
2420 psa_algorithm_t alg,
2421 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002422{
2423 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2424 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002425 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002426
2427 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002429 status = PSA_ERROR_BAD_STATE;
2430 goto exit;
2431 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002432
2433 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 key,
2435 &slot,
2436 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2437 alg);
2438 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002439 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002441
2442 psa_key_attributes_t attributes = {
2443 .core = slot->attr
2444 };
2445
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2447 &operation->mac_size);
2448 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002449 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002451
2452 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002453 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 if (is_sign) {
2455 status = psa_driver_wrapper_mac_sign_setup(operation,
2456 &attributes,
2457 slot->key.data,
2458 slot->key.bytes,
2459 alg);
2460 } else {
2461 status = psa_driver_wrapper_mac_verify_setup(operation,
2462 &attributes,
2463 slot->key.data,
2464 slot->key.bytes,
2465 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002466 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002467
Gilles Peskinefbfac682018-07-08 20:51:54 +02002468exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 if (status != PSA_SUCCESS) {
2470 psa_mac_abort(operation);
2471 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002476}
2477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2479 mbedtls_svc_key_id_t key,
2480 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002481{
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002483}
2484
Gilles Peskine449bd832023-01-11 14:50:10 +01002485psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2486 mbedtls_svc_key_id_t key,
2487 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002488{
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002490}
2491
Gilles Peskine449bd832023-01-11 14:50:10 +01002492psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2493 const uint8_t *input,
2494 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002495{
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 if (operation->id == 0) {
2497 return PSA_ERROR_BAD_STATE;
2498 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002499
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002500 /* Don't require hash implementations to behave correctly on a
2501 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 if (input_length == 0) {
2503 return PSA_SUCCESS;
2504 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2507 input, input_length);
2508 if (status != PSA_SUCCESS) {
2509 psa_mac_abort(operation);
2510 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002513}
2514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2516 uint8_t *mac,
2517 size_t mac_size,
2518 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002519{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002520 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2521 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002522
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002524 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002525 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002526 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002527
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002529 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002530 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002531 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002532
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002533 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2534 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002536 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002537 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002538 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002541 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002542 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002543 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 status = psa_driver_wrapper_mac_sign_finish(operation,
2546 mac, operation->mac_size,
2547 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002548
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002549exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002550 /* In case of success, set the potential excess room in the output buffer
2551 * to an invalid value, to avoid potentially leaking a longer MAC.
2552 * In case of error, set the output length and content to a safe default,
2553 * such that in case the caller misses an error check, the output would be
2554 * an unachievable MAC.
2555 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002557 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002558 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002559 }
mohammad16036df908f2018-04-02 08:34:15 -07002560
Paul Elliottdc42ca82023-02-24 18:11:59 +00002561 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002566}
2567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2569 const uint8_t *mac,
2570 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002571{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002572 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2573 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002576 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002577 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002578 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002579
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002581 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002582 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002583 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002584
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002586 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002587 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002588 }
2589
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 status = psa_driver_wrapper_mac_verify_finish(operation,
2591 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002592
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002593exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002597}
2598
Gilles Peskine449bd832023-01-11 14:50:10 +01002599static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2600 psa_algorithm_t alg,
2601 const uint8_t *input,
2602 size_t input_length,
2603 uint8_t *mac,
2604 size_t mac_size,
2605 size_t *mac_length,
2606 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002607{
2608 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002609 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2610 psa_key_slot_t *slot;
2611 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002612
Ronald Cron51131b52021-06-17 17:17:20 +02002613 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 key,
2615 &slot,
2616 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2617 alg);
2618 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002619 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002621
Ronald Cron51131b52021-06-17 17:17:20 +02002622 psa_key_attributes_t attributes = {
2623 .core = slot->attr
2624 };
2625
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2627 &operation_mac_size);
2628 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002629 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002633 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002634 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002635 }
2636
2637 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 &attributes,
2639 slot->key.data, slot->key.bytes,
2640 alg,
2641 input, input_length,
2642 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002643
2644exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002645 /* In case of success, set the potential excess room in the output buffer
2646 * to an invalid value, to avoid potentially leaking a longer MAC.
2647 * In case of error, set the output length and content to a safe default,
2648 * such that in case the caller misses an error check, the output would be
2649 * an unachievable MAC.
2650 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002652 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002653 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002654 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002655
2656 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002657
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002661}
2662
Gilles Peskine449bd832023-01-11 14:50:10 +01002663psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002664 psa_algorithm_t alg,
2665 const uint8_t *input,
2666 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 uint8_t *mac,
2668 size_t mac_size,
2669 size_t *mac_length)
2670{
2671 return psa_mac_compute_internal(key, alg,
2672 input, input_length,
2673 mac, mac_size, mac_length, 1);
2674}
2675
2676psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2677 psa_algorithm_t alg,
2678 const uint8_t *input,
2679 size_t input_length,
2680 const uint8_t *mac,
2681 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002682{
2683 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002684 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2685 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002686
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 status = psa_mac_compute_internal(key, alg,
2688 input, input_length,
2689 actual_mac, sizeof(actual_mac),
2690 &actual_mac_length, 0);
2691 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002692 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002694
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002696 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002697 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002698 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002700 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002701 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002702 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002703
2704exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002706
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002708}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002709
Gilles Peskinee59236f2018-01-27 23:32:46 +01002710/****************************************************************/
2711/* Asymmetric cryptography */
2712/****************************************************************/
2713
Gilles Peskine449bd832023-01-11 14:50:10 +01002714static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2715 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002716{
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 if (input_is_message) {
2718 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2719 return PSA_ERROR_INVALID_ARGUMENT;
2720 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2723 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2724 return PSA_ERROR_INVALID_ARGUMENT;
2725 }
2726 }
2727 } else {
2728 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2729 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002730 }
2731 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002732
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002734}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002735
Gilles Peskine449bd832023-01-11 14:50:10 +01002736static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2737 int input_is_message,
2738 psa_algorithm_t alg,
2739 const uint8_t *input,
2740 size_t input_length,
2741 uint8_t *signature,
2742 size_t signature_size,
2743 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002744{
2745 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2746 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2747 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002748
2749 *signature_length = 0;
2750
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 status = psa_sign_verify_check_alg(input_is_message, alg);
2752 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002753 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002755
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002756 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002757 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002758 * buffer can in principle be empty since it doesn't actually have
2759 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002760 if (signature_size == 0) {
2761 return PSA_ERROR_BUFFER_TOO_SMALL;
2762 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002763
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002764 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 key, &slot,
2766 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2767 PSA_KEY_USAGE_SIGN_HASH,
2768 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002769
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002771 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002772 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002773
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002775 status = PSA_ERROR_INVALID_ARGUMENT;
2776 goto exit;
2777 }
2778
2779 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002781 };
2782
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002784 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002785 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002786 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 signature, signature_size, signature_length);
2788 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002789
2790 status = psa_driver_wrapper_sign_hash(
2791 &attributes, slot->key.data, slot->key.bytes,
2792 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002794 }
2795
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002796
2797exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002798 psa_wipe_tag_output_buffer(signature, status, signature_size,
2799 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002800
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002802
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002804}
2805
Gilles Peskine449bd832023-01-11 14:50:10 +01002806static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2807 int input_is_message,
2808 psa_algorithm_t alg,
2809 const uint8_t *input,
2810 size_t input_length,
2811 const uint8_t *signature,
2812 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002813{
2814 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2815 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2816 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002817
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 status = psa_sign_verify_check_alg(input_is_message, alg);
2819 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002820 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002822
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002823 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 key, &slot,
2825 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2826 PSA_KEY_USAGE_VERIFY_HASH,
2827 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002828
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 if (status != PSA_SUCCESS) {
2830 return status;
2831 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002832
2833 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002835 };
2836
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002838 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002839 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002840 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 signature, signature_length);
2842 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002843 status = psa_driver_wrapper_verify_hash(
2844 &attributes, slot->key.data, slot->key.bytes,
2845 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002847 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002848
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002850
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002852
2853}
2854
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002855psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002856 const psa_key_attributes_t *attributes,
2857 const uint8_t *key_buffer,
2858 size_t key_buffer_size,
2859 psa_algorithm_t alg,
2860 const uint8_t *input,
2861 size_t input_length,
2862 uint8_t *signature,
2863 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002865{
2866 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002867
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002869 size_t hash_length;
2870 uint8_t hash[PSA_HASH_MAX_SIZE];
2871
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002872 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 PSA_ALG_SIGN_GET_HASH(alg),
2874 input, input_length,
2875 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002876
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002878 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002880
2881 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 attributes, key_buffer, key_buffer_size,
2883 alg, hash, hash_length,
2884 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002885 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002886
Gilles Peskine449bd832023-01-11 14:50:10 +01002887 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002888}
2889
Gilles Peskine449bd832023-01-11 14:50:10 +01002890psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2891 psa_algorithm_t alg,
2892 const uint8_t *input,
2893 size_t input_length,
2894 uint8_t *signature,
2895 size_t signature_size,
2896 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002897{
2898 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002899 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002901}
2902
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002903psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002904 const psa_key_attributes_t *attributes,
2905 const uint8_t *key_buffer,
2906 size_t key_buffer_size,
2907 psa_algorithm_t alg,
2908 const uint8_t *input,
2909 size_t input_length,
2910 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002912{
2913 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002914
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002916 size_t hash_length;
2917 uint8_t hash[PSA_HASH_MAX_SIZE];
2918
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002919 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 PSA_ALG_SIGN_GET_HASH(alg),
2921 input, input_length,
2922 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002923
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002925 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002927
2928 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 attributes, key_buffer, key_buffer_size,
2930 alg, hash, hash_length,
2931 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002932 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002933
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002935}
2936
Gilles Peskine449bd832023-01-11 14:50:10 +01002937psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2938 psa_algorithm_t alg,
2939 const uint8_t *input,
2940 size_t input_length,
2941 const uint8_t *signature,
2942 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002943{
2944 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002945 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002947}
2948
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002949psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002950 const psa_key_attributes_t *attributes,
2951 const uint8_t *key_buffer, size_t key_buffer_size,
2952 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002954{
Gilles Peskine449bd832023-01-11 14:50:10 +01002955 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2956 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2957 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002958#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2960 return mbedtls_psa_rsa_sign_hash(
2961 attributes,
2962 key_buffer, key_buffer_size,
2963 alg, hash, hash_length,
2964 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002965#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2966 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 } else {
2968 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002969 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2971 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002972#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2974 return mbedtls_psa_ecdsa_sign_hash(
2975 attributes,
2976 key_buffer, key_buffer_size,
2977 alg, hash, hash_length,
2978 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002979#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2980 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 } else {
2982 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002983 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002984 }
Ronald Cron4501c982021-03-19 20:09:32 +01002985
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 (void) key_buffer;
2987 (void) key_buffer_size;
2988 (void) hash;
2989 (void) hash_length;
2990 (void) signature;
2991 (void) signature_size;
2992 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002993
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002995}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002996
Gilles Peskine449bd832023-01-11 14:50:10 +01002997psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2998 psa_algorithm_t alg,
2999 const uint8_t *hash,
3000 size_t hash_length,
3001 uint8_t *signature,
3002 size_t signature_size,
3003 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003004{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003005 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003006 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003008}
3009
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003010psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003011 const psa_key_attributes_t *attributes,
3012 const uint8_t *key_buffer, size_t key_buffer_size,
3013 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003015{
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3017 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3018 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003019#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3021 return mbedtls_psa_rsa_verify_hash(
3022 attributes,
3023 key_buffer, key_buffer_size,
3024 alg, hash, hash_length,
3025 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003026#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3027 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 } else {
3029 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003030 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3032 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003033#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3035 return mbedtls_psa_ecdsa_verify_hash(
3036 attributes,
3037 key_buffer, key_buffer_size,
3038 alg, hash, hash_length,
3039 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003040#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3041 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003042 } else {
3043 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003044 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003045 }
Ronald Cron4501c982021-03-19 20:09:32 +01003046
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 (void) key_buffer;
3048 (void) key_buffer_size;
3049 (void) hash;
3050 (void) hash_length;
3051 (void) signature;
3052 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003053
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003055}
3056
Gilles Peskine449bd832023-01-11 14:50:10 +01003057psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3058 psa_algorithm_t alg,
3059 const uint8_t *hash,
3060 size_t hash_length,
3061 const uint8_t *signature,
3062 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003063{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003064 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003065 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003067}
3068
Gilles Peskine449bd832023-01-11 14:50:10 +01003069psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3070 psa_algorithm_t alg,
3071 const uint8_t *input,
3072 size_t input_length,
3073 const uint8_t *salt,
3074 size_t salt_length,
3075 uint8_t *output,
3076 size_t output_size,
3077 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003078{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003079 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003080 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003081 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003082
Darryl Green5cc689a2018-07-24 15:34:10 +01003083 (void) input;
3084 (void) input_length;
3085 (void) salt;
3086 (void) output;
3087 (void) output_size;
3088
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003089 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003090
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3092 return PSA_ERROR_INVALID_ARGUMENT;
3093 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003094
Ronald Cron5c522922020-11-14 16:35:34 +01003095 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3097 if (status != PSA_SUCCESS) {
3098 return status;
3099 }
3100 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3101 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003102 status = PSA_ERROR_INVALID_ARGUMENT;
3103 goto exit;
3104 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003105
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003106 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003108 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003109
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003110 status = psa_driver_wrapper_asymmetric_encrypt(
3111 &attributes, slot->key.data, slot->key.bytes,
3112 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003114exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003116
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003118}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003119
Gilles Peskine449bd832023-01-11 14:50:10 +01003120psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3121 psa_algorithm_t alg,
3122 const uint8_t *input,
3123 size_t input_length,
3124 const uint8_t *salt,
3125 size_t salt_length,
3126 uint8_t *output,
3127 size_t output_size,
3128 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003129{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003130 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003131 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003132 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003133
Darryl Green5cc689a2018-07-24 15:34:10 +01003134 (void) input;
3135 (void) input_length;
3136 (void) salt;
3137 (void) output;
3138 (void) output_size;
3139
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003140 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003141
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3143 return PSA_ERROR_INVALID_ARGUMENT;
3144 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003145
Ronald Cron5c522922020-11-14 16:35:34 +01003146 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3148 if (status != PSA_SUCCESS) {
3149 return status;
3150 }
3151 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003152 status = PSA_ERROR_INVALID_ARGUMENT;
3153 goto exit;
3154 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003155
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003156 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003158 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003159
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003160 status = psa_driver_wrapper_asymmetric_decrypt(
3161 &attributes, slot->key.data, slot->key.bytes,
3162 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003164
3165exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003167
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003169}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003170
Paul Elliott9fe12f62022-11-30 19:16:02 +00003171/****************************************************************/
3172/* Asymmetric interruptible cryptography */
3173/****************************************************************/
3174
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003175static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3176
Paul Elliott9fe12f62022-11-30 19:16:02 +00003177void psa_interruptible_set_max_ops(uint32_t max_ops)
3178{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003179 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003180}
3181
3182uint32_t psa_interruptible_get_max_ops(void)
3183{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003184 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003185}
3186
Paul Elliott9fe12f62022-11-30 19:16:02 +00003187uint32_t psa_sign_hash_get_num_ops(
3188 const psa_sign_hash_interruptible_operation_t *operation)
3189{
Paul Elliott296ede92022-12-15 17:00:30 +00003190 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003191}
3192
3193uint32_t psa_verify_hash_get_num_ops(
3194 const psa_verify_hash_interruptible_operation_t *operation)
3195{
Paul Elliott296ede92022-12-15 17:00:30 +00003196 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003197}
3198
Paul Elliott59ad9452022-12-18 15:09:02 +00003199static psa_status_t psa_sign_hash_abort_internal(
3200 psa_sign_hash_interruptible_operation_t *operation)
3201{
3202 if (operation->id == 0) {
3203 /* The object has (apparently) been initialized but it is not (yet)
3204 * in use. It's ok to call abort on such an object, and there's
3205 * nothing to do. */
3206 return PSA_SUCCESS;
3207 }
3208
3209 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3210
3211 status = psa_driver_wrapper_sign_hash_abort(operation);
3212
3213 operation->id = 0;
3214
Paul Elliotte6145dc2023-02-07 12:51:21 +00003215 /* Do not clear either the error_occurred or num_ops elements here as they
3216 * only want to be cleared by the application calling abort, not by abort
3217 * being called at completion of an operation. */
3218
Paul Elliott59ad9452022-12-18 15:09:02 +00003219 return status;
3220}
3221
Paul Elliott9fe12f62022-11-30 19:16:02 +00003222psa_status_t psa_sign_hash_start(
3223 psa_sign_hash_interruptible_operation_t *operation,
3224 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3225 const uint8_t *hash, size_t hash_length)
3226{
3227 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3228 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3229 psa_key_slot_t *slot;
3230
Paul Elliottc9774412023-02-06 15:14:07 +00003231 /* Check that start has not been previously called, or operation has not
3232 * previously errored. */
3233 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003234 return PSA_ERROR_BAD_STATE;
3235 }
3236
Paul Elliott9fe12f62022-11-30 19:16:02 +00003237 status = psa_sign_verify_check_alg(0, alg);
3238 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003239 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003240 return status;
3241 }
3242
3243 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3244 PSA_KEY_USAGE_SIGN_HASH,
3245 alg);
3246
3247 if (status != PSA_SUCCESS) {
3248 goto exit;
3249 }
3250
3251 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3252 status = PSA_ERROR_INVALID_ARGUMENT;
3253 goto exit;
3254 }
3255
3256 psa_key_attributes_t attributes = {
3257 .core = slot->attr
3258 };
3259
Paul Elliott296ede92022-12-15 17:00:30 +00003260 /* Ensure ops count gets reset, in case of operation re-use. */
3261 operation->num_ops = 0;
3262
Paul Elliott9fe12f62022-11-30 19:16:02 +00003263 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3264 slot->key.data,
3265 slot->key.bytes, alg,
3266 hash, hash_length);
3267exit:
3268
3269 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003270 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003271 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003272 }
3273
3274 unlock_status = psa_unlock_key_slot(slot);
3275
Paul Elliottc9774412023-02-06 15:14:07 +00003276 if (unlock_status != PSA_SUCCESS) {
3277 operation->error_occurred = 1;
3278 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003279
Paul Elliottc9774412023-02-06 15:14:07 +00003280 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003281}
3282
3283
3284psa_status_t psa_sign_hash_complete(
3285 psa_sign_hash_interruptible_operation_t *operation,
3286 uint8_t *signature, size_t signature_size,
3287 size_t *signature_length)
3288{
3289 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3290
3291 *signature_length = 0;
3292
Paul Elliottc9774412023-02-06 15:14:07 +00003293 /* Check that start has been called first, and that operation has not
3294 * previously errored. */
3295 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003296 status = PSA_ERROR_BAD_STATE;
3297 goto exit;
3298 }
3299
Paul Elliott096abc42023-02-03 18:33:23 +00003300 /* Immediately reject a zero-length signature buffer. This guarantees that
3301 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003302 if (signature_size == 0) {
3303 status = PSA_ERROR_BUFFER_TOO_SMALL;
3304 goto exit;
3305 }
3306
3307 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3308 signature_size,
3309 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003310
Paul Elliott296ede92022-12-15 17:00:30 +00003311 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003312 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003313
Paul Elliottebe225c2023-02-10 14:32:53 +00003314exit:
3315
Paul Elliott59200a22023-02-21 15:07:40 +00003316 psa_wipe_tag_output_buffer(signature, status, signature_size,
3317 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003318
Paul Elliott53bb3122023-02-10 14:22:22 +00003319 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003320 if (status != PSA_SUCCESS) {
3321 operation->error_occurred = 1;
3322 }
3323
Paul Elliott59ad9452022-12-18 15:09:02 +00003324 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003325 }
3326
3327 return status;
3328}
3329
3330psa_status_t psa_sign_hash_abort(
3331 psa_sign_hash_interruptible_operation_t *operation)
3332{
Paul Elliott59ad9452022-12-18 15:09:02 +00003333 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3334
3335 status = psa_sign_hash_abort_internal(operation);
3336
3337 /* We clear the number of ops done here, so that it is not cleared when
3338 * the operation fails or succeeds, only on manual abort. */
3339 operation->num_ops = 0;
3340
Paul Elliottc9774412023-02-06 15:14:07 +00003341 /* Likewise, failure state. */
3342 operation->error_occurred = 0;
3343
Paul Elliott59ad9452022-12-18 15:09:02 +00003344 return status;
3345}
3346
3347static psa_status_t psa_verify_hash_abort_internal(
3348 psa_verify_hash_interruptible_operation_t *operation)
3349{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003350 if (operation->id == 0) {
3351 /* The object has (apparently) been initialized but it is not (yet)
3352 * in use. It's ok to call abort on such an object, and there's
3353 * nothing to do. */
3354 return PSA_SUCCESS;
3355 }
3356
Paul Elliott59ad9452022-12-18 15:09:02 +00003357 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3358
3359 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003360
3361 operation->id = 0;
3362
Paul Elliotte6145dc2023-02-07 12:51:21 +00003363 /* Do not clear either the error_occurred or num_ops elements here as they
3364 * only want to be cleared by the application calling abort, not by abort
3365 * being called at completion of an operation. */
3366
Paul Elliott59ad9452022-12-18 15:09:02 +00003367 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003368}
3369
3370psa_status_t psa_verify_hash_start(
3371 psa_verify_hash_interruptible_operation_t *operation,
3372 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3373 const uint8_t *hash, size_t hash_length,
3374 const uint8_t *signature, size_t signature_length)
3375{
3376 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3377 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3378 psa_key_slot_t *slot;
3379
Paul Elliottc9774412023-02-06 15:14:07 +00003380 /* Check that start has not been previously called, or operation has not
3381 * previously errored. */
3382 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003383 return PSA_ERROR_BAD_STATE;
3384 }
3385
3386 status = psa_sign_verify_check_alg(0, alg);
3387 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003388 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003389 return status;
3390 }
3391
3392 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3393 PSA_KEY_USAGE_VERIFY_HASH,
3394 alg);
3395
3396 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003397 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003398 return status;
3399 }
3400
3401 psa_key_attributes_t attributes = {
3402 .core = slot->attr
3403 };
3404
Paul Elliott296ede92022-12-15 17:00:30 +00003405 /* Ensure ops count gets reset, in case of operation re-use. */
3406 operation->num_ops = 0;
3407
Paul Elliott9fe12f62022-11-30 19:16:02 +00003408 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3409 slot->key.data,
3410 slot->key.bytes,
3411 alg, hash, hash_length,
3412 signature, signature_length);
3413
3414 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003415 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003416 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003417 }
3418
3419 unlock_status = psa_unlock_key_slot(slot);
3420
Paul Elliottc9774412023-02-06 15:14:07 +00003421 if (unlock_status != PSA_SUCCESS) {
3422 operation->error_occurred = 1;
3423 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003424
Paul Elliottc9774412023-02-06 15:14:07 +00003425 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003426}
3427
3428psa_status_t psa_verify_hash_complete(
3429 psa_verify_hash_interruptible_operation_t *operation)
3430{
3431 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3432
Paul Elliottc9774412023-02-06 15:14:07 +00003433 /* Check that start has been called first, and that operation has not
3434 * previously errored. */
3435 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003436 status = PSA_ERROR_BAD_STATE;
3437 goto exit;
3438 }
3439
3440 status = psa_driver_wrapper_verify_hash_complete(operation);
3441
Paul Elliott296ede92022-12-15 17:00:30 +00003442 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003443 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003444 operation);
3445
Paul Elliottebe225c2023-02-10 14:32:53 +00003446exit:
3447
Paul Elliott9fe12f62022-11-30 19:16:02 +00003448 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003449 if (status != PSA_SUCCESS) {
3450 operation->error_occurred = 1;
3451 }
3452
Paul Elliott59ad9452022-12-18 15:09:02 +00003453 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003454 }
3455
3456 return status;
3457}
3458
3459psa_status_t psa_verify_hash_abort(
3460 psa_verify_hash_interruptible_operation_t *operation)
3461{
Paul Elliott59ad9452022-12-18 15:09:02 +00003462 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003463
Paul Elliott59ad9452022-12-18 15:09:02 +00003464 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003465
Paul Elliott59ad9452022-12-18 15:09:02 +00003466 /* We clear the number of ops done here, so that it is not cleared when
3467 * the operation fails or succeeds, only on manual abort. */
3468 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003469
Paul Elliottc9774412023-02-06 15:14:07 +00003470 /* Likewise, failure state. */
3471 operation->error_occurred = 0;
3472
Paul Elliott59ad9452022-12-18 15:09:02 +00003473 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003474}
3475
Paul Elliott588f8ed2022-12-02 18:10:26 +00003476/****************************************************************/
3477/* Asymmetric interruptible cryptography internal */
3478/* implementations */
3479/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003480
Paul Elliott588f8ed2022-12-02 18:10:26 +00003481void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3482{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003483
Paul Elliott588f8ed2022-12-02 18:10:26 +00003484#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3485 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3486 defined(MBEDTLS_ECP_RESTARTABLE)
3487
3488 /* Internal implementation uses zero to indicate infinite number max ops,
3489 * therefore avoid this value, and set to minimum possible. */
3490 if (max_ops == 0) {
3491 max_ops = 1;
3492 }
3493
Paul Elliott588f8ed2022-12-02 18:10:26 +00003494 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003495#else
3496 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003497#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3498 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3499 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3500}
3501
Paul Elliott588f8ed2022-12-02 18:10:26 +00003502uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003503 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003504{
3505#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3506 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3507 defined(MBEDTLS_ECP_RESTARTABLE)
3508
Paul Elliott93d9ca82023-02-15 18:14:21 +00003509 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003510#else
3511 (void) operation;
3512 return 0;
3513#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3514 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3515 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3516}
3517
3518uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003519 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003520{
3521 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3522 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3523 defined(MBEDTLS_ECP_RESTARTABLE)
3524
Paul Elliott93d9ca82023-02-15 18:14:21 +00003525 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003526#else
3527 (void) operation;
3528 return 0;
3529#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3530 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3531 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3532}
3533
3534psa_status_t mbedtls_psa_sign_hash_start(
3535 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3536 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3537 size_t key_buffer_size, psa_algorithm_t alg,
3538 const uint8_t *hash, size_t hash_length)
3539{
3540 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003541 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003542
Paul Elliott068fe072023-01-16 13:59:15 +00003543 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3544 return PSA_ERROR_NOT_SUPPORTED;
3545 }
3546
3547 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003548 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003549 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003550
3551#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003552 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3553 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003554
Paul Elliotta1c94092023-02-15 16:38:04 +00003555 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3556
Paul Elliott93d9ca82023-02-15 18:14:21 +00003557 /* Ensure num_ops is zero'ed in case of context re-use. */
3558 operation->num_ops = 0;
3559
Paul Elliott068fe072023-01-16 13:59:15 +00003560 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3561 attributes->core.bits,
3562 key_buffer,
3563 key_buffer_size,
3564 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003565
Paul Elliott068fe072023-01-16 13:59:15 +00003566 if (status != PSA_SUCCESS) {
3567 return status;
3568 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003569
Paul Elliott1bc59df2023-02-05 13:41:57 +00003570 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003571 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003572
Paul Elliott068fe072023-01-16 13:59:15 +00003573 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3574 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3575 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003576
Paul Elliott0290a762023-02-09 14:30:24 +00003577 /* We only need to store the same length of hash as the private key size
3578 * here, it would be truncated by the internal implementation anyway. */
3579 required_hash_length = (hash_length < operation->coordinate_bytes ?
3580 hash_length : operation->coordinate_bytes);
3581
Paul Elliottba70ad42023-02-15 18:23:53 +00003582 if (required_hash_length > sizeof(operation->hash)) {
3583 /* Shouldn't happen, but better safe than sorry. */
3584 return PSA_ERROR_CORRUPTION_DETECTED;
3585 }
3586
Paul Elliott0290a762023-02-09 14:30:24 +00003587 memcpy(operation->hash, hash, required_hash_length);
3588 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003589
3590 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003591
3592#else
Paul Elliott068fe072023-01-16 13:59:15 +00003593 (void) operation;
3594 (void) key_buffer;
3595 (void) key_buffer_size;
3596 (void) alg;
3597 (void) hash;
3598 (void) hash_length;
3599 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003600 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003601
Paul Elliott068fe072023-01-16 13:59:15 +00003602 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003603#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3604 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3605 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003606}
3607
3608psa_status_t mbedtls_psa_sign_hash_complete(
3609 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3610 uint8_t *signature, size_t signature_size,
3611 size_t *signature_length)
3612{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003613#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3614 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3615 defined(MBEDTLS_ECP_RESTARTABLE)
3616
Paul Elliotta1c94092023-02-15 16:38:04 +00003617 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003618 mbedtls_mpi r;
3619 mbedtls_mpi s;
3620
Paul Elliott46845252023-02-03 14:59:11 +00003621 mbedtls_mpi_init(&r);
3622 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003623
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003624 /* Ensure max_ops is set to the current value (or default). */
3625 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3626
Paul Elliotta1c94092023-02-15 16:38:04 +00003627 if (signature_size < 2 * operation->coordinate_bytes) {
3628 status = PSA_ERROR_BUFFER_TOO_SMALL;
3629 goto exit;
3630 }
3631
Paul Elliott588f8ed2022-12-02 18:10:26 +00003632 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003633
Paul Elliott588f8ed2022-12-02 18:10:26 +00003634#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3635 status = mbedtls_to_psa_error(
3636 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003637 &r,
3638 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003639 &operation->ctx->d,
3640 operation->hash,
3641 operation->hash_length,
3642 operation->md_alg,
3643 mbedtls_psa_get_random,
3644 MBEDTLS_PSA_RANDOM_STATE,
3645 &operation->restart_ctx));
3646#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003647 status = PSA_ERROR_NOT_SUPPORTED;
3648 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003649#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3650 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003651 status = mbedtls_to_psa_error(
3652 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003653 &r,
3654 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003655 &operation->ctx->d,
3656 operation->hash,
3657 operation->hash_length,
3658 mbedtls_psa_get_random,
3659 MBEDTLS_PSA_RANDOM_STATE,
3660 mbedtls_psa_get_random,
3661 MBEDTLS_PSA_RANDOM_STATE,
3662 &operation->restart_ctx));
3663 }
3664
Paul Elliottf8e5b562023-02-19 18:43:45 +00003665 /* Hide the fact that the restart context only holds a delta of number of
3666 * ops done during the last operation, not an absolute value. */
3667 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3668
Paul Elliott724bd252023-02-08 12:35:08 +00003669 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003670 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003671 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003672 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003673 operation->coordinate_bytes)
3674 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003675
3676 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003677 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003678 }
3679
3680 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003681 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003682 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003683 operation->coordinate_bytes,
3684 operation->coordinate_bytes)
3685 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003686
3687 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003688 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003689 }
3690
Paul Elliott1bc59df2023-02-05 13:41:57 +00003691 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003692
Paul Elliott724bd252023-02-08 12:35:08 +00003693 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003694 }
Paul Elliott724bd252023-02-08 12:35:08 +00003695
3696exit:
3697
3698 mbedtls_mpi_free(&r);
3699 mbedtls_mpi_free(&s);
3700 return status;
3701
Paul Elliott588f8ed2022-12-02 18:10:26 +00003702 #else
3703
3704 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003705 (void) signature;
3706 (void) signature_size;
3707 (void) signature_length;
3708
3709 return PSA_ERROR_NOT_SUPPORTED;
3710
3711#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3712 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3713 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3714}
3715
3716psa_status_t mbedtls_psa_sign_hash_abort(
3717 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3718{
3719
3720#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3721 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3722 defined(MBEDTLS_ECP_RESTARTABLE)
3723
3724 if (operation->ctx) {
3725 mbedtls_ecdsa_free(operation->ctx);
3726 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003727 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003728 }
3729
3730 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3731
Paul Elliott93d9ca82023-02-15 18:14:21 +00003732 operation->num_ops = 0;
3733
Paul Elliott588f8ed2022-12-02 18:10:26 +00003734 return PSA_SUCCESS;
3735
3736#else
3737
3738 (void) operation;
3739
3740 return PSA_ERROR_NOT_SUPPORTED;
3741
3742#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3743 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3744 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3745}
3746
3747psa_status_t mbedtls_psa_verify_hash_start(
3748 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3749 const psa_key_attributes_t *attributes,
3750 const uint8_t *key_buffer, size_t key_buffer_size,
3751 psa_algorithm_t alg,
3752 const uint8_t *hash, size_t hash_length,
3753 const uint8_t *signature, size_t signature_length)
3754{
3755 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003756 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003757 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003758
Paul Elliott068fe072023-01-16 13:59:15 +00003759 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3760 return PSA_ERROR_NOT_SUPPORTED;
3761 }
3762
3763 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003764 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003765 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003766
3767#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003768 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3769 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003770
Paul Elliotta1c94092023-02-15 16:38:04 +00003771 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3772 mbedtls_mpi_init(&operation->r);
3773 mbedtls_mpi_init(&operation->s);
3774
Paul Elliott93d9ca82023-02-15 18:14:21 +00003775 /* Ensure num_ops is zero'ed in case of context re-use. */
3776 operation->num_ops = 0;
3777
Paul Elliott068fe072023-01-16 13:59:15 +00003778 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3779 attributes->core.bits,
3780 key_buffer,
3781 key_buffer_size,
3782 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003783
Paul Elliott068fe072023-01-16 13:59:15 +00003784 if (status != PSA_SUCCESS) {
3785 return status;
3786 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003787
Paul Elliottc569fc22023-02-10 13:02:54 +00003788 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003789
Paul Elliott1bc59df2023-02-05 13:41:57 +00003790 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003791 return PSA_ERROR_INVALID_SIGNATURE;
3792 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003793
Paul Elliott068fe072023-01-16 13:59:15 +00003794 status = mbedtls_to_psa_error(
3795 mbedtls_mpi_read_binary(&operation->r,
3796 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003797 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003798
Paul Elliott068fe072023-01-16 13:59:15 +00003799 if (status != PSA_SUCCESS) {
3800 return status;
3801 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003802
Paul Elliott068fe072023-01-16 13:59:15 +00003803 status = mbedtls_to_psa_error(
3804 mbedtls_mpi_read_binary(&operation->s,
3805 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003806 coordinate_bytes,
3807 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003808
Paul Elliott068fe072023-01-16 13:59:15 +00003809 if (status != PSA_SUCCESS) {
3810 return status;
3811 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003812
Paul Elliott2c9843f2023-02-15 17:32:42 +00003813 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003814
Paul Elliott2c9843f2023-02-15 17:32:42 +00003815 if (status != PSA_SUCCESS) {
3816 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003817 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003818
Paul Elliott0290a762023-02-09 14:30:24 +00003819 /* We only need to store the same length of hash as the private key size
3820 * here, it would be truncated by the internal implementation anyway. */
3821 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3822 coordinate_bytes);
3823
Paul Elliottba70ad42023-02-15 18:23:53 +00003824 if (required_hash_length > sizeof(operation->hash)) {
3825 /* Shouldn't happen, but better safe than sorry. */
3826 return PSA_ERROR_CORRUPTION_DETECTED;
3827 }
3828
Paul Elliott0290a762023-02-09 14:30:24 +00003829 memcpy(operation->hash, hash, required_hash_length);
3830 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003831
3832 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003833#else
Paul Elliott068fe072023-01-16 13:59:15 +00003834 (void) operation;
3835 (void) key_buffer;
3836 (void) key_buffer_size;
3837 (void) alg;
3838 (void) hash;
3839 (void) hash_length;
3840 (void) signature;
3841 (void) signature_length;
3842 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003843 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003844 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003845
Paul Elliott068fe072023-01-16 13:59:15 +00003846 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003847#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3848 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3849 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003850}
3851
3852psa_status_t mbedtls_psa_verify_hash_complete(
3853 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3854{
3855
3856#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3857 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3858 defined(MBEDTLS_ECP_RESTARTABLE)
3859
Paul Elliottf8e5b562023-02-19 18:43:45 +00003860 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3861
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003862 /* Ensure max_ops is set to the current value (or default). */
3863 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3864
Paul Elliottf8e5b562023-02-19 18:43:45 +00003865 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003866 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3867 operation->hash,
3868 operation->hash_length,
3869 &operation->ctx->Q,
3870 &operation->r,
3871 &operation->s,
3872 &operation->restart_ctx));
3873
Paul Elliottf8e5b562023-02-19 18:43:45 +00003874 /* Hide the fact that the restart context only holds a delta of number of
3875 * ops done during the last operation, not an absolute value. */
3876 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3877
3878 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003879#else
3880 (void) operation;
3881
3882 return PSA_ERROR_NOT_SUPPORTED;
3883
3884#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3885 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3886 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3887}
3888
3889psa_status_t mbedtls_psa_verify_hash_abort(
3890 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3891{
3892
3893#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3894 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3895 defined(MBEDTLS_ECP_RESTARTABLE)
3896
3897 if (operation->ctx) {
3898 mbedtls_ecdsa_free(operation->ctx);
3899 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003900 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003901 }
3902
3903 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3904
Paul Elliott93d9ca82023-02-15 18:14:21 +00003905 operation->num_ops = 0;
3906
Paul Elliott588f8ed2022-12-02 18:10:26 +00003907 mbedtls_mpi_free(&operation->r);
3908 mbedtls_mpi_free(&operation->s);
3909
3910 return PSA_SUCCESS;
3911
3912#else
3913 (void) operation;
3914
3915 return PSA_ERROR_NOT_SUPPORTED;
3916
3917#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3918 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3919 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3920}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003921
mohammad1603503973b2018-03-12 15:59:30 +02003922/****************************************************************/
3923/* Symmetric cryptography */
3924/****************************************************************/
3925
Gilles Peskine449bd832023-01-11 14:50:10 +01003926static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3927 mbedtls_svc_key_id_t key,
3928 psa_algorithm_t alg,
3929 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003930{
3931 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3932 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003933 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003934 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3935 PSA_KEY_USAGE_ENCRYPT :
3936 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003937
3938 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003939 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003940 status = PSA_ERROR_BAD_STATE;
3941 goto exit;
3942 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003943
Gilles Peskine449bd832023-01-11 14:50:10 +01003944 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003945 status = PSA_ERROR_INVALID_ARGUMENT;
3946 goto exit;
3947 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003948
Gilles Peskine449bd832023-01-11 14:50:10 +01003949 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3950 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003951 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003952 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003953
Ronald Cron49fafa92021-03-10 08:34:23 +01003954 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003955 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003956 * so we only set it (in the driver wrapper) after resources have been
3957 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003958 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003959 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003960 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003961 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003962 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003963 }
3964 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003965
Ronald Crona4af55f2020-12-14 14:36:06 +01003966 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003967 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003968 };
3969
Ronald Cronab99ac22020-10-01 16:38:28 +02003970 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003971 if (cipher_operation == MBEDTLS_ENCRYPT) {
3972 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3973 &attributes,
3974 slot->key.data,
3975 slot->key.bytes,
3976 alg);
3977 } else {
3978 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3979 &attributes,
3980 slot->key.data,
3981 slot->key.bytes,
3982 alg);
3983 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003984
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003985exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003986 if (status != PSA_SUCCESS) {
3987 psa_cipher_abort(operation);
3988 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003989
Gilles Peskine449bd832023-01-11 14:50:10 +01003990 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003991
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003993}
3994
Gilles Peskine449bd832023-01-11 14:50:10 +01003995psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3996 mbedtls_svc_key_id_t key,
3997 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003998{
Gilles Peskine449bd832023-01-11 14:50:10 +01003999 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004000}
4001
Gilles Peskine449bd832023-01-11 14:50:10 +01004002psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4003 mbedtls_svc_key_id_t key,
4004 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004005{
Gilles Peskine449bd832023-01-11 14:50:10 +01004006 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004007}
4008
Gilles Peskine449bd832023-01-11 14:50:10 +01004009psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4010 uint8_t *iv,
4011 size_t iv_size,
4012 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004013{
Ronald Cron6d051732020-10-01 14:10:20 +02004014 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004015 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4016 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004017
Gilles Peskine449bd832023-01-11 14:50:10 +01004018 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004019 status = PSA_ERROR_BAD_STATE;
4020 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004021 }
4022
Gilles Peskine449bd832023-01-11 14:50:10 +01004023 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004024 status = PSA_ERROR_BAD_STATE;
4025 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004026 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004027
Ronald Cron2fb90522021-07-05 12:31:44 +02004028 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004029 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004030 status = PSA_ERROR_BUFFER_TOO_SMALL;
4031 goto exit;
4032 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004033
Gilles Peskine449bd832023-01-11 14:50:10 +01004034 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004035 status = PSA_ERROR_GENERIC_ERROR;
4036 goto exit;
4037 }
4038
Gilles Peskine449bd832023-01-11 14:50:10 +01004039 status = psa_generate_random(local_iv, default_iv_length);
4040 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004041 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004042 }
Ronald Cron5618a392021-03-26 09:52:26 +01004043
Gilles Peskine449bd832023-01-11 14:50:10 +01004044 status = psa_driver_wrapper_cipher_set_iv(operation,
4045 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004046
4047exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004048 if (status == PSA_SUCCESS) {
4049 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004050 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004051 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004052 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004053 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004054 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004055 }
Ronald Cron6d051732020-10-01 14:10:20 +02004056
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004058}
4059
Gilles Peskine449bd832023-01-11 14:50:10 +01004060psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4061 const uint8_t *iv,
4062 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004063{
Ronald Cron6d051732020-10-01 14:10:20 +02004064 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004065
Gilles Peskine449bd832023-01-11 14:50:10 +01004066 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004067 status = PSA_ERROR_BAD_STATE;
4068 goto exit;
4069 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004070
Gilles Peskine449bd832023-01-11 14:50:10 +01004071 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004072 status = PSA_ERROR_BAD_STATE;
4073 goto exit;
4074 }
Ronald Crona0d68172021-03-26 10:15:08 +01004075
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004077 status = PSA_ERROR_INVALID_ARGUMENT;
4078 goto exit;
4079 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004080
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 status = psa_driver_wrapper_cipher_set_iv(operation,
4082 iv,
4083 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004084
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004085exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004086 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004087 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004088 } else {
4089 psa_cipher_abort(operation);
4090 }
4091 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004092}
4093
Gilles Peskine449bd832023-01-11 14:50:10 +01004094psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4095 const uint8_t *input,
4096 size_t input_length,
4097 uint8_t *output,
4098 size_t output_size,
4099 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004100{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004101 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004102
Gilles Peskine449bd832023-01-11 14:50:10 +01004103 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004104 status = PSA_ERROR_BAD_STATE;
4105 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004106 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004107
Gilles Peskine449bd832023-01-11 14:50:10 +01004108 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004109 status = PSA_ERROR_BAD_STATE;
4110 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004111 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004112
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 status = psa_driver_wrapper_cipher_update(operation,
4114 input,
4115 input_length,
4116 output,
4117 output_size,
4118 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004119
4120exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004121 if (status != PSA_SUCCESS) {
4122 psa_cipher_abort(operation);
4123 }
Ronald Cron6d051732020-10-01 14:10:20 +02004124
Gilles Peskine449bd832023-01-11 14:50:10 +01004125 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004126}
4127
Gilles Peskine449bd832023-01-11 14:50:10 +01004128psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4129 uint8_t *output,
4130 size_t output_size,
4131 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004132{
David Saadab4ecc272019-02-14 13:48:10 +02004133 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004134
Gilles Peskine449bd832023-01-11 14:50:10 +01004135 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004136 status = PSA_ERROR_BAD_STATE;
4137 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004138 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004139
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004141 status = PSA_ERROR_BAD_STATE;
4142 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004143 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004144
Gilles Peskine449bd832023-01-11 14:50:10 +01004145 status = psa_driver_wrapper_cipher_finish(operation,
4146 output,
4147 output_size,
4148 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004149
4150exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 if (status == PSA_SUCCESS) {
4152 return psa_cipher_abort(operation);
4153 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004154 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004155 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004156
Gilles Peskine449bd832023-01-11 14:50:10 +01004157 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004158 }
mohammad1603503973b2018-03-12 15:59:30 +02004159}
4160
Gilles Peskine449bd832023-01-11 14:50:10 +01004161psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004162{
Gilles Peskine449bd832023-01-11 14:50:10 +01004163 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004164 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004165 * in use. It's ok to call abort on such an object, and there's
4166 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004167 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004168 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004169
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004171
Ronald Cron49fafa92021-03-10 08:34:23 +01004172 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004173 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004174 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004175
Gilles Peskine449bd832023-01-11 14:50:10 +01004176 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004177}
4178
Gilles Peskine449bd832023-01-11 14:50:10 +01004179psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4180 psa_algorithm_t alg,
4181 const uint8_t *input,
4182 size_t input_length,
4183 uint8_t *output,
4184 size_t output_size,
4185 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004186{
4187 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004188 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004189 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004190 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4191 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004192
Gilles Peskine449bd832023-01-11 14:50:10 +01004193 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004194 status = PSA_ERROR_INVALID_ARGUMENT;
4195 goto exit;
4196 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004197
Gilles Peskine449bd832023-01-11 14:50:10 +01004198 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4199 PSA_KEY_USAGE_ENCRYPT,
4200 alg);
4201 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004202 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004204
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004205 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004206 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004207 };
4208
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4210 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004211 status = PSA_ERROR_GENERIC_ERROR;
4212 goto exit;
4213 }
4214
Gilles Peskine449bd832023-01-11 14:50:10 +01004215 if (default_iv_length > 0) {
4216 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004217 status = PSA_ERROR_BUFFER_TOO_SMALL;
4218 goto exit;
4219 }
4220
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 status = psa_generate_random(local_iv, default_iv_length);
4222 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004223 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004224 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004225 }
4226
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004227 status = psa_driver_wrapper_cipher_encrypt(
4228 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004229 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004230 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004232
4233exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 unlock_status = psa_unlock_key_slot(slot);
4235 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004236 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004237 }
Ronald Cron23919522021-07-08 19:00:07 +02004238
Gilles Peskine449bd832023-01-11 14:50:10 +01004239 if (status == PSA_SUCCESS) {
4240 if (default_iv_length > 0) {
4241 memcpy(output, local_iv, default_iv_length);
4242 }
4243 *output_length += default_iv_length;
4244 } else {
4245 *output_length = 0;
4246 }
4247
4248 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004249}
4250
Gilles Peskine449bd832023-01-11 14:50:10 +01004251psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4252 psa_algorithm_t alg,
4253 const uint8_t *input,
4254 size_t input_length,
4255 uint8_t *output,
4256 size_t output_size,
4257 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004258{
4259 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004260 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004261 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004262
Gilles Peskine449bd832023-01-11 14:50:10 +01004263 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004264 status = PSA_ERROR_INVALID_ARGUMENT;
4265 goto exit;
4266 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004267
Gilles Peskine449bd832023-01-11 14:50:10 +01004268 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4269 PSA_KEY_USAGE_DECRYPT,
4270 alg);
4271 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004272 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004273 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004274
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004275 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004277 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004278
Gilles Peskine449bd832023-01-11 14:50:10 +01004279 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4280 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004281 status = PSA_ERROR_INVALID_ARGUMENT;
4282 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004283 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004284 status = PSA_ERROR_INVALID_ARGUMENT;
4285 goto exit;
4286 }
4287
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004288 status = psa_driver_wrapper_cipher_decrypt(
4289 &attributes, slot->key.data, slot->key.bytes,
4290 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004291 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004292
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004293exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004294 unlock_status = psa_unlock_key_slot(slot);
4295 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004296 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004298
Gilles Peskine449bd832023-01-11 14:50:10 +01004299 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004300 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004301 }
Ronald Cron23919522021-07-08 19:00:07 +02004302
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004304}
4305
4306
mohammad16035955c982018-04-26 00:53:03 +03004307/****************************************************************/
4308/* AEAD */
4309/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004310
Paul Elliottbaff51c2021-09-28 17:44:45 +01004311/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004312static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004313{
Gilles Peskine449bd832023-01-11 14:50:10 +01004314 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004315}
4316
4317/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004318static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4319 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004320{
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004322
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004324#if defined(PSA_WANT_ALG_GCM)
4325 case PSA_ALG_GCM:
4326 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 * arbitrarily large nonces. Please note that we do not generally
4328 * recommend the usage of nonces of greater length than
4329 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4330 * size, which can then lead to collisions if you encrypt a very
4331 * large number of messages.*/
4332 if (nonce_length != 0) {
4333 return PSA_SUCCESS;
4334 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004335 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004336#endif /* PSA_WANT_ALG_GCM */
4337#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004338 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004339 if (nonce_length >= 7 && nonce_length <= 13) {
4340 return PSA_SUCCESS;
4341 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004342 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004343#endif /* PSA_WANT_ALG_CCM */
4344#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004345 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004346 if (nonce_length == 12) {
4347 return PSA_SUCCESS;
4348 } else if (nonce_length == 8) {
4349 return PSA_ERROR_NOT_SUPPORTED;
4350 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004351 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004352#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004353 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004354 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004355 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004356 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004357
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004359}
4360
Gilles Peskine449bd832023-01-11 14:50:10 +01004361static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004362{
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4364 return PSA_ERROR_INVALID_ARGUMENT;
4365 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004366
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004368}
4369
Gilles Peskine449bd832023-01-11 14:50:10 +01004370psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4371 psa_algorithm_t alg,
4372 const uint8_t *nonce,
4373 size_t nonce_length,
4374 const uint8_t *additional_data,
4375 size_t additional_data_length,
4376 const uint8_t *plaintext,
4377 size_t plaintext_length,
4378 uint8_t *ciphertext,
4379 size_t ciphertext_size,
4380 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004381{
Ronald Cron215633c2021-03-16 17:15:37 +01004382 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4383 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004384
mohammad1603f08a5502018-06-03 15:05:47 +03004385 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004386
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 status = psa_aead_check_algorithm(alg);
4388 if (status != PSA_SUCCESS) {
4389 return status;
4390 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004391
Ronald Cron9a986162021-03-26 12:40:07 +01004392 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004393 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4394 if (status != PSA_SUCCESS) {
4395 return status;
4396 }
mohammad16035955c982018-04-26 00:53:03 +03004397
Ronald Cron215633c2021-03-16 17:15:37 +01004398 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004400 };
mohammad16035955c982018-04-26 00:53:03 +03004401
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 status = psa_aead_check_nonce_length(alg, nonce_length);
4403 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004404 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004406
Ronald Cronde822812021-03-17 16:08:20 +01004407 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004408 &attributes, slot->key.data, slot->key.bytes,
4409 alg,
4410 nonce, nonce_length,
4411 additional_data, additional_data_length,
4412 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004413 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004414
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4416 memset(ciphertext, 0, ciphertext_size);
4417 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004418
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004419exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004421
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004423}
4424
Gilles Peskine449bd832023-01-11 14:50:10 +01004425psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4426 psa_algorithm_t alg,
4427 const uint8_t *nonce,
4428 size_t nonce_length,
4429 const uint8_t *additional_data,
4430 size_t additional_data_length,
4431 const uint8_t *ciphertext,
4432 size_t ciphertext_length,
4433 uint8_t *plaintext,
4434 size_t plaintext_size,
4435 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004436{
Ronald Cron215633c2021-03-16 17:15:37 +01004437 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4438 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004439
Gilles Peskineee652a32018-06-01 19:23:52 +02004440 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004441
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 status = psa_aead_check_algorithm(alg);
4443 if (status != PSA_SUCCESS) {
4444 return status;
4445 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004446
Ronald Cron9a986162021-03-26 12:40:07 +01004447 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4449 if (status != PSA_SUCCESS) {
4450 return status;
4451 }
mohammad16035955c982018-04-26 00:53:03 +03004452
Ronald Cron215633c2021-03-16 17:15:37 +01004453 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004455 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004456
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 status = psa_aead_check_nonce_length(alg, nonce_length);
4458 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004459 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004461
Ronald Cronde822812021-03-17 16:08:20 +01004462 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004463 &attributes, slot->key.data, slot->key.bytes,
4464 alg,
4465 nonce, nonce_length,
4466 additional_data, additional_data_length,
4467 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004469
Gilles Peskine449bd832023-01-11 14:50:10 +01004470 if (status != PSA_SUCCESS && plaintext_size != 0) {
4471 memset(plaintext, 0, plaintext_size);
4472 }
mohammad160360a64d02018-06-03 17:20:42 +03004473
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004474exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004475 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004476
Gilles Peskine449bd832023-01-11 14:50:10 +01004477 return status;
mohammad16035955c982018-04-26 00:53:03 +03004478}
4479
Gilles Peskine449bd832023-01-11 14:50:10 +01004480static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4481{
4482 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004483
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004485#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004486 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004487 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4489 return PSA_ERROR_INVALID_ARGUMENT;
4490 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004491 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004492#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004493
Przemek Stekiel86679c72022-10-06 17:06:56 +02004494#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004495 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004496 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4498 return PSA_ERROR_INVALID_ARGUMENT;
4499 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004500 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004501#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004502
Przemek Stekiel86679c72022-10-06 17:06:56 +02004503#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004505 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 if (tag_len != 16) {
4507 return PSA_ERROR_INVALID_ARGUMENT;
4508 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004509 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004510#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004511
4512 default:
4513 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004515 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004517}
4518
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004519/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004520static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4521 int is_encrypt,
4522 mbedtls_svc_key_id_t key,
4523 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004524{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004525 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4526 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4527 psa_key_slot_t *slot = NULL;
4528 psa_key_usage_t key_usage = 0;
4529
Gilles Peskine449bd832023-01-11 14:50:10 +01004530 status = psa_aead_check_algorithm(alg);
4531 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004532 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004533 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004534
Gilles Peskine449bd832023-01-11 14:50:10 +01004535 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004536 status = PSA_ERROR_BAD_STATE;
4537 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004538 }
4539
Gilles Peskine449bd832023-01-11 14:50:10 +01004540 if (operation->nonce_set || operation->lengths_set ||
4541 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004542 status = PSA_ERROR_BAD_STATE;
4543 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004544 }
4545
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004547 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004548 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004549 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004551
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4553 alg);
4554 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004555 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004557
4558 psa_key_attributes_t attributes = {
4559 .core = slot->attr
4560 };
4561
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004563 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004564 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004565
Gilles Peskine449bd832023-01-11 14:50:10 +01004566 if (is_encrypt) {
4567 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4568 &attributes,
4569 slot->key.data,
4570 slot->key.bytes,
4571 alg);
4572 } else {
4573 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4574 &attributes,
4575 slot->key.data,
4576 slot->key.bytes,
4577 alg);
4578 }
4579 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004580 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004582
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004584
4585exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004587
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004589 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004591 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 } else {
4593 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004594 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004595
Gilles Peskine449bd832023-01-11 14:50:10 +01004596 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004597}
4598
Paul Elliott302ff6b2021-04-20 18:10:30 +01004599/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004600psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4601 mbedtls_svc_key_id_t key,
4602 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004603{
Gilles Peskine449bd832023-01-11 14:50:10 +01004604 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004605}
4606
4607/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004608psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4609 mbedtls_svc_key_id_t key,
4610 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004611{
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004613}
4614
4615/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004616psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4617 uint8_t *nonce,
4618 size_t nonce_size,
4619 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004620{
4621 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004622 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004623 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004624
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004625 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004626
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004628 status = PSA_ERROR_BAD_STATE;
4629 goto exit;
4630 }
4631
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004633 status = PSA_ERROR_BAD_STATE;
4634 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004635 }
4636
Paul Elliotte193ea82021-10-01 13:00:16 +01004637 /* For CCM, this size may not be correct according to the PSA
4638 * specification. The PSA Crypto 1.0.1 specification states:
4639 *
4640 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4641 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4642 *
4643 * However this restriction that L has to be the smallest integer is not
4644 * applied in practice, and it is not implementable here since the
4645 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004646 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4647 operation->alg);
4648 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004649 status = PSA_ERROR_BUFFER_TOO_SMALL;
4650 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004651 }
4652
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 status = psa_generate_random(local_nonce, required_nonce_size);
4654 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004655 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004656 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004657
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004659
Paul Elliott39dc6b82021-05-11 19:16:09 +01004660exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 if (status == PSA_SUCCESS) {
4662 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004663 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 } else {
4665 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004666 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004667
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004669}
4670
4671/* Set the nonce for a multipart authenticated encryption or decryption
4672 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004673psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4674 const uint8_t *nonce,
4675 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004676{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004677 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4678
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004680 status = PSA_ERROR_BAD_STATE;
4681 goto exit;
4682 }
4683
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004685 status = PSA_ERROR_BAD_STATE;
4686 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004687 }
4688
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4690 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004691 status = PSA_ERROR_INVALID_ARGUMENT;
4692 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004693 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004694
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4696 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004697
Paul Elliott39dc6b82021-05-11 19:16:09 +01004698exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004699 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004700 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 } else {
4702 psa_aead_abort(operation);
4703 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004704
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004706}
4707
4708/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004709psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4710 size_t ad_length,
4711 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004712{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004713 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4714
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004716 status = PSA_ERROR_BAD_STATE;
4717 goto exit;
4718 }
4719
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 if (operation->lengths_set || operation->ad_started ||
4721 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004722 status = PSA_ERROR_BAD_STATE;
4723 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004724 }
4725
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004727#if defined(PSA_WANT_ALG_GCM)
4728 case PSA_ALG_GCM:
4729 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 * bits. Without the guard this code will generate warnings on 32bit
4731 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004732#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004733 if (((uint64_t) ad_length) >> 61 != 0 ||
4734 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004735 status = PSA_ERROR_INVALID_ARGUMENT;
4736 goto exit;
4737 }
Paul Elliott325d3742021-09-27 17:56:28 +01004738#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004739 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004740#endif /* PSA_WANT_ALG_GCM */
4741#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004742 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004743 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004744 status = PSA_ERROR_INVALID_ARGUMENT;
4745 goto exit;
4746 }
4747 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004748#endif /* PSA_WANT_ALG_CCM */
4749#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004750 case PSA_ALG_CHACHA20_POLY1305:
4751 /* No length restrictions for ChaChaPoly. */
4752 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004753#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004754 default:
4755 break;
4756 }
Paul Elliott325d3742021-09-27 17:56:28 +01004757
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4759 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004760
Paul Elliott39dc6b82021-05-11 19:16:09 +01004761exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004763 operation->ad_remaining = ad_length;
4764 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004765 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004766 } else {
4767 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004768 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004769
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004771}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004772
4773/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004774psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4775 const uint8_t *input,
4776 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004777{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004778 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4779
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004781 status = PSA_ERROR_BAD_STATE;
4782 goto exit;
4783 }
4784
Gilles Peskine449bd832023-01-11 14:50:10 +01004785 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004786 status = PSA_ERROR_BAD_STATE;
4787 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004788 }
4789
Gilles Peskine449bd832023-01-11 14:50:10 +01004790 if (operation->lengths_set) {
4791 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004792 status = PSA_ERROR_INVALID_ARGUMENT;
4793 goto exit;
4794 }
4795
4796 operation->ad_remaining -= input_length;
4797 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004798#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004800 status = PSA_ERROR_BAD_STATE;
4801 goto exit;
4802 }
4803#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004804
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 status = psa_driver_wrapper_aead_update_ad(operation, input,
4806 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004807
Paul Elliott39dc6b82021-05-11 19:16:09 +01004808exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004810 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004811 } else {
4812 psa_aead_abort(operation);
4813 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004814
Gilles Peskine449bd832023-01-11 14:50:10 +01004815 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004816}
4817
4818/* Encrypt or decrypt a message fragment in an active multipart AEAD
4819 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004820psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4821 const uint8_t *input,
4822 size_t input_length,
4823 uint8_t *output,
4824 size_t output_size,
4825 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004826{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004827 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004828
4829 *output_length = 0;
4830
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004832 status = PSA_ERROR_BAD_STATE;
4833 goto exit;
4834 }
4835
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004837 status = PSA_ERROR_BAD_STATE;
4838 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004839 }
4840
Gilles Peskine449bd832023-01-11 14:50:10 +01004841 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004842 /* Additional data length was supplied, but not all the additional
4843 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004845 status = PSA_ERROR_INVALID_ARGUMENT;
4846 goto exit;
4847 }
4848
4849 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004851 status = PSA_ERROR_INVALID_ARGUMENT;
4852 goto exit;
4853 }
4854
4855 operation->body_remaining -= input_length;
4856 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004857#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004858 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004859 status = PSA_ERROR_BAD_STATE;
4860 goto exit;
4861 }
4862#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004863
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4865 output, output_size,
4866 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004867
Paul Elliott39dc6b82021-05-11 19:16:09 +01004868exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004870 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 } else {
4872 psa_aead_abort(operation);
4873 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004874
Gilles Peskine449bd832023-01-11 14:50:10 +01004875 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004876}
4877
Gilles Peskine449bd832023-01-11 14:50:10 +01004878static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004879{
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 if (operation->id == 0 || !operation->nonce_set) {
4881 return PSA_ERROR_BAD_STATE;
4882 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004883
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4885 operation->body_remaining != 0)) {
4886 return PSA_ERROR_INVALID_ARGUMENT;
4887 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004888
Gilles Peskine449bd832023-01-11 14:50:10 +01004889 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004890}
4891
Paul Elliott302ff6b2021-04-20 18:10:30 +01004892/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004893psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4894 uint8_t *ciphertext,
4895 size_t ciphertext_size,
4896 size_t *ciphertext_length,
4897 uint8_t *tag,
4898 size_t tag_size,
4899 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004900{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004901 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4902
Paul Elliott302ff6b2021-04-20 18:10:30 +01004903 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004904 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004905
Gilles Peskine449bd832023-01-11 14:50:10 +01004906 status = psa_aead_final_checks(operation);
4907 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004908 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004909 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004910
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004912 status = PSA_ERROR_BAD_STATE;
4913 goto exit;
4914 }
4915
Gilles Peskine449bd832023-01-11 14:50:10 +01004916 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4917 ciphertext_size,
4918 ciphertext_length,
4919 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004920
Paul Elliott39dc6b82021-05-11 19:16:09 +01004921exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004922
4923
Paul Elliottf47b0952021-05-21 18:02:33 +01004924 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004925 * the zero tag size, make sure the tag is set to something implausible.
4926 * Even if the operation succeeds, make sure we clear the rest of the
4927 * buffer to prevent potential leakage of anything previously placed in
4928 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004929 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004930
Gilles Peskine449bd832023-01-11 14:50:10 +01004931 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004932
Gilles Peskine449bd832023-01-11 14:50:10 +01004933 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004934}
4935
4936/* Finish authenticating and decrypting a message in a multipart AEAD
4937 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004938psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4939 uint8_t *plaintext,
4940 size_t plaintext_size,
4941 size_t *plaintext_length,
4942 const uint8_t *tag,
4943 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004944{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004945 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4946
Paul Elliott302ff6b2021-04-20 18:10:30 +01004947 *plaintext_length = 0;
4948
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 status = psa_aead_final_checks(operation);
4950 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004951 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004952 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004953
Gilles Peskine449bd832023-01-11 14:50:10 +01004954 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004955 status = PSA_ERROR_BAD_STATE;
4956 goto exit;
4957 }
4958
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4960 plaintext_size,
4961 plaintext_length,
4962 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004963
4964exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004966
Gilles Peskine449bd832023-01-11 14:50:10 +01004967 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004968}
4969
4970/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004971psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004972{
4973 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4974
Gilles Peskine449bd832023-01-11 14:50:10 +01004975 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004976 /* The object has (apparently) been initialized but it is not (yet)
4977 * in use. It's ok to call abort on such an object, and there's
4978 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004979 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004980 }
4981
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004983
Gilles Peskine449bd832023-01-11 14:50:10 +01004984 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004985
Gilles Peskine449bd832023-01-11 14:50:10 +01004986 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004987}
4988
Gilles Peskinee59236f2018-01-27 23:32:46 +01004989/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004990/* Generators */
4991/****************************************************************/
4992
Przemek Stekiel69c46792022-06-10 12:59:51 +02004993#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004994 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004995 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
4996 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08004997#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004998#endif /* At least one builtin KDF */
4999
Przemek Stekiel69c46792022-06-10 12:59:51 +02005000#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005001 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5002 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5003static psa_status_t psa_key_derivation_start_hmac(
5004 psa_mac_operation_t *operation,
5005 psa_algorithm_t hash_alg,
5006 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005007 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005008{
5009 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5010 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005011 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5012 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5013 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005014
Steven Cooreman72f736a2021-05-07 14:14:37 +02005015 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005016 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005017
Gilles Peskine449bd832023-01-11 14:50:10 +01005018 status = psa_driver_wrapper_mac_sign_setup(operation,
5019 &attributes,
5020 hmac_key, hmac_key_length,
5021 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005022
Gilles Peskine449bd832023-01-11 14:50:10 +01005023 psa_reset_key_attributes(&attributes);
5024 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005025}
5026#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005027
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005028#define HKDF_STATE_INIT 0 /* no input yet */
5029#define HKDF_STATE_STARTED 1 /* got salt */
5030#define HKDF_STATE_KEYED 2 /* got key */
5031#define HKDF_STATE_OUTPUT 3 /* output started */
5032
Gilles Peskinecbe66502019-05-16 16:59:18 +02005033static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005034 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005035{
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5037 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5038 } else {
5039 return operation->alg;
5040 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005041}
5042
Gilles Peskine449bd832023-01-11 14:50:10 +01005043psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005044{
5045 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5047 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005048 /* The object has (apparently) been initialized but it is not
5049 * in use. It's ok to call abort on such an object, and there's
5050 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005052#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005053 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5054 mbedtls_free(operation->ctx.hkdf.info);
5055 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5056 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005057#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005058#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5059 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5061 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5062 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5063 if (operation->ctx.tls12_prf.secret != NULL) {
5064 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5065 operation->ctx.tls12_prf.secret_length);
5066 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005067 }
5068
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 if (operation->ctx.tls12_prf.seed != NULL) {
5070 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5071 operation->ctx.tls12_prf.seed_length);
5072 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005073 }
5074
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 if (operation->ctx.tls12_prf.label != NULL) {
5076 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5077 operation->ctx.tls12_prf.label_length);
5078 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005079 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005080#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 if (operation->ctx.tls12_prf.other_secret != NULL) {
5082 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5083 operation->ctx.tls12_prf.other_secret_length);
5084 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005085 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005086#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005087 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005088
5089 /* We leave the fields Ai and output_block to be erased safely by the
5090 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005092#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5093 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005094#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5096 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5097 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5098 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005099#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005100 {
5101 status = PSA_ERROR_BAD_STATE;
5102 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005103 mbedtls_platform_zeroize(operation, sizeof(*operation));
5104 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005105}
5106
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005107psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005108 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005109{
Gilles Peskine449bd832023-01-11 14:50:10 +01005110 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005111 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005112 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005113 }
5114
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005115 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005117}
5118
Gilles Peskine449bd832023-01-11 14:50:10 +01005119psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5120 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005121{
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 if (operation->alg == 0) {
5123 return PSA_ERROR_BAD_STATE;
5124 }
5125 if (capacity > operation->capacity) {
5126 return PSA_ERROR_INVALID_ARGUMENT;
5127 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005128 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005130}
5131
Przemek Stekiel69c46792022-06-10 12:59:51 +02005132#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005133/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005134static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5135 psa_algorithm_t kdf_alg,
5136 uint8_t *output,
5137 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005138{
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5140 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005141 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005142 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005143#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005145#else
5146 const uint8_t last_block = 0xff;
5147#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005148
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 if (hkdf->state < HKDF_STATE_KEYED ||
5150 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005151#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005153#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 )) {
5155 return PSA_ERROR_BAD_STATE;
5156 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005157 hkdf->state = HKDF_STATE_OUTPUT;
5158
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005160 /* Copy what remains of the current block */
5161 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005163 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 }
5165 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005166 output += n;
5167 output_length -= n;
5168 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005170 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005172 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005173 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005174 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005175 * object was corrupted or if this function is called directly
5176 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 if (hkdf->block_number == last_block) {
5178 return PSA_ERROR_BAD_STATE;
5179 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005180
5181 /* We need a new block */
5182 ++hkdf->block_number;
5183 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005184
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5186 hash_alg,
5187 hkdf->prk,
5188 hash_length);
5189 if (status != PSA_SUCCESS) {
5190 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005191 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005192
5193 if (hkdf->block_number != 1) {
5194 status = psa_mac_update(&hkdf->hmac,
5195 hkdf->output_block,
5196 hash_length);
5197 if (status != PSA_SUCCESS) {
5198 return status;
5199 }
5200 }
5201 status = psa_mac_update(&hkdf->hmac,
5202 hkdf->info,
5203 hkdf->info_length);
5204 if (status != PSA_SUCCESS) {
5205 return status;
5206 }
5207 status = psa_mac_update(&hkdf->hmac,
5208 &hkdf->block_number, 1);
5209 if (status != PSA_SUCCESS) {
5210 return status;
5211 }
5212 status = psa_mac_sign_finish(&hkdf->hmac,
5213 hkdf->output_block,
5214 sizeof(hkdf->output_block),
5215 &hmac_output_length);
5216 if (status != PSA_SUCCESS) {
5217 return status;
5218 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005219 }
5220
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005222}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005223#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005224
John Durkop07cc04a2020-11-16 22:08:34 -08005225#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5226 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005227static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5228 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005230{
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5232 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005233 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5234 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005235 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005236
Janos Follath7742fee2019-06-17 12:58:10 +01005237 /* We can't be wanting more output after block 0xff, otherwise
5238 * the capacity check in psa_key_derivation_output_bytes() would have
5239 * prevented this call. It could happen only if the operation
5240 * object was corrupted or if this function is called directly
5241 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 if (tls12_prf->block_number == 0xff) {
5243 return PSA_ERROR_CORRUPTION_DETECTED;
5244 }
Janos Follath7742fee2019-06-17 12:58:10 +01005245
5246 /* We need a new block */
5247 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005248 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005249
5250 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5251 *
5252 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5253 *
5254 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5255 * HMAC_hash(secret, A(2) + seed) +
5256 * HMAC_hash(secret, A(3) + seed) + ...
5257 *
5258 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005259 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005260 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005261 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005262 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005263 * is currently extracted as `output_block` and where i is
5264 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005265 */
5266
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 status = psa_key_derivation_start_hmac(&hmac,
5268 hash_alg,
5269 tls12_prf->secret,
5270 tls12_prf->secret_length);
5271 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005272 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005274
5275 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005277 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5278 * the variable seed and in this instance means it in the context of the
5279 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005280 status = psa_mac_update(&hmac,
5281 tls12_prf->label,
5282 tls12_prf->label_length);
5283 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005284 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 }
5286 status = psa_mac_update(&hmac,
5287 tls12_prf->seed,
5288 tls12_prf->seed_length);
5289 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005290 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 }
5292 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005293 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5295 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005296 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005298 }
5299
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 status = psa_mac_sign_finish(&hmac,
5301 tls12_prf->Ai, hash_length,
5302 &hmac_output_length);
5303 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005304 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005305 }
5306 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005307 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005309
5310 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 status = psa_key_derivation_start_hmac(&hmac,
5312 hash_alg,
5313 tls12_prf->secret,
5314 tls12_prf->secret_length);
5315 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005316 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 }
5318 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5319 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005320 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005321 }
5322 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5323 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005324 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 }
5326 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5327 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005328 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 }
5330 status = psa_mac_sign_finish(&hmac,
5331 tls12_prf->output_block, hash_length,
5332 &hmac_output_length);
5333 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005334 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005336
Janos Follath7742fee2019-06-17 12:58:10 +01005337
5338cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 cleanup_status = psa_mac_abort(&hmac);
5340 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005341 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005343
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005345}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005346
Janos Follath844eb0e2019-06-19 12:10:49 +01005347static psa_status_t psa_key_derivation_tls12_prf_read(
5348 psa_tls12_prf_key_derivation_t *tls12_prf,
5349 psa_algorithm_t alg,
5350 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005352{
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5354 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005355 psa_status_t status;
5356 uint8_t offset, length;
5357
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005359 case PSA_TLS12_PRF_STATE_LABEL_SET:
5360 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5361 break;
5362 case PSA_TLS12_PRF_STATE_OUTPUT:
5363 break;
5364 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005366 }
5367
Gilles Peskine449bd832023-01-11 14:50:10 +01005368 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005369 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005370 if (tls12_prf->left_in_block == 0) {
5371 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5372 alg);
5373 if (status != PSA_SUCCESS) {
5374 return status;
5375 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005376
5377 continue;
5378 }
5379
Gilles Peskine449bd832023-01-11 14:50:10 +01005380 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005381 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005383 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005385
5386 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005388 output += length;
5389 output_length -= length;
5390 tls12_prf->left_in_block -= length;
5391 }
5392
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005394}
John Durkop07cc04a2020-11-16 22:08:34 -08005395#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5396 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005397
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005398#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5399static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5400 psa_tls12_ecjpake_to_pms_t *ecjpake,
5401 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005403{
5404 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005405 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005406
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 if (output_length != 32) {
5408 return PSA_ERROR_INVALID_ARGUMENT;
5409 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005410
Gilles Peskine449bd832023-01-11 14:50:10 +01005411 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5412 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5413 &output_size);
5414 if (status != PSA_SUCCESS) {
5415 return status;
5416 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005417
Gilles Peskine449bd832023-01-11 14:50:10 +01005418 if (output_size != output_length) {
5419 return PSA_ERROR_GENERIC_ERROR;
5420 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005421
Gilles Peskine449bd832023-01-11 14:50:10 +01005422 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005423}
5424#endif
5425
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005426psa_status_t psa_key_derivation_output_bytes(
5427 psa_key_derivation_operation_t *operation,
5428 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005430{
5431 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005433
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005435 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005436 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005437 }
5438
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005440 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005441 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005442 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005443 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005444 goto exit;
5445 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005447 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005448 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005449 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5450 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005451 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005452 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005454 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005455 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005456
Przemek Stekiel69c46792022-06-10 12:59:51 +02005457#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5459 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5460 output, output_length);
5461 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005462#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005463#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5464 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5466 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5467 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5468 kdf_alg, output,
5469 output_length);
5470 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005471#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5472 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005473#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005475 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5477 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005478#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5479
Gilles Peskineeab56e42018-07-12 17:12:33 +02005480 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005481 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005483 }
5484
5485exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005487 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005488 * This allows us to differentiate between exhausted operations and
5489 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5490 * operations. */
5491 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005493 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005494 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005495 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005497}
5498
David Brown7807bf72021-02-09 16:28:23 -07005499#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005500static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005501{
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 if (data_size >= 8) {
5503 mbedtls_des_key_set_parity(data);
5504 }
5505 if (data_size >= 16) {
5506 mbedtls_des_key_set_parity(data + 8);
5507 }
5508 if (data_size >= 24) {
5509 mbedtls_des_key_set_parity(data + 16);
5510 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005511}
David Brown7807bf72021-02-09 16:28:23 -07005512#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005513
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005514/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 * ECC keys on a Weierstrass elliptic curve require the generation
5516 * of a private key which is an integer
5517 * in the range [1, N - 1], where N is the boundary of the private key domain:
5518 * N is the prime p for Diffie-Hellman, or the order of the
5519 * curve’s base point for ECC.
5520 *
5521 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5522 * This function generates the private key using the following process:
5523 *
5524 * 1. Draw a byte string of length ceiling(m/8) bytes.
5525 * 2. If m is not a multiple of 8, set the most significant
5526 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5527 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5528 * 4. If k > N - 2, discard the result and return to step 1.
5529 * 5. Output k + 1 as the private key.
5530 *
5531 * This method allows compliance to NIST standards, specifically the methods titled
5532 * Key-Pair Generation by Testing Candidates in the following publications:
5533 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5534 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5535 * Diffie-Hellman keys.
5536 *
5537 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5538 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5539 *
5540 * Note: Function allocates memory for *data buffer, so given *data should be
5541 * always NULL.
5542 */
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005543#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5544 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005545 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5546 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5547 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005548static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005549 psa_key_slot_t *slot,
5550 size_t bits,
5551 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005552 uint8_t **data
5553 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005554{
Valerio Setti52789862023-04-07 12:13:11 +02005555#if defined(MBEDTLS_ECP_LIGHT)
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005556 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005557 mbedtls_mpi k;
5558 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005559 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005560 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005561
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 mbedtls_mpi_init(&k);
5563 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005564
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005565 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005567 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005569
Gilles Peskine449bd832023-01-11 14:50:10 +01005570 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005571 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005572 goto cleanup;
5573 }
5574
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005575 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005577
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005579
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005580 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005581 /* Let m be the bit size of N. */
5582 size_t m = ecp_group.nbits;
5583
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005585
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005586 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005588
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005589 /* Note: This function is always called with *data == NULL and it
5590 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005591 *data = mbedtls_calloc(1, m_bytes);
5592 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005593 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005594 goto cleanup;
5595 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005596
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005598 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005599 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005600 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005602
5603 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005605 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005606 * (8 * ceiling(m/8) - m) bits of the first byte in
5607 * the string to zero.
5608 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005609 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005610 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005611 }
5612
5613 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005614 * big-endian byte string.
5615 */
5616 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005617
5618 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 * Result of comparison is returned. When it indicates error
5620 * then this function is called again.
5621 */
5622 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005623 }
5624
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005625 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005626 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5627 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005628cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 if (ret != 0) {
5630 status = mbedtls_to_psa_error(ret);
5631 }
5632 if (status != PSA_SUCCESS) {
5633 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005634 *data = NULL;
5635 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005636 mbedtls_mpi_free(&k);
5637 mbedtls_mpi_free(&diff_N_2);
5638 return status;
Valerio Setti52789862023-04-07 12:13:11 +02005639#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005640 (void) slot;
5641 (void) bits;
5642 (void) operation;
5643 (void) data;
5644 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti52789862023-04-07 12:13:11 +02005645#endif /* MBEDTLS_ECP_LIGHT */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005646}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005647
5648/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5649 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5650 *
5651 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5652 * draw a 32-byte string and process it as specified in
5653 * Elliptic Curves for Security [RFC7748] §5.
5654 *
5655 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5656 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5657 *
5658 * Note: Function allocates memory for *data buffer, so given *data should be
5659 * always NULL.
5660 */
5661
5662static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5663 size_t bits,
5664 psa_key_derivation_operation_t *operation,
5665 uint8_t **data
5666 )
5667{
5668 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005669 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005670
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005672 case 255:
5673 output_length = 32;
5674 break;
5675 case 448:
5676 output_length = 56;
5677 break;
5678 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005680 break;
5681 }
5682
Gilles Peskine449bd832023-01-11 14:50:10 +01005683 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005684
Gilles Peskine449bd832023-01-11 14:50:10 +01005685 if (*data == NULL) {
5686 return PSA_ERROR_INSUFFICIENT_MEMORY;
5687 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005688
Gilles Peskine449bd832023-01-11 14:50:10 +01005689 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005690
Gilles Peskine449bd832023-01-11 14:50:10 +01005691 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005692 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005693 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005694
Gilles Peskine449bd832023-01-11 14:50:10 +01005695 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005696 case 255:
5697 (*data)[0] &= 248;
5698 (*data)[31] &= 127;
5699 (*data)[31] |= 64;
5700 break;
5701 case 448:
5702 (*data)[0] &= 252;
5703 (*data)[55] |= 128;
5704 break;
5705 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005706 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005707 break;
5708 }
5709
5710 return status;
5711}
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005712#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5713 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005714 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5715 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5716 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005717
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005718static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005719 psa_key_slot_t *slot,
5720 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005722{
5723 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305725 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005726 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005727
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5729 return PSA_ERROR_INVALID_ARGUMENT;
5730 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005731
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005732#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5733 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005734 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5735 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5736 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5738 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5739 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005740 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005741 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5742 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005743 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 }
5745 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005746 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5748 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005749 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005750 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005751 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005752 } else
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005753#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5754 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005755 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5756 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5757 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 if (key_type_is_raw_bytes(slot->attr.type)) {
5759 if (bits % 8 != 0) {
5760 return PSA_ERROR_INVALID_ARGUMENT;
5761 }
5762 data = mbedtls_calloc(1, bytes);
5763 if (data == NULL) {
5764 return PSA_ERROR_INSUFFICIENT_MEMORY;
5765 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005766
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 status = psa_key_derivation_output_bytes(operation, data, bytes);
5768 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005769 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 }
David Brown12ca5032021-02-11 11:02:00 -07005771#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005772 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5773 psa_des_set_key_parity(data, bytes);
5774 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005775#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005776 } else {
5777 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005778 }
Ronald Crondd04d422020-11-28 15:14:42 +01005779
Ronald Cronbf33c932020-11-28 18:06:53 +01005780 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005781 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005783 };
5784
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5786 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5787 &storage_size);
5788 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305789 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 }
Archanad8a83dc2021-06-14 10:04:16 +05305791 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 status = psa_allocate_buffer_to_slot(slot, storage_size);
5793 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305794 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005795 }
Archanad8a83dc2021-06-14 10:04:16 +05305796
Gilles Peskine449bd832023-01-11 14:50:10 +01005797 status = psa_driver_wrapper_import_key(&attributes,
5798 data, bytes,
5799 slot->key.data,
5800 slot->key.bytes,
5801 &slot->key.bytes, &bits);
5802 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005803 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005804 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005805
5806exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 mbedtls_free(data);
5808 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005809}
5810
Gilles Peskine449bd832023-01-11 14:50:10 +01005811psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5812 psa_key_derivation_operation_t *operation,
5813 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005814{
5815 psa_status_t status;
5816 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005817 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005818
Ronald Cron81709fc2020-11-14 12:10:32 +01005819 *key = MBEDTLS_SVC_KEY_ID_INIT;
5820
Gilles Peskine0f84d622019-09-12 19:03:13 +02005821 /* Reject any attempt to create a zero-length key so that we don't
5822 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005823 if (psa_get_key_bits(attributes) == 0) {
5824 return PSA_ERROR_INVALID_ARGUMENT;
5825 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005826
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 if (operation->alg == PSA_ALG_NONE) {
5828 return PSA_ERROR_BAD_STATE;
5829 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005830
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 if (!operation->can_output_key) {
5832 return PSA_ERROR_NOT_PERMITTED;
5833 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005834
Gilles Peskine449bd832023-01-11 14:50:10 +01005835 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5836 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005837#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005838 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005839 /* Deriving a key in a secure element is not implemented yet. */
5840 status = PSA_ERROR_NOT_SUPPORTED;
5841 }
5842#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 if (status == PSA_SUCCESS) {
5844 status = psa_generate_derived_key_internal(slot,
5845 attributes->core.bits,
5846 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005847 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 if (status == PSA_SUCCESS) {
5849 status = psa_finish_key_creation(slot, driver, key);
5850 }
5851 if (status != PSA_SUCCESS) {
5852 psa_fail_key_creation(slot, driver);
5853 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005854
Gilles Peskine449bd832023-01-11 14:50:10 +01005855 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005856}
5857
Gilles Peskineeab56e42018-07-12 17:12:33 +02005858
5859
5860/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005861/* Key derivation */
5862/****************************************************************/
5863
Steven Cooreman932ffb72021-02-15 12:14:32 +01005864#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005865static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005866{
5867#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5869 return 1;
5870 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005871#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005872#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5874 return 1;
5875 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005876#endif
5877#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5879 return 1;
5880 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005881#endif
5882#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5884 return 1;
5885 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005886#endif
5887#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005888 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5889 return 1;
5890 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005891#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005892#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5894 return 1;
5895 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005896#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005897 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005898}
5899
Gilles Peskine449bd832023-01-11 14:50:10 +01005900static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005901{
5902 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005903 psa_status_t status = psa_hash_setup(&operation, alg);
5904 psa_hash_abort(&operation);
5905 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005906}
5907
Gilles Peskine969c5d62019-01-16 15:53:06 +01005908static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005909 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005910 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005911{
Janos Follath5fe19732019-06-20 15:09:30 +01005912 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5913 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005914 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005915
Gilles Peskine969c5d62019-01-16 15:53:06 +01005916 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005917 if (!is_kdf_alg_supported(kdf_alg)) {
5918 return PSA_ERROR_NOT_SUPPORTED;
5919 }
John Durkop07cc04a2020-11-16 22:08:34 -08005920
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005921 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005922 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005923 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5924 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5925 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5926 if (hash_size == 0) {
5927 return PSA_ERROR_NOT_SUPPORTED;
5928 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005929
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005930 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5931 * we might fail later, which is somewhat unfriendly and potentially
5932 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 psa_status_t status = psa_hash_try_support(hash_alg);
5934 if (status != PSA_SUCCESS) {
5935 return status;
5936 }
5937 } else {
5938 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005939 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005940
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5942 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
5943 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
5944 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005945 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005946#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005947 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005948 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
5949 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005950 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005952#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5953 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 operation->capacity = 255 * hash_size;
5955 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005956}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005957
Gilles Peskine449bd832023-01-11 14:50:10 +01005958static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005959{
5960#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005961 if (alg == PSA_ALG_ECDH) {
5962 return PSA_SUCCESS;
5963 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005964#endif
5965 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005966 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005967}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005968
5969static int psa_key_derivation_allows_free_form_secret_input(
5970 psa_algorithm_t kdf_alg)
5971{
5972#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
5973 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5974 return 0;
5975 }
5976#endif
5977 (void) kdf_alg;
5978 return 1;
5979}
John Durkop07cc04a2020-11-16 22:08:34 -08005980#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005981
Gilles Peskine449bd832023-01-11 14:50:10 +01005982psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
5983 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005984{
5985 psa_status_t status;
5986
Gilles Peskine449bd832023-01-11 14:50:10 +01005987 if (operation->alg != 0) {
5988 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005989 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005990
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
5992 return PSA_ERROR_INVALID_ARGUMENT;
5993 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
5994#if defined(AT_LEAST_ONE_BUILTIN_KDF)
5995 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
5996 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
5997 status = psa_key_agreement_try_support(ka_alg);
5998 if (status != PSA_SUCCESS) {
5999 return status;
6000 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006001 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6002 return PSA_ERROR_INVALID_ARGUMENT;
6003 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6005#else
6006 return PSA_ERROR_NOT_SUPPORTED;
6007#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6008 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6009#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6010 status = psa_key_derivation_setup_kdf(operation, alg);
6011#else
6012 return PSA_ERROR_NOT_SUPPORTED;
6013#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6014 } else {
6015 return PSA_ERROR_INVALID_ARGUMENT;
6016 }
6017
6018 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006019 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 }
6021 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006022}
6023
Przemek Stekiel69c46792022-06-10 12:59:51 +02006024#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006025static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6026 psa_algorithm_t kdf_alg,
6027 psa_key_derivation_step_t step,
6028 const uint8_t *data,
6029 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006030{
Gilles Peskine449bd832023-01-11 14:50:10 +01006031 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006032 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006034 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006035#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6037 return PSA_ERROR_INVALID_ARGUMENT;
6038 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006039#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006040 if (hkdf->state != HKDF_STATE_INIT) {
6041 return PSA_ERROR_BAD_STATE;
6042 } else {
6043 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6044 hash_alg,
6045 data, data_length);
6046 if (status != PSA_SUCCESS) {
6047 return status;
6048 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006049 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006050 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006051 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006052 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006053#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006055 /* We shouldn't be in different state as HKDF_EXPAND only allows
6056 * two inputs: SECRET (this case) and INFO which does not modify
6057 * the state. It could happen only if the hkdf
6058 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006059 if (hkdf->state != HKDF_STATE_INIT) {
6060 return PSA_ERROR_BAD_STATE;
6061 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006062
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006063 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6065 return PSA_ERROR_INVALID_ARGUMENT;
6066 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006067
Gilles Peskine449bd832023-01-11 14:50:10 +01006068 memcpy(hkdf->prk, data, data_length);
6069 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006070#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006071 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006072 /* HKDF: If no salt was provided, use an empty salt.
6073 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006075#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6077 return PSA_ERROR_BAD_STATE;
6078 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006079#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6081 hash_alg,
6082 NULL, 0);
6083 if (status != PSA_SUCCESS) {
6084 return status;
6085 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006086 hkdf->state = HKDF_STATE_STARTED;
6087 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 if (hkdf->state != HKDF_STATE_STARTED) {
6089 return PSA_ERROR_BAD_STATE;
6090 }
6091 status = psa_mac_update(&hkdf->hmac,
6092 data, data_length);
6093 if (status != PSA_SUCCESS) {
6094 return status;
6095 }
6096 status = psa_mac_sign_finish(&hkdf->hmac,
6097 hkdf->prk,
6098 sizeof(hkdf->prk),
6099 &data_length);
6100 if (status != PSA_SUCCESS) {
6101 return status;
6102 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006103 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006104
Gilles Peskine2b522db2019-04-12 15:11:49 +02006105 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006106 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006107#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006109 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006111 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006113#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006114 {
6115 /* Block 0 is empty, and the next block will be
6116 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006118 }
6119
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006121 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006122#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6124 return PSA_ERROR_INVALID_ARGUMENT;
6125 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006126#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006127#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006128 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6129 hkdf->state == HKDF_STATE_INIT) {
6130 return PSA_ERROR_BAD_STATE;
6131 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006132#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 if (hkdf->state == HKDF_STATE_OUTPUT) {
6134 return PSA_ERROR_BAD_STATE;
6135 }
6136 if (hkdf->info_set) {
6137 return PSA_ERROR_BAD_STATE;
6138 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006139 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006140 if (data_length != 0) {
6141 hkdf->info = mbedtls_calloc(1, data_length);
6142 if (hkdf->info == NULL) {
6143 return PSA_ERROR_INSUFFICIENT_MEMORY;
6144 }
6145 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006146 }
6147 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006148 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006149 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006151 }
6152}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006153#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006154
John Durkop07cc04a2020-11-16 22:08:34 -08006155#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6156 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006157static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6158 const uint8_t *data,
6159 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006160{
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6162 return PSA_ERROR_BAD_STATE;
6163 }
Janos Follathf08e2652019-06-13 09:05:41 +01006164
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 if (data_length != 0) {
6166 prf->seed = mbedtls_calloc(1, data_length);
6167 if (prf->seed == NULL) {
6168 return PSA_ERROR_INSUFFICIENT_MEMORY;
6169 }
Janos Follathf08e2652019-06-13 09:05:41 +01006170
Gilles Peskine449bd832023-01-11 14:50:10 +01006171 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006172 prf->seed_length = data_length;
6173 }
Janos Follathf08e2652019-06-13 09:05:41 +01006174
Gilles Peskine43f958b2020-12-13 14:55:14 +01006175 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006176
Gilles Peskine449bd832023-01-11 14:50:10 +01006177 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006178}
6179
Gilles Peskine449bd832023-01-11 14:50:10 +01006180static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6181 const uint8_t *data,
6182 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006183{
Gilles Peskine449bd832023-01-11 14:50:10 +01006184 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6185 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6186 return PSA_ERROR_BAD_STATE;
6187 }
Janos Follath81550542019-06-13 14:26:34 +01006188
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 if (data_length != 0) {
6190 prf->secret = mbedtls_calloc(1, data_length);
6191 if (prf->secret == NULL) {
6192 return PSA_ERROR_INSUFFICIENT_MEMORY;
6193 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006194
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006196 prf->secret_length = data_length;
6197 }
Janos Follath81550542019-06-13 14:26:34 +01006198
Gilles Peskine43f958b2020-12-13 14:55:14 +01006199 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006200
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006202}
6203
Gilles Peskine449bd832023-01-11 14:50:10 +01006204static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6205 const uint8_t *data,
6206 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006207{
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6209 return PSA_ERROR_BAD_STATE;
6210 }
Janos Follath63028dd2019-06-13 09:15:47 +01006211
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 if (data_length != 0) {
6213 prf->label = mbedtls_calloc(1, data_length);
6214 if (prf->label == NULL) {
6215 return PSA_ERROR_INSUFFICIENT_MEMORY;
6216 }
Janos Follath63028dd2019-06-13 09:15:47 +01006217
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006219 prf->label_length = data_length;
6220 }
Janos Follath63028dd2019-06-13 09:15:47 +01006221
Gilles Peskine43f958b2020-12-13 14:55:14 +01006222 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006223
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006225}
6226
Gilles Peskine449bd832023-01-11 14:50:10 +01006227static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6228 psa_key_derivation_step_t step,
6229 const uint8_t *data,
6230 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006231{
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006233 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006235 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006236 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006237 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006239 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006240 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006241 }
6242}
John Durkop07cc04a2020-11-16 22:08:34 -08006243#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6244 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6245
6246#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6247static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6248 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006249 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006251{
6252 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6254 4 + data_length + prf->other_secret_length :
6255 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006256
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6258 return PSA_ERROR_INVALID_ARGUMENT;
6259 }
John Durkop07cc04a2020-11-16 22:08:34 -08006260
Gilles Peskine449bd832023-01-11 14:50:10 +01006261 uint8_t *pms = mbedtls_calloc(1, pms_len);
6262 if (pms == NULL) {
6263 return PSA_ERROR_INSUFFICIENT_MEMORY;
6264 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006265 uint8_t *cur = pms;
6266
6267 /* pure-PSK:
6268 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006269 *
6270 * The premaster secret is formed as follows: if the PSK is N octets
6271 * long, concatenate a uint16 with the value N, N zero octets, a second
6272 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006273 *
6274 * mixed-PSK:
6275 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6276 * follows: concatenate a uint16 with the length of the other secret,
6277 * the other secret itself, uint16 with the length of PSK, and the
6278 * PSK itself.
6279 * For details please check:
6280 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6281 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6282 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006283 */
6284
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6286 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6287 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6288 if (prf->other_secret_length != 0) {
6289 memcpy(cur, prf->other_secret, prf->other_secret_length);
6290 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006291 cur += prf->other_secret_length;
6292 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 } else {
6294 *cur++ = MBEDTLS_BYTE_1(data_length);
6295 *cur++ = MBEDTLS_BYTE_0(data_length);
6296 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006297 cur += data_length;
6298 }
6299
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 *cur++ = MBEDTLS_BYTE_1(data_length);
6301 *cur++ = MBEDTLS_BYTE_0(data_length);
6302 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006303 cur += data_length;
6304
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006306
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 mbedtls_platform_zeroize(pms, pms_len);
6308 mbedtls_free(pms);
6309 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006310}
Janos Follath6660f0e2019-06-17 08:44:03 +01006311
Przemek Stekiele47201b2022-04-19 13:53:28 +02006312static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6313 psa_tls12_prf_key_derivation_t *prf,
6314 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006316{
Gilles Peskine449bd832023-01-11 14:50:10 +01006317 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6318 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006319 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006320
6321 if (data_length != 0) {
6322 prf->other_secret = mbedtls_calloc(1, data_length);
6323 if (prf->other_secret == NULL) {
6324 return PSA_ERROR_INSUFFICIENT_MEMORY;
6325 }
6326
6327 memcpy(prf->other_secret, data, data_length);
6328 prf->other_secret_length = data_length;
6329 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006330 prf->other_secret_length = 0;
6331 }
6332
6333 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6334
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006336}
6337
Janos Follath6660f0e2019-06-17 08:44:03 +01006338static psa_status_t psa_tls12_prf_psk_to_ms_input(
6339 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006340 psa_key_derivation_step_t step,
6341 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006343{
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006345 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 return psa_tls12_prf_psk_to_ms_set_key(prf,
6347 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006348 break;
6349 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006350 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6351 data,
6352 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006353 break;
6354 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006355 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006356 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006357
Przemek Stekiele47201b2022-04-19 13:53:28 +02006358 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006359}
John Durkop07cc04a2020-11-16 22:08:34 -08006360#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006361
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006362#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6363static psa_status_t psa_tls12_ecjpake_to_pms_input(
6364 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006365 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006366 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006368{
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6370 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6371 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006372 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006373
6374 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 if (data[0] != 0x04) {
6376 return PSA_ERROR_INVALID_ARGUMENT;
6377 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006378
6379 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006381
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006383}
6384#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006385/** Check whether the given key type is acceptable for the given
6386 * input step of a key derivation.
6387 *
6388 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6389 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6390 * Both secret and non-secret inputs can alternatively have the type
6391 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6392 * that the input was passed as a buffer rather than via a key object.
6393 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006394static int psa_key_derivation_check_input_type(
6395 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006397{
Gilles Peskine449bd832023-01-11 14:50:10 +01006398 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006399 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006400 if (key_type == PSA_KEY_TYPE_DERIVE) {
6401 return PSA_SUCCESS;
6402 }
6403 if (key_type == PSA_KEY_TYPE_NONE) {
6404 return PSA_SUCCESS;
6405 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006406 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006407 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 if (key_type == PSA_KEY_TYPE_DERIVE) {
6409 return PSA_SUCCESS;
6410 }
6411 if (key_type == PSA_KEY_TYPE_NONE) {
6412 return PSA_SUCCESS;
6413 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006414 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006415 case PSA_KEY_DERIVATION_INPUT_LABEL:
6416 case PSA_KEY_DERIVATION_INPUT_SALT:
6417 case PSA_KEY_DERIVATION_INPUT_INFO:
6418 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006419 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6420 return PSA_SUCCESS;
6421 }
6422 if (key_type == PSA_KEY_TYPE_NONE) {
6423 return PSA_SUCCESS;
6424 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006425 break;
6426 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006428}
6429
Janos Follathb80a94e2019-06-12 15:54:46 +01006430static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006431 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006432 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006433 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006434 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006435 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006436{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006437 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006439
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 status = psa_key_derivation_check_input_type(step, key_type);
6441 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006442 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006444
Przemek Stekiel69c46792022-06-10 12:59:51 +02006445#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6447 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6448 step, data, data_length);
6449 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006450#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006451#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6453 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6454 step, data, data_length);
6455 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006456#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6457#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006458 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6459 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6460 step, data, data_length);
6461 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006462#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006463#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006464 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006465 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6467 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006468#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006469 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006470 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006471 (void) data;
6472 (void) data_length;
6473 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006474 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006475 }
6476
Gilles Peskine224b0d62019-09-23 18:13:17 +02006477exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006478 if (status != PSA_SUCCESS) {
6479 psa_key_derivation_abort(operation);
6480 }
6481 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006482}
6483
Janos Follath51f4a0f2019-06-14 11:35:55 +01006484psa_status_t psa_key_derivation_input_bytes(
6485 psa_key_derivation_operation_t *operation,
6486 psa_key_derivation_step_t step,
6487 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006489{
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 return psa_key_derivation_input_internal(operation, step,
6491 PSA_KEY_TYPE_NONE,
6492 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006493}
6494
Janos Follath51f4a0f2019-06-14 11:35:55 +01006495psa_status_t psa_key_derivation_input_key(
6496 psa_key_derivation_operation_t *operation,
6497 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006499{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006500 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006501 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006502 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006503
Ronald Cron5c522922020-11-14 16:35:34 +01006504 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006505 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6506 if (status != PSA_SUCCESS) {
6507 psa_key_derivation_abort(operation);
6508 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006509 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006510
6511 /* Passing a key object as a SECRET input unlocks the permission
6512 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006513 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006514 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006515 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006516
Gilles Peskine449bd832023-01-11 14:50:10 +01006517 status = psa_key_derivation_input_internal(operation,
6518 step, slot->attr.type,
6519 slot->key.data,
6520 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006521
Gilles Peskine449bd832023-01-11 14:50:10 +01006522 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006523
Gilles Peskine449bd832023-01-11 14:50:10 +01006524 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006525}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006526
6527
6528
6529/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006530/* Key agreement */
6531/****************************************************************/
6532
Gilles Peskine449bd832023-01-11 14:50:10 +01006533psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6534 const uint8_t *key_buffer,
6535 size_t key_buffer_size,
6536 psa_algorithm_t alg,
6537 const uint8_t *peer_key,
6538 size_t peer_key_length,
6539 uint8_t *shared_secret,
6540 size_t shared_secret_size,
6541 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006542{
Gilles Peskine449bd832023-01-11 14:50:10 +01006543 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006544#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006545 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006546 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6547 key_buffer_size, alg,
6548 peer_key, peer_key_length,
6549 shared_secret,
6550 shared_secret_size,
6551 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006552#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006553 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006554 (void) attributes;
6555 (void) key_buffer;
6556 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006557 (void) peer_key;
6558 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006559 (void) shared_secret;
6560 (void) shared_secret_size;
6561 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006562 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006563 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006564}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006565
Aditya Deshpande17845b82022-10-13 17:21:01 +01006566/** Internal function for raw key agreement
6567 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006568 * to the driver's implementation if a driver is present.
6569 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006570 * (psa_key_agreement_raw_builtin).
6571 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006572static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6573 psa_key_slot_t *private_key,
6574 const uint8_t *peer_key,
6575 size_t peer_key_length,
6576 uint8_t *shared_secret,
6577 size_t shared_secret_size,
6578 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006579{
Gilles Peskine449bd832023-01-11 14:50:10 +01006580 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6581 return PSA_ERROR_NOT_SUPPORTED;
6582 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006583
6584 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006585 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006586 };
6587
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 return psa_driver_wrapper_key_agreement(&attributes,
6589 private_key->key.data,
6590 private_key->key.bytes, alg,
6591 peer_key, peer_key_length,
6592 shared_secret,
6593 shared_secret_size,
6594 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006595}
6596
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006597/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006598 * to potentially free embedded data structures and wipe confidential data.
6599 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006600static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6601 psa_key_derivation_step_t step,
6602 psa_key_slot_t *private_key,
6603 const uint8_t *peer_key,
6604 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006605{
6606 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006607 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006608 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006609 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006610
6611 /* Step 1: run the secret agreement algorithm to generate the shared
6612 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006613 status = psa_key_agreement_raw_internal(ka_alg,
6614 private_key,
6615 peer_key, peer_key_length,
6616 shared_secret,
6617 sizeof(shared_secret),
6618 &shared_secret_length);
6619 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006620 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006621 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006622
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006623 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006624 * the shared secret. A shared secret is permitted wherever a key
6625 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006626 status = psa_key_derivation_input_internal(operation, step,
6627 PSA_KEY_TYPE_DERIVE,
6628 shared_secret,
6629 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006630exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006631 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6632 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006633}
6634
Gilles Peskine449bd832023-01-11 14:50:10 +01006635psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6636 psa_key_derivation_step_t step,
6637 mbedtls_svc_key_id_t private_key,
6638 const uint8_t *peer_key,
6639 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006640{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006641 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006642 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006643 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006644
Gilles Peskine449bd832023-01-11 14:50:10 +01006645 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6646 return PSA_ERROR_INVALID_ARGUMENT;
6647 }
Ronald Cron5c522922020-11-14 16:35:34 +01006648 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006649 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6650 if (status != PSA_SUCCESS) {
6651 return status;
6652 }
6653 status = psa_key_agreement_internal(operation, step,
6654 slot,
6655 peer_key, peer_key_length);
6656 if (status != PSA_SUCCESS) {
6657 psa_key_derivation_abort(operation);
6658 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006659 /* If a private key has been added as SECRET, we allow the derived
6660 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006661 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006662 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006663 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006664 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006665
Gilles Peskine449bd832023-01-11 14:50:10 +01006666 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006667
Gilles Peskine449bd832023-01-11 14:50:10 +01006668 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006669}
6670
Gilles Peskine449bd832023-01-11 14:50:10 +01006671psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6672 mbedtls_svc_key_id_t private_key,
6673 const uint8_t *peer_key,
6674 size_t peer_key_length,
6675 uint8_t *output,
6676 size_t output_size,
6677 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006678{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006679 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006680 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006681 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006682
Gilles Peskine449bd832023-01-11 14:50:10 +01006683 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006684 status = PSA_ERROR_INVALID_ARGUMENT;
6685 goto exit;
6686 }
Ronald Cron5c522922020-11-14 16:35:34 +01006687 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006688 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6689 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006690 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006692
Gilles Peskine3e561302022-04-14 00:17:15 +02006693 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6694 * for the output size. The PSA specification only guarantees that this
6695 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6696 * but it might be nice to allow smaller buffers if the output fits.
6697 * At the time of writing this comment, with only ECDH implemented,
6698 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6699 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6700 * be exact for it as well. */
6701 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006702 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6703 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006704 status = PSA_ERROR_BUFFER_TOO_SMALL;
6705 goto exit;
6706 }
6707
Gilles Peskine449bd832023-01-11 14:50:10 +01006708 status = psa_key_agreement_raw_internal(alg, slot,
6709 peer_key, peer_key_length,
6710 output, output_size,
6711 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006712
6713exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006714 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006715 /* If an error happens and is not handled properly, the output
6716 * may be used as a key to protect sensitive data. Arrange for such
6717 * a key to be random, which is likely to result in decryption or
6718 * verification errors. This is better than filling the buffer with
6719 * some constant data such as zeros, which would result in the data
6720 * being protected with a reproducible, easily knowable key.
6721 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006722 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006723 *output_length = output_size;
6724 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006725
Gilles Peskine449bd832023-01-11 14:50:10 +01006726 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006727
Gilles Peskine449bd832023-01-11 14:50:10 +01006728 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006729}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006730
6731
Gilles Peskine30524eb2020-11-13 17:02:26 +01006732
Gilles Peskine86a440b2018-11-12 18:39:40 +01006733/****************************************************************/
6734/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006735/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006736
Gilles Peskine30524eb2020-11-13 17:02:26 +01006737/** Initialize the PSA random generator.
6738 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006739static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006740{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006741#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006742 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006743#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6744
Gilles Peskine30524eb2020-11-13 17:02:26 +01006745 /* Set default configuration if
6746 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006747 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006748 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006749 }
6750 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006751 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006752 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006753
Gilles Peskine449bd832023-01-11 14:50:10 +01006754 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006755#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6756 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6757 /* The PSA entropy injection feature depends on using NV seed as an entropy
6758 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006759 mbedtls_entropy_add_source(&rng->entropy,
6760 mbedtls_nv_seed_poll, NULL,
6761 MBEDTLS_ENTROPY_BLOCK_SIZE,
6762 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006763#endif
6764
Gilles Peskine449bd832023-01-11 14:50:10 +01006765 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006766#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006767}
6768
6769/** Deinitialize the PSA random generator.
6770 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006771static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006772{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006773#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006774 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006775#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01006776 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
6777 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006778#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006779}
6780
6781/** Seed the PSA random generator.
6782 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006783static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006784{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006785#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6786 /* Do nothing: the external RNG seeds itself. */
6787 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006788 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006789#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006790 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01006791 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
6792 drbg_seed, sizeof(drbg_seed) - 1);
6793 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006794#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006795}
6796
Gilles Peskine449bd832023-01-11 14:50:10 +01006797psa_status_t psa_generate_random(uint8_t *output,
6798 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006799{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006800 GUARD_MODULE_INITIALIZED;
6801
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006802#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6803
6804 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006805 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6806 output, output_size,
6807 &output_length);
6808 if (status != PSA_SUCCESS) {
6809 return status;
6810 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006811 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006812 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006813 if (output_length != output_size) {
6814 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6815 }
6816 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006817
6818#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6819
Gilles Peskine449bd832023-01-11 14:50:10 +01006820 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006821 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006822 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6823 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6824 output_size);
6825 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6826 output, request_size);
6827 if (ret != 0) {
6828 return mbedtls_to_psa_error(ret);
6829 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006830 output_size -= request_size;
6831 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006832 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006833 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006834#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006835}
6836
Gilles Peskine8814fc42020-12-14 15:33:44 +01006837/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006838 *
6839 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6840 * `psa_generate_random(...)`. The state parameter is ignored since the
6841 * PSA API doesn't support passing an explicit state.
6842 *
6843 * In the non-external case, psa_generate_random() calls an
6844 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6845 * and semantics as mbedtls_psa_get_random(). As an optimization,
6846 * instead of doing this back-and-forth between the PSA API and the
6847 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6848 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006849 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006850#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6851int mbedtls_psa_get_random(void *p_rng,
6852 unsigned char *output,
6853 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01006854{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006855 /* This function takes a pointer to the RNG state because that's what
6856 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6857 * its own state internally and doesn't let the caller access that state.
6858 * So we just ignore the state parameter, and in practice we'll pass
6859 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006860 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006861 psa_status_t status = psa_generate_random(output, output_size);
6862 if (status == PSA_SUCCESS) {
6863 return 0;
6864 } else {
6865 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
6866 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01006867}
6868#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6869
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006870#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006871#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006872
Gilles Peskine449bd832023-01-11 14:50:10 +01006873psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
6874 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006875{
Gilles Peskine449bd832023-01-11 14:50:10 +01006876 if (global_data.initialized) {
6877 return PSA_ERROR_NOT_PERMITTED;
6878 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006879
Gilles Peskine449bd832023-01-11 14:50:10 +01006880 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
6881 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
6882 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
6883 return PSA_ERROR_INVALID_ARGUMENT;
6884 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006885
Gilles Peskine449bd832023-01-11 14:50:10 +01006886 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006887}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006888#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006889
Ronald Cron3772afe2021-02-08 16:10:05 +01006890/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006891 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006892 * \param type The key type
6893 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006894 *
6895 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006896 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006897 * \retval #PSA_ERROR_INVALID_ARGUMENT
6898 * The size in bits of the key is not valid.
6899 * \retval #PSA_ERROR_NOT_SUPPORTED
6900 * The type and/or the size in bits of the key or the combination of
6901 * the two is not supported.
6902 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006903static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006904 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006905{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006906 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006907
Gilles Peskine449bd832023-01-11 14:50:10 +01006908 if (key_type_is_raw_bytes(type)) {
6909 status = psa_validate_unstructured_key_bit_size(type, bits);
6910 if (status != PSA_SUCCESS) {
6911 return status;
6912 }
6913 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006914#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006915 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6916 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
6917 return PSA_ERROR_NOT_SUPPORTED;
6918 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006919
Ronald Cron01b2aba2020-10-05 09:42:02 +02006920 /* Accept only byte-aligned keys, for the same reasons as
6921 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006922 if (bits % 8 != 0) {
6923 return PSA_ERROR_NOT_SUPPORTED;
6924 }
6925 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006926#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006927
Ronald Cron5c4d3862020-12-07 11:07:24 +01006928#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006929 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01006930 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006931 return PSA_SUCCESS;
6932 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006933#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006934 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006935 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006936 }
6937
Gilles Peskine449bd832023-01-11 14:50:10 +01006938 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006939}
6940
Ronald Cron55ed0592020-10-05 10:30:40 +02006941psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006942 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006944{
6945 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006946 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006947
Gilles Peskine449bd832023-01-11 14:50:10 +01006948 if ((attributes->domain_parameters == NULL) &&
6949 (attributes->domain_parameters_size != 0)) {
6950 return PSA_ERROR_INVALID_ARGUMENT;
6951 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02006952
Gilles Peskine449bd832023-01-11 14:50:10 +01006953 if (key_type_is_raw_bytes(type)) {
6954 status = psa_generate_random(key_buffer, key_buffer_size);
6955 if (status != PSA_SUCCESS) {
6956 return status;
6957 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006958
David Brown12ca5032021-02-11 11:02:00 -07006959#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006960 if (type == PSA_KEY_TYPE_DES) {
6961 psa_des_set_key_parity(key_buffer, key_buffer_size);
6962 }
David Brown8107e312021-02-12 08:08:46 -07006963#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01006964 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01006965
Jaeden Amero424fa932021-05-14 08:34:32 +01006966#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
6967 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01006968 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
6969 return mbedtls_psa_rsa_generate_key(attributes,
6970 key_buffer,
6971 key_buffer_size,
6972 key_buffer_length);
6973 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01006974#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
6975 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006976
John Durkop9814fa22020-11-04 12:28:15 -08006977#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006978 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6979 return mbedtls_psa_ecp_generate_key(attributes,
6980 key_buffer,
6981 key_buffer_size,
6982 key_buffer_length);
6983 } else
John Durkop9814fa22020-11-04 12:28:15 -08006984#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006985 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006986 (void) key_buffer_length;
6987 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02006988 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006989
Gilles Peskine449bd832023-01-11 14:50:10 +01006990 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006991}
Darryl Green0c6575a2018-11-07 16:05:30 +00006992
Gilles Peskine449bd832023-01-11 14:50:10 +01006993psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
6994 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006995{
6996 psa_status_t status;
6997 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006998 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006999 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007000
Ronald Cron81709fc2020-11-14 12:10:32 +01007001 *key = MBEDTLS_SVC_KEY_ID_INIT;
7002
Gilles Peskine0f84d622019-09-12 19:03:13 +02007003 /* Reject any attempt to create a zero-length key so that we don't
7004 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007005 if (psa_get_key_bits(attributes) == 0) {
7006 return PSA_ERROR_INVALID_ARGUMENT;
7007 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007008
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007009 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007010 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7011 return PSA_ERROR_INVALID_ARGUMENT;
7012 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007013
Gilles Peskine449bd832023-01-11 14:50:10 +01007014 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7015 &slot, &driver);
7016 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007017 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007018 }
Gilles Peskine11792082019-08-06 18:36:36 +02007019
Ronald Cron977c2472020-10-13 08:32:21 +02007020 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307021 * storage ( thus not in the case of generating a key in a secure element
7022 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7023 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007024 if (slot->key.data == NULL) {
7025 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7026 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007027 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007028 attributes->core.type, attributes->core.bits);
7029 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007030 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007031 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007032
7033 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 attributes->core.type,
7035 attributes->core.bits);
7036 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007037 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007038 attributes, &key_buffer_size);
7039 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007040 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007041 }
Ronald Cron977c2472020-10-13 08:32:21 +02007042 }
Ronald Cron977c2472020-10-13 08:32:21 +02007043
Gilles Peskine449bd832023-01-11 14:50:10 +01007044 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7045 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007046 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007047 }
Ronald Cron977c2472020-10-13 08:32:21 +02007048 }
7049
Gilles Peskine449bd832023-01-11 14:50:10 +01007050 status = psa_driver_wrapper_generate_key(attributes,
7051 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007052
Gilles Peskine449bd832023-01-11 14:50:10 +01007053 if (status != PSA_SUCCESS) {
7054 psa_remove_key_data_from_memory(slot);
7055 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007056
Gilles Peskine11792082019-08-06 18:36:36 +02007057exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007058 if (status == PSA_SUCCESS) {
7059 status = psa_finish_key_creation(slot, driver, key);
7060 }
7061 if (status != PSA_SUCCESS) {
7062 psa_fail_key_creation(slot, driver);
7063 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007064
Gilles Peskine449bd832023-01-11 14:50:10 +01007065 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007066}
7067
Gilles Peskinee59236f2018-01-27 23:32:46 +01007068/****************************************************************/
7069/* Module setup */
7070/****************************************************************/
7071
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007072#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007073psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007074 void (* entropy_init)(mbedtls_entropy_context *ctx),
7075 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007076{
Gilles Peskine449bd832023-01-11 14:50:10 +01007077 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7078 return PSA_ERROR_BAD_STATE;
7079 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007080 global_data.rng.entropy_init = entropy_init;
7081 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007082 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007083}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007084#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007085
Gilles Peskine449bd832023-01-11 14:50:10 +01007086void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007087{
Gilles Peskine449bd832023-01-11 14:50:10 +01007088 psa_wipe_all_key_slots();
7089 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7090 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007091 }
7092 /* Wipe all remaining data, including configuration.
7093 * In particular, this sets all state indicator to the value
7094 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007095 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007096
7097 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007099}
7100
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007101#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7102/** Recover a transaction that was interrupted by a power failure.
7103 *
7104 * This function is called during initialization, before psa_crypto_init()
7105 * returns. If this function returns a failure status, the initialization
7106 * fails.
7107 */
7108static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007109 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007110{
Gilles Peskine449bd832023-01-11 14:50:10 +01007111 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007112 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7113 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007114 /* TODO - fall through to the failure case until this
7115 * is implemented.
7116 * https://github.com/ARMmbed/mbed-crypto/issues/218
7117 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007118 default:
7119 /* We found an unsupported transaction in the storage.
7120 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007121 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007122 }
7123}
7124#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7125
Gilles Peskine449bd832023-01-11 14:50:10 +01007126psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007127{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007128 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007129
Gilles Peskinec6b69072018-11-20 21:42:52 +01007130 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007131 if (global_data.initialized != 0) {
7132 return PSA_SUCCESS;
7133 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007134
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007135 /* Init drivers */
7136 status = psa_driver_wrapper_init();
7137 if (status != PSA_SUCCESS) {
7138 goto exit;
7139 }
7140 global_data.drivers_initialized = 1;
7141
Gilles Peskine30524eb2020-11-13 17:02:26 +01007142 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007143 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007144 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007145 status = mbedtls_psa_random_seed(&global_data.rng);
7146 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007147 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007148 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007149 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007150
Gilles Peskine449bd832023-01-11 14:50:10 +01007151 status = psa_initialize_key_slots();
7152 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007153 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007154 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007155
Gilles Peskine4b734222019-07-24 15:56:31 +02007156#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007157 status = psa_crypto_load_transaction();
7158 if (status == PSA_SUCCESS) {
7159 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7160 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007161 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007162 }
7163 status = psa_crypto_stop_transaction();
7164 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007165 /* There's no transaction to complete. It's all good. */
7166 status = PSA_SUCCESS;
7167 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007168#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007169
Gilles Peskinec6b69072018-11-20 21:42:52 +01007170 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007171 global_data.initialized = 1;
7172
7173exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007174 if (status != PSA_SUCCESS) {
7175 mbedtls_psa_crypto_free();
7176 }
7177 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007178}
7179
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007180psa_status_t psa_crypto_driver_pake_get_password_len(
7181 const psa_crypto_driver_pake_inputs_t *inputs,
7182 size_t *password_len)
7183{
7184 if (inputs->password_len == 0) {
7185 return PSA_ERROR_BAD_STATE;
7186 }
7187
7188 *password_len = inputs->password_len;
7189
7190 return PSA_SUCCESS;
7191}
7192
7193psa_status_t psa_crypto_driver_pake_get_password(
7194 const psa_crypto_driver_pake_inputs_t *inputs,
7195 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7196{
7197 if (inputs->password_len == 0) {
7198 return PSA_ERROR_BAD_STATE;
7199 }
7200
7201 if (buffer_size < inputs->password_len) {
7202 return PSA_ERROR_BUFFER_TOO_SMALL;
7203 }
7204
7205 memcpy(buffer, inputs->password, inputs->password_len);
7206 *buffer_length = inputs->password_len;
7207
7208 return PSA_SUCCESS;
7209}
7210
7211psa_status_t psa_crypto_driver_pake_get_role(
7212 const psa_crypto_driver_pake_inputs_t *inputs,
7213 psa_pake_role_t *role)
7214{
7215 if (inputs->role == PSA_PAKE_ROLE_NONE) {
7216 return PSA_ERROR_BAD_STATE;
7217 }
7218
7219 *role = inputs->role;
7220
7221 return PSA_SUCCESS;
7222}
7223
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007224psa_status_t psa_crypto_driver_pake_get_user_len(
7225 const psa_crypto_driver_pake_inputs_t *inputs,
7226 size_t *user_len)
7227{
7228 if (inputs->user_len == 0) {
7229 return PSA_ERROR_BAD_STATE;
7230 }
7231
7232 *user_len = inputs->user_len;
7233
7234 return PSA_SUCCESS;
7235}
7236
7237psa_status_t psa_crypto_driver_pake_get_user(
7238 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007239 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007240{
7241 if (inputs->user_len == 0) {
7242 return PSA_ERROR_BAD_STATE;
7243 }
7244
Przemek Stekielc0e62502023-03-14 11:49:36 +01007245 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007246 return PSA_ERROR_BUFFER_TOO_SMALL;
7247 }
7248
Przemek Stekielc0e62502023-03-14 11:49:36 +01007249 memcpy(user_id, inputs->user, inputs->user_len);
7250 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007251
7252 return PSA_SUCCESS;
7253}
7254
7255psa_status_t psa_crypto_driver_pake_get_peer_len(
7256 const psa_crypto_driver_pake_inputs_t *inputs,
7257 size_t *peer_len)
7258{
7259 if (inputs->peer_len == 0) {
7260 return PSA_ERROR_BAD_STATE;
7261 }
7262
7263 *peer_len = inputs->peer_len;
7264
7265 return PSA_SUCCESS;
7266}
7267
7268psa_status_t psa_crypto_driver_pake_get_peer(
7269 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007270 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007271{
7272 if (inputs->peer_len == 0) {
7273 return PSA_ERROR_BAD_STATE;
7274 }
7275
Przemek Stekielc0e62502023-03-14 11:49:36 +01007276 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007277 return PSA_ERROR_BUFFER_TOO_SMALL;
7278 }
7279
Przemek Stekielc0e62502023-03-14 11:49:36 +01007280 memcpy(peer_id, inputs->peer, inputs->peer_len);
7281 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007282
7283 return PSA_SUCCESS;
7284}
7285
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007286psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7287 const psa_crypto_driver_pake_inputs_t *inputs,
7288 psa_pake_cipher_suite_t *cipher_suite)
7289{
7290 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7291 return PSA_ERROR_BAD_STATE;
7292 }
7293
7294 *cipher_suite = inputs->cipher_suite;
7295
7296 return PSA_SUCCESS;
7297}
7298
Neil Armstronga7d08c32022-06-01 18:21:20 +02007299psa_status_t psa_pake_setup(
7300 psa_pake_operation_t *operation,
7301 const psa_pake_cipher_suite_t *cipher_suite)
7302{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007303 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007304
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007305 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007306 status = PSA_ERROR_BAD_STATE;
7307 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007308 }
7309
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007310 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007311 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007312 status = PSA_ERROR_INVALID_ARGUMENT;
7313 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007314 }
7315
Przemek Stekiel51eac532022-12-07 11:04:51 +01007316 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7317
Przemek Stekiele12ed362022-12-21 12:54:46 +01007318 operation->alg = cipher_suite->algorithm;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007319 operation->data.inputs.cipher_suite = *cipher_suite;
7320
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007321#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007322 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007323 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007324 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007325
Przemek Stekiele12ed362022-12-21 12:54:46 +01007326 computation_stage->state = PSA_PAKE_STATE_SETUP;
7327 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7328 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7329 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007330 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007331#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007332 {
7333 status = PSA_ERROR_NOT_SUPPORTED;
7334 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007335 }
7336
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007337 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7338
Przemek Stekiel51eac532022-12-07 11:04:51 +01007339 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007340exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007341 psa_pake_abort(operation);
7342 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007343}
7344
7345psa_status_t psa_pake_set_password_key(
7346 psa_pake_operation_t *operation,
7347 mbedtls_svc_key_id_t password)
7348{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007349 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007350 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7351 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007352
Przemek Stekiel51eac532022-12-07 11:04:51 +01007353 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007354 status = PSA_ERROR_BAD_STATE;
7355 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007356 }
7357
Przemek Stekiel6c764412022-11-22 14:05:12 +01007358 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7359 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007360 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007361 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007362 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007363 }
7364
Przemek Stekiel6c764412022-11-22 14:05:12 +01007365 psa_key_attributes_t attributes = {
7366 .core = slot->attr
7367 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007368
Przemek Stekiel51eac532022-12-07 11:04:51 +01007369 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007370
Przemek Stekiel51eac532022-12-07 11:04:51 +01007371 if (type != PSA_KEY_TYPE_PASSWORD &&
7372 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7373 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007374 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007375 }
7376
Przemek Stekiel51eac532022-12-07 11:04:51 +01007377 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7378 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007379 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007380 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007381 }
7382
7383 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7384 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007385 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007386exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007387 if (status != PSA_SUCCESS) {
7388 psa_pake_abort(operation);
7389 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007390 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007391 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007392}
7393
7394psa_status_t psa_pake_set_user(
7395 psa_pake_operation_t *operation,
7396 const uint8_t *user_id,
7397 size_t user_id_len)
7398{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007399 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007400
7401 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007402 status = PSA_ERROR_BAD_STATE;
7403 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007404 }
7405
Przemek Stekiel51eac532022-12-07 11:04:51 +01007406 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007407 status = PSA_ERROR_INVALID_ARGUMENT;
7408 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007409 }
7410
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007411 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007412 status = PSA_ERROR_BAD_STATE;
7413 goto exit;
7414 }
7415
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007416 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007417 if ((user_id_len != sizeof(jpake_server_id) ||
7418 memcmp(user_id, jpake_server_id, user_id_len) != 0) &&
7419 (user_id_len != sizeof(jpake_client_id) ||
7420 memcmp(user_id, jpake_client_id, user_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007421 status = PSA_ERROR_NOT_SUPPORTED;
7422 goto exit;
7423 }
7424
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007425 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7426 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007427 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7428 goto exit;
7429 }
7430
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007431 memcpy(operation->data.inputs.user, user_id, user_id_len);
7432 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007433
7434 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007435exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007436 psa_pake_abort(operation);
7437 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007438}
7439
7440psa_status_t psa_pake_set_peer(
7441 psa_pake_operation_t *operation,
7442 const uint8_t *peer_id,
7443 size_t peer_id_len)
7444{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007445 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007446
7447 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007448 status = PSA_ERROR_BAD_STATE;
7449 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007450 }
7451
Przemek Stekiel51eac532022-12-07 11:04:51 +01007452 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007453 status = PSA_ERROR_INVALID_ARGUMENT;
7454 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007455 }
7456
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007457 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007458 status = PSA_ERROR_BAD_STATE;
7459 goto exit;
7460 }
7461
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007462 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007463 if ((peer_id_len != sizeof(jpake_server_id) ||
7464 memcmp(peer_id, jpake_server_id, peer_id_len) != 0) &&
7465 (peer_id_len != sizeof(jpake_client_id) ||
7466 memcmp(peer_id, jpake_client_id, peer_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007467 status = PSA_ERROR_NOT_SUPPORTED;
7468 goto exit;
7469 }
7470
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007471 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7472 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007473 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7474 goto exit;
7475 }
7476
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007477 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7478 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007479
7480 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007481exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007482 psa_pake_abort(operation);
7483 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007484}
7485
7486psa_status_t psa_pake_set_role(
7487 psa_pake_operation_t *operation,
7488 psa_pake_role_t role)
7489{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007490 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007491
Przemek Stekiel51eac532022-12-07 11:04:51 +01007492 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007493 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007494 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007495 }
7496
Przemek Stekiel09104b82023-03-06 14:11:51 +01007497 switch (operation->alg) {
7498#if defined(PSA_WANT_ALG_JPAKE)
7499 case PSA_ALG_JPAKE:
7500 if (role == PSA_PAKE_ROLE_NONE) {
7501 return PSA_SUCCESS;
7502 }
7503 status = PSA_ERROR_INVALID_ARGUMENT;
7504 break;
7505#endif
7506 default:
7507 (void) role;
7508 status = PSA_ERROR_NOT_SUPPORTED;
7509 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007510 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007511exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007512 psa_pake_abort(operation);
7513 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007514}
7515
Przemek Stekielb09c4872023-01-17 12:05:38 +01007516/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007517#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007518static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007519 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007520{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007521 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007522 case PSA_PAKE_OUTPUT_X1_X2:
7523 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007524 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007525 case PSA_PAKE_X1_STEP_KEY_SHARE:
7526 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007527 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7528 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007529 case PSA_PAKE_X1_STEP_ZK_PROOF:
7530 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007531 case PSA_PAKE_X2_STEP_KEY_SHARE:
7532 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007533 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7534 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007535 case PSA_PAKE_X2_STEP_ZK_PROOF:
7536 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007537 default:
7538 return PSA_JPAKE_STEP_INVALID;
7539 }
7540 break;
7541 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007542 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007543 case PSA_PAKE_X1_STEP_KEY_SHARE:
7544 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007545 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7546 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007547 case PSA_PAKE_X1_STEP_ZK_PROOF:
7548 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007549 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007550 return PSA_JPAKE_STEP_INVALID;
7551 }
7552 break;
7553 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007554 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007555 case PSA_PAKE_X1_STEP_KEY_SHARE:
7556 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007557 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7558 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007559 case PSA_PAKE_X1_STEP_ZK_PROOF:
7560 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007561 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007562 return PSA_JPAKE_STEP_INVALID;
7563 }
7564 break;
7565 default:
7566 return PSA_JPAKE_STEP_INVALID;
7567 }
7568 return PSA_JPAKE_STEP_INVALID;
7569}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007570#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007571
Przemek Stekiel2797d372022-12-22 11:19:22 +01007572static psa_status_t psa_pake_complete_inputs(
7573 psa_pake_operation_t *operation)
7574{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007575 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007576 /* Create copy of the inputs on stack as inputs share memory
7577 with the driver context which will be setup by the driver. */
7578 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007579
Przemek Stekielfde11282023-03-13 16:06:09 +01007580 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007581 return PSA_ERROR_BAD_STATE;
7582 }
7583
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007584 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007585 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7586 return PSA_ERROR_BAD_STATE;
7587 }
7588 if (memcmp(inputs.user, jpake_client_id, inputs.user_len) == 0 &&
7589 memcmp(inputs.peer, jpake_server_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007590 inputs.role = PSA_PAKE_ROLE_CLIENT;
7591 } else
Przemek Stekielfde11282023-03-13 16:06:09 +01007592 if (memcmp(inputs.user, jpake_server_id, inputs.user_len) == 0 &&
7593 memcmp(inputs.peer, jpake_client_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007594 inputs.role = PSA_PAKE_ROLE_SERVER;
7595 }
7596
7597 if (inputs.role != PSA_PAKE_ROLE_CLIENT &&
7598 inputs.role != PSA_PAKE_ROLE_SERVER) {
7599 return PSA_ERROR_NOT_SUPPORTED;
7600 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007601 }
7602
Przemek Stekiel18620a32023-01-17 16:34:52 +01007603 /* Clear driver context */
7604 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7605
7606 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007607
7608 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007609 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7610 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007611
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007612 /* User and peer are translated to role. */
7613 mbedtls_free(inputs.user);
7614 mbedtls_free(inputs.peer);
7615
Przemek Stekiel2797d372022-12-22 11:19:22 +01007616 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007617#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007618 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007619 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007620 psa_jpake_computation_stage_t *computation_stage =
7621 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007622 computation_stage->state = PSA_PAKE_STATE_READY;
7623 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7624 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7625 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007626 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007627#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007628 {
7629 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007630 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007631 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007632 return status;
7633}
7634
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007635#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007636static psa_status_t psa_jpake_output_prologue(
7637 psa_pake_operation_t *operation,
7638 psa_pake_step_t step)
7639{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007640 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7641 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7642 step != PSA_PAKE_STEP_ZK_PROOF) {
7643 return PSA_ERROR_INVALID_ARGUMENT;
7644 }
7645
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007646 psa_jpake_computation_stage_t *computation_stage =
7647 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007648
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007649 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7650 return PSA_ERROR_BAD_STATE;
7651 }
7652
7653 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7654 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
7655 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
7656 return PSA_ERROR_BAD_STATE;
7657 }
7658
7659 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7660 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007661 return PSA_ERROR_BAD_STATE;
7662 }
7663
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007664 switch (computation_stage->output_step) {
7665 case PSA_PAKE_STEP_X1_X2:
7666 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007667 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007668 case PSA_PAKE_STEP_X2S:
7669 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007670 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007671 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007672 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007673 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007674
7675 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7676 }
7677
7678 /* Check if step matches current sequence */
7679 switch (computation_stage->sequence) {
7680 case PSA_PAKE_X1_STEP_KEY_SHARE:
7681 case PSA_PAKE_X2_STEP_KEY_SHARE:
7682 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7683 return PSA_ERROR_BAD_STATE;
7684 }
7685 break;
7686
7687 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7688 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7689 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7690 return PSA_ERROR_BAD_STATE;
7691 }
7692 break;
7693
7694 case PSA_PAKE_X1_STEP_ZK_PROOF:
7695 case PSA_PAKE_X2_STEP_ZK_PROOF:
7696 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7697 return PSA_ERROR_BAD_STATE;
7698 }
7699 break;
7700
7701 default:
7702 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007703 }
7704
7705 return PSA_SUCCESS;
7706}
7707
7708static psa_status_t psa_jpake_output_epilogue(
7709 psa_pake_operation_t *operation)
7710{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007711 psa_jpake_computation_stage_t *computation_stage =
7712 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007713
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007714 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007715 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007716 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007717 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007718 computation_stage->state = PSA_PAKE_STATE_READY;
7719 computation_stage->output_step++;
7720 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7721 } else {
7722 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007723 }
7724
7725 return PSA_SUCCESS;
7726}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007727#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007728
Neil Armstronga7d08c32022-06-01 18:21:20 +02007729psa_status_t psa_pake_output(
7730 psa_pake_operation_t *operation,
7731 psa_pake_step_t step,
7732 uint8_t *output,
7733 size_t output_size,
7734 size_t *output_length)
7735{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007736 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007737 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007738 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007739
7740 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007741 status = psa_pake_complete_inputs(operation);
7742 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007743 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007744 }
7745 }
7746
7747 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007748 status = PSA_ERROR_BAD_STATE;
7749 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007750 }
7751
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007752 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007753 status = PSA_ERROR_INVALID_ARGUMENT;
7754 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007755 }
7756
Przemek Stekiele12ed362022-12-21 12:54:46 +01007757 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007758#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007759 case PSA_ALG_JPAKE:
7760 status = psa_jpake_output_prologue(operation, step);
7761 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007762 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007763 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007764 driver_step = convert_jpake_computation_stage_to_driver_step(
7765 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007766 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007767#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007768 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007769 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007770 status = PSA_ERROR_NOT_SUPPORTED;
7771 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007772 }
7773
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007774 status = psa_driver_wrapper_pake_output(operation, driver_step,
7775 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007776
7777 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007778 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007779 }
7780
7781 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007782#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007783 case PSA_ALG_JPAKE:
7784 status = psa_jpake_output_epilogue(operation);
7785 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007786 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007787 }
7788 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007789#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007790 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007791 status = PSA_ERROR_NOT_SUPPORTED;
7792 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007793 }
7794
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007795 return PSA_SUCCESS;
7796exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007797 psa_pake_abort(operation);
7798 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007799}
7800
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007801#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007802static psa_status_t psa_jpake_input_prologue(
7803 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007804 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007805{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007806 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7807 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7808 step != PSA_PAKE_STEP_ZK_PROOF) {
7809 return PSA_ERROR_INVALID_ARGUMENT;
7810 }
7811
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007812 psa_jpake_computation_stage_t *computation_stage =
7813 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007814
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007815 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7816 return PSA_ERROR_BAD_STATE;
7817 }
7818
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007819 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7820 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
7821 computation_stage->state != PSA_PAKE_INPUT_X4S) {
7822 return PSA_ERROR_BAD_STATE;
7823 }
7824
7825 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7826 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007827 return PSA_ERROR_BAD_STATE;
7828 }
7829
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007830 switch (computation_stage->input_step) {
7831 case PSA_PAKE_STEP_X1_X2:
7832 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007833 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007834 case PSA_PAKE_STEP_X2S:
7835 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007836 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007837 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007838 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007839 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007840
7841 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7842 }
7843
7844 /* Check if step matches current sequence */
7845 switch (computation_stage->sequence) {
7846 case PSA_PAKE_X1_STEP_KEY_SHARE:
7847 case PSA_PAKE_X2_STEP_KEY_SHARE:
7848 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7849 return PSA_ERROR_BAD_STATE;
7850 }
7851 break;
7852
7853 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7854 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7855 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7856 return PSA_ERROR_BAD_STATE;
7857 }
7858 break;
7859
7860 case PSA_PAKE_X1_STEP_ZK_PROOF:
7861 case PSA_PAKE_X2_STEP_ZK_PROOF:
7862 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7863 return PSA_ERROR_BAD_STATE;
7864 }
7865 break;
7866
7867 default:
7868 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007869 }
7870
7871 return PSA_SUCCESS;
7872}
7873
Przemek Stekiele12ed362022-12-21 12:54:46 +01007874static psa_status_t psa_jpake_input_epilogue(
7875 psa_pake_operation_t *operation)
7876{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007877 psa_jpake_computation_stage_t *computation_stage =
7878 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007879
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007880 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007881 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007882 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007883 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007884 computation_stage->state = PSA_PAKE_STATE_READY;
7885 computation_stage->input_step++;
7886 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7887 } else {
7888 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007889 }
7890
7891 return PSA_SUCCESS;
7892}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007893#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007894
Neil Armstronga7d08c32022-06-01 18:21:20 +02007895psa_status_t psa_pake_input(
7896 psa_pake_operation_t *operation,
7897 psa_pake_step_t step,
7898 const uint8_t *input,
7899 size_t input_length)
7900{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007901 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007902 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007903
7904 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007905 status = psa_pake_complete_inputs(operation);
7906 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007907 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007908 }
7909 }
7910
7911 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007912 status = PSA_ERROR_BAD_STATE;
7913 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007914 }
7915
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007916 if (input_length == 0 || input_length > PSA_PAKE_INPUT_MAX_SIZE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007917 status = PSA_ERROR_INVALID_ARGUMENT;
7918 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007919 }
7920
Przemek Stekiele12ed362022-12-21 12:54:46 +01007921 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007922#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007923 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007924 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007925 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007926 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007927 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007928 driver_step = convert_jpake_computation_stage_to_driver_step(
7929 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007930 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007931#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007932 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007933 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007934 status = PSA_ERROR_NOT_SUPPORTED;
7935 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007936 }
7937
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007938 status = psa_driver_wrapper_pake_input(operation, driver_step,
7939 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007940
7941 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007942 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007943 }
7944
7945 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007946#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007947 case PSA_ALG_JPAKE:
7948 status = psa_jpake_input_epilogue(operation);
7949 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007950 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007951 }
7952 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007953#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007954 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007955 status = PSA_ERROR_NOT_SUPPORTED;
7956 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007957 }
7958
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007959 return PSA_SUCCESS;
7960exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007961 psa_pake_abort(operation);
7962 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007963}
7964
7965psa_status_t psa_pake_get_implicit_key(
7966 psa_pake_operation_t *operation,
7967 psa_key_derivation_operation_t *output)
7968{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007969 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007970 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007971 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01007972 size_t shared_key_len = 0;
7973
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007974 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007975 status = PSA_ERROR_BAD_STATE;
7976 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007977 }
7978
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007979#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007980 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007981 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01007982 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007983 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
7984 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007985 status = PSA_ERROR_BAD_STATE;
7986 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007987 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01007988 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007989#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007990 {
7991 status = PSA_ERROR_NOT_SUPPORTED;
7992 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007993 }
7994
Przemek Stekiel0c781802022-11-29 14:53:13 +01007995 status = psa_driver_wrapper_pake_get_implicit_key(operation,
7996 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01007997 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01007998 &shared_key_len);
7999
8000 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008001 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008002 }
8003
8004 status = psa_key_derivation_input_bytes(output,
8005 PSA_KEY_DERIVATION_INPUT_SECRET,
8006 shared_key,
8007 shared_key_len);
8008
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008009 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008010exit:
8011 abort_status = psa_pake_abort(operation);
8012 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008013}
8014
8015psa_status_t psa_pake_abort(
8016 psa_pake_operation_t *operation)
8017{
Przemek Stekield69dca92023-01-31 19:59:20 +01008018 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008019
Przemek Stekield69dca92023-01-31 19:59:20 +01008020 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008021 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008022 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008023
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008024 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8025 if (operation->data.inputs.password != NULL) {
8026 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008027 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008028 mbedtls_free(operation->data.inputs.password);
8029 }
8030 if (operation->data.inputs.user != NULL) {
8031 mbedtls_free(operation->data.inputs.user);
8032 }
8033 if (operation->data.inputs.peer != NULL) {
8034 mbedtls_free(operation->data.inputs.peer);
8035 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008036 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008037 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008038
Przemek Stekield69dca92023-01-31 19:59:20 +01008039 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008040}
Neil Armstronga7d08c32022-06-01 18:21:20 +02008041
Gilles Peskinee59236f2018-01-27 23:32:46 +01008042#endif /* MBEDTLS_PSA_CRYPTO_C */