Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file psa/crypto_struct.h |
| 3 | * |
| 4 | * \brief PSA cryptography module: Mbed TLS structured type implementations |
Gilles Peskine | 07c91f5 | 2018-06-28 18:02:53 +0200 | [diff] [blame] | 5 | * |
| 6 | * \note This file may not be included directly. Applications must |
| 7 | * include psa/crypto.h. |
| 8 | * |
| 9 | * This file contains the definitions of some data structures with |
| 10 | * implementation-specific definitions. |
| 11 | * |
| 12 | * In implementations with isolation between the application and the |
| 13 | * cryptography module, it is expected that the front-end and the back-end |
| 14 | * would have different versions of this file. |
Gilles Peskine | b4e73e9 | 2019-08-13 15:00:57 +0200 | [diff] [blame] | 15 | * |
| 16 | * <h3>Design notes about multipart operation structures</h3> |
| 17 | * |
Ronald Cron | 980230e | 2021-04-01 15:37:49 +0200 | [diff] [blame] | 18 | * For multipart operations without driver delegation support, each multipart |
| 19 | * operation structure contains a `psa_algorithm_t alg` field which indicates |
| 20 | * which specific algorithm the structure is for. When the structure is not in |
| 21 | * use, `alg` is 0. Most of the structure consists of a union which is |
| 22 | * discriminated by `alg`. |
Gilles Peskine | b4e73e9 | 2019-08-13 15:00:57 +0200 | [diff] [blame] | 23 | * |
Ronald Cron | 980230e | 2021-04-01 15:37:49 +0200 | [diff] [blame] | 24 | * For multipart operations with driver delegation support, each multipart |
| 25 | * operation structure contains an `unsigned int id` field indicating which |
| 26 | * driver got assigned to do the operation. When the structure is not in use, |
| 27 | * 'id' is 0. The structure contains also a driver context which is the union |
| 28 | * of the contexts of all drivers able to handle the type of multipart |
| 29 | * operation. |
| 30 | * |
| 31 | * Note that when `alg` or `id` is 0, the content of other fields is undefined. |
Gilles Peskine | b4e73e9 | 2019-08-13 15:00:57 +0200 | [diff] [blame] | 32 | * In particular, it is not guaranteed that a freshly-initialized structure |
| 33 | * is all-zero: we initialize structures to something like `{0, 0}`, which |
| 34 | * is only guaranteed to initializes the first member of the union; |
| 35 | * GCC and Clang initialize the whole structure to 0 (at the time of writing), |
| 36 | * but MSVC and CompCert don't. |
| 37 | * |
| 38 | * In Mbed Crypto, multipart operation structures live independently from |
| 39 | * the key. This allows Mbed Crypto to free the key objects when destroying |
| 40 | * a key slot. If a multipart operation needs to remember the key after |
| 41 | * the setup function returns, the operation structure needs to contain a |
| 42 | * copy of the key. |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 43 | */ |
| 44 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 45 | * Copyright The Mbed TLS Contributors |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 46 | * SPDX-License-Identifier: Apache-2.0 |
| 47 | * |
| 48 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 49 | * not use this file except in compliance with the License. |
| 50 | * You may obtain a copy of the License at |
| 51 | * |
| 52 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 53 | * |
| 54 | * Unless required by applicable law or agreed to in writing, software |
| 55 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 56 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 57 | * See the License for the specific language governing permissions and |
| 58 | * limitations under the License. |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 59 | */ |
| 60 | |
| 61 | #ifndef PSA_CRYPTO_STRUCT_H |
| 62 | #define PSA_CRYPTO_STRUCT_H |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 63 | #include "mbedtls/private_access.h" |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 64 | |
Jaeden Amero | 8013f44 | 2019-08-16 16:13:51 +0100 | [diff] [blame] | 65 | #ifdef __cplusplus |
| 66 | extern "C" { |
| 67 | #endif |
| 68 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 69 | /* Include the Mbed TLS configuration file, the way Mbed TLS does it |
| 70 | * in each of its header files. */ |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 71 | #include "mbedtls/build_info.h" |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 72 | |
Steven Cooreman | 61398ec | 2021-04-26 12:04:53 +0200 | [diff] [blame] | 73 | /* Include the context definition for the compiled-in drivers for the primitive |
| 74 | * algorithms. */ |
Steven Cooreman | 675501d | 2021-04-26 11:54:58 +0200 | [diff] [blame] | 75 | #include "psa/crypto_driver_contexts_primitives.h" |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 76 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 77 | struct psa_hash_operation_s { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 78 | /** Unique ID indicating which driver got assigned to do the |
| 79 | * operation. Since driver contexts are driver-specific, swapping |
| 80 | * drivers halfway through the operation is not supported. |
Ronald Cron | 980230e | 2021-04-01 15:37:49 +0200 | [diff] [blame] | 81 | * ID values are auto-generated in psa_driver_wrappers.h. |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 82 | * ID value zero means the context is not valid or not assigned to |
Ronald Cron | 980230e | 2021-04-01 15:37:49 +0200 | [diff] [blame] | 83 | * any driver (i.e. the driver context is not active, in use). */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 84 | unsigned int MBEDTLS_PRIVATE(id); |
| 85 | psa_driver_hash_context_t MBEDTLS_PRIVATE(ctx); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 86 | }; |
| 87 | |
Janos Follath | 33434a9 | 2021-05-26 09:25:33 +0100 | [diff] [blame] | 88 | #define PSA_HASH_OPERATION_INIT { 0, { 0 } } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 89 | static inline struct psa_hash_operation_s psa_hash_operation_init(void) |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 90 | { |
| 91 | const struct psa_hash_operation_s v = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 92 | return v; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 95 | struct psa_cipher_operation_s { |
Steven Cooreman | 61398ec | 2021-04-26 12:04:53 +0200 | [diff] [blame] | 96 | /** Unique ID indicating which driver got assigned to do the |
| 97 | * operation. Since driver contexts are driver-specific, swapping |
| 98 | * drivers halfway through the operation is not supported. |
| 99 | * ID values are auto-generated in psa_crypto_driver_wrappers.h |
| 100 | * ID value zero means the context is not valid or not assigned to |
| 101 | * any driver (i.e. none of the driver contexts are active). */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 102 | unsigned int MBEDTLS_PRIVATE(id); |
Steven Cooreman | 61398ec | 2021-04-26 12:04:53 +0200 | [diff] [blame] | 103 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 104 | unsigned int MBEDTLS_PRIVATE(iv_required) : 1; |
| 105 | unsigned int MBEDTLS_PRIVATE(iv_set) : 1; |
Steven Cooreman | 61398ec | 2021-04-26 12:04:53 +0200 | [diff] [blame] | 106 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 107 | uint8_t MBEDTLS_PRIVATE(default_iv_length); |
Steven Cooreman | 61398ec | 2021-04-26 12:04:53 +0200 | [diff] [blame] | 108 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 109 | psa_driver_cipher_context_t MBEDTLS_PRIVATE(ctx); |
Steven Cooreman | 61398ec | 2021-04-26 12:04:53 +0200 | [diff] [blame] | 110 | }; |
| 111 | |
Janos Follath | 33434a9 | 2021-05-26 09:25:33 +0100 | [diff] [blame] | 112 | #define PSA_CIPHER_OPERATION_INIT { 0, 0, 0, 0, { 0 } } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 113 | static inline struct psa_cipher_operation_s psa_cipher_operation_init(void) |
Steven Cooreman | 61398ec | 2021-04-26 12:04:53 +0200 | [diff] [blame] | 114 | { |
| 115 | const struct psa_cipher_operation_s v = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | return v; |
Steven Cooreman | 61398ec | 2021-04-26 12:04:53 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Steven Cooreman | 3c8dd63 | 2021-04-26 12:16:27 +0200 | [diff] [blame] | 119 | /* Include the context definition for the compiled-in drivers for the composite |
| 120 | * algorithms. */ |
| 121 | #include "psa/crypto_driver_contexts_composites.h" |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 123 | struct psa_mac_operation_s { |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 124 | /** Unique ID indicating which driver got assigned to do the |
| 125 | * operation. Since driver contexts are driver-specific, swapping |
| 126 | * drivers halfway through the operation is not supported. |
| 127 | * ID values are auto-generated in psa_driver_wrappers.h |
| 128 | * ID value zero means the context is not valid or not assigned to |
| 129 | * any driver (i.e. none of the driver contexts are active). */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 130 | unsigned int MBEDTLS_PRIVATE(id); |
| 131 | uint8_t MBEDTLS_PRIVATE(mac_size); |
| 132 | unsigned int MBEDTLS_PRIVATE(is_sign) : 1; |
| 133 | psa_driver_mac_context_t MBEDTLS_PRIVATE(ctx); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 134 | }; |
| 135 | |
Janos Follath | 33434a9 | 2021-05-26 09:25:33 +0100 | [diff] [blame] | 136 | #define PSA_MAC_OPERATION_INIT { 0, 0, 0, { 0 } } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 137 | static inline struct psa_mac_operation_s psa_mac_operation_init(void) |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 138 | { |
| 139 | const struct psa_mac_operation_s v = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 140 | return v; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 143 | struct psa_aead_operation_s { |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 144 | |
Paul Elliott | 6504aa6 | 2021-04-20 17:09:36 +0100 | [diff] [blame] | 145 | /** Unique ID indicating which driver got assigned to do the |
| 146 | * operation. Since driver contexts are driver-specific, swapping |
| 147 | * drivers halfway through the operation is not supported. |
| 148 | * ID values are auto-generated in psa_crypto_driver_wrappers.h |
| 149 | * ID value zero means the context is not valid or not assigned to |
| 150 | * any driver (i.e. none of the driver contexts are active). */ |
Paul Elliott | c7e7fe5 | 2021-09-27 09:23:40 +0100 | [diff] [blame] | 151 | unsigned int MBEDTLS_PRIVATE(id); |
Paul Elliott | 6504aa6 | 2021-04-20 17:09:36 +0100 | [diff] [blame] | 152 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 153 | psa_algorithm_t MBEDTLS_PRIVATE(alg); |
Paul Elliott | 71b0567 | 2021-09-24 11:18:13 +0100 | [diff] [blame] | 154 | psa_key_type_t MBEDTLS_PRIVATE(key_type); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 155 | |
Paul Elliott | 71b0567 | 2021-09-24 11:18:13 +0100 | [diff] [blame] | 156 | size_t MBEDTLS_PRIVATE(ad_remaining); |
| 157 | size_t MBEDTLS_PRIVATE(body_remaining); |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 158 | |
Paul Elliott | 71b0567 | 2021-09-24 11:18:13 +0100 | [diff] [blame] | 159 | unsigned int MBEDTLS_PRIVATE(nonce_set) : 1; |
| 160 | unsigned int MBEDTLS_PRIVATE(lengths_set) : 1; |
| 161 | unsigned int MBEDTLS_PRIVATE(ad_started) : 1; |
| 162 | unsigned int MBEDTLS_PRIVATE(body_started) : 1; |
| 163 | unsigned int MBEDTLS_PRIVATE(is_encrypt) : 1; |
Paul Elliott | 07a30c4 | 2021-04-20 14:13:23 +0100 | [diff] [blame] | 164 | |
Paul Elliott | 71b0567 | 2021-09-24 11:18:13 +0100 | [diff] [blame] | 165 | psa_driver_aead_context_t MBEDTLS_PRIVATE(ctx); |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 166 | }; |
| 167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 168 | #define PSA_AEAD_OPERATION_INIT { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, { 0 } } |
| 169 | static inline struct psa_aead_operation_s psa_aead_operation_init(void) |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 170 | { |
| 171 | const struct psa_aead_operation_s v = PSA_AEAD_OPERATION_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 172 | return v; |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 175 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 176 | defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \ |
| 177 | defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 178 | typedef struct { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 179 | uint8_t *MBEDTLS_PRIVATE(info); |
| 180 | size_t MBEDTLS_PRIVATE(info_length); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 181 | #if PSA_HASH_MAX_SIZE > 0xff |
| 182 | #error "PSA_HASH_MAX_SIZE does not fit in uint8_t" |
| 183 | #endif |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 184 | uint8_t MBEDTLS_PRIVATE(offset_in_block); |
| 185 | uint8_t MBEDTLS_PRIVATE(block_number); |
| 186 | unsigned int MBEDTLS_PRIVATE(state) : 2; |
| 187 | unsigned int MBEDTLS_PRIVATE(info_set) : 1; |
Gilles Peskine | e3d9c9d | 2021-11-08 22:12:47 +0100 | [diff] [blame] | 188 | uint8_t MBEDTLS_PRIVATE(output_block)[PSA_HASH_MAX_SIZE]; |
| 189 | uint8_t MBEDTLS_PRIVATE(prk)[PSA_HASH_MAX_SIZE]; |
| 190 | struct psa_mac_operation_s MBEDTLS_PRIVATE(hmac); |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 191 | } psa_hkdf_key_derivation_t; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 192 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF || |
| 193 | MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT || |
| 194 | MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 195 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 196 | typedef struct { |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 197 | uint8_t MBEDTLS_PRIVATE(data)[PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE]; |
| 198 | } psa_tls12_ecjpake_to_pms_t; |
| 199 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 200 | |
Steven Cooreman | ac8d82a | 2021-04-29 18:01:53 +0200 | [diff] [blame] | 201 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 202 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 203 | typedef enum { |
Przemek Stekiel | c4b814a | 2022-04-07 12:28:08 +0200 | [diff] [blame] | 204 | PSA_TLS12_PRF_STATE_INIT, /* no input provided */ |
| 205 | PSA_TLS12_PRF_STATE_SEED_SET, /* seed has been set */ |
| 206 | PSA_TLS12_PRF_STATE_OTHER_KEY_SET, /* other key has been set - optional */ |
| 207 | PSA_TLS12_PRF_STATE_KEY_SET, /* key has been set */ |
| 208 | PSA_TLS12_PRF_STATE_LABEL_SET, /* label has been set */ |
| 209 | PSA_TLS12_PRF_STATE_OUTPUT /* output has been started */ |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 210 | } psa_tls12_prf_key_derivation_state_t; |
| 211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 212 | typedef struct psa_tls12_prf_key_derivation_s { |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 213 | #if PSA_HASH_MAX_SIZE > 0xff |
| 214 | #error "PSA_HASH_MAX_SIZE does not fit in uint8_t" |
| 215 | #endif |
| 216 | |
| 217 | /* Indicates how many bytes in the current HMAC block have |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 218 | * not yet been read by the user. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 219 | uint8_t MBEDTLS_PRIVATE(left_in_block); |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 220 | |
| 221 | /* The 1-based number of the block. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 222 | uint8_t MBEDTLS_PRIVATE(block_number); |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 223 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 224 | psa_tls12_prf_key_derivation_state_t MBEDTLS_PRIVATE(state); |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 225 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 226 | uint8_t *MBEDTLS_PRIVATE(secret); |
| 227 | size_t MBEDTLS_PRIVATE(secret_length); |
| 228 | uint8_t *MBEDTLS_PRIVATE(seed); |
| 229 | size_t MBEDTLS_PRIVATE(seed_length); |
| 230 | uint8_t *MBEDTLS_PRIVATE(label); |
| 231 | size_t MBEDTLS_PRIVATE(label_length); |
Przemek Stekiel | 7f1c89d | 2022-04-19 14:00:37 +0200 | [diff] [blame] | 232 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Przemek Stekiel | f4e8f01 | 2022-04-07 12:23:31 +0200 | [diff] [blame] | 233 | uint8_t *MBEDTLS_PRIVATE(other_secret); |
| 234 | size_t MBEDTLS_PRIVATE(other_secret_length); |
Przemek Stekiel | 7f1c89d | 2022-04-19 14:00:37 +0200 | [diff] [blame] | 235 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 236 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 237 | uint8_t MBEDTLS_PRIVATE(Ai)[PSA_HASH_MAX_SIZE]; |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 238 | |
Janos Follath | 33434a9 | 2021-05-26 09:25:33 +0100 | [diff] [blame] | 239 | /* `HMAC_hash( prk, A( i ) + seed )` in the notation of RFC 5246, Sect. 5. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 240 | uint8_t MBEDTLS_PRIVATE(output_block)[PSA_HASH_MAX_SIZE]; |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 241 | } psa_tls12_prf_key_derivation_t; |
Steven Cooreman | ac8d82a | 2021-04-29 18:01:53 +0200 | [diff] [blame] | 242 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 243 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 245 | struct psa_key_derivation_s { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 246 | psa_algorithm_t MBEDTLS_PRIVATE(alg); |
| 247 | unsigned int MBEDTLS_PRIVATE(can_output_key) : 1; |
| 248 | size_t MBEDTLS_PRIVATE(capacity); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 249 | union { |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 250 | /* Make the union non-empty even with no supported algorithms. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 251 | uint8_t MBEDTLS_PRIVATE(dummy); |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 252 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 253 | defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \ |
| 254 | defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 255 | psa_hkdf_key_derivation_t MBEDTLS_PRIVATE(hkdf); |
Steven Cooreman | ac8d82a | 2021-04-29 18:01:53 +0200 | [diff] [blame] | 256 | #endif |
| 257 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 258 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 259 | psa_tls12_prf_key_derivation_t MBEDTLS_PRIVATE(tls12_prf); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 260 | #endif |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 261 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 262 | psa_tls12_ecjpake_to_pms_t MBEDTLS_PRIVATE(tls12_ecjpake_to_pms); |
| 263 | #endif |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 264 | } MBEDTLS_PRIVATE(ctx); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 265 | }; |
| 266 | |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 267 | /* This only zeroes out the first byte in the union, the rest is unspecified. */ |
Janos Follath | 33434a9 | 2021-05-26 09:25:33 +0100 | [diff] [blame] | 268 | #define PSA_KEY_DERIVATION_OPERATION_INIT { 0, 0, 0, { 0 } } |
Janos Follath | 0dcda95 | 2021-06-07 14:52:13 +0100 | [diff] [blame] | 269 | static inline struct psa_key_derivation_s psa_key_derivation_operation_init( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 270 | void) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 271 | { |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 272 | const struct psa_key_derivation_s v = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 273 | return v; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 276 | struct psa_key_policy_s { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 277 | psa_key_usage_t MBEDTLS_PRIVATE(usage); |
| 278 | psa_algorithm_t MBEDTLS_PRIVATE(alg); |
| 279 | psa_algorithm_t MBEDTLS_PRIVATE(alg2); |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 280 | }; |
Gilles Peskine | a3dd737 | 2019-04-19 19:42:26 +0200 | [diff] [blame] | 281 | typedef struct psa_key_policy_s psa_key_policy_t; |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 282 | |
Janos Follath | 33434a9 | 2021-05-26 09:25:33 +0100 | [diff] [blame] | 283 | #define PSA_KEY_POLICY_INIT { 0, 0, 0 } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 284 | static inline struct psa_key_policy_s psa_key_policy_init(void) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 285 | { |
| 286 | const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 287 | return v; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Gilles Peskine | 68cc433b | 2019-07-30 17:42:47 +0200 | [diff] [blame] | 290 | /* The type used internally for key sizes. |
| 291 | * Public interfaces use size_t, but internally we use a smaller type. */ |
| 292 | typedef uint16_t psa_key_bits_t; |
| 293 | /* The maximum value of the type used to represent bit-sizes. |
| 294 | * This is used to mark an invalid key size. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 295 | #define PSA_KEY_BITS_TOO_LARGE ((psa_key_bits_t) -1) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 296 | /* The maximum size of a key in bits. |
Gilles Peskine | 68cc433b | 2019-07-30 17:42:47 +0200 | [diff] [blame] | 297 | * Currently defined as the maximum that can be represented, rounded down |
| 298 | * to a whole number of bytes. |
| 299 | * This is an uncast value so that it can be used in preprocessor |
| 300 | * conditionals. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 301 | #define PSA_MAX_KEY_BITS 0xfff8 |
| 302 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 303 | /** A mask of flags that can be stored in key attributes. |
| 304 | * |
| 305 | * This type is also used internally to store flags in slots. Internal |
| 306 | * flags are defined in library/psa_crypto_core.h. Internal flags may have |
| 307 | * the same value as external flags if they are properly handled during |
| 308 | * key creation and in psa_get_key_attributes. |
| 309 | */ |
| 310 | typedef uint16_t psa_key_attributes_flag_t; |
| 311 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 312 | #define MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 313 | ((psa_key_attributes_flag_t) 0x0001) |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 314 | |
| 315 | /* A mask of key attribute flags used externally only. |
| 316 | * Only meant for internal checks inside the library. */ |
| 317 | #define MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ( \ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 318 | MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER | \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 319 | 0) |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 320 | |
| 321 | /* A mask of key attribute flags used both internally and externally. |
| 322 | * Currently there aren't any. */ |
| 323 | #define MBEDTLS_PSA_KA_MASK_DUAL_USE ( \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 324 | 0) |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 326 | typedef struct { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 327 | psa_key_type_t MBEDTLS_PRIVATE(type); |
| 328 | psa_key_bits_t MBEDTLS_PRIVATE(bits); |
| 329 | psa_key_lifetime_t MBEDTLS_PRIVATE(lifetime); |
| 330 | mbedtls_svc_key_id_t MBEDTLS_PRIVATE(id); |
| 331 | psa_key_policy_t MBEDTLS_PRIVATE(policy); |
| 332 | psa_key_attributes_flag_t MBEDTLS_PRIVATE(flags); |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 333 | } psa_core_key_attributes_t; |
| 334 | |
Janos Follath | 33434a9 | 2021-05-26 09:25:33 +0100 | [diff] [blame] | 335 | #define PSA_CORE_KEY_ATTRIBUTES_INIT { PSA_KEY_TYPE_NONE, 0, \ |
| 336 | PSA_KEY_LIFETIME_VOLATILE, \ |
| 337 | MBEDTLS_SVC_KEY_ID_INIT, \ |
| 338 | PSA_KEY_POLICY_INIT, 0 } |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 340 | struct psa_key_attributes_s { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 341 | psa_core_key_attributes_t MBEDTLS_PRIVATE(core); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 342 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 343 | psa_key_slot_number_t MBEDTLS_PRIVATE(slot_number); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 344 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 345 | void *MBEDTLS_PRIVATE(domain_parameters); |
| 346 | size_t MBEDTLS_PRIVATE(domain_parameters_size); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 347 | }; |
| 348 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 349 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 33434a9 | 2021-05-26 09:25:33 +0100 | [diff] [blame] | 350 | #define PSA_KEY_ATTRIBUTES_INIT { PSA_CORE_KEY_ATTRIBUTES_INIT, 0, NULL, 0 } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 351 | #else |
Janos Follath | 33434a9 | 2021-05-26 09:25:33 +0100 | [diff] [blame] | 352 | #define PSA_KEY_ATTRIBUTES_INIT { PSA_CORE_KEY_ATTRIBUTES_INIT, NULL, 0 } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 353 | #endif |
| 354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 355 | static inline struct psa_key_attributes_s psa_key_attributes_init(void) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 356 | { |
| 357 | const struct psa_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 358 | return v; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 359 | } |
| 360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 361 | static inline void psa_set_key_id(psa_key_attributes_t *attributes, |
| 362 | mbedtls_svc_key_id_t key) |
Gilles Peskine | dc8219a | 2019-05-15 16:11:15 +0200 | [diff] [blame] | 363 | { |
Mateusz Starzyk | 363eb29 | 2021-05-19 17:32:44 +0200 | [diff] [blame] | 364 | psa_key_lifetime_t lifetime = attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime); |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 365 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 366 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id) = key; |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 368 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 369 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime) = |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 370 | PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( |
| 371 | PSA_KEY_LIFETIME_PERSISTENT, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | PSA_KEY_LIFETIME_GET_LOCATION(lifetime)); |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 373 | } |
Gilles Peskine | dc8219a | 2019-05-15 16:11:15 +0200 | [diff] [blame] | 374 | } |
| 375 | |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 376 | static inline mbedtls_svc_key_id_t psa_get_key_id( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 377 | const psa_key_attributes_t *attributes) |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 378 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 379 | return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id); |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 380 | } |
| 381 | |
Ronald Cron | 6b5ff53 | 2020-10-16 14:38:19 +0200 | [diff] [blame] | 382 | #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 383 | static inline void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes, |
| 384 | mbedtls_key_owner_id_t owner) |
Ronald Cron | 6b5ff53 | 2020-10-16 14:38:19 +0200 | [diff] [blame] | 385 | { |
Mateusz Starzyk | 363eb29 | 2021-05-19 17:32:44 +0200 | [diff] [blame] | 386 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(owner) = owner; |
Ronald Cron | 6b5ff53 | 2020-10-16 14:38:19 +0200 | [diff] [blame] | 387 | } |
| 388 | #endif |
| 389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 390 | static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes, |
| 391 | psa_key_lifetime_t lifetime) |
Gilles Peskine | dc8219a | 2019-05-15 16:11:15 +0200 | [diff] [blame] | 392 | { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 393 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime) = lifetime; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 394 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 395 | #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER |
Mateusz Starzyk | 363eb29 | 2021-05-19 17:32:44 +0200 | [diff] [blame] | 396 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id) = 0; |
Jaeden Amero | e3cdf28 | 2019-08-20 12:58:20 +0100 | [diff] [blame] | 397 | #else |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 398 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id) = 0; |
Jaeden Amero | e3cdf28 | 2019-08-20 12:58:20 +0100 | [diff] [blame] | 399 | #endif |
| 400 | } |
Gilles Peskine | dc8219a | 2019-05-15 16:11:15 +0200 | [diff] [blame] | 401 | } |
| 402 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 403 | static inline psa_key_lifetime_t psa_get_key_lifetime( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 404 | const psa_key_attributes_t *attributes) |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 405 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 406 | return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime); |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 407 | } |
| 408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 409 | static inline void psa_extend_key_usage_flags(psa_key_usage_t *usage_flags) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 410 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 411 | if (*usage_flags & PSA_KEY_USAGE_SIGN_HASH) { |
gabor-mezei-arm | 43110b6 | 2021-06-23 16:48:08 +0200 | [diff] [blame] | 412 | *usage_flags |= PSA_KEY_USAGE_SIGN_MESSAGE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 413 | } |
gabor-mezei-arm | 86bf008 | 2021-04-29 15:57:57 +0200 | [diff] [blame] | 414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 415 | if (*usage_flags & PSA_KEY_USAGE_VERIFY_HASH) { |
gabor-mezei-arm | 43110b6 | 2021-06-23 16:48:08 +0200 | [diff] [blame] | 416 | *usage_flags |= PSA_KEY_USAGE_VERIFY_MESSAGE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes, |
| 421 | psa_key_usage_t usage_flags) |
| 422 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 423 | psa_extend_key_usage_flags(&usage_flags); |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 424 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage) = usage_flags; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | static inline psa_key_usage_t psa_get_key_usage_flags( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 428 | const psa_key_attributes_t *attributes) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 429 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 430 | return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 431 | } |
| 432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 433 | static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes, |
| 434 | psa_algorithm_t alg) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 435 | { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 436 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg) = alg; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | static inline psa_algorithm_t psa_get_key_algorithm( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 440 | const psa_key_attributes_t *attributes) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 441 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 442 | return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 443 | } |
| 444 | |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 445 | /* This function is declared in crypto_extra.h, which comes after this |
| 446 | * header file, but we need the function here, so repeat the declaration. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 447 | psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes, |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 448 | psa_key_type_t type, |
| 449 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 450 | size_t data_length); |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 452 | static inline void psa_set_key_type(psa_key_attributes_t *attributes, |
| 453 | psa_key_type_t type) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 454 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 455 | if (attributes->MBEDTLS_PRIVATE(domain_parameters) == NULL) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 456 | /* Common case: quick path */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 457 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(type) = type; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 458 | } else { |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 459 | /* Call the bigger function to free the old domain parameters. |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 460 | * Ignore any errors which may arise due to type requiring |
| 461 | * non-default domain parameters, since this function can't |
| 462 | * report errors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 463 | (void) psa_set_key_domain_parameters(attributes, type, NULL, 0); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 464 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | static inline psa_key_type_t psa_get_key_type( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 468 | const psa_key_attributes_t *attributes) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 469 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 470 | return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(type); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 471 | } |
| 472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 473 | static inline void psa_set_key_bits(psa_key_attributes_t *attributes, |
| 474 | size_t bits) |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 475 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 476 | if (bits > PSA_MAX_KEY_BITS) { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 477 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits) = PSA_KEY_BITS_TOO_LARGE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 478 | } else { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 479 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits) = (psa_key_bits_t) bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 480 | } |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 481 | } |
| 482 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 483 | static inline size_t psa_get_key_bits( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 484 | const psa_key_attributes_t *attributes) |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 485 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 486 | return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits); |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 487 | } |
| 488 | |
Paul Elliott | 1265f00 | 2022-09-09 17:15:43 +0100 | [diff] [blame] | 489 | /** |
| 490 | * \brief The context for PSA interruptible hash signing. |
Paul Elliott | 1265f00 | 2022-09-09 17:15:43 +0100 | [diff] [blame] | 491 | */ |
| 492 | struct psa_sign_hash_interruptible_operation_s { |
Paul Elliott | 2d24792 | 2022-11-29 14:54:44 +0000 | [diff] [blame] | 493 | /** Unique ID indicating which driver got assigned to do the |
| 494 | * operation. Since driver contexts are driver-specific, swapping |
| 495 | * drivers halfway through the operation is not supported. |
| 496 | * ID values are auto-generated in psa_crypto_driver_wrappers.h |
| 497 | * ID value zero means the context is not valid or not assigned to |
| 498 | * any driver (i.e. none of the driver contexts are active). */ |
| 499 | unsigned int MBEDTLS_PRIVATE(id); |
| 500 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 501 | psa_driver_sign_hash_interruptible_context_t MBEDTLS_PRIVATE(ctx); |
| 502 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 503 | unsigned int MBEDTLS_PRIVATE(error_occurred) : 1; |
| 504 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 505 | uint32_t MBEDTLS_PRIVATE(num_ops); |
Paul Elliott | 1265f00 | 2022-09-09 17:15:43 +0100 | [diff] [blame] | 506 | }; |
| 507 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 508 | #define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 } |
Paul Elliott | 1265f00 | 2022-09-09 17:15:43 +0100 | [diff] [blame] | 509 | |
| 510 | static inline struct psa_sign_hash_interruptible_operation_s |
| 511 | psa_sign_hash_interruptible_operation_init(void) |
| 512 | { |
| 513 | const struct psa_sign_hash_interruptible_operation_s v = |
| 514 | PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT; |
| 515 | |
| 516 | return v; |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * \brief The context for PSA interruptible hash verification. |
Paul Elliott | 1265f00 | 2022-09-09 17:15:43 +0100 | [diff] [blame] | 521 | */ |
| 522 | struct psa_verify_hash_interruptible_operation_s { |
Paul Elliott | 2d24792 | 2022-11-29 14:54:44 +0000 | [diff] [blame] | 523 | /** Unique ID indicating which driver got assigned to do the |
| 524 | * operation. Since driver contexts are driver-specific, swapping |
| 525 | * drivers halfway through the operation is not supported. |
| 526 | * ID values are auto-generated in psa_crypto_driver_wrappers.h |
| 527 | * ID value zero means the context is not valid or not assigned to |
| 528 | * any driver (i.e. none of the driver contexts are active). */ |
| 529 | unsigned int MBEDTLS_PRIVATE(id); |
| 530 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 531 | psa_driver_verify_hash_interruptible_context_t MBEDTLS_PRIVATE(ctx); |
| 532 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 533 | unsigned int MBEDTLS_PRIVATE(error_occurred) : 1; |
| 534 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 535 | uint32_t MBEDTLS_PRIVATE(num_ops); |
Paul Elliott | 1265f00 | 2022-09-09 17:15:43 +0100 | [diff] [blame] | 536 | }; |
| 537 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 538 | #define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 } |
Paul Elliott | 1265f00 | 2022-09-09 17:15:43 +0100 | [diff] [blame] | 539 | |
| 540 | static inline struct psa_verify_hash_interruptible_operation_s |
| 541 | psa_verify_hash_interruptible_operation_init(void) |
| 542 | { |
| 543 | const struct psa_verify_hash_interruptible_operation_s v = |
| 544 | PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT; |
| 545 | |
| 546 | return v; |
| 547 | } |
| 548 | |
Jaeden Amero | 8013f44 | 2019-08-16 16:13:51 +0100 | [diff] [blame] | 549 | #ifdef __cplusplus |
| 550 | } |
| 551 | #endif |
| 552 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 553 | #endif /* PSA_CRYPTO_STRUCT_H */ |