blob: e0ceb2f924692c24d38d8de9e0d40a84fdf070da [file] [log] [blame]
Antonio de Angelis377a1552018-11-22 17:02:40 +00001/*
Maulik Patel28659c42021-01-06 14:09:22 +00002 * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7/**
Jamie Foxcc31d402019-01-28 17:13:52 +00008 * \file psa/crypto_struct.h
Antonio de Angelis377a1552018-11-22 17:02:40 +00009 *
10 * \brief PSA cryptography module: structured type implementations
11 *
12 * \note This file may not be included directly. Applications must
Jamie Foxcc31d402019-01-28 17:13:52 +000013 * include psa/crypto.h.
Antonio de Angelis377a1552018-11-22 17:02:40 +000014 *
15 * This file contains the definitions of some data structures with
16 * implementation-specific definitions.
17 *
18 * In implementations with isolation between the application and the
19 * cryptography module, it is expected that the front-end and the back-end
20 * would have different versions of this file.
21 */
22
23#ifndef PSA_CRYPTO_STRUCT_H
24#define PSA_CRYPTO_STRUCT_H
25
Antonio de Angelis04debbd2019-10-14 12:12:52 +010026#ifdef __cplusplus
27extern "C" {
28#endif
29
30/*
31 * Note that the below structures are different from the decalrations in
32 * mbed-crypto. This is because TF-M maintains 'front-end' and 'back-end'
33 * versions of this header. In the front-end version, exported to NS
34 * clients in interface/include/psa, a crypto operation is defined as an
35 * opaque handle to a context in the Crypto service. The back-end
36 * version, directly included from the mbed-crypto repo by the Crypto
37 * service, contains the full definition of the operation structs.
38 *
39 * One of the functions of the Crypto service is to allocate the back-end
40 * operation contexts in its own partition memory (in crypto_alloc.c),
41 * and then do the mapping between front-end operation handles passed by
42 * NS clients and the corresponding back-end operation contexts. The
43 * advantage of doing it this way is that internal mbed-crypto state is never
44 * exposed to the NS client.
45 */
46
Antonio de Angelis377a1552018-11-22 17:02:40 +000047struct psa_hash_operation_s
48{
49 uint32_t handle;
50};
51
Jamie Fox0e54ebc2019-04-09 14:21:04 +010052#define PSA_HASH_OPERATION_INIT {0}
53static inline struct psa_hash_operation_s psa_hash_operation_init( void )
54{
55 const struct psa_hash_operation_s v = PSA_HASH_OPERATION_INIT;
56 return( v );
57}
58
Antonio de Angelis377a1552018-11-22 17:02:40 +000059struct psa_mac_operation_s
60{
61 uint32_t handle;
62};
63
Jamie Fox0e54ebc2019-04-09 14:21:04 +010064#define PSA_MAC_OPERATION_INIT {0}
65static inline struct psa_mac_operation_s psa_mac_operation_init( void )
66{
67 const struct psa_mac_operation_s v = PSA_MAC_OPERATION_INIT;
68 return( v );
69}
70
Antonio de Angelis377a1552018-11-22 17:02:40 +000071struct psa_cipher_operation_s
72{
73 uint32_t handle;
74};
75
Jamie Fox0e54ebc2019-04-09 14:21:04 +010076#define PSA_CIPHER_OPERATION_INIT {0}
77static inline struct psa_cipher_operation_s psa_cipher_operation_init( void )
78{
79 const struct psa_cipher_operation_s v = PSA_CIPHER_OPERATION_INIT;
80 return( v );
81}
82
Antonio de Angelis04debbd2019-10-14 12:12:52 +010083struct psa_aead_operation_s
Jamie Fox0e54ebc2019-04-09 14:21:04 +010084{
85 uint32_t handle;
86};
87
Antonio de Angelis04debbd2019-10-14 12:12:52 +010088#define PSA_AEAD_OPERATION_INIT {0}
89static inline struct psa_aead_operation_s psa_aead_operation_init( void )
Jamie Fox0e54ebc2019-04-09 14:21:04 +010090{
Antonio de Angelis04debbd2019-10-14 12:12:52 +010091 const struct psa_aead_operation_s v = PSA_AEAD_OPERATION_INIT;
92 return( v );
93}
94
95struct psa_key_derivation_s
96{
97 uint32_t handle;
98};
99
100#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
101static inline struct psa_key_derivation_s psa_key_derivation_operation_init( void )
102{
103 const struct psa_key_derivation_s v = PSA_KEY_DERIVATION_OPERATION_INIT;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100104 return( v );
105}
106
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100107/* The type used internally for key sizes.
108 * Public interfaces use size_t, but internally we use a smaller type. */
109typedef uint16_t psa_key_bits_t;
110/* The maximum value of the type used to represent bit-sizes.
111 * This is used to mark an invalid key size. */
112#define PSA_KEY_BITS_TOO_LARGE ( (psa_key_bits_t) ( -1 ) )
113/* The maximum size of a key in bits.
114 * Currently defined as the maximum that can be represented, rounded down
115 * to a whole number of bytes.
116 * This is an uncast value so that it can be used in preprocessor
117 * conditionals. */
118#define PSA_MAX_KEY_BITS 0xfff8
119
Soby Mathewd7b79f22020-05-21 15:06:54 +0100120#define PSA_KEY_ATTRIBUTES_INIT PSA_CLIENT_KEY_ATTRIBUTES_INIT
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100121
Soby Mathewd7b79f22020-05-21 15:06:54 +0100122static inline struct psa_client_key_attributes_s psa_key_attributes_init( void )
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100123{
Soby Mathewd7b79f22020-05-21 15:06:54 +0100124 const struct psa_client_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100125 return( v );
126}
127
128static inline void psa_set_key_id(psa_key_attributes_t *attributes,
Maulik Patel28659c42021-01-06 14:09:22 +0000129 psa_key_id_t key)
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100130{
Maulik Patel28659c42021-01-06 14:09:22 +0000131 psa_key_lifetime_t lifetime = attributes->lifetime;
132
133 attributes->id = key;
134
135 if( PSA_KEY_LIFETIME_IS_VOLATILE(lifetime))
136 {
137 attributes->lifetime =
138 PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
139 PSA_KEY_LIFETIME_PERSISTENT,
140 PSA_KEY_LIFETIME_GET_LOCATION(lifetime));
141 }
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100142}
143
144static inline psa_key_id_t psa_get_key_id(
145 const psa_key_attributes_t *attributes)
146{
Soby Mathewd7b79f22020-05-21 15:06:54 +0100147 return( attributes->id );
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100148}
149
150static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
151 psa_key_lifetime_t lifetime)
152{
Soby Mathewd7b79f22020-05-21 15:06:54 +0100153 attributes->lifetime = lifetime;
Maulik Patel28659c42021-01-06 14:09:22 +0000154 if(PSA_KEY_LIFETIME_IS_VOLATILE(lifetime))
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100155 {
Soby Mathewd7b79f22020-05-21 15:06:54 +0100156 attributes->id = 0;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100157 }
158}
159
160static inline psa_key_lifetime_t psa_get_key_lifetime(
161 const psa_key_attributes_t *attributes)
162{
Soby Mathewd7b79f22020-05-21 15:06:54 +0100163 return( attributes->lifetime );
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100164}
165
166static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
167 psa_key_usage_t usage_flags)
168{
Soby Mathewd7b79f22020-05-21 15:06:54 +0100169 attributes->usage = usage_flags;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100170}
171
172static inline psa_key_usage_t psa_get_key_usage_flags(
173 const psa_key_attributes_t *attributes)
174{
Soby Mathewd7b79f22020-05-21 15:06:54 +0100175 return( attributes->usage );
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100176}
177
178static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
179 psa_algorithm_t alg)
180{
Soby Mathewd7b79f22020-05-21 15:06:54 +0100181 attributes->alg = alg;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100182}
183
184static inline psa_algorithm_t psa_get_key_algorithm(
185 const psa_key_attributes_t *attributes)
186{
Soby Mathewd7b79f22020-05-21 15:06:54 +0100187 return( attributes->alg );
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100188}
189
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100190static inline void psa_set_key_type(psa_key_attributes_t *attributes,
191 psa_key_type_t type)
192{
Soby Mathewd7b79f22020-05-21 15:06:54 +0100193 attributes->type = type;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100194}
195
196static inline psa_key_type_t psa_get_key_type(
197 const psa_key_attributes_t *attributes)
198{
Soby Mathewd7b79f22020-05-21 15:06:54 +0100199 return( attributes->type );
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100200}
201
202static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
203 size_t bits)
204{
205 if( bits > PSA_MAX_KEY_BITS )
Soby Mathewd7b79f22020-05-21 15:06:54 +0100206 attributes->bits = PSA_KEY_BITS_TOO_LARGE;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100207 else
Soby Mathewd7b79f22020-05-21 15:06:54 +0100208 attributes->bits = bits;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100209}
210
211static inline size_t psa_get_key_bits(
212 const psa_key_attributes_t *attributes)
213{
Soby Mathewd7b79f22020-05-21 15:06:54 +0100214 return( attributes->bits );
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100215}
216
217#ifdef __cplusplus
218}
219#endif
220
Antonio de Angelis377a1552018-11-22 17:02:40 +0000221#endif /* PSA_CRYPTO_STRUCT_H */