blob: 804bd340c4e6b922aeacaab760b315a7405de872 [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
Jaeden Amero8013f442019-08-16 16:13:51 +010058#ifdef __cplusplus
59extern "C" {
60#endif
61
Gilles Peskine9ef733f2018-02-07 21:05:37 +010062/* Include the Mbed TLS configuration file, the way Mbed TLS does it
63 * in each of its header files. */
64#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Amerod58a00d2019-06-07 11:49:59 +010065#include "mbedtls/config.h"
Gilles Peskine9ef733f2018-02-07 21:05:37 +010066#else
67#include MBEDTLS_CONFIG_FILE
68#endif
69
70#include "mbedtls/cipher.h"
71#include "mbedtls/cmac.h"
72#include "mbedtls/gcm.h"
73#include "mbedtls/md.h"
74#include "mbedtls/md2.h"
75#include "mbedtls/md4.h"
76#include "mbedtls/md5.h"
77#include "mbedtls/ripemd160.h"
78#include "mbedtls/sha1.h"
79#include "mbedtls/sha256.h"
80#include "mbedtls/sha512.h"
81
82struct psa_hash_operation_s
83{
84 psa_algorithm_t alg;
85 union
86 {
Gilles Peskine058e0b92018-03-22 16:20:19 +010087 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +010088#if defined(MBEDTLS_MD2_C)
89 mbedtls_md2_context md2;
90#endif
91#if defined(MBEDTLS_MD4_C)
92 mbedtls_md4_context md4;
93#endif
94#if defined(MBEDTLS_MD5_C)
95 mbedtls_md5_context md5;
96#endif
97#if defined(MBEDTLS_RIPEMD160_C)
98 mbedtls_ripemd160_context ripemd160;
99#endif
100#if defined(MBEDTLS_SHA1_C)
101 mbedtls_sha1_context sha1;
102#endif
103#if defined(MBEDTLS_SHA256_C)
104 mbedtls_sha256_context sha256;
105#endif
106#if defined(MBEDTLS_SHA512_C)
107 mbedtls_sha512_context sha512;
108#endif
109 } ctx;
110};
111
Jaeden Amero6a25b412019-01-04 11:47:44 +0000112#define PSA_HASH_OPERATION_INIT {0, {0}}
113static inline struct psa_hash_operation_s psa_hash_operation_init( void )
114{
115 const struct psa_hash_operation_s v = PSA_HASH_OPERATION_INIT;
116 return( v );
117}
118
Gilles Peskinea05219c2018-11-16 16:02:56 +0100119#if defined(MBEDTLS_MD_C)
Gilles Peskine2d277862018-06-18 15:41:12 +0200120typedef struct
121{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +0300122 /** The hash context. */
123 struct psa_hash_operation_s hash_ctx;
124 /** The HMAC part of the context. */
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +0200125 uint8_t opad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +0300126} psa_hmac_internal_data;
Gilles Peskinea05219c2018-11-16 16:02:56 +0100127#endif /* MBEDTLS_MD_C */
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +0300128
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100129struct psa_mac_operation_s
130{
131 psa_algorithm_t alg;
Darryl Green80bed232018-07-26 13:03:38 +0100132 unsigned int key_set : 1;
133 unsigned int iv_required : 1;
134 unsigned int iv_set : 1;
135 unsigned int has_input : 1;
136 unsigned int is_sign : 1;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100137 uint8_t mac_size;
138 union
139 {
Gilles Peskine058e0b92018-03-22 16:20:19 +0100140 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100141#if defined(MBEDTLS_MD_C)
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +0300142 psa_hmac_internal_data hmac;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100143#endif
144#if defined(MBEDTLS_CMAC_C)
145 mbedtls_cipher_context_t cmac;
146#endif
147 } ctx;
148};
149
Jaeden Amero769ce272019-01-04 11:48:03 +0000150#define PSA_MAC_OPERATION_INIT {0, 0, 0, 0, 0, 0, 0, {0}}
151static inline struct psa_mac_operation_s psa_mac_operation_init( void )
152{
153 const struct psa_mac_operation_s v = PSA_MAC_OPERATION_INIT;
154 return( v );
155}
156
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100157struct psa_cipher_operation_s
158{
159 psa_algorithm_t alg;
Darryl Green80bed232018-07-26 13:03:38 +0100160 unsigned int key_set : 1;
161 unsigned int iv_required : 1;
162 unsigned int iv_set : 1;
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100163 uint8_t iv_size;
164 uint8_t block_size;
165 union
166 {
Jaeden Amero5a5dc772019-01-04 15:33:37 +0000167 unsigned dummy; /* Enable easier initializing of the union. */
mohammad1603503973b2018-03-12 15:59:30 +0200168 mbedtls_cipher_context_t cipher;
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100169 } ctx;
170};
171
Jaeden Amero5bae2272019-01-04 11:48:27 +0000172#define PSA_CIPHER_OPERATION_INIT {0, 0, 0, 0, 0, 0, {0}}
173static inline struct psa_cipher_operation_s psa_cipher_operation_init( void )
174{
175 const struct psa_cipher_operation_s v = PSA_CIPHER_OPERATION_INIT;
176 return( v );
177}
178
Gilles Peskine30a9e412019-01-14 18:36:12 +0100179struct psa_aead_operation_s
180{
181 psa_algorithm_t alg;
182 unsigned int key_set : 1;
183 unsigned int iv_set : 1;
184 uint8_t iv_size;
185 uint8_t block_size;
186 union
187 {
188 unsigned dummy; /* Enable easier initializing of the union. */
189 mbedtls_cipher_context_t cipher;
190 } ctx;
191};
192
193#define PSA_AEAD_OPERATION_INIT {0, 0, 0, 0, 0, {0}}
194static inline struct psa_aead_operation_s psa_aead_operation_init( void )
195{
196 const struct psa_aead_operation_s v = PSA_AEAD_OPERATION_INIT;
197 return( v );
198}
199
Gilles Peskinea05219c2018-11-16 16:02:56 +0100200#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +0200201typedef struct
202{
203 uint8_t *info;
204 size_t info_length;
205 psa_hmac_internal_data hmac;
206 uint8_t prk[PSA_HASH_MAX_SIZE];
207 uint8_t output_block[PSA_HASH_MAX_SIZE];
208#if PSA_HASH_MAX_SIZE > 0xff
209#error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
210#endif
211 uint8_t offset_in_block;
212 uint8_t block_number;
Gilles Peskine22c51512019-04-12 15:07:32 +0200213 unsigned int state : 2;
214 unsigned int info_set : 1;
Gilles Peskinecbe66502019-05-16 16:59:18 +0200215} psa_hkdf_key_derivation_t;
Gilles Peskinea05219c2018-11-16 16:02:56 +0100216#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +0200217
Janos Follathe3e81662019-06-11 14:07:27 +0100218/*
219 * If this option is not turned on, then the function `psa_key_derivation()`
220 * is removed. And the new psa_tls12_prf_key_derivation_t context is used along
221 * with the corresponding new API.
222 *
223 * The sole purpose of this option is to make the transition to the new API
224 * smoother. Once the transition is complete it can and should be removed
225 * along with the old API and its implementation.
226 */
227#define PSA_PRE_1_0_KEY_DERIVATION
228
Gilles Peskinea05219c2018-11-16 16:02:56 +0100229#if defined(MBEDTLS_MD_C)
Janos Follath999f6482019-06-11 12:04:10 +0100230#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +0200231typedef struct psa_tls12_prf_key_derivation_s
Hanno Beckerc8a41d72018-10-09 17:33:01 +0100232{
233 /* The TLS 1.2 PRF uses the key for each HMAC iteration,
Gilles Peskine35675b62019-05-16 17:26:11 +0200234 * hence we must store it for the lifetime of the operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +0100235 * This is different from HKDF, where the key is only used
236 * in the extraction phase, but not during expansion. */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +0200237 uint8_t *key;
Hanno Beckerc8a41d72018-10-09 17:33:01 +0100238 size_t key_len;
239
240 /* `A(i) + seed` in the notation of RFC 5246, Sect. 5 */
Hanno Becker580fba12018-11-13 20:50:45 +0000241 uint8_t *Ai_with_seed;
242 size_t Ai_with_seed_len;
Hanno Beckerc8a41d72018-10-09 17:33:01 +0100243
244 /* `HMAC_hash( prk, A(i) + seed )` in the notation of RFC 5246, Sect. 5. */
245 uint8_t output_block[PSA_HASH_MAX_SIZE];
246
247#if PSA_HASH_MAX_SIZE > 0xff
248#error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
249#endif
250
251 /* Indicates how many bytes in the current HMAC block have
252 * already been read by the user. */
253 uint8_t offset_in_block;
254
255 /* The 1-based number of the block. */
256 uint8_t block_number;
257
Gilles Peskinecbe66502019-05-16 16:59:18 +0200258} psa_tls12_prf_key_derivation_t;
Janos Follath999f6482019-06-11 12:04:10 +0100259#else
260
261typedef enum
262{
263 TLS12_PRF_STATE_INIT, /* no input provided */
264 TLS12_PRF_STATE_SEED_SET, /* seed has been set */
265 TLS12_PRF_STATE_KEY_SET, /* key has been set */
266 TLS12_PRF_STATE_LABEL_SET, /* label has been set */
267 TLS12_PRF_STATE_OUTPUT /* output has been started */
268} psa_tls12_prf_key_derivation_state_t;
269
270typedef struct psa_tls12_prf_key_derivation_s
271{
272#if PSA_HASH_MAX_SIZE > 0xff
273#error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
274#endif
275
276 /* Indicates how many bytes in the current HMAC block have
Janos Follath844eb0e2019-06-19 12:10:49 +0100277 * not yet been read by the user. */
278 uint8_t left_in_block;
Janos Follath999f6482019-06-11 12:04:10 +0100279
280 /* The 1-based number of the block. */
281 uint8_t block_number;
282
283 psa_tls12_prf_key_derivation_state_t state;
284
285 uint8_t *seed;
286 size_t seed_length;
287 uint8_t *label;
288 size_t label_length;
289 psa_hmac_internal_data hmac;
290 uint8_t Ai[PSA_HASH_MAX_SIZE];
291
292 /* `HMAC_hash( prk, A(i) + seed )` in the notation of RFC 5246, Sect. 5. */
293 uint8_t output_block[PSA_HASH_MAX_SIZE];
294} psa_tls12_prf_key_derivation_t;
295#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +0100296#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +0100297
Gilles Peskinecbe66502019-05-16 16:59:18 +0200298struct psa_key_derivation_s
Gilles Peskineeab56e42018-07-12 17:12:33 +0200299{
300 psa_algorithm_t alg;
301 size_t capacity;
302 union
303 {
Janos Follathadbec812019-06-14 11:05:39 +0100304 /* Make the union non-empty even with no supported algorithms. */
305 uint8_t dummy;
Gilles Peskinebef7f142018-07-12 17:22:21 +0200306#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +0200307 psa_hkdf_key_derivation_t hkdf;
308 psa_tls12_prf_key_derivation_t tls12_prf;
Gilles Peskinebef7f142018-07-12 17:22:21 +0200309#endif
Gilles Peskineeab56e42018-07-12 17:12:33 +0200310 } ctx;
311};
312
Janos Follathadbec812019-06-14 11:05:39 +0100313/* This only zeroes out the first byte in the union, the rest is unspecified. */
314#define PSA_KEY_DERIVATION_OPERATION_INIT {0, 0, {0}}
Gilles Peskinecbe66502019-05-16 16:59:18 +0200315static inline struct psa_key_derivation_s psa_key_derivation_operation_init( void )
Gilles Peskineeab56e42018-07-12 17:12:33 +0200316{
Gilles Peskinecbe66502019-05-16 16:59:18 +0200317 const struct psa_key_derivation_s v = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +0200318 return( v );
319}
320
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100321struct psa_key_policy_s
322{
323 psa_key_usage_t usage;
324 psa_algorithm_t alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200325 psa_algorithm_t alg2;
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100326};
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200327typedef struct psa_key_policy_s psa_key_policy_t;
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100328
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200329#define PSA_KEY_POLICY_INIT {0, 0, 0}
Jaeden Amero70261c52019-01-04 11:47:20 +0000330static inline struct psa_key_policy_s psa_key_policy_init( void )
331{
332 const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT;
333 return( v );
334}
335
Gilles Peskine68cc433b2019-07-30 17:42:47 +0200336/* The type used internally for key sizes.
337 * Public interfaces use size_t, but internally we use a smaller type. */
338typedef uint16_t psa_key_bits_t;
339/* The maximum value of the type used to represent bit-sizes.
340 * This is used to mark an invalid key size. */
341#define PSA_KEY_BITS_TOO_LARGE ( (psa_key_bits_t) ( -1 ) )
Gilles Peskinec744d992019-07-30 17:26:54 +0200342/* The maximum size of a key in bits.
Gilles Peskine68cc433b2019-07-30 17:42:47 +0200343 * Currently defined as the maximum that can be represented, rounded down
344 * to a whole number of bytes.
345 * This is an uncast value so that it can be used in preprocessor
346 * conditionals. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200347#define PSA_MAX_KEY_BITS 0xfff8
348
Gilles Peskine91e8c332019-08-02 19:19:39 +0200349/** A mask of flags that can be stored in key attributes.
350 *
351 * This type is also used internally to store flags in slots. Internal
352 * flags are defined in library/psa_crypto_core.h. Internal flags may have
353 * the same value as external flags if they are properly handled during
354 * key creation and in psa_get_key_attributes.
355 */
356typedef uint16_t psa_key_attributes_flag_t;
357
Gilles Peskinec8000c02019-08-02 20:15:51 +0200358#define MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER \
359 ( (psa_key_attributes_flag_t) 0x0001 )
Gilles Peskine91e8c332019-08-02 19:19:39 +0200360
361/* A mask of key attribute flags used externally only.
362 * Only meant for internal checks inside the library. */
363#define MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ( \
Gilles Peskinec8000c02019-08-02 20:15:51 +0200364 MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER | \
Gilles Peskine91e8c332019-08-02 19:19:39 +0200365 0 )
366
367/* A mask of key attribute flags used both internally and externally.
368 * Currently there aren't any. */
369#define MBEDTLS_PSA_KA_MASK_DUAL_USE ( \
370 0 )
371
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200372typedef struct
373{
374 psa_key_type_t type;
375 psa_key_lifetime_t lifetime;
376 psa_key_id_t id;
377 psa_key_policy_t policy;
Gilles Peskine68cc433b2019-07-30 17:42:47 +0200378 psa_key_bits_t bits;
Gilles Peskine91e8c332019-08-02 19:19:39 +0200379 psa_key_attributes_flag_t flags;
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200380} psa_core_key_attributes_t;
381
Jaeden Amero39f03fc2019-08-20 11:11:55 +0100382#define PSA_CORE_KEY_ATTRIBUTES_INIT {0, 0, PSA_KEY_ID_INIT, PSA_KEY_POLICY_INIT, 0, 0}
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200383
Gilles Peskine4747d192019-04-17 15:05:45 +0200384struct psa_key_attributes_s
385{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200386 psa_core_key_attributes_t core;
Gilles Peskinec8000c02019-08-02 20:15:51 +0200387#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
388 psa_key_slot_number_t slot_number;
389#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +0200390 void *domain_parameters;
391 size_t domain_parameters_size;
Gilles Peskine4747d192019-04-17 15:05:45 +0200392};
393
Gilles Peskinec8000c02019-08-02 20:15:51 +0200394#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
395#define PSA_KEY_ATTRIBUTES_INIT {PSA_CORE_KEY_ATTRIBUTES_INIT, 0, NULL, 0}
396#else
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200397#define PSA_KEY_ATTRIBUTES_INIT {PSA_CORE_KEY_ATTRIBUTES_INIT, NULL, 0}
Gilles Peskinec8000c02019-08-02 20:15:51 +0200398#endif
399
Gilles Peskine4747d192019-04-17 15:05:45 +0200400static inline struct psa_key_attributes_s psa_key_attributes_init( void )
401{
402 const struct psa_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT;
403 return( v );
404}
405
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200406static inline void psa_set_key_id(psa_key_attributes_t *attributes,
407 psa_key_id_t id)
408{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200409 attributes->core.id = id;
410 if( attributes->core.lifetime == PSA_KEY_LIFETIME_VOLATILE )
411 attributes->core.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200412}
413
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200414static inline psa_key_id_t psa_get_key_id(
415 const psa_key_attributes_t *attributes)
416{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200417 return( attributes->core.id );
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200418}
419
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200420static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
421 psa_key_lifetime_t lifetime)
422{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200423 attributes->core.lifetime = lifetime;
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200424 if( lifetime == PSA_KEY_LIFETIME_VOLATILE )
Jaeden Ameroe3cdf282019-08-20 12:58:20 +0100425 {
426#ifdef MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER
427 attributes->core.id.key_id = 0;
428 attributes->core.id.owner = 0;
429#else
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200430 attributes->core.id = 0;
Jaeden Ameroe3cdf282019-08-20 12:58:20 +0100431#endif
432 }
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200433}
434
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200435static inline psa_key_lifetime_t psa_get_key_lifetime(
436 const psa_key_attributes_t *attributes)
437{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200438 return( attributes->core.lifetime );
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200439}
440
Gilles Peskine4747d192019-04-17 15:05:45 +0200441static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
442 psa_key_usage_t usage_flags)
443{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200444 attributes->core.policy.usage = usage_flags;
Gilles Peskine4747d192019-04-17 15:05:45 +0200445}
446
447static inline psa_key_usage_t psa_get_key_usage_flags(
448 const psa_key_attributes_t *attributes)
449{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200450 return( attributes->core.policy.usage );
Gilles Peskine4747d192019-04-17 15:05:45 +0200451}
452
453static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
454 psa_algorithm_t alg)
455{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200456 attributes->core.policy.alg = alg;
Gilles Peskine4747d192019-04-17 15:05:45 +0200457}
458
459static inline psa_algorithm_t psa_get_key_algorithm(
460 const psa_key_attributes_t *attributes)
461{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200462 return( attributes->core.policy.alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200463}
464
Gilles Peskine24f10f82019-05-16 12:18:32 +0200465/* This function is declared in crypto_extra.h, which comes after this
466 * header file, but we need the function here, so repeat the declaration. */
467psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
468 psa_key_type_t type,
469 const uint8_t *data,
470 size_t data_length);
471
Gilles Peskine4747d192019-04-17 15:05:45 +0200472static inline void psa_set_key_type(psa_key_attributes_t *attributes,
473 psa_key_type_t type)
474{
Gilles Peskineb699f072019-04-26 16:06:02 +0200475 if( attributes->domain_parameters == NULL )
476 {
477 /* Common case: quick path */
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200478 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +0200479 }
480 else
481 {
482 /* Call the bigger function to free the old domain paramteres.
483 * Ignore any errors which may arise due to type requiring
484 * non-default domain parameters, since this function can't
485 * report errors. */
486 (void) psa_set_key_domain_parameters( attributes, type, NULL, 0 );
487 }
Gilles Peskine4747d192019-04-17 15:05:45 +0200488}
489
490static inline psa_key_type_t psa_get_key_type(
491 const psa_key_attributes_t *attributes)
492{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200493 return( attributes->core.type );
Gilles Peskine4747d192019-04-17 15:05:45 +0200494}
495
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200496static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
497 size_t bits)
498{
Gilles Peskine68cc433b2019-07-30 17:42:47 +0200499 if( bits > PSA_MAX_KEY_BITS )
500 attributes->core.bits = PSA_KEY_BITS_TOO_LARGE;
501 else
502 attributes->core.bits = (psa_key_bits_t) bits;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200503}
504
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200505static inline size_t psa_get_key_bits(
506 const psa_key_attributes_t *attributes)
507{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200508 return( attributes->core.bits );
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200509}
510
Jaeden Amero8013f442019-08-16 16:13:51 +0100511#ifdef __cplusplus
512}
513#endif
514
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100515#endif /* PSA_CRYPTO_STRUCT_H */