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