Mistake on this page?
Report an issue in GitHub or email us
TARGET_TFM/TARGET_TFM_LATEST/include/psa/crypto_types.h
1 /*
2  * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 /**
8  * \file psa/crypto_types.h
9  *
10  * \brief PSA cryptography module: type aliases.
11  *
12  * \note This file may not be included directly. Applications must
13  * include psa/crypto.h. Drivers must include the appropriate driver
14  * header file.
15  *
16  * This file contains portable definitions of integral types for properties
17  * of cryptographic keys, designations of cryptographic algorithms, and
18  * error codes returned by the library.
19  *
20  * This header file does not declare any function.
21  */
22 
23 #ifndef PSA_CRYPTO_TYPES_H
24 #define PSA_CRYPTO_TYPES_H
25 
26 #include <stdint.h>
27 
28 /** \defgroup error Error codes
29  * @{
30  */
31 
32 /**
33  * \brief Function return status.
34  *
35  * This is either #PSA_SUCCESS (which is zero), indicating success,
36  * or a small negative value indicating that an error occurred. Errors are
37  * encoded as one of the \c PSA_ERROR_xxx values defined here. */
38 /* If #PSA_SUCCESS is already defined, it means that #psa_status_t
39  * is also defined in an external header, so prevent its multiple
40  * definition.
41  */
42 #ifndef PSA_SUCCESS
43 typedef int32_t psa_status_t;
44 #endif
45 
46 /**@}*/
47 
48 /** \defgroup crypto_types Key and algorithm types
49  * @{
50  */
51 
52 /** \brief Encoding of a key type.
53  */
54 typedef uint16_t psa_key_type_t;
55 
56 /** The type of PSA elliptic curve family identifiers.
57  *
58  * The curve identifier is required to create an ECC key using the
59  * PSA_KEY_TYPE_ECC_KEY_PAIR() or PSA_KEY_TYPE_ECC_PUBLIC_KEY()
60  * macros.
61  *
62  * Values defined by this standard will never be in the range 0x80-0xff.
63  * Vendors who define additional families must use an encoding in this range.
64  */
65 typedef uint8_t psa_ecc_family_t;
66 
67 /** The type of PSA Diffie-Hellman group family identifiers.
68  *
69  * The group identifier is required to create an Diffie-Hellman key using the
70  * PSA_KEY_TYPE_DH_KEY_PAIR() or PSA_KEY_TYPE_DH_PUBLIC_KEY()
71  * macros.
72  *
73  * Values defined by this standard will never be in the range 0x80-0xff.
74  * Vendors who define additional families must use an encoding in this range.
75  */
76 typedef uint8_t psa_dh_family_t;
77 
78 /** \brief Encoding of a cryptographic algorithm.
79  *
80  * For algorithms that can be applied to multiple key types, this type
81  * does not encode the key type. For example, for symmetric ciphers
82  * based on a block cipher, #psa_algorithm_t encodes the block cipher
83  * mode and the padding mode while the block cipher itself is encoded
84  * via #psa_key_type_t.
85  */
86 typedef uint32_t psa_algorithm_t;
87 
88 /**@}*/
89 
90 /** \defgroup key_lifetimes Key lifetimes
91  * @{
92  */
93 
94 /** Encoding of key lifetimes.
95  *
96  * The lifetime of a key indicates where it is stored and what system actions
97  * may create and destroy it.
98  *
99  * Lifetime values have the following structure:
100  * - Bits 0-7 (#PSA_KEY_LIFETIME_GET_PERSISTENCE(\c lifetime)):
101  * persistence level. This value indicates what device management
102  * actions can cause it to be destroyed. In particular, it indicates
103  * whether the key is _volatile_ or _persistent_.
104  * See ::psa_key_persistence_t for more information.
105  * - Bits 8-31 (#PSA_KEY_LIFETIME_GET_LOCATION(\c lifetime)):
106  * location indicator. This value indicates which part of the system
107  * has access to the key material and can perform operations using the key.
108  * See ::psa_key_location_t for more information.
109  *
110  * Volatile keys are automatically destroyed when the application instance
111  * terminates or on a power reset of the device. Persistent keys are
112  * preserved until the application explicitly destroys them or until an
113  * integration-specific device management event occurs (for example,
114  * a factory reset).
115  *
116  * Persistent keys have a key identifier of type #psa_key_id_t.
117  * This identifier remains valid throughout the lifetime of the key,
118  * even if the application instance that created the key terminates.
119  * The application can call psa_open_key() to open a persistent key that
120  * it created previously.
121  *
122  * The default lifetime of a key is #PSA_KEY_LIFETIME_VOLATILE. The lifetime
123  * #PSA_KEY_LIFETIME_PERSISTENT is supported if persistent storage is
124  * available. Other lifetime values may be supported depending on the
125  * library configuration.
126  */
127 typedef uint32_t psa_key_lifetime_t;
128 
129 /** Encoding of key persistence levels.
130  *
131  * What distinguishes different persistence levels is what device management
132  * events may cause keys to be destroyed. _Volatile_ keys are destroyed
133  * by a power reset. Persistent keys may be destroyed by events such as
134  * a transfer of ownership or a factory reset. What management events
135  * actually affect persistent keys at different levels is outside the
136  * scope of the PSA Cryptography specification.
137  *
138  * The PSA Cryptography specification defines the following values of
139  * persistence levels:
140  * - \c 0 = #PSA_KEY_PERSISTENCE_VOLATILE: volatile key.
141  * A volatile key is automatically destroyed by the implementation when
142  * the application instance terminates. In particular, a volatile key
143  * is automatically destroyed on a power reset of the device.
144  * - \c 1 = #PSA_KEY_PERSISTENCE_DEFAULT:
145  * persistent key with a default lifetime.
146  * - \c 2-254: currently not supported by Mbed TLS.
147  * - \c 255 = #PSA_KEY_PERSISTENCE_READ_ONLY:
148  * read-only or write-once key.
149  * A key with this persistence level cannot be destroyed.
150  * Mbed TLS does not currently offer a way to create such keys, but
151  * integrations of Mbed TLS can use it for built-in keys that the
152  * application cannot modify (for example, a hardware unique key (HUK)).
153  *
154  * \note Key persistence levels are 8-bit values. Key management
155  * interfaces operate on lifetimes (type ::psa_key_lifetime_t) which
156  * encode the persistence as the lower 8 bits of a 32-bit value.
157  */
158 typedef uint8_t psa_key_persistence_t;
159 
160 /** Encoding of key location indicators.
161  *
162  * If an integration of Mbed TLS can make calls to external
163  * cryptoprocessors such as secure elements, the location of a key
164  * indicates which secure element performs the operations on the key.
165  * Depending on the design of the secure element, the key
166  * material may be stored either in the secure element, or
167  * in wrapped (encrypted) form alongside the key metadata in the
168  * primary local storage.
169  *
170  * The PSA Cryptography API specification defines the following values of
171  * location indicators:
172  * - \c 0: primary local storage.
173  * This location is always available.
174  * The primary local storage is typically the same storage area that
175  * contains the key metadata.
176  * - \c 1: primary secure element.
177  * Integrations of Mbed TLS should support this value if there is a secure
178  * element attached to the operating environment.
179  * As a guideline, secure elements may provide higher resistance against
180  * side channel and physical attacks than the primary local storage, but may
181  * have restrictions on supported key types, sizes, policies and operations
182  * and may have different performance characteristics.
183  * - \c 2-0x7fffff: other locations defined by a PSA specification.
184  * The PSA Cryptography API does not currently assign any meaning to these
185  * locations, but future versions of that specification or other PSA
186  * specifications may do so.
187  * - \c 0x800000-0xffffff: vendor-defined locations.
188  * No PSA specification will assign a meaning to locations in this range.
189  *
190  * \note Key location indicators are 24-bit values. Key management
191  * interfaces operate on lifetimes (type ::psa_key_lifetime_t) which
192  * encode the location as the upper 24 bits of a 32-bit value.
193  */
194 typedef uint32_t psa_key_location_t;
195 
196 /** Encoding of identifiers of persistent keys.
197  *
198  * - Applications may freely choose key identifiers in the range
199  * #PSA_KEY_ID_USER_MIN to #PSA_KEY_ID_USER_MAX.
200  * - The implementation may define additional key identifiers in the range
201  * #PSA_KEY_ID_VENDOR_MIN to #PSA_KEY_ID_VENDOR_MAX.
202  * - 0 is reserved as an invalid key identifier.
203  * - Key identifiers outside these ranges are reserved for future use.
204  */
205 typedef uint32_t psa_key_id_t;
206 #define PSA_KEY_ID_INIT 0
207 
208 /**@}*/
209 
210 /** \defgroup policy Key policies
211  * @{
212  */
213 
214 /** \brief Encoding of permitted usage on a key. */
215 typedef uint32_t psa_key_usage_t;
216 
217 /**@}*/
218 
219 /** \defgroup attributes Key attributes
220  * @{
221  */
222 
223 /** The type of a structure containing key attributes.
224  *
225  * This is an opaque structure that can represent the metadata of a key
226  * object. Metadata that can be stored in attributes includes:
227  * - The location of the key in storage, indicated by its key identifier
228  * and its lifetime.
229  * - The key's policy, comprising usage flags and a specification of
230  * the permitted algorithm(s).
231  * - Information about the key itself: the key type and its size.
232  * - Additional implementation-defined attributes.
233  *
234  * The actual key material is not considered an attribute of a key.
235  * Key attributes do not contain information that is generally considered
236  * highly confidential.
237  *
238  * An attribute structure works like a simple data structure where each function
239  * `psa_set_key_xxx` sets a field and the corresponding function
240  * `psa_get_key_xxx` retrieves the value of the corresponding field.
241  * However, a future version of the library may report values that are
242  * equivalent to the original one, but have a different encoding. Invalid
243  * values may be mapped to different, also invalid values.
244  *
245  * An attribute structure may contain references to auxiliary resources,
246  * for example pointers to allocated memory or indirect references to
247  * pre-calculated values. In order to free such resources, the application
248  * must call psa_reset_key_attributes(). As an exception, calling
249  * psa_reset_key_attributes() on an attribute structure is optional if
250  * the structure has only been modified by the following functions
251  * since it was initialized or last reset with psa_reset_key_attributes():
252  * - psa_set_key_id()
253  * - psa_set_key_lifetime()
254  * - psa_set_key_type()
255  * - psa_set_key_bits()
256  * - psa_set_key_usage_flags()
257  * - psa_set_key_algorithm()
258  *
259  * Before calling any function on a key attribute structure, the application
260  * must initialize it by any of the following means:
261  * - Set the structure to all-bits-zero, for example:
262  * \code
263  * psa_key_attributes_t attributes;
264  * memset(&attributes, 0, sizeof(attributes));
265  * \endcode
266  * - Initialize the structure to logical zero values, for example:
267  * \code
268  * psa_key_attributes_t attributes = {0};
269  * \endcode
270  * - Initialize the structure to the initializer #PSA_KEY_ATTRIBUTES_INIT,
271  * for example:
272  * \code
273  * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
274  * \endcode
275  * - Assign the result of the function psa_key_attributes_init()
276  * to the structure, for example:
277  * \code
278  * psa_key_attributes_t attributes;
279  * attributes = psa_key_attributes_init();
280  * \endcode
281  *
282  * A freshly initialized attribute structure contains the following
283  * values:
284  *
285  * - lifetime: #PSA_KEY_LIFETIME_VOLATILE.
286  * - key identifier: 0 (which is not a valid key identifier).
287  * - type: \c 0 (meaning that the type is unspecified).
288  * - key size: \c 0 (meaning that the size is unspecified).
289  * - usage flags: \c 0 (which allows no usage except exporting a public key).
290  * - algorithm: \c 0 (which allows no cryptographic usage, but allows
291  * exporting).
292  *
293  * A typical sequence to create a key is as follows:
294  * -# Create and initialize an attribute structure.
295  * -# If the key is persistent, call psa_set_key_id().
296  * Also call psa_set_key_lifetime() to place the key in a non-default
297  * location.
298  * -# Set the key policy with psa_set_key_usage_flags() and
299  * psa_set_key_algorithm().
300  * -# Set the key type with psa_set_key_type().
301  * Skip this step if copying an existing key with psa_copy_key().
302  * -# When generating a random key with psa_generate_key() or deriving a key
303  * with psa_key_derivation_output_key(), set the desired key size with
304  * psa_set_key_bits().
305  * -# Call a key creation function: psa_import_key(), psa_generate_key(),
306  * psa_key_derivation_output_key() or psa_copy_key(). This function reads
307  * the attribute structure, creates a key with these attributes, and
308  * outputs a key identifier to the newly created key.
309  * -# The attribute structure is now no longer necessary.
310  * You may call psa_reset_key_attributes(), although this is optional
311  * with the workflow presented here because the attributes currently
312  * defined in this specification do not require any additional resources
313  * beyond the structure itself.
314  *
315  * A typical sequence to query a key's attributes is as follows:
316  * -# Call psa_get_key_attributes().
317  * -# Call `psa_get_key_xxx` functions to retrieve the attribute(s) that
318  * you are interested in.
319  * -# Call psa_reset_key_attributes() to free any resources that may be
320  * used by the attribute structure.
321  *
322  * Once a key has been created, it is impossible to change its attributes.
323  */
325 
326 /**@}*/
327 
328 /** \defgroup derivation Key derivation
329  * @{
330  */
331 
332 /** \brief Encoding of the step of a key derivation. */
333 typedef uint16_t psa_key_derivation_step_t;
334 
335 /**@}*/
336 
337 #endif /* PSA_CRYPTO_TYPES_H */
uint8_t psa_key_persistence_t
Encoding of key persistence levels.
uint16_t psa_key_derivation_step_t
Encoding of the step of a key derivation.
uint32_t psa_key_id_t
Encoding of identifiers of persistent keys.
uint8_t psa_dh_family_t
The type of PSA Diffie-Hellman group family identifiers.
uint32_t psa_key_location_t
Encoding of key location indicators.
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
uint32_t psa_key_usage_t
Encoding of permitted usage on a key.
uint16_t psa_key_type_t
Encoding of a key type.
uint32_t psa_key_lifetime_t
Encoding of key lifetimes.
uint8_t psa_ecc_family_t
The type of PSA elliptic curve family identifiers.
int32_t psa_status_t
Function return status.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.