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 | * |
| 18 | * Each multipart operation structure contains a `psa_algorithm_t alg` |
| 19 | * field which indicates which specific algorithm the structure is for. |
| 20 | * When the structure is not in use, `alg` is 0. Most of the structure |
| 21 | * consists of a union which is discriminated by `alg`. |
| 22 | * |
| 23 | * Note that when `alg` is 0, the content of other fields is undefined. |
| 24 | * In particular, it is not guaranteed that a freshly-initialized structure |
| 25 | * is all-zero: we initialize structures to something like `{0, 0}`, which |
| 26 | * is only guaranteed to initializes the first member of the union; |
| 27 | * GCC and Clang initialize the whole structure to 0 (at the time of writing), |
| 28 | * but MSVC and CompCert don't. |
| 29 | * |
| 30 | * In Mbed Crypto, multipart operation structures live independently from |
| 31 | * the key. This allows Mbed Crypto to free the key objects when destroying |
| 32 | * a key slot. If a multipart operation needs to remember the key after |
| 33 | * the setup function returns, the operation structure needs to contain a |
| 34 | * copy of the key. |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 35 | */ |
| 36 | /* |
| 37 | * Copyright (C) 2018, ARM Limited, All Rights Reserved |
| 38 | * SPDX-License-Identifier: Apache-2.0 |
| 39 | * |
| 40 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 41 | * not use this file except in compliance with the License. |
| 42 | * You may obtain a copy of the License at |
| 43 | * |
| 44 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 45 | * |
| 46 | * Unless required by applicable law or agreed to in writing, software |
| 47 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 48 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 49 | * See the License for the specific language governing permissions and |
| 50 | * limitations under the License. |
| 51 | * |
| 52 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 53 | */ |
| 54 | |
| 55 | #ifndef PSA_CRYPTO_STRUCT_H |
| 56 | #define PSA_CRYPTO_STRUCT_H |
| 57 | |
| 58 | /* Include the Mbed TLS configuration file, the way Mbed TLS does it |
| 59 | * in each of its header files. */ |
| 60 | #if !defined(MBEDTLS_CONFIG_FILE) |
Jaeden Amero | d58a00d | 2019-06-07 11:49:59 +0100 | [diff] [blame] | 61 | #include "mbedtls/config.h" |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 62 | #else |
| 63 | #include MBEDTLS_CONFIG_FILE |
| 64 | #endif |
| 65 | |
| 66 | #include "mbedtls/cipher.h" |
| 67 | #include "mbedtls/cmac.h" |
| 68 | #include "mbedtls/gcm.h" |
| 69 | #include "mbedtls/md.h" |
| 70 | #include "mbedtls/md2.h" |
| 71 | #include "mbedtls/md4.h" |
| 72 | #include "mbedtls/md5.h" |
| 73 | #include "mbedtls/ripemd160.h" |
| 74 | #include "mbedtls/sha1.h" |
| 75 | #include "mbedtls/sha256.h" |
| 76 | #include "mbedtls/sha512.h" |
| 77 | |
| 78 | struct psa_hash_operation_s |
| 79 | { |
| 80 | psa_algorithm_t alg; |
| 81 | union |
| 82 | { |
Gilles Peskine | 058e0b9 | 2018-03-22 16:20:19 +0100 | [diff] [blame] | 83 | unsigned dummy; /* Make the union non-empty even with no supported algorithms. */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 84 | #if defined(MBEDTLS_MD2_C) |
| 85 | mbedtls_md2_context md2; |
| 86 | #endif |
| 87 | #if defined(MBEDTLS_MD4_C) |
| 88 | mbedtls_md4_context md4; |
| 89 | #endif |
| 90 | #if defined(MBEDTLS_MD5_C) |
| 91 | mbedtls_md5_context md5; |
| 92 | #endif |
| 93 | #if defined(MBEDTLS_RIPEMD160_C) |
| 94 | mbedtls_ripemd160_context ripemd160; |
| 95 | #endif |
| 96 | #if defined(MBEDTLS_SHA1_C) |
| 97 | mbedtls_sha1_context sha1; |
| 98 | #endif |
| 99 | #if defined(MBEDTLS_SHA256_C) |
| 100 | mbedtls_sha256_context sha256; |
| 101 | #endif |
| 102 | #if defined(MBEDTLS_SHA512_C) |
| 103 | mbedtls_sha512_context sha512; |
| 104 | #endif |
| 105 | } ctx; |
| 106 | }; |
| 107 | |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 108 | #define PSA_HASH_OPERATION_INIT {0, {0}} |
| 109 | static inline struct psa_hash_operation_s psa_hash_operation_init( void ) |
| 110 | { |
| 111 | const struct psa_hash_operation_s v = PSA_HASH_OPERATION_INIT; |
| 112 | return( v ); |
| 113 | } |
| 114 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 115 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 116 | typedef struct |
| 117 | { |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 118 | /** The hash context. */ |
| 119 | struct psa_hash_operation_s hash_ctx; |
| 120 | /** The HMAC part of the context. */ |
Gilles Peskine | b3e6e5d | 2018-06-18 22:16:43 +0200 | [diff] [blame] | 121 | uint8_t opad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 122 | } psa_hmac_internal_data; |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 123 | #endif /* MBEDTLS_MD_C */ |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 124 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 125 | struct psa_mac_operation_s |
| 126 | { |
| 127 | psa_algorithm_t alg; |
Darryl Green | 80bed23 | 2018-07-26 13:03:38 +0100 | [diff] [blame] | 128 | unsigned int key_set : 1; |
| 129 | unsigned int iv_required : 1; |
| 130 | unsigned int iv_set : 1; |
| 131 | unsigned int has_input : 1; |
| 132 | unsigned int is_sign : 1; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 133 | uint8_t mac_size; |
| 134 | union |
| 135 | { |
Gilles Peskine | 058e0b9 | 2018-03-22 16:20:19 +0100 | [diff] [blame] | 136 | unsigned dummy; /* Make the union non-empty even with no supported algorithms. */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 137 | #if defined(MBEDTLS_MD_C) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 138 | psa_hmac_internal_data hmac; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 139 | #endif |
| 140 | #if defined(MBEDTLS_CMAC_C) |
| 141 | mbedtls_cipher_context_t cmac; |
| 142 | #endif |
| 143 | } ctx; |
| 144 | }; |
| 145 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 146 | #define PSA_MAC_OPERATION_INIT {0, 0, 0, 0, 0, 0, 0, {0}} |
| 147 | static inline struct psa_mac_operation_s psa_mac_operation_init( void ) |
| 148 | { |
| 149 | const struct psa_mac_operation_s v = PSA_MAC_OPERATION_INIT; |
| 150 | return( v ); |
| 151 | } |
| 152 | |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 153 | struct psa_cipher_operation_s |
| 154 | { |
| 155 | psa_algorithm_t alg; |
Darryl Green | 80bed23 | 2018-07-26 13:03:38 +0100 | [diff] [blame] | 156 | unsigned int key_set : 1; |
| 157 | unsigned int iv_required : 1; |
| 158 | unsigned int iv_set : 1; |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 159 | uint8_t iv_size; |
| 160 | uint8_t block_size; |
| 161 | union |
| 162 | { |
Jaeden Amero | 5a5dc77 | 2019-01-04 15:33:37 +0000 | [diff] [blame] | 163 | unsigned dummy; /* Enable easier initializing of the union. */ |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 164 | mbedtls_cipher_context_t cipher; |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 165 | } ctx; |
| 166 | }; |
| 167 | |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 168 | #define PSA_CIPHER_OPERATION_INIT {0, 0, 0, 0, 0, 0, {0}} |
| 169 | static inline struct psa_cipher_operation_s psa_cipher_operation_init( void ) |
| 170 | { |
| 171 | const struct psa_cipher_operation_s v = PSA_CIPHER_OPERATION_INIT; |
| 172 | return( v ); |
| 173 | } |
| 174 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 175 | struct psa_aead_operation_s |
| 176 | { |
| 177 | psa_algorithm_t alg; |
| 178 | unsigned int key_set : 1; |
| 179 | unsigned int iv_set : 1; |
| 180 | uint8_t iv_size; |
| 181 | uint8_t block_size; |
| 182 | union |
| 183 | { |
| 184 | unsigned dummy; /* Enable easier initializing of the union. */ |
| 185 | mbedtls_cipher_context_t cipher; |
| 186 | } ctx; |
| 187 | }; |
| 188 | |
| 189 | #define PSA_AEAD_OPERATION_INIT {0, 0, 0, 0, 0, {0}} |
| 190 | static inline struct psa_aead_operation_s psa_aead_operation_init( void ) |
| 191 | { |
| 192 | const struct psa_aead_operation_s v = PSA_AEAD_OPERATION_INIT; |
| 193 | return( v ); |
| 194 | } |
| 195 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 196 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 197 | typedef struct |
| 198 | { |
| 199 | uint8_t *info; |
| 200 | size_t info_length; |
| 201 | psa_hmac_internal_data hmac; |
| 202 | uint8_t prk[PSA_HASH_MAX_SIZE]; |
| 203 | uint8_t output_block[PSA_HASH_MAX_SIZE]; |
| 204 | #if PSA_HASH_MAX_SIZE > 0xff |
| 205 | #error "PSA_HASH_MAX_SIZE does not fit in uint8_t" |
| 206 | #endif |
| 207 | uint8_t offset_in_block; |
| 208 | uint8_t block_number; |
Gilles Peskine | 22c5151 | 2019-04-12 15:07:32 +0200 | [diff] [blame] | 209 | unsigned int state : 2; |
| 210 | unsigned int info_set : 1; |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 211 | } psa_hkdf_key_derivation_t; |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 212 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 213 | |
Janos Follath | e3e8166 | 2019-06-11 14:07:27 +0100 | [diff] [blame] | 214 | /* |
| 215 | * If this option is not turned on, then the function `psa_key_derivation()` |
| 216 | * is removed. And the new psa_tls12_prf_key_derivation_t context is used along |
| 217 | * with the corresponding new API. |
| 218 | * |
| 219 | * The sole purpose of this option is to make the transition to the new API |
| 220 | * smoother. Once the transition is complete it can and should be removed |
| 221 | * along with the old API and its implementation. |
| 222 | */ |
| 223 | #define PSA_PRE_1_0_KEY_DERIVATION |
| 224 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 225 | #if defined(MBEDTLS_MD_C) |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 226 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 227 | typedef struct psa_tls12_prf_key_derivation_s |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 228 | { |
| 229 | /* The TLS 1.2 PRF uses the key for each HMAC iteration, |
Gilles Peskine | 35675b6 | 2019-05-16 17:26:11 +0200 | [diff] [blame] | 230 | * hence we must store it for the lifetime of the operation. |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 231 | * This is different from HKDF, where the key is only used |
| 232 | * in the extraction phase, but not during expansion. */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 233 | uint8_t *key; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 234 | size_t key_len; |
| 235 | |
| 236 | /* `A(i) + seed` in the notation of RFC 5246, Sect. 5 */ |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 237 | uint8_t *Ai_with_seed; |
| 238 | size_t Ai_with_seed_len; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 239 | |
| 240 | /* `HMAC_hash( prk, A(i) + seed )` in the notation of RFC 5246, Sect. 5. */ |
| 241 | uint8_t output_block[PSA_HASH_MAX_SIZE]; |
| 242 | |
| 243 | #if PSA_HASH_MAX_SIZE > 0xff |
| 244 | #error "PSA_HASH_MAX_SIZE does not fit in uint8_t" |
| 245 | #endif |
| 246 | |
| 247 | /* Indicates how many bytes in the current HMAC block have |
| 248 | * already been read by the user. */ |
| 249 | uint8_t offset_in_block; |
| 250 | |
| 251 | /* The 1-based number of the block. */ |
| 252 | uint8_t block_number; |
| 253 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 254 | } psa_tls12_prf_key_derivation_t; |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 255 | #else |
| 256 | |
| 257 | typedef enum |
| 258 | { |
| 259 | TLS12_PRF_STATE_INIT, /* no input provided */ |
| 260 | TLS12_PRF_STATE_SEED_SET, /* seed has been set */ |
| 261 | TLS12_PRF_STATE_KEY_SET, /* key has been set */ |
| 262 | TLS12_PRF_STATE_LABEL_SET, /* label has been set */ |
| 263 | TLS12_PRF_STATE_OUTPUT /* output has been started */ |
| 264 | } psa_tls12_prf_key_derivation_state_t; |
| 265 | |
| 266 | typedef struct psa_tls12_prf_key_derivation_s |
| 267 | { |
| 268 | #if PSA_HASH_MAX_SIZE > 0xff |
| 269 | #error "PSA_HASH_MAX_SIZE does not fit in uint8_t" |
| 270 | #endif |
| 271 | |
| 272 | /* Indicates how many bytes in the current HMAC block have |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 273 | * not yet been read by the user. */ |
| 274 | uint8_t left_in_block; |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 275 | |
| 276 | /* The 1-based number of the block. */ |
| 277 | uint8_t block_number; |
| 278 | |
| 279 | psa_tls12_prf_key_derivation_state_t state; |
| 280 | |
| 281 | uint8_t *seed; |
| 282 | size_t seed_length; |
| 283 | uint8_t *label; |
| 284 | size_t label_length; |
| 285 | psa_hmac_internal_data hmac; |
| 286 | uint8_t Ai[PSA_HASH_MAX_SIZE]; |
| 287 | |
| 288 | /* `HMAC_hash( prk, A(i) + seed )` in the notation of RFC 5246, Sect. 5. */ |
| 289 | uint8_t output_block[PSA_HASH_MAX_SIZE]; |
| 290 | } psa_tls12_prf_key_derivation_t; |
| 291 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 292 | #endif /* MBEDTLS_MD_C */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 293 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 294 | struct psa_key_derivation_s |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 295 | { |
| 296 | psa_algorithm_t alg; |
| 297 | size_t capacity; |
| 298 | union |
| 299 | { |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 300 | /* Make the union non-empty even with no supported algorithms. */ |
| 301 | uint8_t dummy; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 302 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 303 | psa_hkdf_key_derivation_t hkdf; |
| 304 | psa_tls12_prf_key_derivation_t tls12_prf; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 305 | #endif |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 306 | } ctx; |
| 307 | }; |
| 308 | |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 309 | /* This only zeroes out the first byte in the union, the rest is unspecified. */ |
| 310 | #define PSA_KEY_DERIVATION_OPERATION_INIT {0, 0, {0}} |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 311 | static inline struct psa_key_derivation_s psa_key_derivation_operation_init( void ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 312 | { |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 313 | const struct psa_key_derivation_s v = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 314 | return( v ); |
| 315 | } |
| 316 | |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 317 | struct psa_key_policy_s |
| 318 | { |
| 319 | psa_key_usage_t usage; |
| 320 | psa_algorithm_t alg; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 321 | psa_algorithm_t alg2; |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 322 | }; |
Gilles Peskine | a3dd737 | 2019-04-19 19:42:26 +0200 | [diff] [blame] | 323 | typedef struct psa_key_policy_s psa_key_policy_t; |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 324 | |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 325 | #define PSA_KEY_POLICY_INIT {0, 0, 0} |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 326 | static inline struct psa_key_policy_s psa_key_policy_init( void ) |
| 327 | { |
| 328 | const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT; |
| 329 | return( v ); |
| 330 | } |
| 331 | |
Gilles Peskine | 68cc433b | 2019-07-30 17:42:47 +0200 | [diff] [blame] | 332 | /* The type used internally for key sizes. |
| 333 | * Public interfaces use size_t, but internally we use a smaller type. */ |
| 334 | typedef uint16_t psa_key_bits_t; |
| 335 | /* The maximum value of the type used to represent bit-sizes. |
| 336 | * This is used to mark an invalid key size. */ |
| 337 | #define PSA_KEY_BITS_TOO_LARGE ( (psa_key_bits_t) ( -1 ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 338 | /* The maximum size of a key in bits. |
Gilles Peskine | 68cc433b | 2019-07-30 17:42:47 +0200 | [diff] [blame] | 339 | * Currently defined as the maximum that can be represented, rounded down |
| 340 | * to a whole number of bytes. |
| 341 | * This is an uncast value so that it can be used in preprocessor |
| 342 | * conditionals. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 343 | #define PSA_MAX_KEY_BITS 0xfff8 |
| 344 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 345 | /** A mask of flags that can be stored in key attributes. |
| 346 | * |
| 347 | * This type is also used internally to store flags in slots. Internal |
| 348 | * flags are defined in library/psa_crypto_core.h. Internal flags may have |
| 349 | * the same value as external flags if they are properly handled during |
| 350 | * key creation and in psa_get_key_attributes. |
| 351 | */ |
| 352 | typedef uint16_t psa_key_attributes_flag_t; |
| 353 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 354 | #define MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER \ |
| 355 | ( (psa_key_attributes_flag_t) 0x0001 ) |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 356 | |
| 357 | /* A mask of key attribute flags used externally only. |
| 358 | * Only meant for internal checks inside the library. */ |
| 359 | #define MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ( \ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 360 | MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER | \ |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 361 | 0 ) |
| 362 | |
| 363 | /* A mask of key attribute flags used both internally and externally. |
| 364 | * Currently there aren't any. */ |
| 365 | #define MBEDTLS_PSA_KA_MASK_DUAL_USE ( \ |
| 366 | 0 ) |
| 367 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 368 | typedef struct |
| 369 | { |
| 370 | psa_key_type_t type; |
| 371 | psa_key_lifetime_t lifetime; |
| 372 | psa_key_id_t id; |
| 373 | psa_key_policy_t policy; |
Gilles Peskine | 68cc433b | 2019-07-30 17:42:47 +0200 | [diff] [blame] | 374 | psa_key_bits_t bits; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 375 | psa_key_attributes_flag_t flags; |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 376 | } psa_core_key_attributes_t; |
| 377 | |
Gilles Peskine | 68cc433b | 2019-07-30 17:42:47 +0200 | [diff] [blame] | 378 | #define PSA_CORE_KEY_ATTRIBUTES_INIT {0, 0, 0, {0, 0, 0}, 0, 0} |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 379 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 380 | struct psa_key_attributes_s |
| 381 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 382 | psa_core_key_attributes_t core; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 383 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 384 | psa_key_slot_number_t slot_number; |
| 385 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 386 | void *domain_parameters; |
| 387 | size_t domain_parameters_size; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 388 | }; |
| 389 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 390 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 391 | #define PSA_KEY_ATTRIBUTES_INIT {PSA_CORE_KEY_ATTRIBUTES_INIT, 0, NULL, 0} |
| 392 | #else |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 393 | #define PSA_KEY_ATTRIBUTES_INIT {PSA_CORE_KEY_ATTRIBUTES_INIT, NULL, 0} |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 394 | #endif |
| 395 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 396 | static inline struct psa_key_attributes_s psa_key_attributes_init( void ) |
| 397 | { |
| 398 | const struct psa_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT; |
| 399 | return( v ); |
| 400 | } |
| 401 | |
Gilles Peskine | dc8219a | 2019-05-15 16:11:15 +0200 | [diff] [blame] | 402 | static inline void psa_set_key_id(psa_key_attributes_t *attributes, |
| 403 | psa_key_id_t id) |
| 404 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 405 | attributes->core.id = id; |
| 406 | if( attributes->core.lifetime == PSA_KEY_LIFETIME_VOLATILE ) |
| 407 | attributes->core.lifetime = PSA_KEY_LIFETIME_PERSISTENT; |
Gilles Peskine | dc8219a | 2019-05-15 16:11:15 +0200 | [diff] [blame] | 408 | } |
| 409 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 410 | static inline psa_key_id_t psa_get_key_id( |
| 411 | const psa_key_attributes_t *attributes) |
| 412 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 413 | return( attributes->core.id ); |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 414 | } |
| 415 | |
Gilles Peskine | dc8219a | 2019-05-15 16:11:15 +0200 | [diff] [blame] | 416 | static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes, |
| 417 | psa_key_lifetime_t lifetime) |
| 418 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 419 | attributes->core.lifetime = lifetime; |
Gilles Peskine | dc8219a | 2019-05-15 16:11:15 +0200 | [diff] [blame] | 420 | if( lifetime == PSA_KEY_LIFETIME_VOLATILE ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 421 | attributes->core.id = 0; |
Gilles Peskine | dc8219a | 2019-05-15 16:11:15 +0200 | [diff] [blame] | 422 | } |
| 423 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 424 | static inline psa_key_lifetime_t psa_get_key_lifetime( |
| 425 | const psa_key_attributes_t *attributes) |
| 426 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 427 | return( attributes->core.lifetime ); |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 428 | } |
| 429 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 430 | static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes, |
| 431 | psa_key_usage_t usage_flags) |
| 432 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 433 | attributes->core.policy.usage = usage_flags; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | static inline psa_key_usage_t psa_get_key_usage_flags( |
| 437 | const psa_key_attributes_t *attributes) |
| 438 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 439 | return( attributes->core.policy.usage ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes, |
| 443 | psa_algorithm_t alg) |
| 444 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 445 | attributes->core.policy.alg = alg; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | static inline psa_algorithm_t psa_get_key_algorithm( |
| 449 | const psa_key_attributes_t *attributes) |
| 450 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 451 | return( attributes->core.policy.alg ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 454 | /* This function is declared in crypto_extra.h, which comes after this |
| 455 | * header file, but we need the function here, so repeat the declaration. */ |
| 456 | psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes, |
| 457 | psa_key_type_t type, |
| 458 | const uint8_t *data, |
| 459 | size_t data_length); |
| 460 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 461 | static inline void psa_set_key_type(psa_key_attributes_t *attributes, |
| 462 | psa_key_type_t type) |
| 463 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 464 | if( attributes->domain_parameters == NULL ) |
| 465 | { |
| 466 | /* Common case: quick path */ |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 467 | attributes->core.type = type; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 468 | } |
| 469 | else |
| 470 | { |
| 471 | /* Call the bigger function to free the old domain paramteres. |
| 472 | * Ignore any errors which may arise due to type requiring |
| 473 | * non-default domain parameters, since this function can't |
| 474 | * report errors. */ |
| 475 | (void) psa_set_key_domain_parameters( attributes, type, NULL, 0 ); |
| 476 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static inline psa_key_type_t psa_get_key_type( |
| 480 | const psa_key_attributes_t *attributes) |
| 481 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 482 | return( attributes->core.type ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 483 | } |
| 484 | |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 485 | static inline void psa_set_key_bits(psa_key_attributes_t *attributes, |
| 486 | size_t bits) |
| 487 | { |
Gilles Peskine | 68cc433b | 2019-07-30 17:42:47 +0200 | [diff] [blame] | 488 | if( bits > PSA_MAX_KEY_BITS ) |
| 489 | attributes->core.bits = PSA_KEY_BITS_TOO_LARGE; |
| 490 | else |
| 491 | attributes->core.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 492 | } |
| 493 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 494 | static inline size_t psa_get_key_bits( |
| 495 | const psa_key_attributes_t *attributes) |
| 496 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 497 | return( attributes->core.bits ); |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 498 | } |
| 499 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 500 | #endif /* PSA_CRYPTO_STRUCT_H */ |