TF-M Reference Manual  1.2.0
TrustedFirmware-M
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
crypto_struct.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
23 #ifndef PSA_CRYPTO_STRUCT_H
24 #define PSA_CRYPTO_STRUCT_H
25 
26 #ifdef __cplusplus
27 extern "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 
48 {
49  uint32_t handle;
50 };
51 
52 #define PSA_HASH_OPERATION_INIT {0}
53 static inline struct psa_hash_operation_s psa_hash_operation_init( void )
54 {
56  return( v );
57 }
58 
60 {
61  uint32_t handle;
62 };
63 
64 #define PSA_MAC_OPERATION_INIT {0}
65 static inline struct psa_mac_operation_s psa_mac_operation_init( void )
66 {
68  return( v );
69 }
70 
72 {
73  uint32_t handle;
74 };
75 
76 #define PSA_CIPHER_OPERATION_INIT {0}
77 static inline struct psa_cipher_operation_s psa_cipher_operation_init( void )
78 {
80  return( v );
81 }
82 
84 {
85  uint32_t handle;
86 };
87 
88 #define PSA_AEAD_OPERATION_INIT {0}
89 static inline struct psa_aead_operation_s psa_aead_operation_init( void )
90 {
92  return( v );
93 }
94 
96 {
97  uint32_t handle;
98 };
99 
100 #define PSA_KEY_DERIVATION_OPERATION_INIT {0}
101 static inline struct psa_key_derivation_s psa_key_derivation_operation_init( void )
102 {
104  return( v );
105 }
106 
107 /* The type used internally for key sizes.
108  * Public interfaces use size_t, but internally we use a smaller type. */
109 typedef 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 
120 #define PSA_KEY_ATTRIBUTES_INIT PSA_CLIENT_KEY_ATTRIBUTES_INIT
121 
122 static inline struct psa_client_key_attributes_s psa_key_attributes_init( void )
123 {
125  return( v );
126 }
127 
128 static inline void psa_set_key_id(psa_key_attributes_t *attributes,
129  psa_key_id_t id)
130 {
131  attributes->id = id;
132  if( attributes->lifetime == PSA_KEY_LIFETIME_VOLATILE )
134 }
135 
136 static inline psa_key_id_t psa_get_key_id(
137  const psa_key_attributes_t *attributes)
138 {
139  return( attributes->id );
140 }
141 
142 static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
144 {
145  attributes->lifetime = lifetime;
146  if( lifetime == PSA_KEY_LIFETIME_VOLATILE )
147  {
148  attributes->id = 0;
149  }
150 }
151 
152 static inline psa_key_lifetime_t psa_get_key_lifetime(
153  const psa_key_attributes_t *attributes)
154 {
155  return( attributes->lifetime );
156 }
157 
158 static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
159  psa_key_usage_t usage_flags)
160 {
161  attributes->usage = usage_flags;
162 }
163 
164 static inline psa_key_usage_t psa_get_key_usage_flags(
165  const psa_key_attributes_t *attributes)
166 {
167  return( attributes->usage );
168 }
169 
170 static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
172 {
173  attributes->alg = alg;
174 }
175 
176 static inline psa_algorithm_t psa_get_key_algorithm(
177  const psa_key_attributes_t *attributes)
178 {
179  return( attributes->alg );
180 }
181 
182 static inline void psa_set_key_type(psa_key_attributes_t *attributes,
184 {
185  attributes->type = type;
186 }
187 
188 static inline psa_key_type_t psa_get_key_type(
189  const psa_key_attributes_t *attributes)
190 {
191  return( attributes->type );
192 }
193 
194 static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
195  size_t bits)
196 {
197  if( bits > PSA_MAX_KEY_BITS )
198  attributes->bits = PSA_KEY_BITS_TOO_LARGE;
199  else
200  attributes->bits = bits;
201 }
202 
203 static inline size_t psa_get_key_bits(
204  const psa_key_attributes_t *attributes)
205 {
206  return( attributes->bits );
207 }
208 
209 #ifdef __cplusplus
210 }
211 #endif
212 
213 #endif /* PSA_CRYPTO_STRUCT_H */
#define PSA_MAX_KEY_BITS
#define PSA_CIPHER_OPERATION_INIT
Definition: crypto_struct.h:76
uint16_t psa_key_bits_t
#define psa_mac_operation_init
Definition: crypto_spe.h:105
#define PSA_KEY_LIFETIME_VOLATILE
uint32_t psa_key_id_t
Definition: crypto_types.h:223
#define psa_cipher_operation_init
Definition: crypto_spe.h:71
#define PSA_MAC_OPERATION_INIT
Definition: crypto_struct.h:64
#define PSA_AEAD_OPERATION_INIT
Definition: crypto_struct.h:88
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
Definition: crypto_types.h:90
#define PSA_KEY_LIFETIME_PERSISTENT
uint32_t psa_key_usage_t
Encoding of permitted usage on a key.
Definition: crypto_types.h:233
uint16_t psa_key_type_t
Encoding of a key type.
Definition: crypto_types.h:58
#define PSA_HASH_OPERATION_INIT
Definition: crypto_struct.h:52
#define PSA_KEY_BITS_TOO_LARGE
#define psa_hash_operation_init
Definition: crypto_spe.h:87
#define PSA_KEY_ATTRIBUTES_INIT
uint32_t psa_key_lifetime_t
Definition: crypto_types.h:133
#define PSA_KEY_DERIVATION_OPERATION_INIT