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