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 | * |
Fredrik Hesse | cc207bc | 2021-09-28 21:06:08 +0200 | [diff] [blame] | 38 | * In Mbed TLS, multipart operation structures live independently from |
| 39 | * the key. This allows Mbed TLS to free the key objects when destroying |
Gilles Peskine | b4e73e9 | 2019-08-13 15:00:57 +0200 | [diff] [blame] | 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 |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 46 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 47 | */ |
| 48 | |
| 49 | #ifndef PSA_CRYPTO_STRUCT_H |
| 50 | #define PSA_CRYPTO_STRUCT_H |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 51 | #include "mbedtls/private_access.h" |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 52 | |
Jaeden Amero | 8013f44 | 2019-08-16 16:13:51 +0100 | [diff] [blame] | 53 | #ifdef __cplusplus |
| 54 | extern "C" { |
| 55 | #endif |
| 56 | |
Ronald Cron | 7871cb1 | 2023-10-10 08:51:39 +0200 | [diff] [blame] | 57 | /* |
| 58 | * Include the build-time configuration information header. Here, we do not |
| 59 | * include `"mbedtls/build_info.h"` directly but `"psa/build_info.h"`, which |
| 60 | * is basically just an alias to it. This is to ease the maintenance of the |
| 61 | * TF-PSA-Crypto repository which has a different build system and |
| 62 | * configuration. |
| 63 | */ |
| 64 | #include "psa/build_info.h" |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 65 | |
Steven Cooreman | 61398ec | 2021-04-26 12:04:53 +0200 | [diff] [blame] | 66 | /* Include the context definition for the compiled-in drivers for the primitive |
| 67 | * algorithms. */ |
Steven Cooreman | 675501d | 2021-04-26 11:54:58 +0200 | [diff] [blame] | 68 | #include "psa/crypto_driver_contexts_primitives.h" |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 69 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 70 | struct psa_hash_operation_s { |
Antonio de Angelis | 6425a18 | 2024-01-22 11:39:34 +0000 | [diff] [blame] | 71 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C) |
| 72 | mbedtls_psa_client_handle_t handle; |
| 73 | #else |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 74 | /** Unique ID indicating which driver got assigned to do the |
| 75 | * operation. Since driver contexts are driver-specific, swapping |
| 76 | * drivers halfway through the operation is not supported. |
Ronald Cron | 980230e | 2021-04-01 15:37:49 +0200 | [diff] [blame] | 77 | * ID values are auto-generated in psa_driver_wrappers.h. |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 78 | * 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] | 79 | * any driver (i.e. the driver context is not active, in use). */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 80 | unsigned int MBEDTLS_PRIVATE(id); |
| 81 | psa_driver_hash_context_t MBEDTLS_PRIVATE(ctx); |
Antonio de Angelis | 6425a18 | 2024-01-22 11:39:34 +0000 | [diff] [blame] | 82 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 83 | }; |
Antonio de Angelis | 90d1834 | 2024-01-22 13:15:37 +0000 | [diff] [blame] | 84 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C) |
| 85 | #define PSA_HASH_OPERATION_INIT { 0 } |
| 86 | #else |
Janos Follath | 33434a9 | 2021-05-26 09:25:33 +0100 | [diff] [blame] | 87 | #define PSA_HASH_OPERATION_INIT { 0, { 0 } } |
Antonio de Angelis | 90d1834 | 2024-01-22 13:15:37 +0000 | [diff] [blame] | 88 | #endif |
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 { |
Antonio de Angelis | 6425a18 | 2024-01-22 11:39:34 +0000 | [diff] [blame] | 96 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C) |
| 97 | mbedtls_psa_client_handle_t handle; |
| 98 | #else |
Steven Cooreman | 61398ec | 2021-04-26 12:04:53 +0200 | [diff] [blame] | 99 | /** Unique ID indicating which driver got assigned to do the |
| 100 | * operation. Since driver contexts are driver-specific, swapping |
| 101 | * drivers halfway through the operation is not supported. |
| 102 | * ID values are auto-generated in psa_crypto_driver_wrappers.h |
| 103 | * ID value zero means the context is not valid or not assigned to |
| 104 | * any driver (i.e. none of the driver contexts are active). */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 105 | unsigned int MBEDTLS_PRIVATE(id); |
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 | unsigned int MBEDTLS_PRIVATE(iv_required) : 1; |
| 108 | unsigned int MBEDTLS_PRIVATE(iv_set) : 1; |
Steven Cooreman | 61398ec | 2021-04-26 12:04:53 +0200 | [diff] [blame] | 109 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 110 | uint8_t MBEDTLS_PRIVATE(default_iv_length); |
Steven Cooreman | 61398ec | 2021-04-26 12:04:53 +0200 | [diff] [blame] | 111 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 112 | psa_driver_cipher_context_t MBEDTLS_PRIVATE(ctx); |
Antonio de Angelis | 6425a18 | 2024-01-22 11:39:34 +0000 | [diff] [blame] | 113 | #endif |
Steven Cooreman | 61398ec | 2021-04-26 12:04:53 +0200 | [diff] [blame] | 114 | }; |
| 115 | |
Antonio de Angelis | 90d1834 | 2024-01-22 13:15:37 +0000 | [diff] [blame] | 116 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C) |
| 117 | #define PSA_CIPHER_OPERATION_INIT { 0 } |
| 118 | #else |
Janos Follath | 33434a9 | 2021-05-26 09:25:33 +0100 | [diff] [blame] | 119 | #define PSA_CIPHER_OPERATION_INIT { 0, 0, 0, 0, { 0 } } |
Antonio de Angelis | 90d1834 | 2024-01-22 13:15:37 +0000 | [diff] [blame] | 120 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 121 | static inline struct psa_cipher_operation_s psa_cipher_operation_init(void) |
Steven Cooreman | 61398ec | 2021-04-26 12:04:53 +0200 | [diff] [blame] | 122 | { |
| 123 | const struct psa_cipher_operation_s v = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | return v; |
Steven Cooreman | 61398ec | 2021-04-26 12:04:53 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Steven Cooreman | 3c8dd63 | 2021-04-26 12:16:27 +0200 | [diff] [blame] | 127 | /* Include the context definition for the compiled-in drivers for the composite |
| 128 | * algorithms. */ |
| 129 | #include "psa/crypto_driver_contexts_composites.h" |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 131 | struct psa_mac_operation_s { |
Antonio de Angelis | 6425a18 | 2024-01-22 11:39:34 +0000 | [diff] [blame] | 132 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C) |
| 133 | mbedtls_psa_client_handle_t handle; |
| 134 | #else |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 135 | /** Unique ID indicating which driver got assigned to do the |
| 136 | * operation. Since driver contexts are driver-specific, swapping |
| 137 | * drivers halfway through the operation is not supported. |
| 138 | * ID values are auto-generated in psa_driver_wrappers.h |
| 139 | * ID value zero means the context is not valid or not assigned to |
| 140 | * any driver (i.e. none of the driver contexts are active). */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 141 | unsigned int MBEDTLS_PRIVATE(id); |
| 142 | uint8_t MBEDTLS_PRIVATE(mac_size); |
| 143 | unsigned int MBEDTLS_PRIVATE(is_sign) : 1; |
| 144 | psa_driver_mac_context_t MBEDTLS_PRIVATE(ctx); |
Antonio de Angelis | 6425a18 | 2024-01-22 11:39:34 +0000 | [diff] [blame] | 145 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 146 | }; |
| 147 | |
Antonio de Angelis | 90d1834 | 2024-01-22 13:15:37 +0000 | [diff] [blame] | 148 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C) |
| 149 | #define PSA_MAC_OPERATION_INIT { 0 } |
| 150 | #else |
Janos Follath | 33434a9 | 2021-05-26 09:25:33 +0100 | [diff] [blame] | 151 | #define PSA_MAC_OPERATION_INIT { 0, 0, 0, { 0 } } |
Antonio de Angelis | 90d1834 | 2024-01-22 13:15:37 +0000 | [diff] [blame] | 152 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 153 | static inline struct psa_mac_operation_s psa_mac_operation_init(void) |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 154 | { |
| 155 | const struct psa_mac_operation_s v = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 156 | return v; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 159 | struct psa_aead_operation_s { |
Antonio de Angelis | 6425a18 | 2024-01-22 11:39:34 +0000 | [diff] [blame] | 160 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C) |
| 161 | mbedtls_psa_client_handle_t handle; |
| 162 | #else |
Paul Elliott | 6504aa6 | 2021-04-20 17:09:36 +0100 | [diff] [blame] | 163 | /** Unique ID indicating which driver got assigned to do the |
| 164 | * operation. Since driver contexts are driver-specific, swapping |
| 165 | * drivers halfway through the operation is not supported. |
| 166 | * ID values are auto-generated in psa_crypto_driver_wrappers.h |
| 167 | * ID value zero means the context is not valid or not assigned to |
| 168 | * any driver (i.e. none of the driver contexts are active). */ |
Paul Elliott | c7e7fe5 | 2021-09-27 09:23:40 +0100 | [diff] [blame] | 169 | unsigned int MBEDTLS_PRIVATE(id); |
Paul Elliott | 6504aa6 | 2021-04-20 17:09:36 +0100 | [diff] [blame] | 170 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 171 | psa_algorithm_t MBEDTLS_PRIVATE(alg); |
Paul Elliott | 71b0567 | 2021-09-24 11:18:13 +0100 | [diff] [blame] | 172 | psa_key_type_t MBEDTLS_PRIVATE(key_type); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 173 | |
Paul Elliott | 71b0567 | 2021-09-24 11:18:13 +0100 | [diff] [blame] | 174 | size_t MBEDTLS_PRIVATE(ad_remaining); |
| 175 | size_t MBEDTLS_PRIVATE(body_remaining); |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 176 | |
Paul Elliott | 71b0567 | 2021-09-24 11:18:13 +0100 | [diff] [blame] | 177 | unsigned int MBEDTLS_PRIVATE(nonce_set) : 1; |
| 178 | unsigned int MBEDTLS_PRIVATE(lengths_set) : 1; |
| 179 | unsigned int MBEDTLS_PRIVATE(ad_started) : 1; |
| 180 | unsigned int MBEDTLS_PRIVATE(body_started) : 1; |
| 181 | unsigned int MBEDTLS_PRIVATE(is_encrypt) : 1; |
Paul Elliott | 07a30c4 | 2021-04-20 14:13:23 +0100 | [diff] [blame] | 182 | |
Paul Elliott | 71b0567 | 2021-09-24 11:18:13 +0100 | [diff] [blame] | 183 | psa_driver_aead_context_t MBEDTLS_PRIVATE(ctx); |
Antonio de Angelis | 6425a18 | 2024-01-22 11:39:34 +0000 | [diff] [blame] | 184 | #endif |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 185 | }; |
| 186 | |
Antonio de Angelis | 90d1834 | 2024-01-22 13:15:37 +0000 | [diff] [blame] | 187 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C) |
| 188 | #define PSA_AEAD_OPERATION_INIT { 0 } |
| 189 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 190 | #define PSA_AEAD_OPERATION_INIT { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, { 0 } } |
Antonio de Angelis | 90d1834 | 2024-01-22 13:15:37 +0000 | [diff] [blame] | 191 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 192 | static inline struct psa_aead_operation_s psa_aead_operation_init(void) |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 193 | { |
| 194 | const struct psa_aead_operation_s v = PSA_AEAD_OPERATION_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | return v; |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Ronald Cron | 2f10fce | 2023-01-11 09:21:47 +0100 | [diff] [blame] | 198 | /* Include the context definition for the compiled-in drivers for the key |
| 199 | * derivation algorithms. */ |
| 200 | #include "psa/crypto_driver_contexts_key_derivation.h" |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 202 | struct psa_key_derivation_s { |
Antonio de Angelis | 6425a18 | 2024-01-22 11:39:34 +0000 | [diff] [blame] | 203 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C) |
| 204 | mbedtls_psa_client_handle_t handle; |
| 205 | #else |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 206 | psa_algorithm_t MBEDTLS_PRIVATE(alg); |
| 207 | unsigned int MBEDTLS_PRIVATE(can_output_key) : 1; |
| 208 | size_t MBEDTLS_PRIVATE(capacity); |
Ronald Cron | 2f10fce | 2023-01-11 09:21:47 +0100 | [diff] [blame] | 209 | psa_driver_key_derivation_context_t MBEDTLS_PRIVATE(ctx); |
Antonio de Angelis | 6425a18 | 2024-01-22 11:39:34 +0000 | [diff] [blame] | 210 | #endif |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 211 | }; |
| 212 | |
Antonio de Angelis | 90d1834 | 2024-01-22 13:15:37 +0000 | [diff] [blame] | 213 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C) |
| 214 | #define PSA_KEY_DERIVATION_OPERATION_INIT { 0 } |
| 215 | #else |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 216 | /* 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] | 217 | #define PSA_KEY_DERIVATION_OPERATION_INIT { 0, 0, 0, { 0 } } |
Antonio de Angelis | 90d1834 | 2024-01-22 13:15:37 +0000 | [diff] [blame] | 218 | #endif |
Janos Follath | 0dcda95 | 2021-06-07 14:52:13 +0100 | [diff] [blame] | 219 | static inline struct psa_key_derivation_s psa_key_derivation_operation_init( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 220 | void) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 221 | { |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 222 | const struct psa_key_derivation_s v = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 223 | return v; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 224 | } |
| 225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 226 | struct psa_key_policy_s { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 227 | psa_key_usage_t MBEDTLS_PRIVATE(usage); |
| 228 | psa_algorithm_t MBEDTLS_PRIVATE(alg); |
| 229 | psa_algorithm_t MBEDTLS_PRIVATE(alg2); |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 230 | }; |
Gilles Peskine | a3dd737 | 2019-04-19 19:42:26 +0200 | [diff] [blame] | 231 | typedef struct psa_key_policy_s psa_key_policy_t; |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 232 | |
Janos Follath | 33434a9 | 2021-05-26 09:25:33 +0100 | [diff] [blame] | 233 | #define PSA_KEY_POLICY_INIT { 0, 0, 0 } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 234 | static inline struct psa_key_policy_s psa_key_policy_init(void) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 235 | { |
| 236 | const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 237 | return v; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Gilles Peskine | 68cc433b | 2019-07-30 17:42:47 +0200 | [diff] [blame] | 240 | /* The type used internally for key sizes. |
| 241 | * Public interfaces use size_t, but internally we use a smaller type. */ |
| 242 | typedef uint16_t psa_key_bits_t; |
| 243 | /* The maximum value of the type used to represent bit-sizes. |
| 244 | * This is used to mark an invalid key size. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 245 | #define PSA_KEY_BITS_TOO_LARGE ((psa_key_bits_t) -1) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 246 | /* The maximum size of a key in bits. |
Gilles Peskine | 68cc433b | 2019-07-30 17:42:47 +0200 | [diff] [blame] | 247 | * Currently defined as the maximum that can be represented, rounded down |
| 248 | * to a whole number of bytes. |
| 249 | * This is an uncast value so that it can be used in preprocessor |
| 250 | * conditionals. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 251 | #define PSA_MAX_KEY_BITS 0xfff8 |
| 252 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 253 | /** A mask of flags that can be stored in key attributes. |
| 254 | * |
| 255 | * This type is also used internally to store flags in slots. Internal |
| 256 | * flags are defined in library/psa_crypto_core.h. Internal flags may have |
| 257 | * the same value as external flags if they are properly handled during |
| 258 | * key creation and in psa_get_key_attributes. |
| 259 | */ |
| 260 | typedef uint16_t psa_key_attributes_flag_t; |
| 261 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 262 | #define MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 263 | ((psa_key_attributes_flag_t) 0x0001) |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 264 | |
| 265 | /* A mask of key attribute flags used externally only. |
| 266 | * Only meant for internal checks inside the library. */ |
| 267 | #define MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ( \ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 268 | MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER | \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 269 | 0) |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 270 | |
| 271 | /* A mask of key attribute flags used both internally and externally. |
| 272 | * Currently there aren't any. */ |
| 273 | #define MBEDTLS_PSA_KA_MASK_DUAL_USE ( \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 274 | 0) |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 276 | typedef struct { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 277 | psa_key_type_t MBEDTLS_PRIVATE(type); |
| 278 | psa_key_bits_t MBEDTLS_PRIVATE(bits); |
| 279 | psa_key_lifetime_t MBEDTLS_PRIVATE(lifetime); |
| 280 | mbedtls_svc_key_id_t MBEDTLS_PRIVATE(id); |
| 281 | psa_key_policy_t MBEDTLS_PRIVATE(policy); |
| 282 | psa_key_attributes_flag_t MBEDTLS_PRIVATE(flags); |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 283 | } psa_core_key_attributes_t; |
| 284 | |
Janos Follath | 33434a9 | 2021-05-26 09:25:33 +0100 | [diff] [blame] | 285 | #define PSA_CORE_KEY_ATTRIBUTES_INIT { PSA_KEY_TYPE_NONE, 0, \ |
| 286 | PSA_KEY_LIFETIME_VOLATILE, \ |
| 287 | MBEDTLS_SVC_KEY_ID_INIT, \ |
| 288 | PSA_KEY_POLICY_INIT, 0 } |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 290 | struct psa_key_attributes_s { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 291 | psa_core_key_attributes_t MBEDTLS_PRIVATE(core); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 292 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 293 | psa_key_slot_number_t MBEDTLS_PRIVATE(slot_number); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 294 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 9deb549 | 2023-12-18 21:01:18 +0100 | [diff] [blame] | 295 | /* Unlike normal buffers, there are three cases for domain_parameters |
| 296 | * and domain_parameters_size: |
| 297 | * - domain_parameters_size == SIZE_MAX && domain_parameters == NULL: |
| 298 | * Access to domain parameters is not supported for this key. |
| 299 | * This is a hack which should not exist, intended for keys managed |
Gilles Peskine | 1a9e05b | 2023-12-19 12:23:22 +0100 | [diff] [blame] | 300 | * by a driver, because drivers don't support domain parameters. |
Gilles Peskine | 9deb549 | 2023-12-18 21:01:18 +0100 | [diff] [blame] | 301 | * - domain_parameters_size == 0 && domain_parameters == NULL: |
| 302 | * The domain parameters are empty. |
| 303 | * - domain_parameters_size > 0 && |
| 304 | * domain_parameters == valid pointer to domain_parameters_size bytes: |
| 305 | * The domain parameters are non-empty. |
| 306 | */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 307 | void *MBEDTLS_PRIVATE(domain_parameters); |
| 308 | size_t MBEDTLS_PRIVATE(domain_parameters_size); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 309 | }; |
| 310 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 311 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 33434a9 | 2021-05-26 09:25:33 +0100 | [diff] [blame] | 312 | #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] | 313 | #else |
Janos Follath | 33434a9 | 2021-05-26 09:25:33 +0100 | [diff] [blame] | 314 | #define PSA_KEY_ATTRIBUTES_INIT { PSA_CORE_KEY_ATTRIBUTES_INIT, NULL, 0 } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 315 | #endif |
| 316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 317 | static inline struct psa_key_attributes_s psa_key_attributes_init(void) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 318 | { |
| 319 | const struct psa_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 320 | return v; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 323 | static inline void psa_set_key_id(psa_key_attributes_t *attributes, |
| 324 | mbedtls_svc_key_id_t key) |
Gilles Peskine | dc8219a | 2019-05-15 16:11:15 +0200 | [diff] [blame] | 325 | { |
Mateusz Starzyk | 363eb29 | 2021-05-19 17:32:44 +0200 | [diff] [blame] | 326 | psa_key_lifetime_t lifetime = attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime); |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 327 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 328 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id) = key; |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 330 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 331 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime) = |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 332 | PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( |
| 333 | PSA_KEY_LIFETIME_PERSISTENT, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 334 | PSA_KEY_LIFETIME_GET_LOCATION(lifetime)); |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 335 | } |
Gilles Peskine | dc8219a | 2019-05-15 16:11:15 +0200 | [diff] [blame] | 336 | } |
| 337 | |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 338 | static inline mbedtls_svc_key_id_t psa_get_key_id( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 339 | const psa_key_attributes_t *attributes) |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 340 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 341 | return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id); |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 342 | } |
| 343 | |
Ronald Cron | 6b5ff53 | 2020-10-16 14:38:19 +0200 | [diff] [blame] | 344 | #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 345 | static inline void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes, |
| 346 | mbedtls_key_owner_id_t owner) |
Ronald Cron | 6b5ff53 | 2020-10-16 14:38:19 +0200 | [diff] [blame] | 347 | { |
Mateusz Starzyk | 363eb29 | 2021-05-19 17:32:44 +0200 | [diff] [blame] | 348 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(owner) = owner; |
Ronald Cron | 6b5ff53 | 2020-10-16 14:38:19 +0200 | [diff] [blame] | 349 | } |
| 350 | #endif |
| 351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 352 | static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes, |
| 353 | psa_key_lifetime_t lifetime) |
Gilles Peskine | dc8219a | 2019-05-15 16:11:15 +0200 | [diff] [blame] | 354 | { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 355 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime) = lifetime; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 356 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 357 | #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER |
Mateusz Starzyk | 363eb29 | 2021-05-19 17:32:44 +0200 | [diff] [blame] | 358 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id) = 0; |
Jaeden Amero | e3cdf28 | 2019-08-20 12:58:20 +0100 | [diff] [blame] | 359 | #else |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 360 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id) = 0; |
Jaeden Amero | e3cdf28 | 2019-08-20 12:58:20 +0100 | [diff] [blame] | 361 | #endif |
| 362 | } |
Gilles Peskine | dc8219a | 2019-05-15 16:11:15 +0200 | [diff] [blame] | 363 | } |
| 364 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 365 | static inline psa_key_lifetime_t psa_get_key_lifetime( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 366 | const psa_key_attributes_t *attributes) |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 367 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 368 | return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime); |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 369 | } |
| 370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 371 | 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] | 372 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 373 | if (*usage_flags & PSA_KEY_USAGE_SIGN_HASH) { |
gabor-mezei-arm | 43110b6 | 2021-06-23 16:48:08 +0200 | [diff] [blame] | 374 | *usage_flags |= PSA_KEY_USAGE_SIGN_MESSAGE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 375 | } |
gabor-mezei-arm | 86bf008 | 2021-04-29 15:57:57 +0200 | [diff] [blame] | 376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 377 | if (*usage_flags & PSA_KEY_USAGE_VERIFY_HASH) { |
gabor-mezei-arm | 43110b6 | 2021-06-23 16:48:08 +0200 | [diff] [blame] | 378 | *usage_flags |= PSA_KEY_USAGE_VERIFY_MESSAGE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 379 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes, |
| 383 | psa_key_usage_t usage_flags) |
| 384 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 385 | psa_extend_key_usage_flags(&usage_flags); |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 386 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage) = usage_flags; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | static inline psa_key_usage_t psa_get_key_usage_flags( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 390 | const psa_key_attributes_t *attributes) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 391 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 392 | return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 393 | } |
| 394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 395 | static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes, |
| 396 | psa_algorithm_t alg) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 397 | { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 398 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg) = alg; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | static inline psa_algorithm_t psa_get_key_algorithm( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 402 | const psa_key_attributes_t *attributes) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 403 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 404 | return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 405 | } |
| 406 | |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 407 | /* This function is declared in crypto_extra.h, which comes after this |
| 408 | * header file, but we need the function here, so repeat the declaration. */ |
Matthias Schulz | 5a39c4e | 2023-11-09 15:53:01 +0100 | [diff] [blame] | 409 | #if !defined(PSA_SET_KEY_DOMAIN_PARAMETERS) |
| 410 | #define PSA_SET_KEY_DOMAIN_PARAMETERS |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 411 | 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] | 412 | psa_key_type_t type, |
| 413 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 414 | size_t data_length); |
Matthias Schulz | 5a39c4e | 2023-11-09 15:53:01 +0100 | [diff] [blame] | 415 | #endif /* PSA_SET_KEY_DOMAIN_PARAMETERS */ |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | static inline void psa_set_key_type(psa_key_attributes_t *attributes, |
| 418 | psa_key_type_t type) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 419 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 420 | if (attributes->MBEDTLS_PRIVATE(domain_parameters) == NULL) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 421 | /* Common case: quick path */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 422 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(type) = type; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 423 | } else { |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 424 | /* Call the bigger function to free the old domain parameters. |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 425 | * Ignore any errors which may arise due to type requiring |
| 426 | * non-default domain parameters, since this function can't |
| 427 | * report errors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 428 | (void) psa_set_key_domain_parameters(attributes, type, NULL, 0); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 429 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | static inline psa_key_type_t psa_get_key_type( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 433 | const psa_key_attributes_t *attributes) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 434 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 435 | return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(type); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 436 | } |
| 437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 438 | static inline void psa_set_key_bits(psa_key_attributes_t *attributes, |
| 439 | size_t bits) |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 440 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 441 | if (bits > PSA_MAX_KEY_BITS) { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 442 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits) = PSA_KEY_BITS_TOO_LARGE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 443 | } else { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 444 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits) = (psa_key_bits_t) bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 445 | } |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 446 | } |
| 447 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 448 | static inline size_t psa_get_key_bits( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 449 | const psa_key_attributes_t *attributes) |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 450 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 451 | return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits); |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Paul Elliott | 1265f00 | 2022-09-09 17:15:43 +0100 | [diff] [blame] | 454 | /** |
| 455 | * \brief The context for PSA interruptible hash signing. |
Paul Elliott | 1265f00 | 2022-09-09 17:15:43 +0100 | [diff] [blame] | 456 | */ |
| 457 | struct psa_sign_hash_interruptible_operation_s { |
Antonio de Angelis | 4380a33 | 2024-02-02 14:21:24 +0000 | [diff] [blame^] | 458 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C) |
| 459 | mbedtls_psa_client_handle_t handle; |
| 460 | #else |
Paul Elliott | 2d24792 | 2022-11-29 14:54:44 +0000 | [diff] [blame] | 461 | /** Unique ID indicating which driver got assigned to do the |
| 462 | * operation. Since driver contexts are driver-specific, swapping |
| 463 | * drivers halfway through the operation is not supported. |
| 464 | * ID values are auto-generated in psa_crypto_driver_wrappers.h |
| 465 | * ID value zero means the context is not valid or not assigned to |
| 466 | * any driver (i.e. none of the driver contexts are active). */ |
| 467 | unsigned int MBEDTLS_PRIVATE(id); |
| 468 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 469 | psa_driver_sign_hash_interruptible_context_t MBEDTLS_PRIVATE(ctx); |
| 470 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 471 | unsigned int MBEDTLS_PRIVATE(error_occurred) : 1; |
| 472 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 473 | uint32_t MBEDTLS_PRIVATE(num_ops); |
Antonio de Angelis | 4380a33 | 2024-02-02 14:21:24 +0000 | [diff] [blame^] | 474 | #endif |
Paul Elliott | 1265f00 | 2022-09-09 17:15:43 +0100 | [diff] [blame] | 475 | }; |
| 476 | |
Antonio de Angelis | 4380a33 | 2024-02-02 14:21:24 +0000 | [diff] [blame^] | 477 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C) |
| 478 | #define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 } |
| 479 | #else |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 480 | #define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 } |
Antonio de Angelis | 4380a33 | 2024-02-02 14:21:24 +0000 | [diff] [blame^] | 481 | #endif |
Paul Elliott | 1265f00 | 2022-09-09 17:15:43 +0100 | [diff] [blame] | 482 | |
| 483 | static inline struct psa_sign_hash_interruptible_operation_s |
| 484 | psa_sign_hash_interruptible_operation_init(void) |
| 485 | { |
| 486 | const struct psa_sign_hash_interruptible_operation_s v = |
| 487 | PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT; |
| 488 | |
| 489 | return v; |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * \brief The context for PSA interruptible hash verification. |
Paul Elliott | 1265f00 | 2022-09-09 17:15:43 +0100 | [diff] [blame] | 494 | */ |
| 495 | struct psa_verify_hash_interruptible_operation_s { |
Antonio de Angelis | 4380a33 | 2024-02-02 14:21:24 +0000 | [diff] [blame^] | 496 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C) |
| 497 | mbedtls_psa_client_handle_t handle; |
| 498 | #else |
Paul Elliott | 2d24792 | 2022-11-29 14:54:44 +0000 | [diff] [blame] | 499 | /** Unique ID indicating which driver got assigned to do the |
| 500 | * operation. Since driver contexts are driver-specific, swapping |
| 501 | * drivers halfway through the operation is not supported. |
| 502 | * ID values are auto-generated in psa_crypto_driver_wrappers.h |
| 503 | * ID value zero means the context is not valid or not assigned to |
| 504 | * any driver (i.e. none of the driver contexts are active). */ |
| 505 | unsigned int MBEDTLS_PRIVATE(id); |
| 506 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 507 | psa_driver_verify_hash_interruptible_context_t MBEDTLS_PRIVATE(ctx); |
| 508 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 509 | unsigned int MBEDTLS_PRIVATE(error_occurred) : 1; |
| 510 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 511 | uint32_t MBEDTLS_PRIVATE(num_ops); |
Antonio de Angelis | 4380a33 | 2024-02-02 14:21:24 +0000 | [diff] [blame^] | 512 | #endif |
Paul Elliott | 1265f00 | 2022-09-09 17:15:43 +0100 | [diff] [blame] | 513 | }; |
| 514 | |
Antonio de Angelis | 4380a33 | 2024-02-02 14:21:24 +0000 | [diff] [blame^] | 515 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C) |
| 516 | #define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 } |
| 517 | #else |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 518 | #define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 } |
Antonio de Angelis | 4380a33 | 2024-02-02 14:21:24 +0000 | [diff] [blame^] | 519 | #endif |
Paul Elliott | 1265f00 | 2022-09-09 17:15:43 +0100 | [diff] [blame] | 520 | |
| 521 | static inline struct psa_verify_hash_interruptible_operation_s |
| 522 | psa_verify_hash_interruptible_operation_init(void) |
| 523 | { |
| 524 | const struct psa_verify_hash_interruptible_operation_s v = |
| 525 | PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT; |
| 526 | |
| 527 | return v; |
| 528 | } |
| 529 | |
Jaeden Amero | 8013f44 | 2019-08-16 16:13:51 +0100 | [diff] [blame] | 530 | #ifdef __cplusplus |
| 531 | } |
| 532 | #endif |
| 533 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 534 | #endif /* PSA_CRYPTO_STRUCT_H */ |