Skip to content

Commit 0261934

Browse files
committed
psa: Update crypto service for PSA Crypto 1.0b3
1 parent dd3ac9f commit 0261934

File tree

7 files changed

+1893
-559
lines changed

7 files changed

+1893
-559
lines changed

components/TARGET_PSA/services/crypto/COMPONENT_PSA_SRV_IPC/crypto_platform_spe.h

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,33 @@
4040
/** \brief psa_s_function_t enum defines for all the available functions in PSA Crypto. */
4141
typedef enum psa_sec_function_s {
4242
PSA_CRYPTO_INVALID,
43-
PSA_SET_KEY_POLICY,
44-
PSA_GET_KEY_POLICY,
45-
PSA_GET_KEY_LIFETIME,
46-
PSA_ALLOCATE_KEY,
43+
PSA_GET_KEY_ATTRIBUTES,
4744
PSA_OPEN_KEY,
48-
PSA_CREATE_KEY,
4945
PSA_CLOSE_KEY,
5046
PSA_IMPORT_KEY,
5147
PSA_DESTROY_KEY,
52-
PSA_GET_KEY_INFORMATION,
5348
PSA_EXPORT_KEY,
5449
PSA_EXPORT_PUBLIC_KEY,
50+
PSA_HASH_COMPUTE,
51+
PSA_HASH_COMPARE,
5552
PSA_HASH_SETUP,
5653
PSA_HASH_UPDATE,
5754
PSA_HASH_FINISH,
5855
PSA_HASH_VERIFY,
5956
PSA_HASH_ABORT,
57+
PSA_HASH_CLONE,
6058
PSA_HASH_CLONE_BEGIN,
6159
PSA_HASH_CLONE_END,
60+
PSA_MAC_COMPUTE,
61+
PSA_MAC_VERIFY,
6262
PSA_MAC_SIGN_SETUP,
6363
PSA_MAC_VERIFY_SETUP,
6464
PSA_MAC_UPDATE,
6565
PSA_MAC_SIGN_FINISH,
6666
PSA_MAC_VERIFY_FINISH,
6767
PSA_MAC_ABORT,
68+
PSA_CIPHER_ENCRYPT,
69+
PSA_CIPHER_DECRYPT,
6870
PSA_CIPHER_ENCRYPT_SETUP,
6971
PSA_CIPHER_DECRYPT_SETUP,
7072
PSA_CIPHER_GENERATE_IV,
@@ -74,16 +76,30 @@ typedef enum psa_sec_function_s {
7476
PSA_CIPHER_ABORT,
7577
PSA_AEAD_ENCRYPT,
7678
PSA_AEAD_DECRYPT,
79+
PSA_AEAD_ENCRYPT_SETUP,
80+
PSA_AEAD_DECRYPT_SETUP,
81+
PSA_AEAD_GENERATE_NONCE,
82+
PSA_AEAD_SET_NONCE,
83+
PSA_AEAD_SET_LENGTHS,
84+
PSA_AEAD_UPDATE_AD,
85+
PSA_AEAD_UPDATE,
86+
PSA_AEAD_FINISH,
87+
PSA_AEAD_VERIFY,
88+
PSA_AEAD_ABORT,
7789
PSA_ASYMMETRIC_SIGN,
7890
PSA_ASYMMETRIC_VERIFY,
7991
PSA_ASYMMETRIC_ENCRYPT,
8092
PSA_ASYMMETRIC_DECRYPT,
81-
PSA_GET_GENERATOR_CAPACITY,
82-
PSA_GENERATOR_READ,
83-
PSA_GENERATOR_IMPORT_KEY,
84-
PSA_GENERATOR_ABORT,
85-
PSA_KEY_DERIVATION,
86-
PSA_KEY_AGREEMENT,
93+
PSA_KEY_DERIVATION_SETUP,
94+
PSA_KEY_DERIVATION_GET_CAPACITY,
95+
PSA_KEY_DERIVATION_SET_CAPACITY,
96+
PSA_KEY_DERIVATION_INPUT_BYTES,
97+
PSA_KEY_DERIVATION_INPUT_KEY,
98+
PSA_KEY_DERIVATION_KEY_AGREEMENT,
99+
PSA_KEY_DERIVATION_OUTPUT_BYTES,
100+
PSA_KEY_DERIVATION_OUTPUT_KEY,
101+
PSA_KEY_DERIVATION_ABORT,
102+
PSA_RAW_KEY_AGREEMENT,
87103
PSA_GENERATE_RANDOM,
88104
PSA_GENERATE_KEY,
89105
} psa_sec_function_t;
@@ -124,8 +140,6 @@ typedef struct psa_crypto_derivation_ipc_s {
124140
*/
125141
typedef struct psa_key_mng_ipc_s {
126142
psa_key_handle_t handle;
127-
psa_key_lifetime_t lifetime;
128-
psa_key_type_t type;
129143
psa_sec_function_t func;
130144
} psa_key_mng_ipc_t;
131145

components/TARGET_PSA/services/crypto/COMPONENT_PSA_SRV_IPC/crypto_struct_ipc.h

Lines changed: 224 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@
1212
* In implementations with isolation between the application and the
1313
* cryptography module, it is expected that the front-end and the back-end
1414
* would have different versions of this file.
15+
*
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.
1535
*/
1636
/*
1737
* Copyright (C) 2018, ARM Limited, All Rights Reserved
@@ -37,8 +57,11 @@
3757

3858
#include "psa/client.h"
3959

40-
struct psa_hash_operation_s
41-
{
60+
#ifdef __cplusplus
61+
extern "C" {
62+
#endif
63+
64+
struct psa_hash_operation_s {
4265
psa_handle_t handle;
4366
};
4467

@@ -73,29 +96,223 @@ static inline struct psa_cipher_operation_s psa_cipher_operation_init( void )
7396
return( v );
7497
}
7598

76-
struct psa_crypto_generator_s
99+
struct psa_aead_operation_s
100+
{
101+
psa_handle_t handle;
102+
};
103+
104+
#define PSA_AEAD_OPERATION_INIT { PSA_NULL_HANDLE }
105+
static inline struct psa_aead_operation_s psa_aead_operation_init( void )
106+
{
107+
const struct psa_aead_operation_s v = PSA_AEAD_OPERATION_INIT;
108+
return( v );
109+
}
110+
111+
struct psa_key_derivation_s
77112
{
78113
psa_handle_t handle;
79114
};
80115

81-
#define PSA_CRYPTO_GENERATOR_INIT { PSA_NULL_HANDLE }
82-
static inline struct psa_crypto_generator_s psa_crypto_generator_init( void )
116+
/* This only zeroes out the first byte in the union, the rest is unspecified. */
117+
#define PSA_KEY_DERIVATION_OPERATION_INIT { PSA_NULL_HANDLE }
118+
static inline struct psa_key_derivation_s psa_key_derivation_operation_init( void )
83119
{
84-
const struct psa_crypto_generator_s v = PSA_CRYPTO_GENERATOR_INIT;
120+
const struct psa_key_derivation_s v = PSA_KEY_DERIVATION_OPERATION_INIT;
85121
return( v );
86122
}
87123

88124
struct psa_key_policy_s
89125
{
90126
psa_key_usage_t usage;
91127
psa_algorithm_t alg;
128+
psa_algorithm_t alg2;
92129
};
130+
typedef struct psa_key_policy_s psa_key_policy_t;
93131

94-
#define PSA_KEY_POLICY_INIT {0, 0}
132+
#define PSA_KEY_POLICY_INIT {0, 0, 0}
95133
static inline struct psa_key_policy_s psa_key_policy_init( void )
96134
{
97135
const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT;
98136
return( v );
99137
}
100138

139+
/* The type used internally for key sizes.
140+
* Public interfaces use size_t, but internally we use a smaller type. */
141+
typedef uint16_t psa_key_bits_t;
142+
/* The maximum value of the type used to represent bit-sizes.
143+
* This is used to mark an invalid key size. */
144+
#define PSA_KEY_BITS_TOO_LARGE ( (psa_key_bits_t) ( -1 ) )
145+
/* The maximum size of a key in bits.
146+
* Currently defined as the maximum that can be represented, rounded down
147+
* to a whole number of bytes.
148+
* This is an uncast value so that it can be used in preprocessor
149+
* conditionals. */
150+
#define PSA_MAX_KEY_BITS 0xfff8
151+
152+
/** A mask of flags that can be stored in key attributes.
153+
*
154+
* This type is also used internally to store flags in slots. Internal
155+
* flags are defined in library/psa_crypto_core.h. Internal flags may have
156+
* the same value as external flags if they are properly handled during
157+
* key creation and in psa_get_key_attributes.
158+
*/
159+
typedef uint16_t psa_key_attributes_flag_t;
160+
161+
#define MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER \
162+
( (psa_key_attributes_flag_t) 0x0001 )
163+
164+
/* A mask of key attribute flags used externally only.
165+
* Only meant for internal checks inside the library. */
166+
#define MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ( \
167+
MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER | \
168+
0 )
169+
170+
/* A mask of key attribute flags used both internally and externally.
171+
* Currently there aren't any. */
172+
#define MBEDTLS_PSA_KA_MASK_DUAL_USE ( \
173+
0 )
174+
175+
typedef struct
176+
{
177+
psa_key_type_t type;
178+
psa_key_lifetime_t lifetime;
179+
psa_key_id_t id;
180+
psa_key_policy_t policy;
181+
psa_key_bits_t bits;
182+
psa_key_attributes_flag_t flags;
183+
} psa_core_key_attributes_t;
184+
185+
#define PSA_CORE_KEY_ATTRIBUTES_INIT {0, 0, PSA_KEY_ID_INIT, PSA_KEY_POLICY_INIT, 0, 0}
186+
187+
struct psa_key_attributes_s
188+
{
189+
psa_core_key_attributes_t core;
190+
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
191+
psa_key_slot_number_t slot_number;
192+
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
193+
void *domain_parameters;
194+
size_t domain_parameters_size;
195+
};
196+
197+
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
198+
#define PSA_KEY_ATTRIBUTES_INIT {PSA_CORE_KEY_ATTRIBUTES_INIT, 0, NULL, 0}
199+
#else
200+
#define PSA_KEY_ATTRIBUTES_INIT {PSA_CORE_KEY_ATTRIBUTES_INIT, NULL, 0}
201+
#endif
202+
203+
static inline struct psa_key_attributes_s psa_key_attributes_init( void )
204+
{
205+
const struct psa_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT;
206+
return( v );
207+
}
208+
209+
static inline void psa_set_key_id(psa_key_attributes_t *attributes,
210+
psa_key_id_t id)
211+
{
212+
attributes->core.id = id;
213+
if( attributes->core.lifetime == PSA_KEY_LIFETIME_VOLATILE )
214+
attributes->core.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
215+
}
216+
217+
static inline psa_key_id_t psa_get_key_id(
218+
const psa_key_attributes_t *attributes)
219+
{
220+
return( attributes->core.id );
221+
}
222+
223+
static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
224+
psa_key_lifetime_t lifetime)
225+
{
226+
attributes->core.lifetime = lifetime;
227+
if( lifetime == PSA_KEY_LIFETIME_VOLATILE )
228+
{
229+
#ifdef MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER
230+
attributes->core.id.key_id = 0;
231+
attributes->core.id.owner = 0;
232+
#else
233+
attributes->core.id = 0;
234+
#endif
235+
}
236+
}
237+
238+
static inline psa_key_lifetime_t psa_get_key_lifetime(
239+
const psa_key_attributes_t *attributes)
240+
{
241+
return( attributes->core.lifetime );
242+
}
243+
244+
static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
245+
psa_key_usage_t usage_flags)
246+
{
247+
attributes->core.policy.usage = usage_flags;
248+
}
249+
250+
static inline psa_key_usage_t psa_get_key_usage_flags(
251+
const psa_key_attributes_t *attributes)
252+
{
253+
return( attributes->core.policy.usage );
254+
}
255+
256+
static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
257+
psa_algorithm_t alg)
258+
{
259+
attributes->core.policy.alg = alg;
260+
}
261+
262+
static inline psa_algorithm_t psa_get_key_algorithm(
263+
const psa_key_attributes_t *attributes)
264+
{
265+
return( attributes->core.policy.alg );
266+
}
267+
268+
/* This function is declared in crypto_extra.h, which comes after this
269+
* header file, but we need the function here, so repeat the declaration. */
270+
psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
271+
psa_key_type_t type,
272+
const uint8_t *data,
273+
size_t data_length);
274+
275+
static inline void psa_set_key_type(psa_key_attributes_t *attributes,
276+
psa_key_type_t type)
277+
{
278+
if( attributes->domain_parameters == NULL )
279+
{
280+
/* Common case: quick path */
281+
attributes->core.type = type;
282+
}
283+
else
284+
{
285+
/* Call the bigger function to free the old domain paramteres.
286+
* Ignore any errors which may arise due to type requiring
287+
* non-default domain parameters, since this function can't
288+
* report errors. */
289+
(void) psa_set_key_domain_parameters( attributes, type, NULL, 0 );
290+
}
291+
}
292+
293+
static inline psa_key_type_t psa_get_key_type(
294+
const psa_key_attributes_t *attributes)
295+
{
296+
return( attributes->core.type );
297+
}
298+
299+
static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
300+
size_t bits)
301+
{
302+
if( bits > PSA_MAX_KEY_BITS )
303+
attributes->core.bits = PSA_KEY_BITS_TOO_LARGE;
304+
else
305+
attributes->core.bits = (psa_key_bits_t) bits;
306+
}
307+
308+
static inline size_t psa_get_key_bits(
309+
const psa_key_attributes_t *attributes)
310+
{
311+
return( attributes->core.bits );
312+
}
313+
314+
#ifdef __cplusplus
315+
}
316+
#endif
317+
101318
#endif /* PSA_CRYPTO_STRUCT_H */

0 commit comments

Comments
 (0)