1 /**
2  * \file psa/crypto_struct.h
3  *
4  * \brief PSA cryptography module: Mbed TLS structured type implementations
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.
15  *
16  * <h3>Design notes about multipart operation structures</h3>
17  *
18  * For multipart operations without driver delegation support, each multipart
19  * operation structure contains a `psa_algorithm_t alg` field which indicates
20  * which specific algorithm the structure is for. When the structure is not in
21  * use, `alg` is 0. Most of the structure consists of a union which is
22  * discriminated by `alg`.
23  *
24  * For multipart operations with driver delegation support, each multipart
25  * operation structure contains an `unsigned int id` field indicating which
26  * driver got assigned to do the operation. When the structure is not in use,
27  * 'id' is 0. The structure contains also a driver context which is the union
28  * of the contexts of all drivers able to handle the type of multipart
29  * operation.
30  *
31  * Note that when `alg` or `id` is 0, the content of other fields is undefined.
32  * In particular, it is not guaranteed that a freshly-initialized structure
33  * is all-zero: we initialize structures to something like `{0, 0}`, which
34  * is only guaranteed to initializes the first member of the union;
35  * GCC and Clang initialize the whole structure to 0 (at the time of writing),
36  * but MSVC and CompCert don't.
37  *
38  * In Mbed Crypto, multipart operation structures live independently from
39  * the key. This allows Mbed Crypto to free the key objects when destroying
40  * a key slot. If a multipart operation needs to remember the key after
41  * the setup function returns, the operation structure needs to contain a
42  * copy of the key.
43  */
44 /*
45  *  Copyright The Mbed TLS Contributors
46  *  SPDX-License-Identifier: Apache-2.0
47  *
48  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
49  *  not use this file except in compliance with the License.
50  *  You may obtain a copy of the License at
51  *
52  *  http://www.apache.org/licenses/LICENSE-2.0
53  *
54  *  Unless required by applicable law or agreed to in writing, software
55  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
56  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
57  *  See the License for the specific language governing permissions and
58  *  limitations under the License.
59  */
60 
61 #ifndef PSA_CRYPTO_STRUCT_H
62 #define PSA_CRYPTO_STRUCT_H
63 
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
68 /* Include the Mbed TLS configuration file, the way Mbed TLS does it
69  * in each of its header files. */
70 #if !defined(MBEDTLS_CONFIG_FILE)
71 #include "mbedtls/config.h"
72 #else
73 #include MBEDTLS_CONFIG_FILE
74 #endif
75 
76 #include "mbedtls/cmac.h"
77 #include "mbedtls/gcm.h"
78 
79 /* Include the context definition for the compiled-in drivers for the primitive
80  * algorithms. */
81 #include "psa/crypto_driver_contexts_primitives.h"
82 
83 struct psa_hash_operation_s
84 {
85     /** Unique ID indicating which driver got assigned to do the
86      * operation. Since driver contexts are driver-specific, swapping
87      * drivers halfway through the operation is not supported.
88      * ID values are auto-generated in psa_driver_wrappers.h.
89      * ID value zero means the context is not valid or not assigned to
90      * any driver (i.e. the driver context is not active, in use). */
91     unsigned int id;
92     psa_driver_hash_context_t ctx;
93 };
94 
95 #define PSA_HASH_OPERATION_INIT {0, {0}}
psa_hash_operation_init(void)96 static inline struct psa_hash_operation_s psa_hash_operation_init( void )
97 {
98     const struct psa_hash_operation_s v = PSA_HASH_OPERATION_INIT;
99     return( v );
100 }
101 
102 struct psa_cipher_operation_s
103 {
104     /** Unique ID indicating which driver got assigned to do the
105      * operation. Since driver contexts are driver-specific, swapping
106      * drivers halfway through the operation is not supported.
107      * ID values are auto-generated in psa_crypto_driver_wrappers.h
108      * ID value zero means the context is not valid or not assigned to
109      * any driver (i.e. none of the driver contexts are active). */
110     unsigned int id;
111 
112     unsigned int iv_required : 1;
113     unsigned int iv_set : 1;
114 
115     uint8_t default_iv_length;
116 
117     psa_driver_cipher_context_t ctx;
118 };
119 
120 #define PSA_CIPHER_OPERATION_INIT {0, 0, 0, 0, {0}}
psa_cipher_operation_init(void)121 static inline struct psa_cipher_operation_s psa_cipher_operation_init( void )
122 {
123     const struct psa_cipher_operation_s v = PSA_CIPHER_OPERATION_INIT;
124     return( v );
125 }
126 
127 /* Include the context definition for the compiled-in drivers for the composite
128  * algorithms. */
129 #include "psa/crypto_driver_contexts_composites.h"
130 
131 struct psa_mac_operation_s
132 {
133     /** Unique ID indicating which driver got assigned to do the
134      * operation. Since driver contexts are driver-specific, swapping
135      * drivers halfway through the operation is not supported.
136      * ID values are auto-generated in psa_driver_wrappers.h
137      * ID value zero means the context is not valid or not assigned to
138      * any driver (i.e. none of the driver contexts are active). */
139     unsigned int id;
140     uint8_t mac_size;
141     unsigned int is_sign : 1;
142     psa_driver_mac_context_t ctx;
143 };
144 
145 #define PSA_MAC_OPERATION_INIT {0, 0, 0, {0}}
psa_mac_operation_init(void)146 static inline struct psa_mac_operation_s psa_mac_operation_init( void )
147 {
148     const struct psa_mac_operation_s v = PSA_MAC_OPERATION_INIT;
149     return( v );
150 }
151 
152 struct psa_aead_operation_s
153 {
154     psa_algorithm_t alg;
155     unsigned int key_set : 1;
156     unsigned int iv_set : 1;
157     uint8_t iv_size;
158     uint8_t block_size;
159     union
160     {
161         unsigned dummy; /* Enable easier initializing of the union. */
162         mbedtls_cipher_context_t cipher;
163     } ctx;
164 };
165 
166 #define PSA_AEAD_OPERATION_INIT {0, 0, 0, 0, 0, {0}}
psa_aead_operation_init(void)167 static inline struct psa_aead_operation_s psa_aead_operation_init( void )
168 {
169     const struct psa_aead_operation_s v = PSA_AEAD_OPERATION_INIT;
170     return( v );
171 }
172 
173 #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
174 typedef struct
175 {
176     uint8_t *info;
177     size_t info_length;
178 #if PSA_HASH_MAX_SIZE > 0xff
179 #error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
180 #endif
181     uint8_t offset_in_block;
182     uint8_t block_number;
183     unsigned int state : 2;
184     unsigned int info_set : 1;
185     uint8_t output_block[PSA_HASH_MAX_SIZE];
186     uint8_t prk[PSA_HASH_MAX_SIZE];
187     struct psa_mac_operation_s hmac;
188 } psa_hkdf_key_derivation_t;
189 #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
190 
191 #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
192     defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
193 typedef enum
194 {
195     PSA_TLS12_PRF_STATE_INIT,       /* no input provided */
196     PSA_TLS12_PRF_STATE_SEED_SET,   /* seed has been set */
197     PSA_TLS12_PRF_STATE_KEY_SET,    /* key has been set */
198     PSA_TLS12_PRF_STATE_LABEL_SET,  /* label has been set */
199     PSA_TLS12_PRF_STATE_OUTPUT      /* output has been started */
200 } psa_tls12_prf_key_derivation_state_t;
201 
202 typedef struct psa_tls12_prf_key_derivation_s
203 {
204 #if PSA_HASH_MAX_SIZE > 0xff
205 #error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
206 #endif
207 
208     /* Indicates how many bytes in the current HMAC block have
209      * not yet been read by the user. */
210     uint8_t left_in_block;
211 
212     /* The 1-based number of the block. */
213     uint8_t block_number;
214 
215     psa_tls12_prf_key_derivation_state_t state;
216 
217     uint8_t *secret;
218     size_t secret_length;
219     uint8_t *seed;
220     size_t seed_length;
221     uint8_t *label;
222     size_t label_length;
223 
224     uint8_t Ai[PSA_HASH_MAX_SIZE];
225 
226     /* `HMAC_hash( prk, A(i) + seed )` in the notation of RFC 5246, Sect. 5. */
227     uint8_t output_block[PSA_HASH_MAX_SIZE];
228 } psa_tls12_prf_key_derivation_t;
229 #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
230         * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
231 
232 struct psa_key_derivation_s
233 {
234     psa_algorithm_t alg;
235     unsigned int can_output_key : 1;
236     size_t capacity;
237     union
238     {
239         /* Make the union non-empty even with no supported algorithms. */
240         uint8_t dummy;
241 #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
242         psa_hkdf_key_derivation_t hkdf;
243 #endif
244 #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
245     defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
246         psa_tls12_prf_key_derivation_t tls12_prf;
247 #endif
248     } ctx;
249 };
250 
251 /* This only zeroes out the first byte in the union, the rest is unspecified. */
252 #define PSA_KEY_DERIVATION_OPERATION_INIT {0, 0, 0, {0}}
psa_key_derivation_operation_init(void)253 static inline struct psa_key_derivation_s psa_key_derivation_operation_init( void )
254 {
255     const struct psa_key_derivation_s v = PSA_KEY_DERIVATION_OPERATION_INIT;
256     return( v );
257 }
258 
259 struct psa_key_policy_s
260 {
261     psa_key_usage_t usage;
262     psa_algorithm_t alg;
263     psa_algorithm_t alg2;
264 };
265 typedef struct psa_key_policy_s psa_key_policy_t;
266 
267 #define PSA_KEY_POLICY_INIT {0, 0, 0}
psa_key_policy_init(void)268 static inline struct psa_key_policy_s psa_key_policy_init( void )
269 {
270     const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT;
271     return( v );
272 }
273 
274 /* The type used internally for key sizes.
275  * Public interfaces use size_t, but internally we use a smaller type. */
276 typedef uint16_t psa_key_bits_t;
277 /* The maximum value of the type used to represent bit-sizes.
278  * This is used to mark an invalid key size. */
279 #define PSA_KEY_BITS_TOO_LARGE ( (psa_key_bits_t) ( -1 ) )
280 /* The maximum size of a key in bits.
281  * Currently defined as the maximum that can be represented, rounded down
282  * to a whole number of bytes.
283  * This is an uncast value so that it can be used in preprocessor
284  * conditionals. */
285 #define PSA_MAX_KEY_BITS 0xfff8
286 
287 /** A mask of flags that can be stored in key attributes.
288  *
289  * This type is also used internally to store flags in slots. Internal
290  * flags are defined in library/psa_crypto_core.h. Internal flags may have
291  * the same value as external flags if they are properly handled during
292  * key creation and in psa_get_key_attributes.
293  */
294 typedef uint16_t psa_key_attributes_flag_t;
295 
296 #define MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER     \
297     ( (psa_key_attributes_flag_t) 0x0001 )
298 
299 /* A mask of key attribute flags used externally only.
300  * Only meant for internal checks inside the library. */
301 #define MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY (      \
302         MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER |    \
303         0 )
304 
305 /* A mask of key attribute flags used both internally and externally.
306  * Currently there aren't any. */
307 #define MBEDTLS_PSA_KA_MASK_DUAL_USE (          \
308         0 )
309 
310 typedef struct
311 {
312     psa_key_type_t type;
313     psa_key_bits_t bits;
314     psa_key_lifetime_t lifetime;
315     mbedtls_svc_key_id_t id;
316     psa_key_policy_t policy;
317     psa_key_attributes_flag_t flags;
318 } psa_core_key_attributes_t;
319 
320 #define PSA_CORE_KEY_ATTRIBUTES_INIT {PSA_KEY_TYPE_NONE, 0, PSA_KEY_LIFETIME_VOLATILE, MBEDTLS_SVC_KEY_ID_INIT, PSA_KEY_POLICY_INIT, 0}
321 
322 struct psa_key_attributes_s
323 {
324     psa_core_key_attributes_t core;
325 #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
326     psa_key_slot_number_t slot_number;
327 #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
328     void *domain_parameters;
329     size_t domain_parameters_size;
330 };
331 
332 #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
333 #define PSA_KEY_ATTRIBUTES_INIT {PSA_CORE_KEY_ATTRIBUTES_INIT, 0, NULL, 0}
334 #else
335 #define PSA_KEY_ATTRIBUTES_INIT {PSA_CORE_KEY_ATTRIBUTES_INIT, NULL, 0}
336 #endif
337 
psa_key_attributes_init(void)338 static inline struct psa_key_attributes_s psa_key_attributes_init( void )
339 {
340     const struct psa_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT;
341     return( v );
342 }
343 
psa_set_key_id(psa_key_attributes_t * attributes,mbedtls_svc_key_id_t key)344 static inline void psa_set_key_id( psa_key_attributes_t *attributes,
345                                    mbedtls_svc_key_id_t key )
346 {
347     psa_key_lifetime_t lifetime = attributes->core.lifetime;
348 
349     attributes->core.id = key;
350 
351     if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
352     {
353         attributes->core.lifetime =
354             PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
355                 PSA_KEY_LIFETIME_PERSISTENT,
356                 PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) );
357     }
358 }
359 
psa_get_key_id(const psa_key_attributes_t * attributes)360 static inline mbedtls_svc_key_id_t psa_get_key_id(
361     const psa_key_attributes_t *attributes)
362 {
363     return( attributes->core.id );
364 }
365 
366 #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
mbedtls_set_key_owner_id(psa_key_attributes_t * attributes,mbedtls_key_owner_id_t owner)367 static inline void mbedtls_set_key_owner_id( psa_key_attributes_t *attributes,
368                                              mbedtls_key_owner_id_t owner )
369 {
370     attributes->core.id.owner = owner;
371 }
372 #endif
373 
psa_set_key_lifetime(psa_key_attributes_t * attributes,psa_key_lifetime_t lifetime)374 static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
375                                         psa_key_lifetime_t lifetime)
376 {
377     attributes->core.lifetime = lifetime;
378     if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
379     {
380 #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
381         attributes->core.id.key_id = 0;
382 #else
383         attributes->core.id = 0;
384 #endif
385     }
386 }
387 
psa_get_key_lifetime(const psa_key_attributes_t * attributes)388 static inline psa_key_lifetime_t psa_get_key_lifetime(
389     const psa_key_attributes_t *attributes)
390 {
391     return( attributes->core.lifetime );
392 }
393 
psa_extend_key_usage_flags(psa_key_usage_t * usage_flags)394 static inline void psa_extend_key_usage_flags( psa_key_usage_t *usage_flags )
395 {
396     if( *usage_flags & PSA_KEY_USAGE_SIGN_HASH )
397         *usage_flags |= PSA_KEY_USAGE_SIGN_MESSAGE;
398 
399     if( *usage_flags & PSA_KEY_USAGE_VERIFY_HASH )
400         *usage_flags |= PSA_KEY_USAGE_VERIFY_MESSAGE;
401 }
402 
psa_set_key_usage_flags(psa_key_attributes_t * attributes,psa_key_usage_t usage_flags)403 static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
404                                            psa_key_usage_t usage_flags)
405 {
406     psa_extend_key_usage_flags( &usage_flags );
407     attributes->core.policy.usage = usage_flags;
408 }
409 
psa_get_key_usage_flags(const psa_key_attributes_t * attributes)410 static inline psa_key_usage_t psa_get_key_usage_flags(
411     const psa_key_attributes_t *attributes)
412 {
413     return( attributes->core.policy.usage );
414 }
415 
psa_set_key_algorithm(psa_key_attributes_t * attributes,psa_algorithm_t alg)416 static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
417                                          psa_algorithm_t alg)
418 {
419     attributes->core.policy.alg = alg;
420 }
421 
psa_get_key_algorithm(const psa_key_attributes_t * attributes)422 static inline psa_algorithm_t psa_get_key_algorithm(
423     const psa_key_attributes_t *attributes)
424 {
425     return( attributes->core.policy.alg );
426 }
427 
428 /* This function is declared in crypto_extra.h, which comes after this
429  * header file, but we need the function here, so repeat the declaration. */
430 psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
431                                            psa_key_type_t type,
432                                            const uint8_t *data,
433                                            size_t data_length);
434 
psa_set_key_type(psa_key_attributes_t * attributes,psa_key_type_t type)435 static inline void psa_set_key_type(psa_key_attributes_t *attributes,
436                                     psa_key_type_t type)
437 {
438     if( attributes->domain_parameters == NULL )
439     {
440         /* Common case: quick path */
441         attributes->core.type = type;
442     }
443     else
444     {
445         /* Call the bigger function to free the old domain paramteres.
446          * Ignore any errors which may arise due to type requiring
447          * non-default domain parameters, since this function can't
448          * report errors. */
449         (void) psa_set_key_domain_parameters( attributes, type, NULL, 0 );
450     }
451 }
452 
psa_get_key_type(const psa_key_attributes_t * attributes)453 static inline psa_key_type_t psa_get_key_type(
454     const psa_key_attributes_t *attributes)
455 {
456     return( attributes->core.type );
457 }
458 
psa_set_key_bits(psa_key_attributes_t * attributes,size_t bits)459 static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
460                                     size_t bits)
461 {
462     if( bits > PSA_MAX_KEY_BITS )
463         attributes->core.bits = PSA_KEY_BITS_TOO_LARGE;
464     else
465         attributes->core.bits = (psa_key_bits_t) bits;
466 }
467 
psa_get_key_bits(const psa_key_attributes_t * attributes)468 static inline size_t psa_get_key_bits(
469     const psa_key_attributes_t *attributes)
470 {
471     return( attributes->core.bits );
472 }
473 
474 #ifdef __cplusplus
475 }
476 #endif
477 
478 #endif /* PSA_CRYPTO_STRUCT_H */
479