Mbed OS Reference
Loading...
Searching...
No Matches
crypto_types.h
Go to the documentation of this file.
1/**
2 * \file
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
55typedef 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 */
66typedef 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 */
77typedef 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 */
88typedef 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 */
98typedef 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 */
141typedef 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 */
186typedef 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 */
220typedef 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 */
231typedef uint32_t psa_key_id_t;
232
233#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
234/**
235 * Identifier for persistent keys. See #psa_key_lifetime_t.
236 */
238
239#else /* MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER */
240/* Implementation-specific: The Mbed Cryptography library can be built as
241 * part of a multi-client service that exposes the PSA Cryptograpy API in each
242 * client and encodes the client identity in the key identifier argument of
243 * functions such as psa_open_key().
244 */
245typedef struct
246{
247 psa_key_id_t key_id;
248 mbedtls_key_owner_id_t owner;
250
251#endif /* !MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER */
252
253/**@}*/
254
255/** \defgroup policy Key policies
256 * @{
257 */
258
259/** \brief Encoding of permitted usage on a key. */
260typedef uint32_t psa_key_usage_t;
261
262/**@}*/
263
264/** \defgroup attributes Key attributes
265 * @{
266 */
267
268/** The type of a structure containing key attributes.
269 *
270 * This is an opaque structure that can represent the metadata of a key
271 * object. Metadata that can be stored in attributes includes:
272 * - The location of the key in storage, indicated by its key identifier
273 * and its lifetime.
274 * - The key's policy, comprising usage flags and a specification of
275 * the permitted algorithm(s).
276 * - Information about the key itself: the key type and its size.
277 * - Implementations may define additional attributes.
278 *
279 * The actual key material is not considered an attribute of a key.
280 * Key attributes do not contain information that is generally considered
281 * highly confidential.
282 *
283 * An attribute structure can be a simple data structure where each function
284 * `psa_set_key_xxx` sets a field and the corresponding function
285 * `psa_get_key_xxx` retrieves the value of the corresponding field.
286 * However, implementations may report values that are equivalent to the
287 * original one, but have a different encoding. For example, an
288 * implementation may use a more compact representation for types where
289 * many bit-patterns are invalid or not supported, and store all values
290 * that it does not support as a special marker value. In such an
291 * implementation, after setting an invalid value, the corresponding
292 * get function returns an invalid value which may not be the one that
293 * was originally stored.
294 *
295 * An attribute structure may contain references to auxiliary resources,
296 * for example pointers to allocated memory or indirect references to
297 * pre-calculated values. In order to free such resources, the application
298 * must call psa_reset_key_attributes(). As an exception, calling
299 * psa_reset_key_attributes() on an attribute structure is optional if
300 * the structure has only been modified by the following functions
301 * since it was initialized or last reset with psa_reset_key_attributes():
302 * - psa_set_key_id()
303 * - psa_set_key_lifetime()
304 * - psa_set_key_type()
305 * - psa_set_key_bits()
306 * - psa_set_key_usage_flags()
307 * - psa_set_key_algorithm()
308 *
309 * Before calling any function on a key attribute structure, the application
310 * must initialize it by any of the following means:
311 * - Set the structure to all-bits-zero, for example:
312 * \code
313 * psa_key_attributes_t attributes;
314 * memset(&attributes, 0, sizeof(attributes));
315 * \endcode
316 * - Initialize the structure to logical zero values, for example:
317 * \code
318 * psa_key_attributes_t attributes = {0};
319 * \endcode
320 * - Initialize the structure to the initializer #PSA_KEY_ATTRIBUTES_INIT,
321 * for example:
322 * \code
323 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
324 * \endcode
325 * - Assign the result of the function psa_key_attributes_init()
326 * to the structure, for example:
327 * \code
328 * psa_key_attributes_t attributes;
329 * attributes = psa_key_attributes_init();
330 * \endcode
331 *
332 * A freshly initialized attribute structure contains the following
333 * values:
334 *
335 * - lifetime: #PSA_KEY_LIFETIME_VOLATILE.
336 * - key identifier: 0 (which is not a valid key identifier).
337 * - type: \c 0 (meaning that the type is unspecified).
338 * - key size: \c 0 (meaning that the size is unspecified).
339 * - usage flags: \c 0 (which allows no usage except exporting a public key).
340 * - algorithm: \c 0 (which allows no cryptographic usage, but allows
341 * exporting).
342 *
343 * A typical sequence to create a key is as follows:
344 * -# Create and initialize an attribute structure.
345 * -# If the key is persistent, call psa_set_key_id().
346 * Also call psa_set_key_lifetime() to place the key in a non-default
347 * location.
348 * -# Set the key policy with psa_set_key_usage_flags() and
349 * psa_set_key_algorithm().
350 * -# Set the key type with psa_set_key_type().
351 * Skip this step if copying an existing key with psa_copy_key().
352 * -# When generating a random key with psa_generate_key() or deriving a key
353 * with psa_key_derivation_output_key(), set the desired key size with
354 * psa_set_key_bits().
355 * -# Call a key creation function: psa_import_key(), psa_generate_key(),
356 * psa_key_derivation_output_key() or psa_copy_key(). This function reads
357 * the attribute structure, creates a key with these attributes, and
358 * outputs a key identifier to the newly created key.
359 * -# The attribute structure is now no longer necessary.
360 * You may call psa_reset_key_attributes(), although this is optional
361 * with the workflow presented here because the attributes currently
362 * defined in this specification do not require any additional resources
363 * beyond the structure itself.
364 *
365 * A typical sequence to query a key's attributes is as follows:
366 * -# Call psa_get_key_attributes().
367 * -# Call `psa_get_key_xxx` functions to retrieve the attribute(s) that
368 * you are interested in.
369 * -# Call psa_reset_key_attributes() to free any resources that may be
370 * used by the attribute structure.
371 *
372 * Once a key has been created, it is impossible to change its attributes.
373 */
375
376
377#ifndef __DOXYGEN_ONLY__
378#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
379/* Mbed Crypto defines this type in crypto_types.h because it is also
380 * visible to applications through an implementation-specific extension.
381 * For the PSA Cryptography specification, this type is only visible
382 * via crypto_se_driver.h. */
383typedef uint64_t psa_key_slot_number_t;
384#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
385#endif /* !__DOXYGEN_ONLY__ */
386
387/**@}*/
388
389/** \defgroup derivation Key derivation
390 * @{
391 */
392
393/** \brief Encoding of the step of a key derivation. */
395
396/**@}*/
397
398#endif /* PSA_CRYPTO_TYPES_H */
PSA cryptography module: Mbed TLS platform definitions.
uint16_t psa_key_type_t
Encoding of a key type.
Definition: crypto_types.h:66
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
Definition: crypto_types.h:98
uint8_t psa_ecc_family_t
The type of PSA elliptic curve family identifiers.
Definition: crypto_types.h:77
uint8_t psa_dh_family_t
The type of PSA Diffie-Hellman group family identifiers.
Definition: crypto_types.h:88
uint16_t psa_key_derivation_step_t
Encoding of the step of a key derivation.
Definition: crypto_types.h:394
int32_t psa_status_t
Function return status.
Definition: crypto_types.h:55
uint32_t psa_key_id_t
Encoding of identifiers of persistent keys.
Definition: crypto_types.h:231
uint8_t psa_key_persistence_t
Encoding of key persistence levels.
Definition: crypto_types.h:186
uint32_t psa_key_location_t
Encoding of key location indicators.
Definition: crypto_types.h:220
uint32_t psa_key_lifetime_t
Encoding of key lifetimes.
Definition: crypto_types.h:141
psa_key_id_t mbedtls_svc_key_id_t
Identifier for persistent keys.
Definition: crypto_types.h:237
uint32_t psa_key_usage_t
Encoding of permitted usage on a key.
Definition: crypto_types.h:260
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 ...