blob: 38b5953b661ca77742326f5663f77e41855fe6f9 [file] [log] [blame] [view]
Gilles Peskine2e66aca2020-07-13 11:27:13 +02001PSA Cryptoprocessor Driver Interface
2====================================
3
Gilles Peskine3d1bcc62020-08-05 22:20:09 +02004This document describes an interface for cryptoprocessor drivers in the PSA cryptography API. This interface complements the [PSA Cryptography API specification](https://armmbed.github.io/mbed-crypto/psa/#application-programming-interface), which describes the interface between a PSA Cryptography implementation and an application.
Gilles Peskine2e66aca2020-07-13 11:27:13 +02005
6This specification is work in progress and should be considered to be in a beta stage. There is ongoing work to implement this interface in Mbed TLS, which is the reference implementation of the PSA Cryptography API. At this stage, Arm does not expect major changes, but minor changes are expected based on experience from the first implementation and on external feedback.
7
Gilles Peskine2e66aca2020-07-13 11:27:13 +02008## Introduction
9
10### Purpose of the driver interface
11
Gilles Peskinec2592132020-08-07 13:52:43 +020012The PSA Cryptography API defines an interface that allows applications to perform cryptographic operations in a uniform way regardless of how the operations are performed. Under the hood, different keys may be stored and used in different hardware or in different logical partitions, and different algorithms may involve different hardware or software components.
Gilles Peskine2e66aca2020-07-13 11:27:13 +020013
Gilles Peskinec2592132020-08-07 13:52:43 +020014The driver interface allows implementations of the PSA Cryptography API to be built compositionally. An implementation of the PSA Cryptography API is composed of a **core** and zero or more **drivers**. The core handles key management, enforces key usage policies, and dispatches cryptographic operations either to the applicable driver or to built-in code.
Gilles Peskine2e66aca2020-07-13 11:27:13 +020015
16Functions in the PSA Cryptography API invoke functions in the core. Code from the core calls drivers as described in the present document.
17
18### Types of drivers
19
20The PSA Cryptography driver interface supports two types of cryptoprocessors, and accordingly two types of drivers.
21
Gilles Peskinec2592132020-08-07 13:52:43 +020022* **Transparent** drivers implement cryptographic operations on keys that are provided in cleartext at the beginning of each operation. They are typically used for hardware **accelerators**. When a transparent driver is available for a particular combination of parameters (cryptographic algorithm, key type and size, etc.), it is used instead of the default software implementation. Transparent drivers can also be pure software implementations that are distributed as plug-ins to a PSA Cryptography implementation (for example, an alternative implementation with different performance characteristics, or a certified implementation).
Gilles Peskine929ab8a2020-08-05 22:18:38 +020023* **Opaque** drivers implement cryptographic operations on keys that can only be used inside a protected environment such as a **secure element**, a hardware security module, a smartcard, a secure enclave, etc. An opaque driver is invoked for the specific [key location](#lifetimes-and-locations) that the driver is registered for: the dispatch is based on the key's lifetime.
Gilles Peskine2e66aca2020-07-13 11:27:13 +020024
25### Requirements
26
Gilles Peskine7a1e4f92020-08-06 20:54:56 +020027The present specification was designed to fulfill the following high-level requirements.
Gilles Peskine2e66aca2020-07-13 11:27:13 +020028
29[Req.plugins] It is possible to combine multiple drivers from different providers into the same implementation, without any prior arrangement other than choosing certain names and values from disjoint namespaces.
30
31[Req.compile] It is possible to compile the code of each driver and of the core separately, and link them together. A small amount of glue code may need to be compiled once the list of drivers is available.
32
33[Req.types] Support drivers for the following types of hardware: accelerators that operate on keys in cleartext; cryptoprocessors that can wrap keys with a built-in keys but not store user keys; and cryptoprocessors that store key material.
34
Gilles Peskine0dfd10d2020-08-19 21:41:27 +020035[Req.portable] The interface between drivers and the core does not involve any platform-specific consideration. Driver calls are simple C function calls. Interactions with platform-specific hardware happen only inside the driver (and in fact a driver need not involve any hardware at all).
Gilles Peskine2e66aca2020-07-13 11:27:13 +020036
37[Req.location] Applications can tell which location values correspond to which secure element drivers.
38
Gilles Peskineb320d082020-08-05 22:24:21 +020039[Req.fallback] Accelerator drivers can specify that they do not fully support a cryptographic mechanism and that a fallback to core code may be necessary. Conversely, if an accelerator fully supports cryptographic mechanism, the core must be able to omit code for this mechanism.
Gilles Peskine2e66aca2020-07-13 11:27:13 +020040
41[Req.mechanisms] Drivers can specify which mechanisms they support. A driver's code will not be invoked for cryptographic mechanisms that it does not support.
42
43## Overview of drivers
44
45### Deliverables for a driver
46
Gilles Peskinee72e4de2020-08-06 20:56:45 +020047To write a driver, you need to implement some functions with C linkage, and to declare these functions in a **driver description file**. The driver description file declares which functions the driver implements and what cryptographic mechanisms they support. If the driver description references custom types, macros or constants, you also need to provide C header files defining those elements.
Gilles Peskine2e66aca2020-07-13 11:27:13 +020048
49The concrete syntax for a driver description file is JSON. The structure of this JSON file is specified in the section [“Driver description syntax”](#driver-description-syntax).
50
51A driver therefore consists of:
52
53* A driver description file (in JSON format).
Gilles Peskine7a1e4f92020-08-06 20:54:56 +020054* C header files defining the types required by the driver description. The names of these header files are declared in the driver description file.
55* An object file compiled for the target platform defining the entry point functions specified by the driver description. Implementations may allow drivers to be provided as source files and compiled with the core instead of being pre-compiled.
Gilles Peskine2e66aca2020-07-13 11:27:13 +020056
57How to provide the driver description file, the C header files and the object code is implementation-dependent.
58
Gilles Peskine2e66aca2020-07-13 11:27:13 +020059### Driver description syntax
60
61The concrete syntax for a driver description file is JSON.
62
Gilles Peskine5298f682020-08-19 21:53:59 +020063#### Driver description list
Gilles Peskined89cd742020-08-07 23:37:55 +020064
Gilles Peskine5298f682020-08-19 21:53:59 +020065PSA Cryptography core implementations should support multiple drivers. The driver description files are passed to the implementation as an ordered list in an unspecified manner. This may be, for example, a list of file names passed on a command line, or a JSON list whose elements are individual driver descriptions.
Gilles Peskined89cd742020-08-07 23:37:55 +020066
Gilles Peskine2e66aca2020-07-13 11:27:13 +020067#### Driver description top-level element
68
69A driver description is a JSON object containing the following properties:
70
71* `"prefix"` (mandatory, string). This must be a valid prefix for a C identifier. All the types and functions provided by the driver have a name that starts with this prefix unless overridden with a `"name"` element in the applicable capability as described below.
72* `"type"` (mandatory, string). One of `"transparent"` or `"opaque"`.
Gilles Peskine53ba94c2020-08-06 20:58:54 +020073* `"headers"` (optional, array of strings). A list of header files. These header files must define the types, macros and constants referenced by the driver description. They may declare the entry point functions, but this is not required. They may include other PSA headers and standard headers of the platform. Whether they may include other headers is implementation-specific. If omitted, the list of headers is empty. The header files must be present at the specified location relative to a directory on the compiler's include path when compiling glue code between the core and the drivers.
Gilles Peskine2e66aca2020-07-13 11:27:13 +020074* `"capabilities"` (mandatory, array of [capabilities](#driver-description-capability)).
75A list of **capabilities**. Each capability describes a family of functions that the driver implements for a certain class of cryptographic mechanisms.
76* `"key_context"` (not permitted for transparent drivers, mandatory for opaque drivers): information about the [representation of keys](#key-format-for-opaque-drivers).
77* `"persistent_state_size"` (not permitted for transparent drivers, optional for opaque drivers, integer or string). The size in bytes of the [persistent state of the driver](#opaque-driver-persistent-state). This may be either a non-negative integer or a C constant expression of type `size_t`.
Gilles Peskine929ab8a2020-08-05 22:18:38 +020078* `"location"` (not permitted for transparent drivers, optional for opaque drivers, integer or string). The [location value](#lifetimes-and-locations) for which this driver is invoked. In other words, this determines the lifetimes for which the driver is invoked. This may be either a non-negative integer or a C constant expression of type `psa_key_location_t`.
Gilles Peskine2e66aca2020-07-13 11:27:13 +020079
Gilles Peskinec7cf1332020-08-07 23:24:00 +020080### Driver description capability
81
82#### Capability syntax
Gilles Peskine2e66aca2020-07-13 11:27:13 +020083
84A capability declares a family of functions that the driver implements for a certain class of cryptographic mechanisms. The capability specifies which key types and algorithms are covered and the names of the types and functions that implement it.
85
86A capability is a JSON object containing the following properties:
87
Gilles Peskine5298f682020-08-19 21:53:59 +020088* `"entry_points"` (mandatory, list of strings). Each element is the name of a [driver entry point](#driver-entry-points) or driver entry point family. An entry point is a function defined by the driver. If specified, the core will invoke this capability of the driver only when performing one of the specified operations. The driver must implement all the specified entry points, as well as the types if applicable.
Gilles Peskine2e66aca2020-07-13 11:27:13 +020089* `"algorithms"` (optional, list of strings). Each element is an [algorithm specification](#algorithm-specifications). If specified, the core will invoke this capability of the driver only when performing one of the specified algorithms. If omitted, the core will invoke this capability for all applicable algorithms.
90* `"key_types"` (optional, list of strings). Each element is a [key type specification](#key-type-specifications). If specified, the core will invoke this capability of the driver only for operations involving a key with one of the specified key types. If omitted, the core will invoke this capability of the driver for all applicable key types.
91* `"key_sizes"` (optional, list of integers). If specified, the core will invoke this capability of the driver only for operations involving a key with one of the specified key sizes. If omitted, the core will invoke this capability of the driver for all applicable key sizes. Key sizes are expressed in bits.
Gilles Peskine924e2732020-08-07 13:53:16 +020092* `"names"` (optional, object). A mapping from entry point names described by the `"entry_points"` property, to the name of the C function in the driver that implements the corresponding function. If a function is not listed here, name of the driver function that implements it is the driver's prefix followed by an underscore (`_`) followed by the function name. If this property is omitted, it is equivalent to an empty object (so each entry point *suffix* is implemented by a function called *prefix*`_`*suffix*).
Gilles Peskine0dfd10d2020-08-19 21:41:27 +020093* `"fallback"` (optional for transparent drivers, not permitted for opaque drivers, boolean). If present and true, the driver may return `PSA_ERROR_NOT_SUPPORTED`, in which case the core should call another driver or use built-in code to perform this operation. If absent or false, the driver is expected to fully support the mechanisms described by this capability. See the section “[Fallback](#fallback)” for more information.
Gilles Peskine2e66aca2020-07-13 11:27:13 +020094
Gilles Peskinec7cf1332020-08-07 23:24:00 +020095#### Capability semantics
96
Gilles Peskine5298f682020-08-19 21:53:59 +020097When the PSA Cryptography implementation performs a cryptographic mechanism, it invokes available driver entry points as described in the section [“Driver entry points”](#driver-entry-points).
Gilles Peskinec7cf1332020-08-07 23:24:00 +020098
99A driver is considered available for a cryptographic mechanism that invokes a given entry point if all of the following conditions are met:
100
101* The driver specification includes a capability whose `"entry_points"` list either includes the entry point or includes an entry point family that includes the entry point.
102* If the mechanism involves an algorithm:
103 * either the capability does not have an `"algorithms"` property;
104 * or the value of the capability's `"algorithms"` property includes an [algorithm specification](#algorithm-specifications) that matches this algorithm.
105* If the mechanism involves a key:
Gilles Peskine0dfd10d2020-08-19 21:41:27 +0200106 * either the key is transparent (its location is `PSA_KEY_LOCATION_LOCAL_STORAGE`) and the driver is transparent;
Gilles Peskinec7cf1332020-08-07 23:24:00 +0200107 * or the key is opaque (its location is not `PSA_KEY_LOCATION_LOCAL_STORAGE`) and the driver is an opaque driver whose location is the key's location.
108* If the mechanism involves a key:
109 * either the capability does not have a `"key_types"` property;
110 * or the value of the capability's `"key_types"` property includes a [key type specification](#key-type-specifications) that matches this algorithm.
111* If the mechanism involves a key:
112 * either the capability does not have a `"key_sizes"` property;
113 * or the value of the capability's `"key_sizes"` property includes the key's size.
114
Gilles Peskine91cbf562020-08-07 23:25:33 +0200115If a driver includes multiple applicable capabilities for a given combination of entry point, algorithm, key type and key size, and all the capabilities map the entry point to the same function name, the driver is considered available for this cryptographic mechanism. If a driver includes multiple applicable capabilities for a given combination of entry point, algorithm, key type and key size, and at least two of these capabilities map the entry point to the different function names, the driver specification is invalid.
116
Gilles Peskine5298f682020-08-19 21:53:59 +0200117If multiple transparent drivers have applicable capabilities for a given combination of entry point, algorithm, key type and key size, the first matching driver in the [specification list](#driver-description-list) is invoked. If the capability has [fallback](#fallback) enabled and the first driver returns `PSA_ERROR_NOT_SUPPORTED`, the next matching driver is invoked, and so on.
Gilles Peskined89cd742020-08-07 23:37:55 +0200118
Gilles Peskine0dfd10d2020-08-19 21:41:27 +0200119If multiple opaque drivers have the same location, the list of driver specifications is invalid.
Gilles Peskine91cbf562020-08-07 23:25:33 +0200120
Gilles Peskinec7cf1332020-08-07 23:24:00 +0200121#### Capability examples
122
Gilles Peskinedaf017c2020-08-07 23:40:53 +0200123Example 1: the following capability declares that the driver can perform deterministic ECDSA signatures (but not signature verification) using any hash algorithm and any curve that the core supports. If the prefix of this driver is `"acme"`, the function that performs the signature is called `acme_sign_hash`.
124```
125{
126 "entry_points": ["sign_hash"],
127 "algorithms": ["PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH)"],
128}
129```
130
131Example 2: the following capability declares that the driver can perform deterministic ECDSA signatures using SHA-256 or SHA-384 with a SECP256R1 or SECP384R1 private key (with either hash being possible in combination with either curve). If the prefix of this driver is `"acme"`, the function that performs the signature is called `acme_sign_hash`.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200132```
133{
Gilles Peskine924e2732020-08-07 13:53:16 +0200134 "entry_points": ["sign_hash"],
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200135 "algorithms": ["PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256)",
136 "PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_384)"],
137 "key_types": ["PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_CURVE_SECP_R1)"],
138 "key_sizes": [256, 384]
139}
140```
141
142### Algorithm and key specifications
143
144#### Algorithm specifications
145
Gilles Peskinedaf017c2020-08-07 23:40:53 +0200146An algorithm specification is a string consisting of a `PSA_ALG_xxx` macro that specifies a cryptographic algorithm or an algorithm wildcard policy defined by the PSA Cryptography API. If the macro takes arguments, the string must have the syntax of a C macro call and each argument must be an algorithm specification or a decimal or hexadecimal literal with no suffix, depending on the expected type of argument.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200147
148Spaces are optional after commas. Whether other whitespace is permitted is implementation-specific.
149
150Valid examples:
151```
152PSA_ALG_SHA_256
153PSA_ALG_HMAC(PSA_ALG_SHA_256)
154PSA_ALG_KEY_AGREEMENT(PSA_ALG_ECDH, PSA_ALG_HKDF(PSA_ALG_SHA_256))
Gilles Peskinedaf017c2020-08-07 23:40:53 +0200155PSA_ALG_RSA_PSS(PSA_ALG_ANY_HASH)
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200156```
157
158#### Key type specifications
159
160An algorithm specification is a string consisting of a `PSA_KEY_TYPE_xxx` macro that specifies a key type defined by the PSA Cryptography API. If the macro takes an argument, the string must have the syntax of a C macro call and each argument must be the name of a constant of suitable type (curve or group).
161
162The name `_` may be used instead of a curve or group to indicate that the capability concerns all curves or groups.
163
164Valid examples:
165```
166PSA_KEY_TYPE_AES
167PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_CURVE_SECP_R1)
168PSA_KEY_TYPE_ECC_KEY_PAIR(_)
169```
170
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200171### Driver entry points
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200172
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200173#### Overview of driver entry points
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200174
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200175Drivers define functions, each of which implements an aspect of a capability of a driver, such as a cryptographic operation, a part of a cryptographic operation, or a key management action. These functions are called the **entry points** of the driver. Most driver entry points correspond to a particular function in the PSA Cryptography API. For example, if a call to `psa_sign_hash()` is dispatched to a driver, it invokes the driver's `sign_hash` function.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200176
Gilles Peskinec2592132020-08-07 13:52:43 +0200177All driver entry points return a status of type `psa_status_t` which should use the status codes documented for PSA services in general and for PSA Cryptography in particular: `PSA_SUCCESS` indicates that the function succeeded, and `PSA_ERROR_xxx` values indicate that an error occurred.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200178
Gilles Peskinec2592132020-08-07 13:52:43 +0200179The signature of a driver entry point generally looks like the signature of the PSA Cryptography API that it implements, with some modifications. This section gives an overview of modifications that apply to whole classes of entry points. Refer to the reference section for each entry point or entry point family for details.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200180
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200181* For entry points that operate on an existing key, the `psa_key_id_t` parameter is replaced by a sequence of three parameters that describe the key:
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200182 1. `const psa_key_attributes_t *attributes`: the key attributes.
183 2. `const uint8_t *key_buffer`: a key material or key context buffer.
184 3. `size_t key_buffer_size`: the size of the key buffer in bytes.
185
186 For transparent drivers, the key buffer contains the key material, in the same format as defined for `psa_export_key()` and `psa_export_public_key()` in the PSA Cryptography API. For opaque drivers, the content of the key buffer is entirely up to the driver.
187
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200188* For entry points that involve a multi-part operation, the operation state type (`psa_XXX_operation_t`) is replaced by a driver-specific operation state type (*prefix*`_XXX_operation_t`).
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200189
Gilles Peskinece3ec6f2020-10-27 18:31:50 +0100190* For entry points that are involved in key creation, the `psa_key_id_t *` output parameter is replaced by a sequence of parameters that convey the key context:
Gilles Peskine28b3a942020-11-24 13:08:11 +0100191 1. `uint8_t *key_buffer`: a buffer for the key material or key context.
Gilles Peskinece3ec6f2020-10-27 18:31:50 +0100192 2. `size_t key_buffer_size`: the size of the key buffer in bytes.
193 2. `size_t *key_buffer_length`: the length of the data written to the key buffer in bytes.
194
Gilles Peskine7a1e4f92020-08-06 20:54:56 +0200195Some entry points are grouped in families that must be implemented as a whole. If a driver supports an entry point family, it must provide all the entry points in the family.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200196
Gilles Peskine5263e1e2020-11-13 14:14:28 +0100197Drivers can also have entry points related to random generation. A transparent driver can provide a [random generation interface](#random-generation-entry-points). Separately, transparent and opaque drivers can have [entropy collection entry points](#entropy-collection-entry-point).
Gilles Peskinee80978a2020-11-13 13:07:14 +0100198
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200199#### General considerations on driver entry point parameters
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200200
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200201Buffer parameters for driver entry points obey the following conventions:
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200202
203* An input buffer has the type `const uint8_t *` and is immediately followed by a parameter of type `size_t` that indicates the buffer size.
Gilles Peskine7a1e4f92020-08-06 20:54:56 +0200204* An output buffer has the type `uint8_t *` and is immediately followed by a parameter of type `size_t` that indicates the buffer size. A third parameter of type `size_t *` is provided to report the actual length of the data written in the buffer if the function succeeds.
205* An in-out buffer has the type `uint8_t *` and is immediately followed by a parameter of type `size_t` that indicates the buffer size. In-out buffers are only used when the input and the output have the same length.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200206
207Buffers of size 0 may be represented with either a null pointer or a non-null pointer.
208
209Input buffers and other input-only parameters (`const` pointers) may be in read-only memory. Overlap is possible between input buffers, and between an input buffer and an output buffer, but not between two output buffers or between a non-buffer parameter and another parameter.
210
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200211#### Driver entry points for single-part cryptographic operations
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200212
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200213The following driver entry points perform a cryptographic operation in one shot (single-part operation):
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200214
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200215* `"hash_compute"` (transparent drivers only): calculation of a hash. Called by `psa_hash_compute()` and `psa_hash_compare()`. To verify a hash with `psa_hash_compare()`, the core calls the driver's `"hash_compute"` entry point and compares the result with the reference hash value.
216* `"mac_compute"`: calculation of a MAC. Called by `psa_mac_compute()` and possibly `psa_mac_verify()`. To verify a mac with `psa_mac_verify()`, the core calls an applicable driver's `"mac_verify"` entry point if there is one, otherwise the core calls an applicable driver's `"mac_compute"` entry point and compares the result with the reference MAC value.
217* `"mac_verify"`: verification of a MAC. Called by `psa_mac_verify()`. This entry point is mainly useful for drivers of secure elements that verify a MAC without revealing the correct MAC. Although transparent drivers may implement this entry point in addition to `"mac_compute"`, it is generally not useful because the core can call the `"mac_compute"` entry point and compare with the expected MAC value.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200218* `"cipher_encrypt"`: unauthenticated symmetric cipher encryption. Called by `psa_cipher_encrypt()`.
219* `"cipher_decrypt"`: unauthenticated symmetric cipher decryption. Called by `psa_cipher_decrypt()`.
220* `"aead_encrypt"`: authenticated encryption with associated data. Called by `psa_aead_encrypt()`.
221* `"aead_decrypt"`: authenticated decryption with associated data. Called by `psa_aead_decrypt()`.
222* `"asymmetric_encrypt"`: asymmetric encryption. Called by `psa_asymmetric_encrypt()`.
223* `"asymmetric_decrypt"`: asymmetric decryption. Called by `psa_asymmetric_decrypt()`.
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200224* `"sign_hash"`: signature of an already calculated hash. Called by `psa_sign_hash()` and possibly `psa_sign_message()`. To sign a message with `psa_sign_message()`, the core calls an applicable driver's `"sign_message"` entry point if there is one, otherwise the core calls an applicable driver's `"hash_compute"` entry point followed by an applicable driver's `"sign_hash"` entry point.
225* `"verify_hash"`: verification of an already calculated hash. Called by `psa_verify_hash()` and possibly `psa_verify_message()`. To verify a message with `psa_verify_message()`, the core calls an applicable driver's `"verify_message"` entry point if there is one, otherwise the core calls an applicable driver's `"hash_compute"` entry point followed by an applicable driver's `"verify_hash"` entry point.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200226* `"sign_message"`: signature of a message. Called by `psa_sign_message()`.
227* `"verify_message"`: verification of a message. Called by `psa_verify_message()`.
228* `"key_agreement"`: key agreement without a subsequent key derivation. Called by `psa_raw_key_agreement()` and possibly `psa_key_derivation_key_agreement()`.
229
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200230### Driver entry points for multi-part operations
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200231
232#### General considerations on multi-part operations
233
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200234The entry points that implement each step of a multi-part operation are grouped into a family. A driver that implements a multi-part operation must define all of the entry points in this family as well as a type that represents the operation context. The lifecycle of a driver operation context is similar to the lifecycle of an API operation context:
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200235
2361. The core initializes operation context objects to either all-bits-zero or to logical zero (`{0}`), at its discretion.
Gilles Peskineb6c43f62020-08-03 10:55:16 +02002371. The core calls the `xxx_setup` entry point for this operation family. If this fails, the core destroys the operation context object without calling any other driver entry point on it.
2381. The core calls other entry points that manipulate the operation context object, respecting the constraints.
2391. If any entry point fails, the core calls the driver's `xxx_abort` entry point for this operation family, then destroys the operation context object without calling any other driver entry point on it.
2401. If a finish entry point fails, the core destroys the operation context object without calling any other driver entry point on it. The finish entry points are: *prefix*`_mac_sign_finish`, *prefix*`_mac_verify_finish`, *prefix*`_cipher_fnish`, *prefix*`_aead_finish`, *prefix*`_aead_verify`.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200241
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200242If a driver implements a multi-part operation but not the corresponding single-part operation, the core calls the driver's multipart operation entry points to perform the single-part operation.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200243
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200244#### Multi-part operation entry point family `"hash_multipart"`
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200245
Gilles Peskine92149262020-08-03 11:35:49 +0200246This family corresponds to the calculation of a hash in multiple steps.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200247
248This family applies to transparent drivers only.
249
Gilles Peskined4e69272020-08-06 21:10:20 +0200250This family requires the following type and entry points:
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200251
252* Type `"hash_operation_t"`: the type of a hash operation context. It must be possible to copy a hash operation context byte by byte, therefore hash operation contexts must not contain any embedded pointers (except pointers to global data that do not change after the setup step).
253* `"hash_setup"`: called by `psa_hash_setup()`.
254* `"hash_update"`: called by `psa_hash_update()`.
255* `"hash_finish"`: called by `psa_hash_finish()` and `psa_hash_verify()`.
Gilles Peskined4e69272020-08-06 21:10:20 +0200256* `"hash_abort"`: called by all multi-part hash functions of the PSA Cryptography API.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200257
Gilles Peskine7a1e4f92020-08-06 20:54:56 +0200258To verify a hash with `psa_hash_verify()`, the core calls the driver's *prefix*`_hash_finish` entry point and compares the result with the reference hash value.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200259
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200260For example, a driver with the prefix `"acme"` that implements the `"hash_multipart"` entry point family must define the following type and entry points (assuming that the capability does not use the `"names"` property to declare different type and entry point names):
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200261
262```
263typedef ... acme_hash_operation_t;
264psa_status_t acme_hash_setup(acme_hash_operation_t *operation,
265 psa_algorithm_t alg);
266psa_status_t acme_hash_update(acme_hash_operation_t *operation,
267 const uint8_t *input,
268 size_t input_length);
269psa_status_t acme_hash_finish(acme_hash_operation_t *operation,
270 uint8_t *hash,
271 size_t hash_size,
272 size_t *hash_length);
273psa_status_t acme_hash_abort(acme_hash_operation_t *operation);
274```
275
276#### Operation family `"mac_multipart"`
277
278TODO
279
280#### Operation family `"mac_verify_multipart"`
281
282TODO
283
284#### Operation family `"cipher_encrypt_multipart"`
285
286TODO
287
288#### Operation family `"cipher_decrypt_multipart"`
289
290TODO
291
292#### Operation family `"aead_encrypt_multipart"`
293
294TODO
295
296#### Operation family `"aead_decrypt_multipart"`
297
298TODO
299
300#### Operation family `"key_derivation"`
301
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200302This family requires the following type and entry points:
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200303
304* Type `"key_derivation_operation_t"`: the type of a key derivation operation context.
305* `"key_derivation_setup"`: called by `psa_key_derivation_setup()`.
306* `"key_derivation_set_capacity"`: called by `psa_key_derivation_set_capacity()`. The core will always enforce the capacity, therefore this function does not need to do anything for algorithms where the output stream only depends on the effective generated length and not on the capacity.
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200307* `"key_derivation_input_bytes"`: called by `psa_key_derivation_input_bytes()` and `psa_key_derivation_input_key()`. For transparent drivers, when processing a call to `psa_key_derivation_input_key()`, the core always calls the applicable driver's `"key_derivation_input_bytes"` entry point.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200308* `"key_derivation_input_key"` (opaque drivers only)
309* `"key_derivation_output_bytes"`: called by `psa_key_derivation_output_bytes()`; also by `psa_key_derivation_output_key()` for transparent drivers.
Gilles Peskinedaf9d822020-08-06 22:11:30 +0200310* `"key_derivation_output_key"`: called by `psa_key_derivation_output_key()` for transparent drivers when deriving an asymmetric key pair, and also for opaque drivers.
Gilles Peskined4e69272020-08-06 21:10:20 +0200311* `"key_derivation_abort"`: called by all key derivation functions of the PSA Cryptography API.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200312
313TODO: key input and output for opaque drivers; deterministic key generation for transparent drivers
314
315TODO
316
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200317### Driver entry points for key management
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200318
Gilles Peskine28b3a942020-11-24 13:08:11 +0100319The driver entry points for key management differ significantly between [transparent drivers](#key-management-with-transparent-drivers) and [opaque drivers](#key-management-with-opaque-drivers). This section describes common elements. Refer to the applicable section for each driver type for more information.
Gilles Peskinece3ec6f2020-10-27 18:31:50 +0100320
321The entry points that create or format key data have the following prototypes for a driver with the prefix `"acme"`:
322
323```
324psa_status_t acme_import_key(const psa_key_attributes_t *attributes,
325 const uint8_t *data,
326 size_t data_length,
327 uint8_t *key_buffer,
328 size_t key_buffer_size,
329 size_t *key_buffer_length,
Gilles Peskine99e52f62020-11-24 13:09:50 +0100330 size_t *bits); // additional parameter, see below
Gilles Peskinece3ec6f2020-10-27 18:31:50 +0100331psa_status_t acme_generate_key(const psa_key_attributes_t *attributes,
332 uint8_t *key_buffer,
333 size_t key_buffer_size,
334 size_t *key_buffer_length);
335```
336
337TODO: derivation, copy
338
339* The key attributes (`attributes`) have the same semantics as in the PSA Cryptography application interface.
340* For the `"import_key"` entry point, the input in the `data` buffer is either the export format or an implementation-specific format that the core documents as an acceptable input format for `psa_import_key()`.
Gilles Peskine99e52f62020-11-24 13:09:50 +0100341* The size of the key data buffer `key_buffer` is sufficient for the internal representation of the key. For a transparent driver, this is the key's [export format](#key-format-for-transparent-drivers). For an opaque driver, this is the size determined from the driver description and the key attributes, as specified in the section [“Key format for opaque drivers”](#key-format-for-opaque-drivers).
Gilles Peskinece3ec6f2020-10-27 18:31:50 +0100342* For an opaque driver with an `"allocate_key"` entry point, the content of the key data buffer on entry is the output of that entry point.
343* The `"import_key"` entry point must determine or validate the key size and set `*bits` as described in the section [“Key size determination on import”](#key-size-determination-on-import) below.
344
345All key creation entry points must ensure that the resulting key is valid as specified in the section [“Key validation”](#key-validation) below. This is primarily important for import entry points since the key data comes from the application.
346
347#### Key size determination on import
348
Gilles Peskine99e52f62020-11-24 13:09:50 +0100349The `"import_key"` entry point must determine or validate the key size.
Gilles Peskinece3ec6f2020-10-27 18:31:50 +0100350The PSA Cryptography API exposes the key size as part of the key attributes.
Gilles Peskine99e52f62020-11-24 13:09:50 +0100351When importing a key, the key size recorded in the key attributes can be either a size specified by the caller of the API (who may not be trusted), or `0` which indicates that the size must be calculated from the data.
352
353When the core calls the `"import_key"` entry point to process a call to `psa_import_key`, it passes an `attributes` structure such that `psa_get_key_bits(attributes)` is the size passed by the caller of `psa_import_key`. If this size is `0`, the `"import_key"` entry point must set the `bits` input-output parameter to the correct key size. The semantics of `bits` is as follows:
Gilles Peskinece3ec6f2020-10-27 18:31:50 +0100354
355* The core sets `*bits` to `psa_get_key_bits(attributes)` before calling the `"import_key"` entry point.
356* If `*bits == 0`, the driver must determine the key size from the data and set `*bits` to this size. If the key size cannot be determined from the data, the driver must return `PSA_ERROR_INVALID_ARGUMENT` (as of version 1.0 of the PSA Cryptography API specification, it is possible to determine the key size for all standard key types).
Gilles Peskine99e52f62020-11-24 13:09:50 +0100357* If `*bits != 0`, the driver must check the value of `*bits` against the data and return `PSA_ERROR_INVALID_ARGUMENT` if it does not match. If the driver entry point changes `*bits` to a different value but returns `PSA_SUCCESS`, the core will consider the key as invalid and the import will fail.
Gilles Peskinece3ec6f2020-10-27 18:31:50 +0100358
359#### Key validation
360
361Key creation entry points must produce valid key data. Key data is _valid_ if operations involving the key are guaranteed to work functionally and not to cause indirect security loss. Operation functions are supposed to receive valid keys, and should not have to check and report invalid keys. For example:
362
363* If a cryptographic mechanism is defined as having keying material of a certain size, or if the keying material involves integers that have to be in a certain range, key creation must ensure that the keying material has an appropriate size and falls within an appropriate range.
364* If a cryptographic operation involves a division by an integer which is provided as part of a key, key creation must ensure that this integer is nonzero.
Gilles Peskine42286712020-11-24 13:10:58 +0100365* If a cryptographic operation involves two keys A and B (or more), then the creation of A must ensure that using it does not risk compromising B. This applies even if A's policy does not explicitly allow a problematic operation, but A is exportable. In particular, public keys that can potentially be used for key agreement are considered invalid and must not be created if they risk compromising the private key.
Gilles Peskinece3ec6f2020-10-27 18:31:50 +0100366* On the other hand, it is acceptable for import to accept a key that cannot be verified as valid if using this key would at most compromise the key itself and material that is secured with this key. For example, RSA key import does not need to verify that the primes are actually prime. Key import may accept an insecure key if the consequences of the insecurity are no worse than a leak of the key prior to its import.
367
368With opaque drivers, the key context can only be used by code from the same driver, so key validity is primarily intended to report key creation errors at creation time rather than during an operation. With transparent drivers, the key context can potentially be used by code from a different provider, so key validity is critical for interoperability.
369
370This section describes some minimal validity requirements for standard key types.
371
372* For symmetric key types, check that the key size is suitable for the type.
373* For DES (`PSA_KEY_TYPE_DES`), additionally verify the parity bits.
374* For RSA (`PSA_KEY_TYPE_RSA_PUBLIC_KEY`, `PSA_KEY_TYPE_RSA_KEY_PAIR`), check the syntax of the key and make sanity checks on its components. TODO: what sanity checks? Value ranges (e.g. p < n), sanity checks such as parity, minimum and maximum size, what else?
375* For elliptic curve private keys (`PSA_KEY_TYPE_ECC_KEY_PAIR`), check the size and range. TODO: what else?
376* For elliptic curve public keys (`PSA_KEY_TYPE_ECC_PUBLIC_KEY`), check the size and range, and that the point is on the curve. TODO: what else?
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200377
Gilles Peskine5263e1e2020-11-13 14:14:28 +0100378### Entropy collection entry point
379
380A driver can declare an entropy source by providing a `"get_entropy"` entry point. This entry point has the following prototypes for a driver with the prefix `"acme"`:
381
382```
383psa_status_t acme_get_entropy(uint32_t flags,
384 size_t *estimate_bits,
385 uint8_t *output,
386 size_t output_size);
387```
388
389The semantics of the parameters is as follows:
390
391* `flags`: a bit-mask of [entropy collection flags](#entropy-collection-flags).
392* `estimate_bits`: on success, an estimate of the amount of entropy that is present in the `output` buffer, in bits. This must be at least `1` on success. The value is ignored on failure.
393* `output`: on success, this buffer contains non-deterministic data with an estimated entropy of at least `*estimate_bits` bits.
394* `output_size`: the size of the `output` buffer in bytes.
395
396The entry point may return the following statuses:
397
398* `PSA_SUCCESS`: success. The output buffer contains some entropy.
399* `PSA_ERROR_INSUFFICIENT_ENTROPY`: no entropy is available without blocking. This is only permitted if the `PSA_DRIVER_GET_ENTROPY_BLOCK` is clear.
400* Other error codes indicate a transient or permanent failure of the entropy source.
401
402Unlike most other entry points, if multiple transparent drivers include a `"get_entropy"` point, the core will call all of them (as well as the entry points from opaque drivers). Fallback is not applicable to `"get_entropy"`.
403
404#### Entropy collection flags
405
406* `PSA_DRIVER_GET_ENTROPY_BLOCK`: If this flag is set, the driver should block until it has at least one bit of entropy. If this flag is clear, the driver should avoid blocking if no entropy is readily available.
407* `PSA_DRIVER_GET_ENTROPY_KEEPALIVE`: If this flag is set, the driver should expect another call to `acme_get_entropy` after a short time. If this flag is clear, the core is not expecting to call the `"get_entropy"` entry point again within a short amount of time (but it may do so nonetheless).
408
409#### Entropy collection and blocking
410
411The intent of the `BLOCK` and `KEEPALIVE` [flags](#entropy-collection-flags) is to support drivers for TRNG (True Random Number Generator, i.e. an entropy source peripheral) that have a long ramp-up time, especially on platforms with multiple entropy sources.
412
413Here is a suggested call sequence for entropy collection that leverages these flags:
414
4151. The core makes a first round of calls to `"get_entropy"` on every source with the `BLOCK` flag clear and the `KEEPALIVE` flag set, so that drivers can prepare the TRNG peripheral.
4162. The core makes a second round of calls with the `BLOCK` flag set and the `KEEPALIVE` flag clear to gather needed entropy.
4173. If the second round does not collect enough entropy, the core makes more similar rounds, until the total amount of collected entropy is sufficient.
418
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200419### Miscellaneous driver entry points
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200420
421#### Driver initialization
422
Gilles Peskinec2592132020-08-07 13:52:43 +0200423A driver may declare an `"init"` entry point in a capability with no algorithm, key type or key size. If so, the core calls this entry point once during the initialization of the PSA Cryptography subsystem. If the init entry point of any driver fails, the initialization of the PSA Cryptography subsystem fails.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200424
Gilles Peskined4e69272020-08-06 21:10:20 +0200425When multiple drivers have an init entry point, the order in which they are called is unspecified. It is also unspecified whether other drivers' `"init"` entry points are called if one or more init entry point fails.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200426
Gilles Peskine0dfd10d2020-08-19 21:41:27 +0200427On platforms where the PSA Cryptography implementation is a subsystem of a single application, the initialization of the PSA Cryptography subsystem takes place during the call to `psa_crypto_init()`. On platforms where the PSA Cryptography implementation is separate from the application or applications, the initialization of the PSA Cryptography subsystem takes place before or during the first time an application calls `psa_crypto_init()`.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200428
Gilles Peskined4e69272020-08-06 21:10:20 +0200429The init entry point does not take any parameter.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200430
431### Combining multiple drivers
432
Gilles Peskine7a1e4f92020-08-06 20:54:56 +0200433To declare a cryptoprocessor can handle both cleartext and wrapped keys, you need to provide two driver descriptions, one for a transparent driver and one for an opaque driver. You can use the mapping in capabilities' `"names"` property to arrange for multiple driver entry points to map to the same C function.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200434
435## Transparent drivers
436
437### Key format for transparent drivers
438
Gilles Peskinea58d2252020-08-06 21:24:03 +0200439The format of a key for transparent drivers is the same as in applications. Refer to the documentation of [`psa_export_key()`](https://armmbed.github.io/mbed-crypto/html/api/keys/management.html#c.psa_export_key) and [`psa_export_public_key()`](https://armmbed.github.io/mbed-crypto/html/api/keys/management.html#c.psa_export_public_key) in the PSA Cryptography API specification. For custom key types defined by an implementation, refer to the documentation of that implementation.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200440
441### Key management with transparent drivers
442
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200443Transparent drivers may provide the following key management entry points:
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200444
Gilles Peskine12760592020-10-26 18:07:01 +0100445* [`"import_key"`](#key-import-with-transparent-drivers): called by `psa_import_key()`, only when importing a key pair or a public key (key such that `PSA_KEY_TYPE_IS_ASYMMETRIC` is true).
Gilles Peskinea8fc1712020-09-21 13:54:00 +0200446* `"generate_key"`: called by `psa_generate_key()`, only when generating a key pair (key such that `PSA_KEY_TYPE_IS_KEY_PAIR` is true).
447* `"key_derivation_output_key"`: called by `psa_key_derivation_output_key()`, only when deriving a key pair (key such that `PSA_KEY_TYPE_IS_KEY_PAIR` is true).
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200448* `"export_public_key"`: called by the core to obtain the public key of a key pair. The core may call this function at any time to obtain the public key, which can be for `psa_export_public_key()` but also at other times, including during a cryptographic operation that requires the public key such as a call to `psa_verify_message()` on a key pair object.
449
Gilles Peskinea8fc1712020-09-21 13:54:00 +0200450Transparent drivers are not involved when exporting, copying or destroying keys, or when importing, generating or deriving symmetric keys.
451
Gilles Peskine12760592020-10-26 18:07:01 +0100452#### Key import with transparent drivers
453
Gilles Peskine99e52f62020-11-24 13:09:50 +0100454As discussed in [the general section about key management entry points](#driver-entry-points-for-key-management), the key import entry points has the following prototype for a driver with the prefix `"acme"`:
Gilles Peskine12760592020-10-26 18:07:01 +0100455```
456psa_status_t acme_import_key(const psa_key_attributes_t *attributes,
457 const uint8_t *data,
458 size_t data_length,
459 uint8_t *key_buffer,
460 size_t key_buffer_size,
461 size_t *key_buffer_length,
462 size_t *bits);
463```
464
465This entry point has several roles:
466
4671. Parse the key data in the input buffer `data`. The driver must support the export format for the key types that the entry point is declared for. It may support additional formats as specified in the description of [`psa_import_key()`](https://armmbed.github.io/mbed-crypto/html/api/keys/management.html#c.psa_export_key) in the PSA Cryptography API specification.
Gilles Peskine42286712020-11-24 13:10:58 +01004682. Validate the key data. The necessary validation is described in the section [“Key validation with transparent drivers”](#key-validation-with-transparent-drivers) above.
Gilles Peskine12760592020-10-26 18:07:01 +01004693. [Determine the key size](#key-size-determination-on-import) and output it through `*bits`.
4704. Copy the validated key data from `data` to `key_buffer`. The output must be in the canonical format documented for [`psa_export_key()`](https://armmbed.github.io/mbed-crypto/html/api/keys/management.html#c.psa_export_key) or [`psa_export_public_key()`](https://armmbed.github.io/mbed-crypto/html/api/keys/management.html#c.psa_export_public_key), so if the input is not in this format, the entry point must convert it.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200471
Gilles Peskinee80978a2020-11-13 13:07:14 +0100472### Random generation entry points
473
474A transparent driver may provide an operation family that allows it to generate random data. The random generation mechanism must obey the following requirements:
475
476* The random output must be of cryptographic quality, with a uniform distribution. Therefore, if the random generator includes an entropy source, this entropy source must be fed through a CSPRNG (cryptographically secure pseudo-random number generator).
477* Random generation is expected to be fast. (If a device can provide entropy but is slow at generating random data, declare it as an [entropy driver](#entropy-collection-entry-point) instead.)
478* The random generator must be able to incorporate entropy provided by an outside source.
479* The random generator may either be deterministic (in the sense that it always returns the same data when given the same entropy inputs) or non-deterministic (including its own entropy source). In other words, this interface is suitable both for PRNG (pseudo-random number generator, also known as DRBG (deterministic random bit generator)) and for NRBG (non-deterministic random bit generator).
480
481If no driver implements the random generation entry point family, the core provides an unspecified random generation mechanism.
482
483This operation family requires the following type, entry points and parameters (TODO: where exactly are the parameters in the JSON structure?):
484
485* Type `"random_context_t"`: the type of a random generation context.
Gilles Peskine390c5a22020-11-13 14:27:43 +0100486* `"init_random"` (entry point, optional): if this function is present, [the core calls it once](#random-generator-initialization) after allocating a `"random_context_t"` object.
Gilles Peskinea14326f2020-11-13 14:40:57 +0100487* `"add_entropy"` (entry point, optional): the core calls this function to [inject entropy](#entropy-injection). This entry point is optional if the driver is for a peripheral that includes an entropy source of its own, however [random generator drivers without entropy injection](#random-generator-drivers-without-entropy-injection) have limited portability since they can only be used on platforms with no other entropy source.
Gilles Peskine390c5a22020-11-13 14:27:43 +0100488* `"get_random"` (entry point, mandatory): the core calls this function whenever it needs to [obtain random data](#the-get_random-entry-point).
Gilles Peskinea14326f2020-11-13 14:40:57 +0100489* `"initial_entropy_size"` (integer, optional): the minimum number of bytes of entropy that the core must supply before the driver can output random data. This can be `0` if the driver is for a peripheral that includes an entropy source of its own. If omitted, the value is `0`.
Gilles Peskine390c5a22020-11-13 14:27:43 +0100490* `"reseed_entropy_size"` (integer, optional): the minimum number of bytes of entropy that the core must supply when the driver runs out of entropy. This value is also a hint for the size to supply if the core makes additional calls to `"add_entropy"`, for example to enforce prediction resistance. If omitted, the core chooses a value which is at least the expected security strength of the device.
Gilles Peskinee80978a2020-11-13 13:07:14 +0100491
492Random generation is not parametrized by an algorithm. The choice of algorithm is up to the driver.
493
494#### Random generator initialization
495
496The `"init_random"` entry point has the following prototype for a driver with the prefix `"acme"`:
497
498```
499psa_status_t acme_init_random(acme_random_context_t *context);
500```
501
502The core calls this entry point once after allocating a random generation context. Initially, the context object is all-bits-zero.
503
Gilles Peskine0e3b7ce2020-11-13 14:20:03 +0100504If a driver does not have an `"init_random"` entry point, the context object passed to the first call to `"add_entropy"` or `"get_random"` will be all-bits-zero.
Gilles Peskinee80978a2020-11-13 13:07:14 +0100505
506#### Entropy injection
507
508The `"add_entropy"` entry point has the following prototype for a driver with the prefix `"acme"`:
509
510```
511psa_status_t acme_add_entropy(acme_random_context_t *context,
512 const uint8_t *entropy,
513 size_t entropy_size);
514```
515
516The semantics of the parameters is as follows:
517
518* `context`: a random generation context. On the first call to `"add_entropy"`, this object has been initialized by a call to the driver's `"init_random"` entry point if one is present, and to all-bits-zero otherwise.
519* `entropy`: a buffer containing high-entropy data to seed the random generator.
520* `entropy_size`: the size of the `entropy` buffer in bytes. It is guaranteed to be at least `1`, but it may be smaller than the amount of entropy that the driver needs to deliver random data, in which case the core will call the `"add_entropy"` entry point again to supply more entropy.
521
522The core calls this function to supply entropy to the driver. The driver must mix this entropy into its internal state. The driver must mix the whole supplied entropy, even if there is more than what the driver requires, to ensure that all entropy sources are mixed into the random generator state. The driver may mix additional entropy of its own.
523
524The core may call this function at any time. For example, to enforce prediction resistance, the core can call `"add_entropy"` immediately after each call to `"get_random"`. The core must call this function in two circumstances:
525
526* Before any call to the `"get_random"` entry point, to supply `"initial_entropy_size"` bytes of entropy.
527* After a call to the `"get_random"` entry point returns less than the required amount of random data, to supply `"reseed_entropy_size"` bytes of entropy.
528
Gilles Peskine0e3b7ce2020-11-13 14:20:03 +0100529When the driver requires entropy, the core can supply it with one or more successive calls to the `"add_entropy"` entry point. If the required entropy size is zero, the core does not need to call `"add_entropy"`.
Gilles Peskinee80978a2020-11-13 13:07:14 +0100530
Gilles Peskinea14326f2020-11-13 14:40:57 +0100531#### Random generator drivers without entropy injection
532
533Random generator drivers should have the capability to inject additional entropy through the `"add_entropy"` entry point. This ensures that the random generator depends on all the entropy sources that are available on the platform. A driver where a call to `"add_entropy"` does not affect the state of the random generator is not compliant with this specification.
534
535However, a driver may omit the `"add_entropy"` entry point. This limits the driver's portability: implementations of the PSA Cryptography specification may reject drivers without an `"add_entropy"` entry point, or only accept such drivers in certain configurations. In particular, the `"add_entropy"` entry point is required if:
536
537* the integration of PSA Cryptography includes an entropy source that is outside the driver; or
538* the core saves random data in persistent storage to be preserved across platform resets.
539
Gilles Peskinee80978a2020-11-13 13:07:14 +0100540#### The `"get_random"` entry point
541
542The `"get_random"` entry point has the following prototype for a driver with the prefix `"acme"`:
543
544```
545psa_status_t acme_get_random(acme_random_context_t *context,
546 uint8_t *output,
547 size_t output_size,
548 size_t *output_length);
549```
550
551The semantics of the parameters is as follows:
552
Gilles Peskinea14326f2020-11-13 14:40:57 +0100553* `context`: a random generation context. If the driver's `"initial_entropy_size"` property is nonzero, the core must have called `"add_entropy"` at least once with a total of at least `"initial_entropy_size"` bytes of entropy before it calls `"get_random"`. Alternatively, if the driver's `"initial_entropy_size"` property is zero and the core did not call `"add_entropy"`, or if the driver has no `"add_entropy"` entry point, the core must have called `"init_random"` if present, and otherwise the context is all-bits zero.
Gilles Peskinee80978a2020-11-13 13:07:14 +0100554* `output`: on success or partial success, the first `*output_length` bytes of this buffer contain cryptographic-quality random data.
555* `output_size`: the size of the `output` buffer in bytes.
556* `*output_length`: on exit, the number of bytes of random data that the driver has written to the `output` buffer. This is preferably `output_size`, but the driver is allowed to return less data if it runs out of entropy as described below. The core sets this value to 0 on entry.
557
558The driver may return the following status codes:
559
560* `PSA_SUCCESS`: the `output` buffer contains `*output_length` bytes of random data.
561* `PSA_ERROR_INSUFFICIENT_ENTROPY`: the core must supply additional entropy by calling the `"add_entropy"` entry point with at least `"reseed_entropy_size"` bytes. In this case, if `*output_length` is nonzero, the core will use the first `*output_length` bytes of the `output` buffer as random data, and will call `"get_random"` again for the remaining `output_size - *output_length` bytes after calling `"add_entropy"`.
562* `PSA_ERROR_NOT_SUPPORTED`: the random generator is not available. The core will not use the content of `output` or `output_length`. This is only permitted if the driver specification for random generation has the [fallback property](#fallback) enabled.
563* Other error codes such as `PSA_ERROR_COMMUNICATION_FAILURE` or `PSA_ERROR_HARDWARE_FAILURE` indicate a transient or permanent error. The core will not use the content of `output` or `output_length`.
564
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200565### Fallback
566
Gilles Peskine15319452020-08-06 22:47:39 +0200567Sometimes cryptographic accelerators only support certain cryptographic mechanisms partially. The capability description language allows specifying some restrictions, including restrictions on key sizes, but it cannot cover all the possibilities that may arise in practice. Furthermore, it may be desirable to deploy the same binary image on different devices, only some of which have a cryptographic accelerators.
568For these purposes, a transparent driver can declare that it only supports a [capability](#driver-description-capability) partially, by setting the capability's `"fallback"` property to true.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200569
Gilles Peskined89cd742020-08-07 23:37:55 +0200570If a transparent driver entry point is part of a capability which has a true `"fallback"` property and returns `PSA_ERROR_NOT_SUPPORTED`, the core will call the next transparent driver that supports the mechanism, if there is one. The core considers drivers in the order given by the [driver description list](#driver-description-list).
Gilles Peskine15319452020-08-06 22:47:39 +0200571
Gilles Peskine0dfd10d2020-08-19 21:41:27 +0200572If all the available drivers have fallback enabled and return `PSA_ERROR_NOT_SUPPORTED`, the core will perform the operation using built-in code.
Gilles Peskine15319452020-08-06 22:47:39 +0200573As soon as a driver returns any value other than `PSA_ERROR_NOT_SUPPORTED` (`PSA_SUCCESS` or a different error code), this value is returned to the application, without attempting to call any other driver or built-in code.
574
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200575If a transparent driver entry point is part of a capability where the `"fallback"` property is false or omitted, the core should not include any other code for this capability, whether built in or in another transparent driver.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200576
577## Opaque drivers
578
579Opaque drivers allow a PSA Cryptography implementation to delegate cryptographic operations to a separate environment that might not allow exporting key material in cleartext. The opaque driver interface is designed so that the core never inspects the representation of a key. The opaque driver interface is designed to support two subtypes of cryptoprocessors:
580
581* Some cryptoprocessors do not have persistent storage for individual keys. The representation of a key is the key material wrapped with a master key which is located in the cryptoprocessor and never exported from it. The core stores this wrapped key material on behalf of the cryptoprocessor.
582* Some cryptoprocessors have persistent storage for individual keys. The representation of a key is an identifier such as label or slot number. The core stores this identifier.
583
584### Key format for opaque drivers
585
586The format of a key for opaque drivers is an opaque blob. The content of this blob is fully up to the driver. The core merely stores this blob.
587
588Note that since the core stores the key context blob as it is in memory, it must only contain data that is meaningful after a reboot. In particular, it must not contain any pointers or transient handles.
589
590The `"key_context"` property in the [driver description](#driver-description-top-level-element) specifies how to calculate the size of the key context as a function of the key type and size. This is an object with the following properties:
591
592* `"base_size"` (integer or string, optional): this many bytes are included in every key context. If omitted, this value defaults to 0.
593* `"key_pair_size"` (integer or string, optional): this many bytes are included in every key context for a key pair. If omitted, this value defaults to 0.
594* `"public_key_size"` (integer or string, optional): this many bytes are included in every key context for a public key. If omitted, this value defaults to 0.
595* `"symmetric_factor"` (integer or string, optional): every key context for a symmetric key includes this many times the key size. If omitted, this value defaults to 0.
596* `"store_public_key"` (boolean, optional): If specified and true, for a key pair, the key context includes space for the public key. If omitted or false, no additional space is added for the public key.
597* `"size_function"` (string, optional): the name of a function that returns the number of bytes that the driver needs in a key context for a key. This may be a pointer to function. This must be a C identifier; more complex expressions are not permitted. If the core uses this function, it supersedes all the other properties.
598
599The integer properties must be C language constants. A typical value for `"base_size"` is `sizeof(acme_key_context_t)` where `acme_key_context_t` is a type defined in a driver header file.
600
601#### Size of a dynamically allocated key context
602
603If the core supports dynamic allocation for the key context and chooses to use it, and the driver specification includes the `"size_function"` property, the size of the key context is at least
604```
605size_function(key_type, key_bits)
606```
607where `size_function` is the function named in the `"size_function"` property, `key_type` is the key type and `key_bits` is the key size in bits. The prototype of the size function is
608```
609size_t size_function(psa_key_type_t key_type, size_t key_bits);
610```
611
612#### Size of a statically allocated key context
613
614If the core does not support dynamic allocation for the key context or chooses not to use it, or if the driver specification does not include the `"size_function"` property, the size of the key context for a key of type `key_type` and of size `key_bits` bits is:
615
616* For a key pair (`PSA_KEY_TYPE_IS_KEY_PAIR(key_type)` is true):
617 ```
618 base_size + key_pair_size + public_key_overhead
619 ```
620 where `public_key_overhead = PSA_EXPORT_PUBLIC_KEY_MAX_SIZE(key_type, key_bits)` if the `"store_public_key"` property is true and `public_key_overhead = 0` otherwise.
621
622* For a public key (`PSA_KEY_TYPE_IS_PUBLIC_KEY(key_type)` is true):
623 ```
624 base_size + public_key_size
625 ```
626
627* For a symmetric key (not a key pair or public key):
628 ```
629 base_size + symmetric_factor * key_bytes
630 ```
631 where `key_bytes = ((key_bits + 7) / 8)` is the key size in bytes.
632
633#### Key context size for a secure element with storage
634
635If the key is stored in the secure element and the driver only needs to store a label for the key, use `"base_size"` as the size of the label plus any other metadata that the driver needs to store, and omit the other properties.
636
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200637If the key is stored in the secure element, but the secure element does not store the public part of a key pair and cannot recompute it on demand, additionally use the `"store_public_key"` property with the value `true`. Note that this only influences the size of the key context: the driver code must copy the public key to the key context and retrieve it on demand in its `export_public_key` entry point.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200638
639#### Key context size for a secure element without storage
640
641If the key is stored in wrapped form outside the secure element, and the wrapped form of the key plus any metadata has up to *N* bytes of overhead, use *N* as the value of the `"base_size"` property and set the `"symmetric_factor"` property to 1. Set the `"key_pair_size"` and `"public_key_size"` properties appropriately for the largest supported key pair and the largest supported public key respectively.
642
643### Key management with opaque drivers
644
Gilles Peskine7a1e4f92020-08-06 20:54:56 +0200645Opaque drivers may provide the following key management entry points:
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200646
Gilles Peskinee265b9d2020-08-05 22:20:24 +0200647* `"export_key"`: called by `psa_export_key()`, or by `psa_copy_key()` when copying a key from or to a different [location](#lifetimes-and-locations).
Gilles Peskineb6c43f62020-08-03 10:55:16 +0200648* `"export_public_key"`: called by the core to obtain the public key of a key pair. The core may call this entry point at any time to obtain the public key, which can be for `psa_export_public_key()` but also at other times, including during a cryptographic operation that requires the public key such as a call to `psa_verify_message()` on a key pair object.
Gilles Peskine8d06ad02020-08-03 11:37:02 +0200649* `"import_key"`: called by `psa_import_key()`, or by `psa_copy_key()` when copying a key from another location.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200650* `"generate_key"`: called by `psa_generate_key()`.
Gilles Peskinedaf9d822020-08-06 22:11:30 +0200651* `"key_derivation_output_key"`: called by `psa_key_derivation_output_key()`.
Gilles Peskine929ab8a2020-08-05 22:18:38 +0200652* `"copy_key"`: called by `psa_copy_key()` when copying a key within the same [location](#lifetimes-and-locations).
Gilles Peskine8d06ad02020-08-03 11:37:02 +0200653
654In addition, secure elements that store the key material internally must provide the following two entry points:
655
656* `"allocate_key"`: called by `psa_import_key()`, `psa_generate_key()`, `psa_key_derivation_output_key()` or `psa_copy_key()` before creating a key in the location of this driver.
657* `"destroy_key"`: called by `psa_destroy_key()`.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200658
659#### Key creation in a secure element without storage
660
Gilles Peskine8d06ad02020-08-03 11:37:02 +0200661This section describes the key creation process for secure elements that do not store the key material. The driver must obtain a wrapped form of the key material which the core will store. A driver for such a secure element has no `"allocate_key"` or `"destroy_key"` entry point.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200662
Gilles Peskine8d06ad02020-08-03 11:37:02 +0200663When creating a key with an opaque driver which does not have an `"allocate_key"` or `"destroy_key"` entry point:
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200664
6651. The core allocates memory for the key context.
Gilles Peskined4e69272020-08-06 21:10:20 +02006662. The core calls the driver's import, generate, derive or copy entry point.
Gilles Peskine2e66aca2020-07-13 11:27:13 +02006673. The core saves the resulting wrapped key material and any other data that the key context may contain.
668
Gilles Peskine8d06ad02020-08-03 11:37:02 +0200669To destroy a key, the core simply destroys the wrapped key material, without invoking driver code.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200670
Gilles Peskine8d06ad02020-08-03 11:37:02 +0200671#### Key management in a secure element with storage
672
673This section describes the key creation and key destruction processes for secure elements that have persistent storage for the key material. A driver for such a secure element has two mandatory entry points:
674
675* `"allocate_key"`: this function obtains an internal identifier for the key. This may be, for example, a unique label or a slot number.
676* `"destroy_key"`: this function invalidates the internal identifier and destroys the associated key material.
677
Gilles Peskine85b3e132020-08-06 22:20:07 +0200678These functions have the following prototypes for a driver with the prefix `"acme"`:
Gilles Peskine8d06ad02020-08-03 11:37:02 +0200679```
680psa_status_t acme_allocate_key(const psa_key_attributes_t *attributes,
681 uint8_t *key_buffer,
682 size_t key_buffer_size);
683psa_status_t acme_destroy_key(const psa_key_attributes_t *attributes,
684 const uint8_t *key_buffer,
685 size_t key_buffer_size);
686```
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200687
Gilles Peskine92149262020-08-03 11:35:49 +0200688When creating a persistent key with an opaque driver which has an `"allocate_key"` entry point:
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200689
Gilles Peskine8d06ad02020-08-03 11:37:02 +02006901. The core calls the driver's `"allocate_key"` entry point. This function typically allocates an internal identifier for the key without modifying the state of the secure element and stores the identifier in the key context. This function should not modify the state of the secure element. It may modify the copy of the persistent state of the driver in memory.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200691
6921. The core saves the key context to persistent storage.
693
Gilles Peskine8d06ad02020-08-03 11:37:02 +02006941. The core calls the driver's key creation entry point.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200695
Gilles Peskine8d06ad02020-08-03 11:37:02 +02006961. The core saves the updated key context to persistent storage.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200697
Gilles Peskine8d06ad02020-08-03 11:37:02 +0200698If a failure occurs after the `"allocate_key"` step but before the call to the second driver entry point, the core will do one of the following:
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200699
Gilles Peskine8d06ad02020-08-03 11:37:02 +0200700* Fail the creation of the key without indicating this to the driver. This can happen, in particular, if the device loses power immediately after the key allocation entry point returns.
701* Call the driver's `"destroy_key"` entry point.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200702
Gilles Peskine8d06ad02020-08-03 11:37:02 +0200703To destroy a key, the core calls the driver's `"destroy_key"` entry point.
704
Gilles Peskinec2592132020-08-07 13:52:43 +0200705Note that the key allocation and destruction entry points must not rely solely on the key identifier in the key attributes to identify a key. Some implementations of the PSA Cryptography API store keys on behalf of multiple clients, and different clients may use the same key identifier to designate different keys. The manner in which the core distinguishes keys that have the same identifier but are part of the key namespace for different clients is implementation-dependent and is not accessible to drivers. Some typical strategies to allocate an internal key identifier are:
Gilles Peskine8d06ad02020-08-03 11:37:02 +0200706
707* Maintain a set of free slot numbers which is stored either in the secure element or in the driver's persistent storage. To allocate a key slot, find a free slot number, mark it as occupied and store the number in the key context. When the key is destroyed, mark the slot number as free.
708* Maintain a monotonic counter with a practically unbounded range in the secure element or in the driver's persistent storage. To allocate a key slot, increment the counter and store the current value in the key context. Destroying a key does not change the counter.
709
710TODO: explain constraints on how the driver updates its persistent state for resilience
711
712TODO: some of the above doesn't apply to volatile keys
713
714#### Key creation entry points in opaque drivers
715
Gilles Peskine85b3e132020-08-06 22:20:07 +0200716The key creation entry points have the following prototypes for a driver with the prefix `"acme"`:
Gilles Peskine8d06ad02020-08-03 11:37:02 +0200717
718```
719psa_status_t acme_import_key(const psa_key_attributes_t *attributes,
720 const uint8_t *data,
721 size_t data_length,
722 uint8_t *key_buffer,
Gilles Peskine27e69b52020-09-19 00:35:01 +0200723 size_t key_buffer_size,
Gilles Peskine233f91d2020-10-02 17:48:04 +0200724 size_t *key_buffer_length,
725 size_t *bits);
Gilles Peskine8d06ad02020-08-03 11:37:02 +0200726psa_status_t acme_generate_key(const psa_key_attributes_t *attributes,
727 uint8_t *key_buffer,
Gilles Peskine27e69b52020-09-19 00:35:01 +0200728 size_t key_buffer_size,
729 size_t *key_buffer_length);
Gilles Peskine8d06ad02020-08-03 11:37:02 +0200730```
731
Gilles Peskined4e69272020-08-06 21:10:20 +0200732If the driver has an [`"allocate_key"` entry point](#key-management-in-a-secure-element-with-storage), the core calls the `"allocate_key"` entry point with the same attributes on the same key buffer before calling the key creation entry point.
Gilles Peskine8d06ad02020-08-03 11:37:02 +0200733
734TODO: derivation, copy
735
736#### Key export entry points in opaque drivers
737
Gilles Peskine85b3e132020-08-06 22:20:07 +0200738The key export entry points have the following prototypes for a driver with the prefix `"acme"`:
Gilles Peskine8d06ad02020-08-03 11:37:02 +0200739
740```
741psa_status_t acme_export_key(const psa_key_attributes_t *attributes,
742 const uint8_t *key_buffer,
Gilles Peskine22270b52020-09-18 22:54:51 +0200743 size_t key_buffer_size,
Gilles Peskine8d06ad02020-08-03 11:37:02 +0200744 uint8_t *data,
745 size_t data_size,
746 size_t *data_length);
747psa_status_t acme_export_public_key(const psa_key_attributes_t *attributes,
748 const uint8_t *key_buffer,
Gilles Peskine22270b52020-09-18 22:54:51 +0200749 size_t key_buffer_size,
Gilles Peskine8d06ad02020-08-03 11:37:02 +0200750 uint8_t *data,
751 size_t data_size,
752 size_t *data_length);
753```
754
755The core will only call `acme_export_public_key` on a private key. Drivers implementers may choose to store the public key in the key context buffer or to recalculate it on demand. If the key context includes the public key, it needs to have an adequate size; see [“Key format for opaque drivers”](#key-format-for-opaque-drivers).
756
757The core guarantees that the size of the output buffer (`data_size`) is sufficient to export any key with the given attributes. The driver must set `*data_length` to the exact size of the exported key.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200758
759### Opaque driver persistent state
760
761The core maintains persistent state on behalf of an opaque driver. This persistent state consists of a single byte array whose size is given by the `"persistent_state_size"` property in the [driver description](#driver-description-top-level-element).
762
Gilles Peskinec1d388a2020-08-03 12:02:30 +0200763The core loads the persistent state in memory before it calls the driver's [init entry point](#driver-initialization). It is adjusted to match the size declared by the driver, in case a driver upgrade changes the size:
764
765* The first time the driver is loaded on a system, the persistent state is all-bits-zero.
766* If the stored persistent state is smaller than the declared size, the core pads the persistent state with all-bits-zero at the end.
767* If the stored persistent state is larger than the declared size, the core truncates the persistent state to the declared size.
768
769The core provides the following callback functions, which an opaque driver may call while it is processing a call from the driver:
770```
771psa_status_t psa_crypto_driver_get_persistent_state(uint_8_t **persistent_state_ptr);
Gilles Peskine404e1db2020-08-05 22:37:29 +0200772psa_status_t psa_crypto_driver_commit_persistent_state(size_t from, size_t length);
Gilles Peskinec1d388a2020-08-03 12:02:30 +0200773```
774
775`psa_crypto_driver_get_persistent_state` sets `*persistent_state_ptr` to a pointer to the first byte of the persistent state. This pointer remains valid during a call to a driver entry point. Once the entry point returns, the pointer is no longer valid. The core guarantees that calls to `psa_crypto_driver_get_persistent_state` within the same entry point return the same address for the persistent state, but this address may change between calls to an entry point.
776
Gilles Peskine404e1db2020-08-05 22:37:29 +0200777`psa_crypto_driver_commit_persistent_state` updates the persistent state in persistent storage. Only the portion at byte offsets `from` inclusive to `from + length` exclusive is guaranteed to be updated; it is unspecified whether changes made to other parts of the state are taken into account. The driver must call this function after updating the persistent state in memory and before returning from the entry point, otherwise it is unspecified whether the persistent state is updated.
778
779The core will not update the persistent state in storage while an entry point is running except when the entry point calls `psa_crypto_driver_commit_persistent_state`. It may update the persistent state in storage after an entry point returns.
Gilles Peskinec1d388a2020-08-03 12:02:30 +0200780
781In a multithreaded environment, the driver may only call these two functions from the thread that is executing the entry point.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200782
783## How to use drivers from an application
784
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200785### Using transparent drivers
786
787Transparent drivers linked into the library are automatically used for the mechanisms that they implement.
788
789### Using opaque drivers
790
Gilles Peskine929ab8a2020-08-05 22:18:38 +0200791Each opaque driver is assigned a [location](#lifetimes-and-locations). The driver is invoked for all actions that use a key in that location. A key's location is indicated by its lifetime. The application chooses the key's lifetime when it creates the key.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200792
Gilles Peskine85b3e132020-08-06 22:20:07 +0200793For example, the following snippet creates an AES-GCM key which is only accessible inside the secure element designated by the location `PSA_KEY_LOCATION_acme`.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200794```
795psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
796psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
Gilles Peskine71db60b2020-07-13 13:18:28 +0200797 PSA_KEY_PERSISTENCE_DEFAULT, PSA_KEY_LOCATION_acme));
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200798psa_set_key_identifer(&attributes, 42);
799psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
800psa_set_key_size(&attributes, 128);
801psa_set_key_algorithm(&attributes, PSA_ALG_GCM);
802psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
Ronald Croncf56a0a2020-08-04 09:51:30 +0200803psa_key_id_t key;
804psa_generate_key(&attributes, &key);
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200805```
806
Gilles Peskine71db60b2020-07-13 13:18:28 +0200807## Using opaque drivers from an application
808
Gilles Peskine929ab8a2020-08-05 22:18:38 +0200809### Lifetimes and locations
810
811The PSA Cryptography API, version 1.0.0, defines [lifetimes](https://armmbed.github.io/mbed-crypto/html/api/keys/attributes.html?highlight=psa_key_lifetime_t#c.psa_key_lifetime_t) as an attribute of a key that indicates where the key is stored and which application and system actions will create and destroy it. The lifetime is expressed as a 32-bit value (`typedef uint32_t psa_key_lifetime_t`). An upcoming version of the PSA Cryptography API defines more structure for lifetime values to separate these two aspects of the lifetime:
812
813* Bits 0–7 are a _persistence level_. This value indicates what device management actions can cause it to be destroyed. In particular, it indicates whether the key is volatile or persistent.
814* Bits 8–31 are a _location indicator_. This value indicates where the key material is stored and where operations on the key are performed. Location values can be stored in a variable of type `psa_key_location_t`.
815
816An opaque driver is attached to a specific location. Keys in the default location (`PSA_KEY_LOCATION_LOCAL_STORAGE = 0`) are transparent: the core has direct access to the key material. For keys in a location that is managed by an opaque driver, only the secure element has access to the key material and can perform operations on the key, while the core only manipulates a wrapped form of the key or an identifier of the key.
817
818### Creating a key in a secure element
819
Gilles Peskinee265b9d2020-08-05 22:20:24 +0200820The core defines a compile-time constant for each opaque driver indicating its location called `PSA_KEY_LOCATION_`*prefix* where *prefix* is the value of the `"prefix"` property in the driver description. For convenience, Mbed TLS also declares a compile-time constant for the corresponding lifetime with the default persistence called `PSA_KEY_LIFETIME_`*prefix*. Therefore, to declare an opaque key in the location with the prefix `foo` with the default persistence, call `psa_set_key_lifetime` during the key creation as follows:
Gilles Peskine71db60b2020-07-13 13:18:28 +0200821```
822psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_foo);
823```
824
825To declare a volatile key:
826```
827psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
828 PSA_KEY_LOCATION_foo,
829 PSA_KEY_PERSISTENCE_VOLATILE));
830```
831
832Generally speaking, to declare a key with a specified persistence:
833```
834psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
835 PSA_KEY_LOCATION_foo,
836 persistence));
837```
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200838
839## Open questions
840
841### Driver declarations
842
Gilles Peskine2e843ae2020-08-19 21:43:59 +0200843#### Declaring driver entry points
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200844
Gilles Peskine2e843ae2020-08-19 21:43:59 +0200845The core may want to provide declarations for the driver entry points so that it can compile code using them. At the time of writing this paragraph, the driver headers must define types but there is no obligation for them to declare functions. The core knows what the function names and argument types are, so it can generate prototypes.
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200846
847It should be ok for driver functions to be function-like macros or function pointers.
848
849#### Driver location values
850
851How does a driver author decide which location values to use? It should be possible to combine drivers from different sources. Use the same vendor assignment as for PSA services?
852
853Can the driver assembly process generate distinct location values as needed? This can be convenient, but it's also risky: if you upgrade a device, you need the location values to be the same between builds.
854
Gilles Peskine2e843ae2020-08-19 21:43:59 +0200855The current plan is for Arm to maintain a registry of vendors and assign a location namespace to each vendor. Parts of the namespace would be reserved for implementations and integrators.
856
Gilles Peskine15319452020-08-06 22:47:39 +0200857#### Multiple transparent drivers
858
859When multiple transparent drivers implement the same mechanism, which one is called? The first one? The last one? Unspecified? Or is this an error (excluding capabilities with fallback enabled)?
860
Gilles Peskine2e843ae2020-08-19 21:43:59 +0200861The current choice is that the first one is used, which allows having a preference order on drivers, but may mask integration errors.
862
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200863### Driver function interfaces
864
865#### Driver function parameter conventions
866
867Should 0-size buffers be guaranteed to have a non-null pointers?
868
869Should drivers really have to cope with overlap?
870
871Should the core guarantee that the output buffer size has the size indicated by the applicable buffer size macro (which may be an overestimation)?
872
873### Partial computations in drivers
874
875#### Substitution points
876
877Earlier drafts of the driver interface had a concept of _substitution points_: places in the calculation where a driver may be called. Some hardware doesn't do the whole calculation, but only the “main” part. This goes both for transparent and opaque drivers. Some common examples:
878
879* A processor that performs the RSA exponentiation, but not the padding. The driver should be able to leverage the padding code in the core.
880* A processor that performs a block cipher operation only for a single block, or only in ECB mode, or only in CTR mode. The core would perform the block mode (CBC, CTR, CCM, ...).
881
882This concept, or some other way to reuse portable code such as specifying inner functions like `psa_rsa_pad` in the core, should be added to the specification.
883
884### Key management
885
886#### Mixing drivers in key derivation
887
888How does `psa_key_derivation_output_key` work when the extraction part and the expansion part use different drivers?
889
Gilles Peskineab808e72020-08-03 13:43:02 +0200890#### Public key calculation
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200891
Gilles Peskineab808e72020-08-03 13:43:02 +0200892ECC key pairs are represented as the private key value only. The public key needs to be calculated from that. Both transparent drivers and opaque drivers provide a function to calculate the public key (`"export_public_key"`).
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200893
Gilles Peskineab808e72020-08-03 13:43:02 +0200894The specification doesn't mention when the public key might be calculated. The core may calculate it on creation, on demand, or anything in between. Opaque drivers have a choice of storing the public key in the key context or calculating it on demand and can convey whether the core should store the public key with the `"store_public_key"` property. Is this good enough or should the specification include non-functional requirements?
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200895
Gilles Peskinea8fc1712020-09-21 13:54:00 +0200896#### Symmetric key validation with transparent drivers
897
898Should the entry point be called for symmetric keys as well?
899
Gilles Peskine99e52f62020-11-24 13:09:50 +0100900#### Support for custom import formats
901
902[“Driver entry points for key management”](#driver-entry-points-for-key-management) states that the input to `"import_key"` can be an implementation-defined format. Is this a good idea? It reduces driver portability, since a core that accepts a custom format would not work with a driver that doesn't accept this format. On the other hand, if a driver accepts a custom format, the core should let it through because the driver presumably handles it more efficiently (in terms of speed and code size) than the core could.
903
904Allowing custom formats also causes a problem with import: the core can't know the size of the key representation until it knows the bit-size of the key, but determining the bit-size of the key is part of the job of the `"import_key"` entry point. For standard key types, this could plausibly be an issue for RSA private keys, where an implementation might accept a custom format that omits the CRT parameters (or that omits *d*).
905
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200906### Opaque drivers
907
908#### Opaque driver persistent state
909
Gilles Peskinec1d388a2020-08-03 12:02:30 +0200910The driver is allowed to update the state at any time. Is this ok?
911
912An example use case for updating the persistent state at arbitrary times is to renew a key that is used to encrypt communications between the application processor and the secure element.
913
914`psa_crypto_driver_get_persistent_state` does not identify the calling driver, so the driver needs to remember which driver it's calling. This may require a thread-local variable in a multithreaded core. Is this ok?
Gilles Peskine2e66aca2020-07-13 11:27:13 +0200915
916<!--
917Local Variables:
918time-stamp-line-limit: 40
919time-stamp-start: "Time-stamp: *\""
920time-stamp-end: "\""
921time-stamp-format: "%04Y/%02m/%02d %02H:%02M:%02S %Z"
922time-stamp-time-zone: "GMT"
923End:
924-->