blob: 28bbc6ac808fdbcd42f7d35afd5c7ce4b1bdc6e5 [file] [log] [blame]
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001/**
2 * \file psa/crypto_struct.h
3 *
4 * \brief PSA cryptography module: Mbed TLS structured type implementations
Gilles Peskine07c91f52018-06-28 18:02:53 +02005 *
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 Peskineb4e73e92019-08-13 15:00:57 +020015 *
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 Peskine9ef733f2018-02-07 21:05:37 +010035 */
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 Amerod58a00d2019-06-07 11:49:59 +010061#include "mbedtls/config.h"
Gilles Peskine9ef733f2018-02-07 21:05:37 +010062#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
78struct psa_hash_operation_s
79{
80 psa_algorithm_t alg;
81 union
82 {
Gilles Peskine058e0b92018-03-22 16:20:19 +010083 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +010084#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 Amero6a25b412019-01-04 11:47:44 +0000108#define PSA_HASH_OPERATION_INIT {0, {0}}
109static 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 Peskinea05219c2018-11-16 16:02:56 +0100115#if defined(MBEDTLS_MD_C)
Gilles Peskine2d277862018-06-18 15:41:12 +0200116typedef struct
117{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +0300118 /** The hash context. */
119 struct psa_hash_operation_s hash_ctx;
120 /** The HMAC part of the context. */
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +0200121 uint8_t opad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +0300122} psa_hmac_internal_data;
Gilles Peskinea05219c2018-11-16 16:02:56 +0100123#endif /* MBEDTLS_MD_C */
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +0300124
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100125struct psa_mac_operation_s
126{
127 psa_algorithm_t alg;
Darryl Green80bed232018-07-26 13:03:38 +0100128 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 Peskine9ef733f2018-02-07 21:05:37 +0100133 uint8_t mac_size;
134 union
135 {
Gilles Peskine058e0b92018-03-22 16:20:19 +0100136 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100137#if defined(MBEDTLS_MD_C)
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +0300138 psa_hmac_internal_data hmac;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100139#endif
140#if defined(MBEDTLS_CMAC_C)
141 mbedtls_cipher_context_t cmac;
142#endif
143 } ctx;
144};
145
Jaeden Amero769ce272019-01-04 11:48:03 +0000146#define PSA_MAC_OPERATION_INIT {0, 0, 0, 0, 0, 0, 0, {0}}
147static 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 Peskine428dc5a2018-03-03 21:27:18 +0100153struct psa_cipher_operation_s
154{
155 psa_algorithm_t alg;
Darryl Green80bed232018-07-26 13:03:38 +0100156 unsigned int key_set : 1;
157 unsigned int iv_required : 1;
158 unsigned int iv_set : 1;
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100159 uint8_t iv_size;
160 uint8_t block_size;
161 union
162 {
Jaeden Amero5a5dc772019-01-04 15:33:37 +0000163 unsigned dummy; /* Enable easier initializing of the union. */
mohammad1603503973b2018-03-12 15:59:30 +0200164 mbedtls_cipher_context_t cipher;
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100165 } ctx;
166};
167
Jaeden Amero5bae2272019-01-04 11:48:27 +0000168#define PSA_CIPHER_OPERATION_INIT {0, 0, 0, 0, 0, 0, {0}}
169static 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 Peskine30a9e412019-01-14 18:36:12 +0100175struct 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}}
190static 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 Peskinea05219c2018-11-16 16:02:56 +0100196#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +0200197typedef 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 Peskine22c51512019-04-12 15:07:32 +0200209 unsigned int state : 2;
210 unsigned int info_set : 1;
Gilles Peskinecbe66502019-05-16 16:59:18 +0200211} psa_hkdf_key_derivation_t;
Gilles Peskinea05219c2018-11-16 16:02:56 +0100212#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +0200213
Janos Follathe3e81662019-06-11 14:07:27 +0100214/*
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 Peskinea05219c2018-11-16 16:02:56 +0100225#if defined(MBEDTLS_MD_C)
Janos Follath999f6482019-06-11 12:04:10 +0100226#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +0200227typedef struct psa_tls12_prf_key_derivation_s
Hanno Beckerc8a41d72018-10-09 17:33:01 +0100228{
229 /* The TLS 1.2 PRF uses the key for each HMAC iteration,
Gilles Peskine35675b62019-05-16 17:26:11 +0200230 * hence we must store it for the lifetime of the operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +0100231 * This is different from HKDF, where the key is only used
232 * in the extraction phase, but not during expansion. */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +0200233 uint8_t *key;
Hanno Beckerc8a41d72018-10-09 17:33:01 +0100234 size_t key_len;
235
236 /* `A(i) + seed` in the notation of RFC 5246, Sect. 5 */
Hanno Becker580fba12018-11-13 20:50:45 +0000237 uint8_t *Ai_with_seed;
238 size_t Ai_with_seed_len;
Hanno Beckerc8a41d72018-10-09 17:33:01 +0100239
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 Peskinecbe66502019-05-16 16:59:18 +0200254} psa_tls12_prf_key_derivation_t;
Janos Follath999f6482019-06-11 12:04:10 +0100255#else
256
257typedef 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
266typedef 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 Follath844eb0e2019-06-19 12:10:49 +0100273 * not yet been read by the user. */
274 uint8_t left_in_block;
Janos Follath999f6482019-06-11 12:04:10 +0100275
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 Peskinea05219c2018-11-16 16:02:56 +0100292#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +0100293
Gilles Peskinecbe66502019-05-16 16:59:18 +0200294struct psa_key_derivation_s
Gilles Peskineeab56e42018-07-12 17:12:33 +0200295{
296 psa_algorithm_t alg;
297 size_t capacity;
298 union
299 {
Janos Follathadbec812019-06-14 11:05:39 +0100300 /* Make the union non-empty even with no supported algorithms. */
301 uint8_t dummy;
Gilles Peskinebef7f142018-07-12 17:22:21 +0200302#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +0200303 psa_hkdf_key_derivation_t hkdf;
304 psa_tls12_prf_key_derivation_t tls12_prf;
Gilles Peskinebef7f142018-07-12 17:22:21 +0200305#endif
Gilles Peskineeab56e42018-07-12 17:12:33 +0200306 } ctx;
307};
308
Janos Follathadbec812019-06-14 11:05:39 +0100309/* 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 Peskinecbe66502019-05-16 16:59:18 +0200311static inline struct psa_key_derivation_s psa_key_derivation_operation_init( void )
Gilles Peskineeab56e42018-07-12 17:12:33 +0200312{
Gilles Peskinecbe66502019-05-16 16:59:18 +0200313 const struct psa_key_derivation_s v = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +0200314 return( v );
315}
316
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100317struct psa_key_policy_s
318{
319 psa_key_usage_t usage;
320 psa_algorithm_t alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200321 psa_algorithm_t alg2;
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100322};
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200323typedef struct psa_key_policy_s psa_key_policy_t;
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100324
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200325#define PSA_KEY_POLICY_INIT {0, 0, 0}
Jaeden Amero70261c52019-01-04 11:47:20 +0000326static 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 Peskine68cc433b2019-07-30 17:42:47 +0200332/* The type used internally for key sizes.
333 * Public interfaces use size_t, but internally we use a smaller type. */
334typedef 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 Peskinec744d992019-07-30 17:26:54 +0200338/* The maximum size of a key in bits.
Gilles Peskine68cc433b2019-07-30 17:42:47 +0200339 * 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 Peskinec744d992019-07-30 17:26:54 +0200343#define PSA_MAX_KEY_BITS 0xfff8
344
Gilles Peskine91e8c332019-08-02 19:19:39 +0200345/** 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 */
352typedef uint16_t psa_key_attributes_flag_t;
353
Gilles Peskinec8000c02019-08-02 20:15:51 +0200354#define MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER \
355 ( (psa_key_attributes_flag_t) 0x0001 )
Gilles Peskine91e8c332019-08-02 19:19:39 +0200356
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 Peskinec8000c02019-08-02 20:15:51 +0200360 MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER | \
Gilles Peskine91e8c332019-08-02 19:19:39 +0200361 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 Peskine7e0cff92019-07-30 13:48:52 +0200368typedef 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 Peskine68cc433b2019-07-30 17:42:47 +0200374 psa_key_bits_t bits;
Gilles Peskine91e8c332019-08-02 19:19:39 +0200375 psa_key_attributes_flag_t flags;
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200376} psa_core_key_attributes_t;
377
Gilles Peskine68cc433b2019-07-30 17:42:47 +0200378#define PSA_CORE_KEY_ATTRIBUTES_INIT {0, 0, 0, {0, 0, 0}, 0, 0}
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200379
Gilles Peskine4747d192019-04-17 15:05:45 +0200380struct psa_key_attributes_s
381{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200382 psa_core_key_attributes_t core;
Gilles Peskinec8000c02019-08-02 20:15:51 +0200383#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
384 psa_key_slot_number_t slot_number;
385#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +0200386 void *domain_parameters;
387 size_t domain_parameters_size;
Gilles Peskine4747d192019-04-17 15:05:45 +0200388};
389
Gilles Peskinec8000c02019-08-02 20:15:51 +0200390#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
391#define PSA_KEY_ATTRIBUTES_INIT {PSA_CORE_KEY_ATTRIBUTES_INIT, 0, NULL, 0}
392#else
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200393#define PSA_KEY_ATTRIBUTES_INIT {PSA_CORE_KEY_ATTRIBUTES_INIT, NULL, 0}
Gilles Peskinec8000c02019-08-02 20:15:51 +0200394#endif
395
Gilles Peskine4747d192019-04-17 15:05:45 +0200396static 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 Peskinedc8219a2019-05-15 16:11:15 +0200402static inline void psa_set_key_id(psa_key_attributes_t *attributes,
403 psa_key_id_t id)
404{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200405 attributes->core.id = id;
406 if( attributes->core.lifetime == PSA_KEY_LIFETIME_VOLATILE )
407 attributes->core.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200408}
409
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200410static inline psa_key_id_t psa_get_key_id(
411 const psa_key_attributes_t *attributes)
412{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200413 return( attributes->core.id );
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200414}
415
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200416static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
417 psa_key_lifetime_t lifetime)
418{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200419 attributes->core.lifetime = lifetime;
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200420 if( lifetime == PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200421 attributes->core.id = 0;
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200422}
423
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200424static inline psa_key_lifetime_t psa_get_key_lifetime(
425 const psa_key_attributes_t *attributes)
426{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200427 return( attributes->core.lifetime );
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200428}
429
Gilles Peskine4747d192019-04-17 15:05:45 +0200430static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
431 psa_key_usage_t usage_flags)
432{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200433 attributes->core.policy.usage = usage_flags;
Gilles Peskine4747d192019-04-17 15:05:45 +0200434}
435
436static inline psa_key_usage_t psa_get_key_usage_flags(
437 const psa_key_attributes_t *attributes)
438{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200439 return( attributes->core.policy.usage );
Gilles Peskine4747d192019-04-17 15:05:45 +0200440}
441
442static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
443 psa_algorithm_t alg)
444{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200445 attributes->core.policy.alg = alg;
Gilles Peskine4747d192019-04-17 15:05:45 +0200446}
447
448static inline psa_algorithm_t psa_get_key_algorithm(
449 const psa_key_attributes_t *attributes)
450{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200451 return( attributes->core.policy.alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200452}
453
Gilles Peskine24f10f82019-05-16 12:18:32 +0200454/* 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. */
456psa_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 Peskine4747d192019-04-17 15:05:45 +0200461static inline void psa_set_key_type(psa_key_attributes_t *attributes,
462 psa_key_type_t type)
463{
Gilles Peskineb699f072019-04-26 16:06:02 +0200464 if( attributes->domain_parameters == NULL )
465 {
466 /* Common case: quick path */
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200467 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +0200468 }
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 Peskine4747d192019-04-17 15:05:45 +0200477}
478
479static inline psa_key_type_t psa_get_key_type(
480 const psa_key_attributes_t *attributes)
481{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200482 return( attributes->core.type );
Gilles Peskine4747d192019-04-17 15:05:45 +0200483}
484
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200485static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
486 size_t bits)
487{
Gilles Peskine68cc433b2019-07-30 17:42:47 +0200488 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 Peskine3a4f1f82019-04-26 13:49:28 +0200492}
493
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200494static inline size_t psa_get_key_bits(
495 const psa_key_attributes_t *attributes)
496{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200497 return( attributes->core.bits );
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200498}
499
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100500#endif /* PSA_CRYPTO_STRUCT_H */