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 which part of the system
119  *   has access to the key material and can perform operations using the key.
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  * integration-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  * The default lifetime of a key is #PSA_KEY_LIFETIME_VOLATILE. The lifetime
135  * #PSA_KEY_LIFETIME_PERSISTENT is supported if persistent storage is
136  * available. Other lifetime values may be supported depending on the
137  * library configuration.
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  * The PSA Cryptography specification defines the following values of
151  * persistence levels:
152  * - \c 0 = #PSA_KEY_PERSISTENCE_VOLATILE: volatile key.
153  *   A volatile key is automatically destroyed by the implementation when
154  *   the application instance terminates. In particular, a volatile key
155  *   is automatically destroyed on a power reset of the device.
156  * - \c 1 = #PSA_KEY_PERSISTENCE_DEFAULT:
157  *   persistent key with a default lifetime.
158  * - \c 2-254: currently not supported by Mbed TLS.
159  * - \c 255 = #PSA_KEY_PERSISTENCE_READ_ONLY:
160  *   read-only or write-once key.
161  *   A key with this persistence level cannot be destroyed.
162  *   Mbed TLS does not currently offer a way to create such keys, but
163  *   integrations of Mbed TLS can use it for built-in keys that the
164  *   application cannot modify (for example, a hardware unique key (HUK)).
165  *
166  * \note Key persistence levels are 8-bit values. Key management
167  *       interfaces operate on lifetimes (type ::psa_key_lifetime_t) which
168  *       encode the persistence as the lower 8 bits of a 32-bit value.
169  */
170 typedef uint8_t psa_key_persistence_t;
171 
172 /** Encoding of key location indicators.
173  *
174  * If an integration of Mbed TLS can make calls to external
175  * cryptoprocessors such as secure elements, the location of a key
176  * indicates which secure element performs the operations on the key.
177  * Depending on the design of the secure element, the key
178  * material may be stored either in the secure element, or
179  * in wrapped (encrypted) form alongside the key metadata in the
180  * primary local storage.
181  *
182  * The PSA Cryptography API specification defines the following values of
183  * location indicators:
184  * - \c 0: primary local storage.
185  *   This location is always available.
186  *   The primary local storage is typically the same storage area that
187  *   contains the key metadata.
188  * - \c 1: primary secure element.
189  *   Integrations of Mbed TLS should support this value if there is a secure
190  *   element attached to the operating environment.
191  *   As a guideline, secure elements may provide higher resistance against
192  *   side channel and physical attacks than the primary local storage, but may
193  *   have restrictions on supported key types, sizes, policies and operations
194  *   and may have different performance characteristics.
195  * - \c 2-0x7fffff: other locations defined by a PSA specification.
196  *   The PSA Cryptography API does not currently assign any meaning to these
197  *   locations, but future versions of that specification or other PSA
198  *   specifications may do so.
199  * - \c 0x800000-0xffffff: vendor-defined locations.
200  *   No PSA specification will assign a meaning to locations in this range.
201  *
202  * \note Key location indicators are 24-bit values. Key management
203  *       interfaces operate on lifetimes (type ::psa_key_lifetime_t) which
204  *       encode the location as the upper 24 bits of a 32-bit value.
205  */
206 typedef uint32_t psa_key_location_t;
207 
208 /** Encoding of identifiers of persistent keys.
209  *
210  * - Applications may freely choose key identifiers in the range
211  *   #PSA_KEY_ID_USER_MIN to #PSA_KEY_ID_USER_MAX.
212  * - The implementation may define additional key identifiers in the range
213  *   #PSA_KEY_ID_VENDOR_MIN to #PSA_KEY_ID_VENDOR_MAX.
214  * - 0 is reserved as an invalid key identifier.
215  * - Key identifiers outside these ranges are reserved for future use.
216  */
217 typedef uint32_t psa_key_id_t;
218 
219 #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
220 typedef psa_key_id_t mbedtls_svc_key_id_t;
221 
222 #else /* MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER */
223 /* Implementation-specific: The Mbed Cryptography library can be built as
224  * part of a multi-client service that exposes the PSA Cryptograpy API in each
225  * client and encodes the client identity in the key identifier argument of
226  * functions such as psa_open_key().
227  */
228 typedef struct
229 {
230     psa_key_id_t key_id;
231     mbedtls_key_owner_id_t owner;
232 } mbedtls_svc_key_id_t;
233 
234 #endif /* !MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER */
235 
236 /**@}*/
237 
238 /** \defgroup policy Key policies
239  * @{
240  */
241 
242 /** \brief Encoding of permitted usage on a key. */
243 typedef uint32_t psa_key_usage_t;
244 
245 /**@}*/
246 
247 /** \defgroup attributes Key attributes
248  * @{
249  */
250 
251 /** The type of a structure containing key attributes.
252  *
253  * This is an opaque structure that can represent the metadata of a key
254  * object. Metadata that can be stored in attributes includes:
255  * - The location of the key in storage, indicated by its key identifier
256  *   and its lifetime.
257  * - The key's policy, comprising usage flags and a specification of
258  *   the permitted algorithm(s).
259  * - Information about the key itself: the key type and its size.
260  * - Additional implementation-defined attributes.
261  *
262  * The actual key material is not considered an attribute of a key.
263  * Key attributes do not contain information that is generally considered
264  * highly confidential.
265  *
266  * An attribute structure works like a simple data structure where each function
267  * `psa_set_key_xxx` sets a field and the corresponding function
268  * `psa_get_key_xxx` retrieves the value of the corresponding field.
269  * However, a future version of the library  may report values that are
270  * equivalent to the original one, but have a different encoding. Invalid
271  * values may be mapped to different, also invalid values.
272  *
273  * An attribute structure may contain references to auxiliary resources,
274  * for example pointers to allocated memory or indirect references to
275  * pre-calculated values. In order to free such resources, the application
276  * must call psa_reset_key_attributes(). As an exception, calling
277  * psa_reset_key_attributes() on an attribute structure is optional if
278  * the structure has only been modified by the following functions
279  * since it was initialized or last reset with psa_reset_key_attributes():
280  * - psa_set_key_id()
281  * - psa_set_key_lifetime()
282  * - psa_set_key_type()
283  * - psa_set_key_bits()
284  * - psa_set_key_usage_flags()
285  * - psa_set_key_algorithm()
286  *
287  * Before calling any function on a key attribute structure, the application
288  * must initialize it by any of the following means:
289  * - Set the structure to all-bits-zero, for example:
290  *   \code
291  *   psa_key_attributes_t attributes;
292  *   memset(&attributes, 0, sizeof(attributes));
293  *   \endcode
294  * - Initialize the structure to logical zero values, for example:
295  *   \code
296  *   psa_key_attributes_t attributes = {0};
297  *   \endcode
298  * - Initialize the structure to the initializer #PSA_KEY_ATTRIBUTES_INIT,
299  *   for example:
300  *   \code
301  *   psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
302  *   \endcode
303  * - Assign the result of the function psa_key_attributes_init()
304  *   to the structure, for example:
305  *   \code
306  *   psa_key_attributes_t attributes;
307  *   attributes = psa_key_attributes_init();
308  *   \endcode
309  *
310  * A freshly initialized attribute structure contains the following
311  * values:
312  *
313  * - lifetime: #PSA_KEY_LIFETIME_VOLATILE.
314  * - key identifier: 0 (which is not a valid key identifier).
315  * - type: \c 0 (meaning that the type is unspecified).
316  * - key size: \c 0 (meaning that the size is unspecified).
317  * - usage flags: \c 0 (which allows no usage except exporting a public key).
318  * - algorithm: \c 0 (which allows no cryptographic usage, but allows
319  *   exporting).
320  *
321  * A typical sequence to create a key is as follows:
322  * -# Create and initialize an attribute structure.
323  * -# If the key is persistent, call psa_set_key_id().
324  *    Also call psa_set_key_lifetime() to place the key in a non-default
325  *    location.
326  * -# Set the key policy with psa_set_key_usage_flags() and
327  *    psa_set_key_algorithm().
328  * -# Set the key type with psa_set_key_type().
329  *    Skip this step if copying an existing key with psa_copy_key().
330  * -# When generating a random key with psa_generate_key() or deriving a key
331  *    with psa_key_derivation_output_key(), set the desired key size with
332  *    psa_set_key_bits().
333  * -# Call a key creation function: psa_import_key(), psa_generate_key(),
334  *    psa_key_derivation_output_key() or psa_copy_key(). This function reads
335  *    the attribute structure, creates a key with these attributes, and
336  *    outputs a key identifier to the newly created key.
337  * -# The attribute structure is now no longer necessary.
338  *    You may call psa_reset_key_attributes(), although this is optional
339  *    with the workflow presented here because the attributes currently
340  *    defined in this specification do not require any additional resources
341  *    beyond the structure itself.
342  *
343  * A typical sequence to query a key's attributes is as follows:
344  * -# Call psa_get_key_attributes().
345  * -# Call `psa_get_key_xxx` functions to retrieve the attribute(s) that
346  *    you are interested in.
347  * -# Call psa_reset_key_attributes() to free any resources that may be
348  *    used by the attribute structure.
349  *
350  * Once a key has been created, it is impossible to change its attributes.
351  */
352 typedef struct psa_key_attributes_s psa_key_attributes_t;
353 
354 
355 #ifndef __DOXYGEN_ONLY__
356 #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
357 /* Mbed Crypto defines this type in crypto_types.h because it is also
358  * visible to applications through an implementation-specific extension.
359  * For the PSA Cryptography specification, this type is only visible
360  * via crypto_se_driver.h. */
361 typedef uint64_t psa_key_slot_number_t;
362 #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
363 #endif /* !__DOXYGEN_ONLY__ */
364 
365 /**@}*/
366 
367 /** \defgroup derivation Key derivation
368  * @{
369  */
370 
371 /** \brief Encoding of the step of a key derivation. */
372 typedef uint16_t psa_key_derivation_step_t;
373 
374 /**@}*/
375 
376 #endif /* PSA_CRYPTO_TYPES_H */
377