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 | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame^] | 226 | struct psa_key_production_parameters_s { |
Gilles Peskine | 6d81cbc | 2024-02-12 16:25:19 +0100 | [diff] [blame] | 227 | /* Future versions may add other fields in this structure. */ |
| 228 | uint32_t flags; |
| 229 | uint8_t data[]; |
| 230 | }; |
| 231 | |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame^] | 232 | /** The default production parameters for key generation or key derivation. |
Gilles Peskine | 6d81cbc | 2024-02-12 16:25:19 +0100 | [diff] [blame] | 233 | * |
| 234 | * Calling psa_generate_key_ext() or psa_key_derivation_output_key_ext() |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame^] | 235 | * with `params=PSA_KEY_PRODUCTION_PARAMETERS_INIT` and |
| 236 | * `params_data_length == 0` is equivalent to |
Gilles Peskine | 6d81cbc | 2024-02-12 16:25:19 +0100 | [diff] [blame] | 237 | * calling psa_generate_key() or psa_key_derivation_output_key() |
| 238 | * respectively. |
| 239 | */ |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame^] | 240 | #define PSA_KEY_PRODUCTION_PARAMETERS_INIT { 0 } |
Gilles Peskine | 6d81cbc | 2024-02-12 16:25:19 +0100 | [diff] [blame] | 241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 242 | struct psa_key_policy_s { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 243 | psa_key_usage_t MBEDTLS_PRIVATE(usage); |
| 244 | psa_algorithm_t MBEDTLS_PRIVATE(alg); |
| 245 | psa_algorithm_t MBEDTLS_PRIVATE(alg2); |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 246 | }; |
Gilles Peskine | a3dd737 | 2019-04-19 19:42:26 +0200 | [diff] [blame] | 247 | typedef struct psa_key_policy_s psa_key_policy_t; |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 248 | |
Janos Follath | 33434a9 | 2021-05-26 09:25:33 +0100 | [diff] [blame] | 249 | #define PSA_KEY_POLICY_INIT { 0, 0, 0 } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 250 | static inline struct psa_key_policy_s psa_key_policy_init(void) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 251 | { |
| 252 | const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 253 | return v; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Gilles Peskine | 68cc433b | 2019-07-30 17:42:47 +0200 | [diff] [blame] | 256 | /* The type used internally for key sizes. |
| 257 | * Public interfaces use size_t, but internally we use a smaller type. */ |
| 258 | typedef uint16_t psa_key_bits_t; |
| 259 | /* The maximum value of the type used to represent bit-sizes. |
| 260 | * This is used to mark an invalid key size. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 261 | #define PSA_KEY_BITS_TOO_LARGE ((psa_key_bits_t) -1) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 262 | /* The maximum size of a key in bits. |
Gilles Peskine | 68cc433b | 2019-07-30 17:42:47 +0200 | [diff] [blame] | 263 | * Currently defined as the maximum that can be represented, rounded down |
| 264 | * to a whole number of bytes. |
| 265 | * This is an uncast value so that it can be used in preprocessor |
| 266 | * conditionals. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 267 | #define PSA_MAX_KEY_BITS 0xfff8 |
| 268 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 269 | /** A mask of flags that can be stored in key attributes. |
| 270 | * |
| 271 | * This type is also used internally to store flags in slots. Internal |
| 272 | * flags are defined in library/psa_crypto_core.h. Internal flags may have |
| 273 | * the same value as external flags if they are properly handled during |
| 274 | * key creation and in psa_get_key_attributes. |
| 275 | */ |
| 276 | typedef uint16_t psa_key_attributes_flag_t; |
| 277 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 278 | #define MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 279 | ((psa_key_attributes_flag_t) 0x0001) |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 280 | |
| 281 | /* A mask of key attribute flags used externally only. |
| 282 | * Only meant for internal checks inside the library. */ |
| 283 | #define MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ( \ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 284 | MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER | \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 285 | 0) |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 286 | |
| 287 | /* A mask of key attribute flags used both internally and externally. |
| 288 | * Currently there aren't any. */ |
| 289 | #define MBEDTLS_PSA_KA_MASK_DUAL_USE ( \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 290 | 0) |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 292 | typedef struct { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 293 | psa_key_type_t MBEDTLS_PRIVATE(type); |
| 294 | psa_key_bits_t MBEDTLS_PRIVATE(bits); |
| 295 | psa_key_lifetime_t MBEDTLS_PRIVATE(lifetime); |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 296 | psa_key_policy_t MBEDTLS_PRIVATE(policy); |
| 297 | psa_key_attributes_flag_t MBEDTLS_PRIVATE(flags); |
Antonio de Angelis | 6fb1be6 | 2024-02-02 14:05:32 +0000 | [diff] [blame] | 298 | /* This type has a different layout in the client view wrt the |
| 299 | * service view of the key id, i.e. in service view usually is |
Antonio de Angelis | 6932e29 | 2024-02-05 09:49:43 +0000 | [diff] [blame] | 300 | * expected to have MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined |
Antonio de Angelis | 6fb1be6 | 2024-02-02 14:05:32 +0000 | [diff] [blame] | 301 | * thus adding an owner field to the standard psa_key_id_t. For |
| 302 | * implementations with client/service separation, this means the |
| 303 | * object will be marshalled through a transport channel and |
| 304 | * interpreted differently at each side of the transport. Placing |
| 305 | * it at the end of structures allows to interpret the structure |
| 306 | * at the client without reorganizing the memory layout of the |
| 307 | * struct |
| 308 | */ |
Antonio de Angelis | 667cad5 | 2024-01-24 13:34:46 +0000 | [diff] [blame] | 309 | mbedtls_svc_key_id_t MBEDTLS_PRIVATE(id); |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 310 | } psa_core_key_attributes_t; |
| 311 | |
Janos Follath | 33434a9 | 2021-05-26 09:25:33 +0100 | [diff] [blame] | 312 | #define PSA_CORE_KEY_ATTRIBUTES_INIT { PSA_KEY_TYPE_NONE, 0, \ |
| 313 | PSA_KEY_LIFETIME_VOLATILE, \ |
Antonio de Angelis | 667cad5 | 2024-01-24 13:34:46 +0000 | [diff] [blame] | 314 | PSA_KEY_POLICY_INIT, 0, \ |
| 315 | MBEDTLS_SVC_KEY_ID_INIT } |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 317 | struct psa_key_attributes_s { |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 318 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 319 | psa_key_slot_number_t MBEDTLS_PRIVATE(slot_number); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 320 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 9deb549 | 2023-12-18 21:01:18 +0100 | [diff] [blame] | 321 | /* Unlike normal buffers, there are three cases for domain_parameters |
| 322 | * and domain_parameters_size: |
| 323 | * - domain_parameters_size == SIZE_MAX && domain_parameters == NULL: |
| 324 | * Access to domain parameters is not supported for this key. |
| 325 | * This is a hack which should not exist, intended for keys managed |
Gilles Peskine | 1a9e05b | 2023-12-19 12:23:22 +0100 | [diff] [blame] | 326 | * by a driver, because drivers don't support domain parameters. |
Gilles Peskine | 9deb549 | 2023-12-18 21:01:18 +0100 | [diff] [blame] | 327 | * - domain_parameters_size == 0 && domain_parameters == NULL: |
| 328 | * The domain parameters are empty. |
| 329 | * - domain_parameters_size > 0 && |
| 330 | * domain_parameters == valid pointer to domain_parameters_size bytes: |
| 331 | * The domain parameters are non-empty. |
| 332 | */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 333 | void *MBEDTLS_PRIVATE(domain_parameters); |
| 334 | size_t MBEDTLS_PRIVATE(domain_parameters_size); |
Antonio de Angelis | 6fb1be6 | 2024-02-02 14:05:32 +0000 | [diff] [blame] | 335 | /* With client/service separation, struct psa_key_attributes_s is |
| 336 | * marshalled through a transport channel between the client and |
| 337 | * service side implementation of the PSA Crypto APIs, thus having |
| 338 | * the mbedtls_svc_key_id_t id as the last field of this structure |
| 339 | * allows for a more efficient marshalling/unmarshalling of parameters |
| 340 | */ |
Antonio de Angelis | 667cad5 | 2024-01-24 13:34:46 +0000 | [diff] [blame] | 341 | psa_core_key_attributes_t MBEDTLS_PRIVATE(core); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 342 | }; |
| 343 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 344 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Antonio de Angelis | 667cad5 | 2024-01-24 13:34:46 +0000 | [diff] [blame] | 345 | #define PSA_KEY_ATTRIBUTES_INIT { 0, NULL, 0, PSA_CORE_KEY_ATTRIBUTES_INIT } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 346 | #else |
Antonio de Angelis | 667cad5 | 2024-01-24 13:34:46 +0000 | [diff] [blame] | 347 | #define PSA_KEY_ATTRIBUTES_INIT { NULL, 0, PSA_CORE_KEY_ATTRIBUTES_INIT } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 348 | #endif |
| 349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 350 | static inline struct psa_key_attributes_s psa_key_attributes_init(void) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 351 | { |
| 352 | const struct psa_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 353 | return v; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 356 | static inline void psa_set_key_id(psa_key_attributes_t *attributes, |
| 357 | mbedtls_svc_key_id_t key) |
Gilles Peskine | dc8219a | 2019-05-15 16:11:15 +0200 | [diff] [blame] | 358 | { |
Mateusz Starzyk | 363eb29 | 2021-05-19 17:32:44 +0200 | [diff] [blame] | 359 | psa_key_lifetime_t lifetime = attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime); |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 360 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 361 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id) = key; |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 363 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 364 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime) = |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 365 | PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( |
| 366 | PSA_KEY_LIFETIME_PERSISTENT, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 367 | PSA_KEY_LIFETIME_GET_LOCATION(lifetime)); |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 368 | } |
Gilles Peskine | dc8219a | 2019-05-15 16:11:15 +0200 | [diff] [blame] | 369 | } |
| 370 | |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 371 | static inline mbedtls_svc_key_id_t psa_get_key_id( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | const psa_key_attributes_t *attributes) |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 373 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 374 | return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id); |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 375 | } |
| 376 | |
Ronald Cron | 6b5ff53 | 2020-10-16 14:38:19 +0200 | [diff] [blame] | 377 | #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 378 | static inline void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes, |
| 379 | mbedtls_key_owner_id_t owner) |
Ronald Cron | 6b5ff53 | 2020-10-16 14:38:19 +0200 | [diff] [blame] | 380 | { |
Mateusz Starzyk | 363eb29 | 2021-05-19 17:32:44 +0200 | [diff] [blame] | 381 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(owner) = owner; |
Ronald Cron | 6b5ff53 | 2020-10-16 14:38:19 +0200 | [diff] [blame] | 382 | } |
| 383 | #endif |
| 384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 385 | static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes, |
| 386 | psa_key_lifetime_t lifetime) |
Gilles Peskine | dc8219a | 2019-05-15 16:11:15 +0200 | [diff] [blame] | 387 | { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 388 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime) = lifetime; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 389 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 390 | #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER |
Mateusz Starzyk | 363eb29 | 2021-05-19 17:32:44 +0200 | [diff] [blame] | 391 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id) = 0; |
Jaeden Amero | e3cdf28 | 2019-08-20 12:58:20 +0100 | [diff] [blame] | 392 | #else |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 393 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id) = 0; |
Jaeden Amero | e3cdf28 | 2019-08-20 12:58:20 +0100 | [diff] [blame] | 394 | #endif |
| 395 | } |
Gilles Peskine | dc8219a | 2019-05-15 16:11:15 +0200 | [diff] [blame] | 396 | } |
| 397 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 398 | static inline psa_key_lifetime_t psa_get_key_lifetime( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 399 | const psa_key_attributes_t *attributes) |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 400 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 401 | return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime); |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 402 | } |
| 403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 404 | 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] | 405 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 406 | if (*usage_flags & PSA_KEY_USAGE_SIGN_HASH) { |
gabor-mezei-arm | 43110b6 | 2021-06-23 16:48:08 +0200 | [diff] [blame] | 407 | *usage_flags |= PSA_KEY_USAGE_SIGN_MESSAGE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 408 | } |
gabor-mezei-arm | 86bf008 | 2021-04-29 15:57:57 +0200 | [diff] [blame] | 409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 410 | if (*usage_flags & PSA_KEY_USAGE_VERIFY_HASH) { |
gabor-mezei-arm | 43110b6 | 2021-06-23 16:48:08 +0200 | [diff] [blame] | 411 | *usage_flags |= PSA_KEY_USAGE_VERIFY_MESSAGE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 412 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes, |
| 416 | psa_key_usage_t usage_flags) |
| 417 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 418 | psa_extend_key_usage_flags(&usage_flags); |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 419 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage) = usage_flags; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static inline psa_key_usage_t psa_get_key_usage_flags( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 423 | const psa_key_attributes_t *attributes) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 424 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 425 | return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 426 | } |
| 427 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 428 | static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes, |
| 429 | psa_algorithm_t alg) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 430 | { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 431 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg) = alg; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | static inline psa_algorithm_t psa_get_key_algorithm( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 435 | const psa_key_attributes_t *attributes) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 436 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 437 | return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 438 | } |
| 439 | |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 440 | /* This function is declared in crypto_extra.h, which comes after this |
| 441 | * header file, but we need the function here, so repeat the declaration. */ |
Matthias Schulz | 5a39c4e | 2023-11-09 15:53:01 +0100 | [diff] [blame] | 442 | #if !defined(PSA_SET_KEY_DOMAIN_PARAMETERS) |
| 443 | #define PSA_SET_KEY_DOMAIN_PARAMETERS |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 444 | 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] | 445 | psa_key_type_t type, |
| 446 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 447 | size_t data_length); |
Matthias Schulz | 5a39c4e | 2023-11-09 15:53:01 +0100 | [diff] [blame] | 448 | #endif /* PSA_SET_KEY_DOMAIN_PARAMETERS */ |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 450 | static inline void psa_set_key_type(psa_key_attributes_t *attributes, |
| 451 | psa_key_type_t type) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 452 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 453 | if (attributes->MBEDTLS_PRIVATE(domain_parameters) == NULL) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 454 | /* Common case: quick path */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 455 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(type) = type; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 456 | } else { |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 457 | /* Call the bigger function to free the old domain parameters. |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 458 | * Ignore any errors which may arise due to type requiring |
| 459 | * non-default domain parameters, since this function can't |
| 460 | * report errors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 461 | (void) psa_set_key_domain_parameters(attributes, type, NULL, 0); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 462 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | static inline psa_key_type_t psa_get_key_type( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 466 | const psa_key_attributes_t *attributes) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 467 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 468 | return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(type); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 469 | } |
| 470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 471 | static inline void psa_set_key_bits(psa_key_attributes_t *attributes, |
| 472 | size_t bits) |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 473 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 474 | if (bits > PSA_MAX_KEY_BITS) { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 475 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits) = PSA_KEY_BITS_TOO_LARGE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 476 | } else { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 477 | attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits) = (psa_key_bits_t) bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 478 | } |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 479 | } |
| 480 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 481 | static inline size_t psa_get_key_bits( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 482 | const psa_key_attributes_t *attributes) |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 483 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 484 | return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits); |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 485 | } |
| 486 | |
Paul Elliott | 1265f00 | 2022-09-09 17:15:43 +0100 | [diff] [blame] | 487 | /** |
| 488 | * \brief The context for PSA interruptible hash signing. |
Paul Elliott | 1265f00 | 2022-09-09 17:15:43 +0100 | [diff] [blame] | 489 | */ |
| 490 | struct psa_sign_hash_interruptible_operation_s { |
Antonio de Angelis | 4380a33 | 2024-02-02 14:21:24 +0000 | [diff] [blame] | 491 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C) |
| 492 | mbedtls_psa_client_handle_t handle; |
| 493 | #else |
Paul Elliott | 2d24792 | 2022-11-29 14:54:44 +0000 | [diff] [blame] | 494 | /** Unique ID indicating which driver got assigned to do the |
| 495 | * operation. Since driver contexts are driver-specific, swapping |
| 496 | * drivers halfway through the operation is not supported. |
| 497 | * ID values are auto-generated in psa_crypto_driver_wrappers.h |
| 498 | * ID value zero means the context is not valid or not assigned to |
| 499 | * any driver (i.e. none of the driver contexts are active). */ |
| 500 | unsigned int MBEDTLS_PRIVATE(id); |
| 501 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 502 | psa_driver_sign_hash_interruptible_context_t MBEDTLS_PRIVATE(ctx); |
| 503 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 504 | unsigned int MBEDTLS_PRIVATE(error_occurred) : 1; |
| 505 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 506 | uint32_t MBEDTLS_PRIVATE(num_ops); |
Antonio de Angelis | 4380a33 | 2024-02-02 14:21:24 +0000 | [diff] [blame] | 507 | #endif |
Paul Elliott | 1265f00 | 2022-09-09 17:15:43 +0100 | [diff] [blame] | 508 | }; |
| 509 | |
Antonio de Angelis | 4380a33 | 2024-02-02 14:21:24 +0000 | [diff] [blame] | 510 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C) |
| 511 | #define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 } |
| 512 | #else |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 513 | #define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 } |
Antonio de Angelis | 4380a33 | 2024-02-02 14:21:24 +0000 | [diff] [blame] | 514 | #endif |
Paul Elliott | 1265f00 | 2022-09-09 17:15:43 +0100 | [diff] [blame] | 515 | |
| 516 | static inline struct psa_sign_hash_interruptible_operation_s |
| 517 | psa_sign_hash_interruptible_operation_init(void) |
| 518 | { |
| 519 | const struct psa_sign_hash_interruptible_operation_s v = |
| 520 | PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT; |
| 521 | |
| 522 | return v; |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * \brief The context for PSA interruptible hash verification. |
Paul Elliott | 1265f00 | 2022-09-09 17:15:43 +0100 | [diff] [blame] | 527 | */ |
| 528 | struct psa_verify_hash_interruptible_operation_s { |
Antonio de Angelis | 4380a33 | 2024-02-02 14:21:24 +0000 | [diff] [blame] | 529 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C) |
| 530 | mbedtls_psa_client_handle_t handle; |
| 531 | #else |
Paul Elliott | 2d24792 | 2022-11-29 14:54:44 +0000 | [diff] [blame] | 532 | /** Unique ID indicating which driver got assigned to do the |
| 533 | * operation. Since driver contexts are driver-specific, swapping |
| 534 | * drivers halfway through the operation is not supported. |
| 535 | * ID values are auto-generated in psa_crypto_driver_wrappers.h |
| 536 | * ID value zero means the context is not valid or not assigned to |
| 537 | * any driver (i.e. none of the driver contexts are active). */ |
| 538 | unsigned int MBEDTLS_PRIVATE(id); |
| 539 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 540 | psa_driver_verify_hash_interruptible_context_t MBEDTLS_PRIVATE(ctx); |
| 541 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 542 | unsigned int MBEDTLS_PRIVATE(error_occurred) : 1; |
| 543 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 544 | uint32_t MBEDTLS_PRIVATE(num_ops); |
Antonio de Angelis | 4380a33 | 2024-02-02 14:21:24 +0000 | [diff] [blame] | 545 | #endif |
Paul Elliott | 1265f00 | 2022-09-09 17:15:43 +0100 | [diff] [blame] | 546 | }; |
| 547 | |
Antonio de Angelis | 4380a33 | 2024-02-02 14:21:24 +0000 | [diff] [blame] | 548 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C) |
| 549 | #define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 } |
| 550 | #else |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 551 | #define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 } |
Antonio de Angelis | 4380a33 | 2024-02-02 14:21:24 +0000 | [diff] [blame] | 552 | #endif |
Paul Elliott | 1265f00 | 2022-09-09 17:15:43 +0100 | [diff] [blame] | 553 | |
| 554 | static inline struct psa_verify_hash_interruptible_operation_s |
| 555 | psa_verify_hash_interruptible_operation_init(void) |
| 556 | { |
| 557 | const struct psa_verify_hash_interruptible_operation_s v = |
| 558 | PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT; |
| 559 | |
| 560 | return v; |
| 561 | } |
| 562 | |
Jaeden Amero | 8013f44 | 2019-08-16 16:13:51 +0100 | [diff] [blame] | 563 | #ifdef __cplusplus |
| 564 | } |
| 565 | #endif |
| 566 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 567 | #endif /* PSA_CRYPTO_STRUCT_H */ |