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