blob: 0e0ecb22d3ecdefa3f3e6342a55f85c663ae2e36 [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 Peskine9ef733f2018-02-07 21:05:37 +010015 */
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
58struct psa_hash_operation_s
59{
60 psa_algorithm_t alg;
61 union
62 {
Gilles Peskine058e0b92018-03-22 16:20:19 +010063 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +010064#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 Amero6a25b412019-01-04 11:47:44 +000088#define PSA_HASH_OPERATION_INIT {0, {0}}
89static 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 Peskinea05219c2018-11-16 16:02:56 +010095#if defined(MBEDTLS_MD_C)
Gilles Peskine2d277862018-06-18 15:41:12 +020096typedef struct
97{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +030098 /** The hash context. */
99 struct psa_hash_operation_s hash_ctx;
100 /** The HMAC part of the context. */
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +0200101 uint8_t opad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +0300102} psa_hmac_internal_data;
Gilles Peskinea05219c2018-11-16 16:02:56 +0100103#endif /* MBEDTLS_MD_C */
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +0300104
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100105struct psa_mac_operation_s
106{
107 psa_algorithm_t alg;
Darryl Green80bed232018-07-26 13:03:38 +0100108 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 Peskine9ef733f2018-02-07 21:05:37 +0100113 uint8_t mac_size;
114 union
115 {
Gilles Peskine058e0b92018-03-22 16:20:19 +0100116 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100117#if defined(MBEDTLS_MD_C)
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +0300118 psa_hmac_internal_data hmac;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100119#endif
120#if defined(MBEDTLS_CMAC_C)
121 mbedtls_cipher_context_t cmac;
122#endif
123 } ctx;
124};
125
Jaeden Amero769ce272019-01-04 11:48:03 +0000126#define PSA_MAC_OPERATION_INIT {0, 0, 0, 0, 0, 0, 0, {0}}
127static 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 Peskine428dc5a2018-03-03 21:27:18 +0100133struct psa_cipher_operation_s
134{
135 psa_algorithm_t alg;
Darryl Green80bed232018-07-26 13:03:38 +0100136 unsigned int key_set : 1;
137 unsigned int iv_required : 1;
138 unsigned int iv_set : 1;
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100139 uint8_t iv_size;
140 uint8_t block_size;
141 union
142 {
Jaeden Amero5a5dc772019-01-04 15:33:37 +0000143 unsigned dummy; /* Enable easier initializing of the union. */
mohammad1603503973b2018-03-12 15:59:30 +0200144 mbedtls_cipher_context_t cipher;
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100145 } ctx;
146};
147
Jaeden Amero5bae2272019-01-04 11:48:27 +0000148#define PSA_CIPHER_OPERATION_INIT {0, 0, 0, 0, 0, 0, {0}}
149static 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 Peskine30a9e412019-01-14 18:36:12 +0100155struct 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}}
170static 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 Peskinea05219c2018-11-16 16:02:56 +0100176#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +0200177typedef 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 Peskine22c51512019-04-12 15:07:32 +0200189 unsigned int state : 2;
190 unsigned int info_set : 1;
Gilles Peskinecbe66502019-05-16 16:59:18 +0200191} psa_hkdf_key_derivation_t;
Gilles Peskinea05219c2018-11-16 16:02:56 +0100192#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +0200193
Janos Follathe3e81662019-06-11 14:07:27 +0100194/*
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 Peskinea05219c2018-11-16 16:02:56 +0100205#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +0200206typedef struct psa_tls12_prf_key_derivation_s
Hanno Beckerc8a41d72018-10-09 17:33:01 +0100207{
208 /* The TLS 1.2 PRF uses the key for each HMAC iteration,
Gilles Peskine35675b62019-05-16 17:26:11 +0200209 * hence we must store it for the lifetime of the operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +0100210 * This is different from HKDF, where the key is only used
211 * in the extraction phase, but not during expansion. */
212 unsigned char *key;
213 size_t key_len;
214
215 /* `A(i) + seed` in the notation of RFC 5246, Sect. 5 */
Hanno Becker580fba12018-11-13 20:50:45 +0000216 uint8_t *Ai_with_seed;
217 size_t Ai_with_seed_len;
Hanno Beckerc8a41d72018-10-09 17:33:01 +0100218
219 /* `HMAC_hash( prk, A(i) + seed )` in the notation of RFC 5246, Sect. 5. */
220 uint8_t output_block[PSA_HASH_MAX_SIZE];
221
222#if PSA_HASH_MAX_SIZE > 0xff
223#error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
224#endif
225
226 /* Indicates how many bytes in the current HMAC block have
227 * already been read by the user. */
228 uint8_t offset_in_block;
229
230 /* The 1-based number of the block. */
231 uint8_t block_number;
232
Gilles Peskinecbe66502019-05-16 16:59:18 +0200233} psa_tls12_prf_key_derivation_t;
Gilles Peskinea05219c2018-11-16 16:02:56 +0100234#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +0100235
Gilles Peskinecbe66502019-05-16 16:59:18 +0200236struct psa_key_derivation_s
Gilles Peskineeab56e42018-07-12 17:12:33 +0200237{
238 psa_algorithm_t alg;
239 size_t capacity;
240 union
241 {
242 struct
243 {
244 uint8_t *data;
245 size_t size;
246 } buffer;
Gilles Peskinebef7f142018-07-12 17:22:21 +0200247#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +0200248 psa_hkdf_key_derivation_t hkdf;
249 psa_tls12_prf_key_derivation_t tls12_prf;
Gilles Peskinebef7f142018-07-12 17:22:21 +0200250#endif
Gilles Peskineeab56e42018-07-12 17:12:33 +0200251 } ctx;
252};
253
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200254#define PSA_KEY_DERIVATION_OPERATION_INIT {0, 0, {{0, 0}}}
Gilles Peskinecbe66502019-05-16 16:59:18 +0200255static inline struct psa_key_derivation_s psa_key_derivation_operation_init( void )
Gilles Peskineeab56e42018-07-12 17:12:33 +0200256{
Gilles Peskinecbe66502019-05-16 16:59:18 +0200257 const struct psa_key_derivation_s v = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +0200258 return( v );
259}
260
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100261struct psa_key_policy_s
262{
263 psa_key_usage_t usage;
264 psa_algorithm_t alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200265 psa_algorithm_t alg2;
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100266};
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200267typedef struct psa_key_policy_s psa_key_policy_t;
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100268
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200269#define PSA_KEY_POLICY_INIT {0, 0, 0}
Jaeden Amero70261c52019-01-04 11:47:20 +0000270static inline struct psa_key_policy_s psa_key_policy_init( void )
271{
272 const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT;
273 return( v );
274}
275
Gilles Peskine4747d192019-04-17 15:05:45 +0200276struct psa_key_attributes_s
277{
278 psa_key_id_t id;
279 psa_key_lifetime_t lifetime;
280 psa_key_policy_t policy;
281 psa_key_type_t type;
282 size_t bits;
Gilles Peskineb699f072019-04-26 16:06:02 +0200283 void *domain_parameters;
284 size_t domain_parameters_size;
Gilles Peskine4747d192019-04-17 15:05:45 +0200285};
286
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200287#define PSA_KEY_ATTRIBUTES_INIT {0, 0, {0, 0, 0}, 0, 0, NULL, 0}
Gilles Peskine4747d192019-04-17 15:05:45 +0200288static inline struct psa_key_attributes_s psa_key_attributes_init( void )
289{
290 const struct psa_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT;
291 return( v );
292}
293
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200294static inline void psa_set_key_id(psa_key_attributes_t *attributes,
295 psa_key_id_t id)
296{
297 attributes->id = id;
298 if( attributes->lifetime == PSA_KEY_LIFETIME_VOLATILE )
299 attributes->lifetime = PSA_KEY_LIFETIME_PERSISTENT;
300}
301
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200302static inline psa_key_id_t psa_get_key_id(
303 const psa_key_attributes_t *attributes)
304{
305 return( attributes->id );
306}
307
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200308static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
309 psa_key_lifetime_t lifetime)
310{
311 attributes->lifetime = lifetime;
312 if( lifetime == PSA_KEY_LIFETIME_VOLATILE )
313 attributes->id = 0;
314}
315
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200316static inline psa_key_lifetime_t psa_get_key_lifetime(
317 const psa_key_attributes_t *attributes)
318{
319 return( attributes->lifetime );
320}
321
Gilles Peskine4747d192019-04-17 15:05:45 +0200322static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
323 psa_key_usage_t usage_flags)
324{
325 attributes->policy.usage = usage_flags;
326}
327
328static inline psa_key_usage_t psa_get_key_usage_flags(
329 const psa_key_attributes_t *attributes)
330{
331 return( attributes->policy.usage );
332}
333
334static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
335 psa_algorithm_t alg)
336{
337 attributes->policy.alg = alg;
338}
339
340static inline psa_algorithm_t psa_get_key_algorithm(
341 const psa_key_attributes_t *attributes)
342{
343 return( attributes->policy.alg );
344}
345
Gilles Peskine24f10f82019-05-16 12:18:32 +0200346/* This function is declared in crypto_extra.h, which comes after this
347 * header file, but we need the function here, so repeat the declaration. */
348psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
349 psa_key_type_t type,
350 const uint8_t *data,
351 size_t data_length);
352
Gilles Peskine4747d192019-04-17 15:05:45 +0200353static inline void psa_set_key_type(psa_key_attributes_t *attributes,
354 psa_key_type_t type)
355{
Gilles Peskineb699f072019-04-26 16:06:02 +0200356 if( attributes->domain_parameters == NULL )
357 {
358 /* Common case: quick path */
359 attributes->type = type;
360 }
361 else
362 {
363 /* Call the bigger function to free the old domain paramteres.
364 * Ignore any errors which may arise due to type requiring
365 * non-default domain parameters, since this function can't
366 * report errors. */
367 (void) psa_set_key_domain_parameters( attributes, type, NULL, 0 );
368 }
Gilles Peskine4747d192019-04-17 15:05:45 +0200369}
370
371static inline psa_key_type_t psa_get_key_type(
372 const psa_key_attributes_t *attributes)
373{
374 return( attributes->type );
375}
376
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200377static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
378 size_t bits)
379{
380 attributes->bits = bits;
381}
382
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200383static inline size_t psa_get_key_bits(
384 const psa_key_attributes_t *attributes)
385{
386 return( attributes->bits );
387}
388
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100389#endif /* PSA_CRYPTO_STRUCT_H */