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